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#else
42#define D2DEPRECATED /* */
43#endif
44
45#endif // RUNNING_DOXYGEN
46
47
48
49namespace drama {
50
65 template <typename Container>
66 void
67 stringtok (Container &container, std::string const &in,
68 const char * const delimiters = " \t\n\r")
69 {
70 /*
71 * This was sourced from
72 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_h.txt
73 *
74 * on 25/Jun/03.
75 *
76 * After following a reference on
77 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3
78 *
79 * indicating it was suggested by Chris King and tweaked by
80 * Petr Prikryl.
81 *
82 * There is apparently no copyright notice associated with it.
83 */
84
85 const std::string::size_type len = in.length();
86 std::string::size_type i = 0;
87
88 while ( i < len )
89 {
90 // eat leading whitespace
91 i = in.find_first_not_of (delimiters, i);
92 if (i == std::string::npos)
93 return; // nothing left but white space
95 // find the end of the token
96 std::string::size_type j = in.find_first_of (delimiters, i);
97
98 // push token
99 if (j == std::string::npos) {
100 container.push_back (in.substr(i));
101 return;
102 } else
103 container.push_back (in.substr(i, j-i));
104
105 // set up for next loop
106 i = j + 1;
107 }
108 }
109
110
127 std::string FindFile(const std::string &spec, bool impdir=false);
128
129
130
131
143 extern std::string StatusToString(StatusType status);
144
145
162 template <typename T>
163 class ScopeGuard {
164 private:
165 T & _item; // Reference to item.
166 T _exitVal; // Value to set when destroyed.
167 public:
187 ScopeGuard(T &item, const T executionVal) : _item(item) {
188 _exitVal = _item; // Save current value.
189 _item = executionVal;
212 ScopeGuard(T &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
213 _item = executionVal;
215
219 ~ScopeGuard() {
220 _item = _exitVal;
221 }
222
223 };
224
236 template <typename AT, typename T>
237 class ScopeGuardAtomic {
238 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)");
239 private:
240 AT & _item; // Reference to item.
241 T _exitVal; // Value to set when destroyed.
242 public:
265 ScopeGuardAtomic(AT &item, const T executionVal) : _item(item) {
266 _exitVal = _item.load(); // Save current value.
267 _item.store(executionVal);
268 }
294 ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
295 _item.store(executionVal);
296 }
301 _item.store(_exitVal);
302 }
303
304
305
306 };
307
308} // namespace drama
309
310
311
312#endif
ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:321
ScopeGuardAtomic(AT &item, const T executionVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:292
~ScopeGuardAtomic()
Destroy the ScopeGuardAtomic item, restoring its value to the specified exit value.
Definition util.hh:327
This class implements a simple scope guard class with an std::atomic based type as the underlying typ...
Definition util.hh:264
~ScopeGuard()
Destroy the ScopeGuard item, restoring its value to the specified exit value.
Definition util.hh:246
ScopeGuard(T &item, const T executionVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:214
ScopeGuard(T &item, const T executionVal, const T exitVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:239
This class implements a simple scope guard class.
Definition util.hh:190
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1322
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:94
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