ARC SDK
Utils.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef __ARC_UTILS_H__
4 #define __ARC_UTILS_H__
5 
6 #include <cstdlib>
7 // NOTE: On Solaris errno is not working properly if cerrno is included first
8 #include <cerrno>
9 #include <string>
10 #include <list>
11 
12 namespace Arc {
13 
17  std::string GetEnv(const std::string& var);
19 
21  std::string GetEnv(const std::string& var, bool &found);
22 
24  std::list<std::string> GetEnv();
25 
27  bool SetEnv(const std::string& var, const std::string& value, bool overwrite = true);
28 
30  void UnsetEnv(const std::string& var);
31 
32  // These are functions to be used used exclusively for solving
33  // problem with specific libraries which depend too much on
34  // environment variables.
36 
38  void EnvLockAcquire(void);
40 
42  void EnvLockRelease(void);
44 
47  void EnvLockWrap(bool all = false);
49 
52  void EnvLockUnwrap(bool all = false);
54 
57  void EnvLockUnwrapComplete(void);
58 
60 
64  private:
65  bool all_;
66  public:
68 
69  EnvLockWrapper(bool all = false):all_(all) { EnvLockWrap(all_); };
71  ~EnvLockWrapper(void) { EnvLockUnwrap(all_); };
72  };
73 
75 
77  public:
79  ~InterruptGuard();
80  private:
81  void (*saved_sigint_handler)(int);
82  };
83 
85  std::string StrError(int errnum = errno);
86 
88 
93  template<typename T>
94  class AutoPointer {
95  private:
96  T *object;
97  void (*deleter)(T*);
98  static void DefaultDeleter(T* o) { delete o; }
99  void operator=(const AutoPointer<T>&);
100 #if __cplusplus >= 201103L
101  private:
102  AutoPointer(AutoPointer<T> const&);
103 #else
104  // Workaround for older gcc which does not implement construction
105  // of new elements in std::list according to specification.
106  public:
107  AutoPointer(AutoPointer<T> const& o)
108  : object(NULL), deleter(&DefaultDeleter) {
109  operator=(const_cast<AutoPointer<T>&>(o));
110  }
111 #endif
112  public:
114  AutoPointer(void (*d)(T*) = &DefaultDeleter)
115  : object(NULL), deleter(d) {}
117  AutoPointer(T *o, void (*d)(T*) = &DefaultDeleter)
118  : object(o), deleter(d) {}
121  : object(o.Release()), deleter(o.deleter) {}
123  ~AutoPointer(void) {
124  if (object) if(deleter) (*deleter)(object);
125  object = NULL;
126  }
127  AutoPointer<T>& operator=(T* o) {
128  if (object) if(deleter) (*deleter)(object);
129  object = o;
130  return *this;
131  }
132  AutoPointer<T>& operator=(AutoPointer<T>& o) {
133  if (object) if(deleter) (*deleter)(object);
134  object = o.object;
135  o.object = NULL;
136  return *this;
137  }
139  T& operator*(void) const {
140  return *object;
141  }
143  T* operator->(void) const {
144  return object;
145  }
147  operator bool(void) const {
148  return (object != NULL);
149  }
151  bool operator!(void) const {
152  return (object == NULL);
153  }
155  T* Ptr(void) const {
156  return object;
157  }
158  T& operator[](int pos) {
159  return object[pos];
160  }
161  T const& operator[](int pos) const {
162  return object[pos];
163  }
165  T* Release(void) {
166  T* tmp = object;
167  object = NULL;
168  return tmp;
169  }
170  };
171 
173 
182  template<typename T>
184  private:
185  template<typename P>
186  class Base {
187  private:
188  Base(Base<P>&);
189  public:
190  int cnt;
191  P *ptr;
192  bool released;
193  Base(P *p)
194  : cnt(0),
195  ptr(p),
196  released(false) {
197  add();
198  }
199  ~Base(void) {
200  if (ptr && !released)
201  delete ptr;
202  }
203  Base<P>* add(void) {
204  ++cnt;
205  return this;
206  }
207  bool rem(void) {
208  if (--cnt == 0) {
209  if(!released) delete this;
210  return true;
211  }
212  return false;
213  }
214  };
215  Base<T> *object;
216  public:
217  CountedPointer(T *p = NULL)
218  : object(new Base<T>(p)) {}
220  : object(p.object->add()) {}
221  ~CountedPointer(void) {
222  object->rem();
223  }
224  CountedPointer<T>& operator=(T *p) {
225  if (p != object->ptr) {
226  object->rem();
227  object = new Base<T>(p);
228  }
229  return *this;
230  }
231  CountedPointer<T>& operator=(const CountedPointer<T>& p) {
232  if (p.object->ptr != object->ptr) {
233  object->rem();
234  object = p.object->add();
235  }
236  return *this;
237  }
239  T& operator*(void) const {
240  return *(object->ptr);
241  }
243  T* operator->(void) const {
244  return (object->ptr);
245  }
247  operator bool(void) const {
248  return ((object->ptr) != NULL);
249  }
251  bool operator!(void) const {
252  return ((object->ptr) == NULL);
253  }
255  bool operator==(const CountedPointer& p) const {
256  return ((object->ptr) == (p.object->ptr));
257  }
259  bool operator!=(const CountedPointer& p) const {
260  return ((object->ptr) != (p.object->ptr));
261  }
263  bool operator<(const CountedPointer& p) const {
264  return ((object->ptr) < (p.object->ptr));
265  }
267  T* Ptr(void) const {
268  return (object->ptr);
269  }
271  T* Release(void) {
272  T* tmp = object->ptr;
273  object->released = true;
274  return tmp;
275  }
276  };
277 
279  bool PersistentLibraryInit(const std::string& name);
280 
283 } // namespace Arc
284 
285 #endif // __ARC_UTILS_H__
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
Class to provide automatic locking/unlocking of environment on creation/destruction.
Definition: Utils.h:63
T * operator->(void) const
For referring wrapped object.
Definition: Utils.h:143
~EnvLockWrapper(void)
Release environment lock.
Definition: Utils.h:71
T * operator->(void) const
For referring wrapped object.
Definition: Utils.h:243
bool operator!(void) const
Returns true if pointer is NULL and false otherwise.
Definition: Utils.h:151
T & operator*(void) const
For referring wrapped object.
Definition: Utils.h:139
AutoPointer(T *o, void(*d)(T *)=&DefaultDeleter)
Constructor which wraps pointer and optionally defines deletion function.
Definition: Utils.h:117
bool operator<(const CountedPointer &p) const
Comparison operator.
Definition: Utils.h:263
void EnvLockUnwrapComplete(void)
Use after fork() to reset all internal variables and release all locks.
Wrapper for pointer with automatic destruction and multiple references.
Definition: Utils.h:183
T * Ptr(void) const
Cast to original pointer.
Definition: Utils.h:155
T & operator*(void) const
For referring wrapped object.
Definition: Utils.h:239
void EnvLockWrap(bool all=false)
Start code which is using setenv/getenv.
std::string StrError(int errnum=errno)
Portable function for obtaining description of last system error.
EnvLockWrapper(bool all=false)
Create a new environment lock for using setenv/getenv.
Definition: Utils.h:69
AutoPointer(AutoPointer< T > &o)
Moving constructor.
Definition: Utils.h:120
bool operator!(void) const
Returns true if pointer is NULL and false otherwise.
Definition: Utils.h:251
AutoPointer(void(*d)(T *)=&DefaultDeleter)
NULL pointer constructor.
Definition: Utils.h:114
void EnvLockUnwrap(bool all=false)
End code which is using setenv/getenv.
void EnvLockAcquire(void)
Obtain lock on environment.
Marks off a section of code which should not be interrupted by signals.
Definition: Utils.h:76
void UnsetEnv(const std::string &var)
Portable function for unsetting environment variables. Protected by exclusive lock.
bool PersistentLibraryInit(const std::string &name)
Load library and keep persistent.
void EnvLockRelease(void)
Release lock on environment.
Wrapper for pointer with automatic destruction.
Definition: Utils.h:94
bool SetEnv(const std::string &var, const std::string &value, bool overwrite=true)
Portable function for setting environment variables. Protected by exclusive lock. ...
T * Release(void)
Release referred object so that it can be passed to other container.
Definition: Utils.h:165
bool operator!=(const CountedPointer &p) const
Returns true if pointers are not equal.
Definition: Utils.h:259
T * Release(void)
Release referred object so that it can be passed to other container.
Definition: Utils.h:271
T * Ptr(void) const
Cast to original pointer.
Definition: Utils.h:267
std::string GetEnv(const std::string &var)
Portable function for getting environment variables. Protected by shared lock.
~AutoPointer(void)
Destructor destroys wrapped object using assigned deleter.
Definition: Utils.h:123
bool operator==(const CountedPointer &p) const
Returns true if pointers are equal.
Definition: Utils.h:255