ATLAS Offline Software
Loading...
Searching...
No Matches
IoSvc.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5*/
6
7// IoSvc.cxx
8// Implementation file for class IoSvc
9// Author: S.Binet<binet@cern.ch>
11
12// AthenaServices includes
13#include "IoSvc.h"
14
15// STL includes
16#include <limits>
17
18// FrameWork includes
19#include "Gaudi/Property.h"
20
21
22typedef IIoSvc::Fd Fd;
24
26// Public methods:
28
29// Constructors
31IoSvc::IoSvc( const std::string& name,
32 ISvcLocator* pSvcLocator ) :
33 base_class( name, pSvcLocator ),
34 m_fds(),
35 m_last_fd(3) // 1==cout, 2==cerr, 3==clog
36{
37 //
38 // Property declaration
39 //
40 //declareProperty( "Property", m_nProperty );
41
42}
43
44// Destructor
48
49// Athena Service's Hooks
52{
53 ATH_MSG_INFO ("Initializing " << name() << "...");
54
55 return StatusCode::SUCCESS;
56}
57
58StatusCode IoSvc::finalize()
59{
60 ATH_MSG_INFO ("Finalizing " << name() << "...");
61
62 return StatusCode::SUCCESS;
63}
64
66// Const methods:
68
70bool
72{
73 return m_fds.find(fd) != m_fds.end();
74}
75
78Fd
79IoSvc::fd(const std::string& fname) const
80{
81 for (FdMap_t::const_iterator
82 itr = m_fds.begin(),
83 iend= m_fds.end();
84 itr != iend;
85 ++itr) {
86 if (itr->second.fname == fname) {
87 return itr->first;
88 }
89 }
90 return -1;
91}
92
95const std::string&
97{
98 FdMap_t::const_iterator itr = m_fds.find(fd);
99 if (itr != m_fds.end()) {
100 return itr->second.fname;
101 }
102 static const std::string s_empty = "";
103 return s_empty;
104}
105
107IoType
109{
110 FdMap_t::const_iterator itr = m_fds.find(fd);
111 if (itr != m_fds.end()) {
112 return itr->second.mode;
113 }
114 return IIoSvc::INVALID;
115}
116
119Fd
120IoSvc::open(const std::string& fname, IoType mode)
121{
122 // check this `fname` hasn't been already associated.
123 Fd fd = this->fd(fname);
124 if (fd == -1) {
125 // FIXME: use a recycle bin of fds ?
126 if ( m_last_fd == (std::numeric_limits<Fd>::max()-1)) {
127 ATH_MSG_ERROR("too many file descriptors opened!");
128 return -1;
129 }
130 fd = ++m_last_fd;
131 FdInfos infos = {fname, mode};
132 m_fds.insert(std::make_pair(fd, infos));
133 return fd;
134 }
135
136 // check the previous open mode is the same
137 if (mode == this->mode(fd)) {
138 return fd;
139 }
140
141 // inconsistency...
142 return -1;
143}
144
146StatusCode
148{
149 FdMap_t::const_iterator itr = m_fds.find(fd);
150 if (itr != m_fds.end()) {
151 m_fds.erase(itr);
152 return StatusCode::SUCCESS;
153 }
154 return StatusCode::FAILURE;
155}
156
158// Protected methods:
160
162// Const methods:
164
166// Non-const methods:
168
169
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
IIoSvc::Fd Fd
Definition IoSvc.cxx:22
IIoSvc::IoType IoType
Definition IoSvc.cxx:23
IoType
I/O Connection types.
Definition IIoSvc.h:41
@ INVALID
Definition IIoSvc.h:42
int Fd
unix-y file descriptor
Definition IIoSvc.h:38
virtual Fd open(const std::string &fname, IoType mode) override
open file fname with open mode mode
Definition IoSvc.cxx:120
virtual IoType mode(Fd fd) const override
retrieve the open mode associated with file descriptor fd
Definition IoSvc.cxx:108
virtual const std::string & fname(Fd fd) const override
retrieve the file fname associated with file descriptor fd
Definition IoSvc.cxx:96
virtual Fd fd(const std::string &fname) const override
retrieve the file descriptor associated with file fname
Definition IoSvc.cxx:79
virtual StatusCode initialize() override
Gaudi Service Implementation.
Definition IoSvc.cxx:51
IoSvc()
Default constructor:
virtual StatusCode close(Fd fd) override
close file fd
Definition IoSvc.cxx:147
Fd m_last_fd
last created Fd
Definition IoSvc.h:104
IIoSvc::Fd Fd
Definition IoSvc.h:39
virtual StatusCode finalize() override
Definition IoSvc.cxx:58
FdMap_t m_fds
map of fd->fdinfos
Definition IoSvc.h:101
virtual ~IoSvc()
Destructor:
Definition IoSvc.cxx:46
virtual bool has_fd(Fd fd) const override
test if a given file descriptor fd is known to us
Definition IoSvc.cxx:71