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 
11 namespace Arc {
12 
16 
17  std::string GetEnv(const std::string& var);
18 
20  std::string GetEnv(const std::string& var, bool &found);
21 
23  bool SetEnv(const std::string& var, const std::string& value, bool overwrite = true);
24 
26  void UnsetEnv(const std::string& var);
27 
28  // These are functions to be used used exclusively for solving
29  // problem with specific libraries which depend too much on
30  // environment variables.
32 
34  void EnvLockAcquire(void);
36 
38  void EnvLockRelease(void);
40 
43  void EnvLockWrap(bool all = false);
45 
48  void EnvLockUnwrap(bool all = false);
50 
52  void EnvLockUnwrapComplete(void);
53 
55 
59  private:
60  bool all_;
61  public:
63 
64  EnvLockWrapper(bool all = false):all_(all) { EnvLockWrap(all_); };
66  ~EnvLockWrapper(void) { EnvLockUnwrap(all_); };
67  };
68 
70 
72  public:
74  ~InterruptGuard();
75  private:
76  void (*saved_sigint_handler)(int);
77  };
78 
80  std::string StrError(int errnum = errno);
81 
83 
88  template<typename T>
89  class AutoPointer {
90  private:
91  T *object;
92  void operator=(const AutoPointer<T>&) {}
93  AutoPointer(const AutoPointer&) : object(NULL) {}
94  public:
97  : object(NULL) {}
100  : object(o) {}
102  ~AutoPointer(void) {
103  if (object) delete object;
104  }
105  void operator=(T* o) {
106  if (object) delete object;
107  object = o;
108  }
110  T& operator*(void) const {
111  return *object;
112  }
114  T* operator->(void) const {
115  return object;
116  }
118  operator bool(void) const {
119  return (object != NULL);
120  }
122  bool operator!(void) const {
123  return (object == NULL);
124  }
126  T* Ptr(void) const {
127  return object;
128  }
130  T* Release(void) {
131  T* tmp = object;
132  object = NULL;
133  return tmp;
134  }
135  };
136 
138 
147  template<typename T>
149  private:
150  template<typename P>
151  class Base {
152  private:
153  Base(Base<P>&);
154  public:
155  int cnt;
156  P *ptr;
157  bool released;
158  Base(P *p)
159  : cnt(0),
160  ptr(p),
161  released(false) {
162  add();
163  }
164  ~Base(void) {
165  if (ptr && !released)
166  delete ptr;
167  }
168  Base<P>* add(void) {
169  ++cnt;
170  return this;
171  }
172  bool rem(void) {
173  if (--cnt == 0) {
174  if(!released) delete this;
175  return true;
176  }
177  return false;
178  }
179  };
180  Base<T> *object;
181  public:
182  CountedPointer(T *p = NULL)
183  : object(new Base<T>(p)) {}
185  : object(p.object->add()) {}
186  ~CountedPointer(void) {
187  object->rem();
188  }
189  CountedPointer<T>& operator=(T *p) {
190  if (p != object->ptr) {
191  object->rem();
192  object = new Base<T>(p);
193  }
194  return *this;
195  }
196  CountedPointer<T>& operator=(const CountedPointer<T>& p) {
197  if (p.object->ptr != object->ptr) {
198  object->rem();
199  object = p.object->add();
200  }
201  return *this;
202  }
204  T& operator*(void) const {
205  return *(object->ptr);
206  }
208  T* operator->(void) const {
209  return (object->ptr);
210  }
212  operator bool(void) const {
213  return ((object->ptr) != NULL);
214  }
216  bool operator!(void) const {
217  return ((object->ptr) == NULL);
218  }
220  bool operator==(const CountedPointer& p) const {
221  return ((object->ptr) == (p.object->ptr));
222  }
224  bool operator!=(const CountedPointer& p) const {
225  return ((object->ptr) != (p.object->ptr));
226  }
228  bool operator<(const CountedPointer& p) const {
229  return ((object->ptr) < (p.object->ptr));
230  }
232  T* Ptr(void) const {
233  return (object->ptr);
234  }
236  T* Release(void) {
237  T* tmp = object->ptr;
238  object->released = true;
239  return tmp;
240  }
241  };
242 
244  bool PersistentLibraryInit(const std::string& name);
245 
248 } // namespace Arc
249 
250 # endif // __ARC_UTILS_H__