ATLAS Offline Software
IoSvc.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2017 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  ::AthService( 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 
65 // Query the interfaces.
66 // Input: riid, Requested interface ID
67 // ppvInterface, Pointer to requested interface
68 // Return: StatusCode indicating SUCCESS or FAILURE.
69 // N.B. Don't forget to release the interface after use!!!
71 IoSvc::queryInterface(const InterfaceID& riid, void** ppvInterface)
72 {
73  if ( IIoSvc::interfaceID().versionMatch(riid) ) {
74  *ppvInterface = dynamic_cast<IIoSvc*>(this);
75  } else {
76  // Interface is not directly available : try out a base class
77  return ::AthService::queryInterface(riid, ppvInterface);
78  }
79  addRef();
80  return StatusCode::SUCCESS;
81 }
82 
84 // Const methods:
86 
88 bool
90 {
91  return m_fds.find(fd) != m_fds.end();
92 }
93 
96 Fd
97 IoSvc::fd(const std::string& fname) const
98 {
99  for (FdMap_t::const_iterator
100  itr = m_fds.begin(),
101  iend= m_fds.end();
102  itr != iend;
103  ++itr) {
104  if (itr->second.fname == fname) {
105  return itr->first;
106  }
107  }
108  return -1;
109 }
110 
113 const std::string&
115 {
116  FdMap_t::const_iterator itr = m_fds.find(fd);
117  if (itr != m_fds.end()) {
118  return itr->second.fname;
119  }
120  static const std::string s_empty = "";
121  return s_empty;
122 }
123 
125 IoType
127 {
128  FdMap_t::const_iterator itr = m_fds.find(fd);
129  if (itr != m_fds.end()) {
130  return itr->second.mode;
131  }
132  return IIoSvc::INVALID;
133 }
134 
137 Fd
138 IoSvc::open(const std::string& fname, IoType mode)
139 {
140  // check this `fname` hasn't been already associated.
141  Fd fd = this->fd(fname);
142  if (fd == -1) {
143  // FIXME: use a recycle bin of fds ?
144  if ( m_last_fd == (std::numeric_limits<Fd>::max()-1)) {
145  ATH_MSG_ERROR("too many file descriptors opened!");
146  return -1;
147  }
148  fd = ++m_last_fd;
149  FdInfos infos = {fname, mode};
150  m_fds.insert(std::make_pair(fd, infos));
151  return fd;
152  }
153 
154  // check the previous open mode is the same
155  if (mode == this->mode(fd)) {
156  return fd;
157  }
158 
159  // inconsistency...
160  return -1;
161 }
162 
164 StatusCode
166 {
167  FdMap_t::const_iterator itr = m_fds.find(fd);
168  if (itr != m_fds.end()) {
169  m_fds.erase(itr);
170  return StatusCode::SUCCESS;
171  }
172  return StatusCode::FAILURE;
173 }
174 
176 // Protected methods:
178 
180 // Const methods:
182 
184 // Non-const methods:
186 
187 
max
#define max(a, b)
Definition: cfImp.cxx:41
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
IoSvc.h
IoSvc::close
StatusCode close(Fd fd)
close file fd
Definition: IoSvc.cxx:165
IIoSvc::interfaceID
static const InterfaceID & interfaceID()
Definition: IIoSvc.h:128
Fd
IIoSvc::Fd Fd
Definition: IoSvc.cxx:22
IoSvc::FdInfos
Definition: IoSvc.h:100
IoSvc::open
Fd open(const std::string &fname, IoType mode)
open file fname with open mode mode
Definition: IoSvc.cxx:138
IIoSvc
Definition: IIoSvc.h:29
IIoSvc::IoType
IoType
I/O Connection types.
Definition: IIoSvc.h:39
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:111
AthService
Definition: AthService.h:32
IoSvc::finalize
virtual StatusCode finalize()
Definition: IoSvc.cxx:58
IoSvc::initialize
virtual StatusCode initialize()
Gaudi Service Implementation.
Definition: IoSvc.cxx:51
Preparation.mode
mode
Definition: Preparation.py:95
IoSvc::has_fd
bool has_fd(Fd fd) const
test if a given file descriptor fd is known to us
Definition: IoSvc.cxx:89
ReadFromCoolCompare.fd
fd
Definition: ReadFromCoolCompare.py:196
IoSvc::fname
const std::string & fname(Fd fd) const
retrieve the file fname associated with file descriptor fd
Definition: IoSvc.cxx:114
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
IoSvc::~IoSvc
virtual ~IoSvc()
Destructor:
Definition: IoSvc.cxx:46
IoSvc::queryInterface
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
Definition: IoSvc.cxx:71
IoSvc::fd
Fd fd(const std::string &fname) const
retrieve the file descriptor associated with file fname
Definition: IoSvc.cxx:97
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
IoType
IIoSvc::IoType IoType
Definition: IoSvc.cxx:23
IIoSvc::Fd
int Fd
unix-y file descriptor
Definition: IIoSvc.h:36
IoSvc::Fd
IIoSvc::Fd Fd
Definition: IoSvc.h:40
IoSvc::IoSvc
IoSvc()
Default constructor:
IoSvc::m_fds
FdMap_t m_fds
map of fd->fdinfos
Definition: IoSvc.h:108
IoSvc::mode
IoType mode(Fd fd) const
retrieve the open mode associated with file descriptor fd
Definition: IoSvc.cxx:126
IIoSvc::INVALID
@ INVALID
Definition: IIoSvc.h:40