DDS  ver. 3.4
SessionIDFile.h
Go to the documentation of this file.
1 // Copyright 2017 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef __DDS__SESSIONIDFILE__
6 #define __DDS__SESSIONIDFILE__
7 // BOOST
8 #include <boost/filesystem.hpp>
9 #pragma clang diagnostic push
10 #pragma clang diagnostic ignored "-Wdeprecated-register"
11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/uuid_generators.hpp>
13 #include <boost/uuid/uuid_io.hpp>
14 #pragma clang diagnostic pop
15 
16 namespace MiscCommon
17 {
19  {
20  public:
22  : m_bLocked(false)
23  {
24  }
25  CSessionIDFile(const std::string& _sidFile)
26  : m_pathSIDFile(_sidFile)
27  , m_bLocked(false)
28  , m_sid(boost::uuids::nil_uuid())
29  {
30  }
32  {
33  if (m_bLocked)
34  unlock();
35  }
36 
37  boost::uuids::uuid generate()
38  {
39  m_sid = boost::uuids::random_generator()();
40  return m_sid;
41  }
42 
43  std::string string()
44  {
45  return boost::lexical_cast<std::string>(m_sid);
46  }
47 
48  void lock(const boost::uuids::uuid& _sid, const std::string& _sidFile = "")
49  {
50  if (!_sidFile.empty())
51  m_pathSIDFile = _sidFile;
52 
53  std::ofstream f(m_pathSIDFile.string());
54  f << _sid;
55  f.close();
56  m_bLocked = true;
57  }
58 
59  void unlock()
60  {
61  if (boost::filesystem::is_regular_file(m_pathSIDFile))
62  boost::filesystem::remove(m_pathSIDFile);
63 
64  m_bLocked = false;
65  }
66 
67  std::string getLockedSID()
68  {
69  if (!boost::filesystem::is_regular_file(m_pathSIDFile))
70  return std::string();
71  std::string sid;
72  std::ifstream f(m_pathSIDFile.string());
73  f >> sid;
74 
75  return sid;
76  }
77 
78  private:
79  boost::filesystem::path m_pathSIDFile;
80  bool m_bLocked;
81  boost::uuids::uuid m_sid;
82  };
83 } // namespace MiscCommon
84 #endif /* defined(__DDS__SESSIONIDFILE__) */
CSessionIDFile()
Definition: SessionIDFile.h:21
std::string string()
Definition: SessionIDFile.h:43
boost::uuids::uuid generate()
Definition: SessionIDFile.h:37
CSessionIDFile(const std::string &_sidFile)
Definition: SessionIDFile.h:25
void lock(const boost::uuids::uuid &_sid, const std::string &_sidFile="")
Definition: SessionIDFile.h:48
std::string getLockedSID()
Definition: SessionIDFile.h:67
void unlock()
Definition: SessionIDFile.h:59
Definition: SessionIDFile.h:18
~CSessionIDFile()
Definition: SessionIDFile.h:31
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21