ATLAS Offline Software
IoSvc.cxx
Go to the documentation of this file.
1 
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 
22 typedef IIoSvc::Fd Fd;
24 
26 // Public methods:
28 
29 // Constructors
31 IoSvc::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
47 {}
48 
49 // Athena Service's Hooks
52 {
53  ATH_MSG_INFO ("Initializing " << name() << "...");
54 
55  return StatusCode::SUCCESS;
56 }
57 
59 {
60  ATH_MSG_INFO ("Finalizing " << name() << "...");
61 
62  return StatusCode::SUCCESS;
63 }
64 
66 // Const methods:
68 
70 bool
72 {
73  return m_fds.find(fd) != m_fds.end();
74 }
75 
78 Fd
79 IoSvc::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 
95 const 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 
107 IoType
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 
119 Fd
120 IoSvc::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 
146 StatusCode
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 
IoSvc::fd
virtual Fd fd(const std::string &fname) const override
retrieve the file descriptor associated with file fname
Definition: IoSvc.cxx:79
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
IoSvc.h
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
IoSvc::open
virtual Fd open(const std::string &fname, IoType mode) override
open file fname with open mode mode
Definition: IoSvc.cxx:120
IoSvc::has_fd
virtual bool has_fd(Fd fd) const override
test if a given file descriptor fd is known to us
Definition: IoSvc.cxx:71
Fd
IIoSvc::Fd Fd
Definition: IoSvc.cxx:22
IoSvc::FdInfos
Definition: IoSvc.h:93
IIoSvc::IoType
IoType
I/O Connection types.
Definition: IIoSvc.h:41
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IoSvc::m_last_fd
Fd m_last_fd
last created Fd
Definition: IoSvc.h:104
IoSvc::mode
virtual IoType mode(Fd fd) const override
retrieve the open mode associated with file descriptor fd
Definition: IoSvc.cxx:108
IoSvc::close
virtual StatusCode close(Fd fd) override
close file fd
Definition: IoSvc.cxx:147
Preparation.mode
mode
Definition: Preparation.py:94
ReadFromCoolCompare.fd
fd
Definition: ReadFromCoolCompare.py:196
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
IoSvc::~IoSvc
virtual ~IoSvc()
Destructor:
Definition: IoSvc.cxx:46
IoSvc::fname
virtual const std::string & fname(Fd fd) const override
retrieve the file fname associated with file descriptor fd
Definition: IoSvc.cxx:96
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
IoSvc::finalize
virtual StatusCode finalize() override
Definition: IoSvc.cxx:58
IoSvc::initialize
virtual StatusCode initialize() override
Gaudi Service Implementation.
Definition: IoSvc.cxx:51
IoType
IIoSvc::IoType IoType
Definition: IoSvc.cxx:23
IIoSvc::Fd
int Fd
unix-y file descriptor
Definition: IIoSvc.h:38
IoSvc::Fd
IIoSvc::Fd Fd
Definition: IoSvc.h:39
IoSvc::IoSvc
IoSvc()
Default constructor:
IoSvc::m_fds
FdMap_t m_fds
map of fd->fdinfos
Definition: IoSvc.h:101
IIoSvc::INVALID
@ INVALID
Definition: IIoSvc.h:42