Diana Software
QInitFileHandler.hh
Go to the documentation of this file.
1 
2 #ifndef _QGUI_INITFILE_HANDLER_HH_
3 #define _QGUI_INITFILE_HANDLER_HH_
4 
5 #include <Rtypes.h>
6 #include <string>
7 #include <vector>
8 #include "QError.hh"
9 #include "QGlobalDataManager.hh"
10 #include "QStdVector.hh"
11 
18 {
19 public:
21  QInitFileHandler(const std::string& owner, const std::string& filename);
22 
23  QError GetBool(const std::string& name, Bool_t& val) const;
24  QError GetInt(const std::string& name, Int_t& val) const;
25  QError GetDouble(const std::string& name, Double_t& val) const;
26  QError GetString(const std::string& name, std::string& val) const;
27 
28  QError WriteBool(const std::string& name, Bool_t val) const;
29  QError WriteInt(const std::string& name, Int_t val) const;
30  QError WriteDouble(const std::string& name, Double_t val) const;
31  QError WriteString(const std::string& name, const std::string& val) const;
32 
33  void SetOwner(const std::string& owner);
34  void SetFileName(const std::string& fileName) { fFileName = fileName; }
35 
36  template <class T> QError GetStdVector(const std::string& name,
37  std::vector<T>& vec) const;
38  template <class T> QError WriteStdVector(const std::string& name,
39  const std::vector<T>& vec) const;
40 
41  template <class T> QError GetQObject(const std::string& name, T& obj) const;
42  template <class T> QError WriteQObject(const std::string& name,
43  const T& obj) const;
44 
50  static std::string GetInitFileName(const std::string& baseName,
51  const std::string& extension = "txt");
52 
53 private:
54  std::string fOwner;
55  std::string fFileName;
56 };
57 
58 template <class T> QError
59 QInitFileHandler::GetStdVector(const std::string& name,
60  std::vector<T>& vec) const
61 {
62  if(fOwner.empty())
63  return QError(QERR_GLOBAL_HANDLE, __FILE__, __LINE__, "Owner not set");
64  if(fFileName.empty())
65  return QError(QERR_CANNOT_OPEN_FILE, __FILE__, __LINE__,
66  "File name not set");
67 
68  Diana::QGlobalDataManager& dm = Diana::QGlobalDataManager::GetInstance();
69  Diana::GlobalHandle<Diana::QStdVector<T> > handle(name);
70  QError err = dm.Get(fOwner, &handle, fFileName);
71  if(err != QERR_SUCCESS)
72  return err;
73  if(!handle.IsValid())
74  return QError(QERR_GLOBAL_HANDLE, __FILE__, __LINE__,
75  "Object " + name + " is invalid");
76  vec = handle.Get();
77 
78  return QERR_SUCCESS;
79 }
80 
81 template <class T> QError
82 QInitFileHandler::WriteStdVector(const std::string& name,
83  const std::vector<T>& vec) const
84 {
85  if(fOwner.empty())
86  return QError(QERR_GLOBAL_HANDLE, __FILE__, __LINE__, "Owner not set");
87  if(fFileName.empty())
88  return QError(QERR_CANNOT_OPEN_FILE, __FILE__, __LINE__,
89  "File name not set");
90 
91  Diana::QGlobalDataManager& dm = Diana::QGlobalDataManager::GetInstance();
92  dm.SetOwner(fOwner.c_str());
93  Diana::GlobalHandle<Diana::QStdVector<T> > handle(name);
94  handle.Set(vec);
95 
96  QError err = dm.Set(&handle, fFileName);
97  if(err != QERR_SUCCESS)
98  return err;
99 
100  return QERR_SUCCESS;
101 }
102 
103 
104 
105 
106 template <class T> QError
107 QInitFileHandler::GetQObject(const std::string& name, T& obj) const
108 {
109  if(fOwner.empty())
110  return QError(QERR_GLOBAL_HANDLE, __FILE__, __LINE__, "Owner not set");
111  if(fFileName.empty())
112  return QError(QERR_CANNOT_OPEN_FILE, __FILE__, __LINE__,
113  "File name not set");
114 
115  Diana::QGlobalDataManager& dm = Diana::QGlobalDataManager::GetInstance();
116  Diana::GlobalHandle<T> handle(name);
117  QError err = dm.Get(fOwner, &handle, fFileName);
118  if(err != QERR_SUCCESS)
119  return err;
120  if(!handle.IsValid())
121  return QError(QERR_GLOBAL_HANDLE, __FILE__, __LINE__,
122  "Object " + name + " is invalid");
123  obj = handle.Get();
124 
125  return QERR_SUCCESS;
126 }
127 
128 template <class T> QError
129 QInitFileHandler::WriteQObject(const std::string& name, const T& obj) const
130 {
131  if(fOwner.empty())
132  return QError(QERR_GLOBAL_HANDLE, __FILE__, __LINE__, "Owner not set");
133  if(fFileName.empty())
134  return QError(QERR_CANNOT_OPEN_FILE, __FILE__, __LINE__,
135  "File name not set");
136 
137  Diana::QGlobalDataManager& dm = Diana::QGlobalDataManager::GetInstance();
138  dm.SetOwner(fOwner.c_str());
139  Diana::GlobalHandle<T> handle(name);
140  handle.Set(obj);
141 
142  QError err = dm.Set(&handle, fFileName);
143  if(err != QERR_SUCCESS)
144  return err;
145 
146  return QERR_SUCCESS;
147 }
148 
149 #endif
err
Definition: CheckOF.C:114
QVector vec(3)
QGlobalDataManager dm
@ QERR_GLOBAL_HANDLE
Definition: QError.hh:45
@ QERR_CANNOT_OPEN_FILE
Definition: QError.hh:33
@ QERR_SUCCESS
Definition: QError.hh:27
error class with error type and description
Definition: QError.hh:115
void SetOwner(const std::string &owner)
set the module that is accessing this object
QError Set(GlobalHandle< Q > *gh, const std::string &outSource, bool printError=true) const
Set a QObject using a GlobalHandle.
QError Get(const std::string &owner, GlobalHandle< Q > *gh, const std::string &inSource, bool printError=true) const
Get an object using a global handle.
wrapper for global data IO
std::string fFileName
static std::string GetInitFileName(const std::string &baseName, const std::string &extension="txt")
QError WriteDouble(const std::string &name, Double_t val) const
void SetFileName(const std::string &fileName)
QError WriteString(const std::string &name, const std::string &val) const
QError GetString(const std::string &name, std::string &val) const
QError WriteBool(const std::string &name, Bool_t val) const
QError GetInt(const std::string &name, Int_t &val) const
QError GetStdVector(const std::string &name, std::vector< T > &vec) const
void SetOwner(const std::string &owner)
QError WriteStdVector(const std::string &name, const std::vector< T > &vec) const
QError GetDouble(const std::string &name, Double_t &val) const
QError GetBool(const std::string &name, Bool_t &val) const
QError WriteInt(const std::string &name, Int_t val) const
QError GetQObject(const std::string &name, T &obj) const
QError WriteQObject(const std::string &name, const T &obj) const