w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rfd.cpp
Go to the documentation of this file.
1// $Id: Rfd.cpp 1185 2019-07-12 17:29:12Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2019- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-06-15 1163 1.0.1 SetFd() now type bool
8// 2019-06-07 1161 1.0 Initial version
9// ---------------------------------------------------------------------------
10
15#include <errno.h>
16#include <unistd.h>
17
18#include <iostream>
19
20#include "Rfd.hpp"
21
22#include "Rexception.hpp"
23
24using namespace std;
25
31// all method definitions in namespace Retro
32namespace Retro {
33
34//------------------------------------------+-----------------------------------
36
38 : fFd(-1),
39 fCnam("Rfd::")
40{}
41
42//------------------------------------------+-----------------------------------
44
46 : fFd(rhs.fFd),
47 fCnam(move(rhs.fCnam))
48{
49 rhs.fFd = -1;
50}
51
52//------------------------------------------+-----------------------------------
54
55Rfd::Rfd(const char* cnam)
56 : fFd(-1),
57 fCnam(cnam)
58{}
59
60//------------------------------------------+-----------------------------------
62
64{
65 if (IsOpen()) CloseOrCerr();
66}
67
68//------------------------------------------+-----------------------------------
70
71bool Rfd::SetFd(int fd)
72{
73 if (IsOpen())
74 throw Rexception(fCnam+"Open()", "bad state: already open");
75 fFd = fd;
76 return IsOpen();
77}
78
79//------------------------------------------+-----------------------------------
81
83{
84 if (IsOpenNonStd()) {
85 ::close(fFd);
86 fFd = -1;
87 }
88 return;
89}
90
91//------------------------------------------+-----------------------------------
93
95{
96 if (!IsOpen()) {
97 emsg.Init(fCnam+"Close()", "bad state: not open");
98 return false;
99 }
100 if (!IsOpenNonStd()) {
101 fFd = -1;
102 return true;
103 }
104
105 int irc = ::close(fFd);
106 fFd = -1;
107 if (irc < 0) {
108 emsg.InitErrno(fCnam+"Close()", "close() failed: ", errno);
109 return false;
110 }
111
112 return true;
113}
114
115//------------------------------------------+-----------------------------------
117
119{
120 RerrMsg emsg;
121 if (!Close(emsg)) cerr << emsg.Meth() << "-E: " << emsg.Text() << endl;
122 return;
123}
124
125} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
const std::string & Meth() const
FIXME_docs.
Definition: RerrMsg.ipp:39
void Init(const std::string &meth, const std::string &text)
FIXME_docs.
Definition: RerrMsg.cpp:74
const std::string & Text() const
FIXME_docs.
Definition: RerrMsg.ipp:47
void InitErrno(const std::string &meth, const std::string &text, int errnum)
FIXME_docs.
Definition: RerrMsg.cpp:84
FIXME_docs.
Definition: Rexception.hpp:29
FIXME_docs.
Definition: Rfd.hpp:25
bool IsOpenNonStd() const
FIXME_docs.
Definition: Rfd.ipp:36
int fFd
Definition: Rfd.hpp:48
bool IsOpen() const
FIXME_docs.
Definition: Rfd.ipp:28
Rfd()
FIXME_docs.
Definition: Rfd.cpp:37
virtual ~Rfd()
FIXME_docs.
Definition: Rfd.cpp:63
std::string fCnam
Definition: Rfd.hpp:49
void Close()
FIXME_docs.
Definition: Rfd.cpp:82
void CloseOrCerr()
FIXME_docs.
Definition: Rfd.cpp:118
bool SetFd(int fd)
FIXME_docs.
Definition: Rfd.cpp:71
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47