AAO DRAMA/DRAMA2 C++ Interface
DRAMA C++11 and later interface
util.hh
Go to the documentation of this file.
1#ifndef _DRAMA2_UTIL_INC
2#define _DRAMA2_UTIL_INC
3
19/*
20 * History:
21 07-Mar-2015 - TJF - Original version
22 30-Sep-2016 - TJF - Start of history after development.
23 Add StatusToString().
24
25 * The above ID is for Doxygen, this one has the format ACMM is looking for.
26 * "@(#) $Id$"
27 */
28
29#include <string>
30#include "drama2_err.h"
31#include "status.h"
32#include <atomic>
33
34#ifdef RUNNING_DOXYGEN
35#define D2DEPRECATED /* */
36
37#else
38
39#ifdef __GNUC__
40#define D2DEPRECATED __attribute__ ((deprecated))
41#define D2DEPRECATED_EXPLAIN(_a_) __attribute__ ((deprecated(_a_))) // Version with string to help resolve.
42#else
43#define D2DEPRECATED /* */
44#define D2DEPRECATED_EXPLAIN(_a_) /* _a_ */
45#endif
46
47#endif // RUNNING_DOXYGEN
48
49
50
51namespace drama {
52
67 template <typename Container>
68 void
69 stringtok (Container &container, std::string const &in,
70 const char * const delimiters = " \t\n\r")
71 {
72 /*
73 * This was sourced from
74 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_h.txt
75 *
76 * on 25/Jun/03.
77 *
78 * After following a reference on
79 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3
80 *
81 * indicating it was suggested by Chris King and tweaked by
82 * Petr Prikryl.
83 *
84 * There is apparently no copyright notice associated with it.
85 */
86
87 const std::string::size_type len = in.length();
88 std::string::size_type i = 0;
89
90 while ( i < len )
91 {
92 // eat leading whitespace
93 i = in.find_first_not_of (delimiters, i);
94 if (i == std::string::npos)
95 return; // nothing left but white space
97 // find the end of the token
98 std::string::size_type j = in.find_first_of (delimiters, i);
99
100 // push token
101 if (j == std::string::npos) {
102 container.push_back (in.substr(i));
103 return;
104 } else
105 container.push_back (in.substr(i, j-i));
106
107 // set up for next loop
108 i = j + 1;
109 }
110 }
111
112
129 std::string FindFile(const std::string &spec, bool impdir=false);
130
131
132
133
145 extern std::string StatusToString(StatusType status);
146
147
164 template <typename T>
165 class ScopeGuard {
166 private:
167 T & _item; // Reference to item.
168 T _exitVal; // Value to set when destroyed.
169 public:
189 ScopeGuard(T &item, const T executionVal) : _item(item) {
190 _exitVal = _item; // Save current value.
191 _item = executionVal;
214 ScopeGuard(T &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
215 _item = executionVal;
217
221 ~ScopeGuard() {
222 _item = _exitVal;
223 }
224
225 };
226
238 template <typename AT, typename T>
239 class ScopeGuardAtomic {
240 static_assert(std::is_same<AT, std::atomic<T> >::value, "Type AT is not of type std::atomic<T> (Where T = initial/exit value type and AT is the type of the item we are guarding)");
241 private:
242 AT & _item; // Reference to item.
243 T _exitVal; // Value to set when destroyed.
244 public:
267 ScopeGuardAtomic(AT &item, const T executionVal) : _item(item) {
268 _exitVal = _item.load(); // Save current value.
269 _item.store(executionVal);
270 }
296 ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
297 _item.store(executionVal);
298 }
303 _item.store(_exitVal);
304 }
305
306
307
308 };
309
310} // namespace drama
311
312
313
314#endif
ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:323
ScopeGuardAtomic(AT &item, const T executionVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:294
~ScopeGuardAtomic()
Destroy the ScopeGuardAtomic item, restoring its value to the specified exit value.
Definition util.hh:329
This class implements a simple scope guard class with an std::atomic based type as the underlying typ...
Definition util.hh:266
~ScopeGuard()
Destroy the ScopeGuard item, restoring its value to the specified exit value.
Definition util.hh:248
ScopeGuard(T &item, const T executionVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:216
ScopeGuard(T &item, const T executionVal, const T exitVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:241
This class implements a simple scope guard class.
Definition util.hh:192
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1327
void stringtok(Container &container, std::string const &in, const char *const delimiters=" \t\n\r")
stringtok is a replacment for C's strtok() function.
Definition util.hh:96
std::string FindFile(const std::string &spec, bool impdir=false)
Find a file using DulFindFile() specifications.
std::string StatusToString(StatusType status)
Convert a DRAMA status code to text.
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:93