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#include <type_traits>
34
35#ifdef RUNNING_DOXYGEN
36#define D2DEPRECATED /* */
37
38#else
39
40#ifdef __GNUC__
41#define D2DEPRECATED __attribute__ ((deprecated))
42#define D2DEPRECATED_EXPLAIN(_a_) __attribute__ ((deprecated(_a_))) // Version with string to help resolve.
43#else
44#define D2DEPRECATED /* */
45#define D2DEPRECATED_EXPLAIN(_a_) /* _a_ */
46#endif
47
48#endif // RUNNING_DOXYGEN
49
50
51
52namespace drama {
53
68 template <typename Container>
69 void
70 stringtok (Container &container, std::string const &in,
71 const char * const delimiters = " \t\n\r")
72 {
73 /*
74 * This was sourced from
75 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_h.txt
76 *
77 * on 25/Jun/03.
78 *
79 * After following a reference on
80 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3
81 *
82 * indicating it was suggested by Chris King and tweaked by
83 * Petr Prikryl.
84 *
85 * There is apparently no copyright notice associated with it.
86 */
87
88 const std::string::size_type len = in.length();
89 std::string::size_type i = 0;
90
91 while ( i < len )
92 {
93 // eat leading whitespace
94 i = in.find_first_not_of (delimiters, i);
95 if (i == std::string::npos)
96 return; // nothing left but white space
98 // find the end of the token
99 std::string::size_type j = in.find_first_of (delimiters, i);
100
101 // push token
102 if (j == std::string::npos) {
103 container.push_back (in.substr(i));
104 return;
105 } else
106 container.push_back (in.substr(i, j-i));
107
108 // set up for next loop
109 i = j + 1;
110 }
111 }
112
113
130 std::string FindFile(const std::string &spec, bool impdir=false);
131
132
133
134
146 extern std::string StatusToString(StatusType status);
147
148
165 template <typename T>
166 class ScopeGuard {
167 private:
168 T & _item; // Reference to item.
169 T _exitVal; // Value to set when destroyed.
170 public:
190 ScopeGuard(T &item, const T executionVal) : _item(item) {
191 _exitVal = _item; // Save current value.
192 _item = executionVal;
215 ScopeGuard(T &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
216 _item = executionVal;
218
222 ~ScopeGuard() {
223 _item = _exitVal;
224 }
225
226 };
227
239 template <typename AT, typename T>
240 class ScopeGuardAtomic {
241 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)");
242 private:
243 AT & _item; // Reference to item.
244 T _exitVal; // Value to set when destroyed.
245 public:
268 ScopeGuardAtomic(AT &item, const T executionVal) : _item(item) {
269 _exitVal = _item.load(); // Save current value.
270 _item.store(executionVal);
271 }
297 ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
298 _item.store(executionVal);
299 }
304 _item.store(_exitVal);
305 }
306
307
308
309 };
310
311
312 /* Required for void_t definition below */
313 template< class... > struct voider { using type = void; };
322 template< class... T0toN > using void_t = typename voider<T0toN...>::type;
323
328 template <typename, typename = drama::void_t<>> struct is_stl_container : std::false_type{};
334 template <typename T> struct is_stl_container<T, drama::void_t<typename T::iterator>> : std::true_type{};
344 template <typename T> constexpr bool is_stl_container_v = is_stl_container<T>::value;
345
346
347} // namespace drama
348
350
351#endif
ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:324
ScopeGuardAtomic(AT &item, const T executionVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:295
~ScopeGuardAtomic()
Destroy the ScopeGuardAtomic item, restoring its value to the specified exit value.
Definition util.hh:330
This class implements a simple scope guard class with an std::atomic based type as the underlying typ...
Definition util.hh:267
~ScopeGuard()
Destroy the ScopeGuard item, restoring its value to the specified exit value.
Definition util.hh:249
ScopeGuard(T &item, const T executionVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:217
ScopeGuard(T &item, const T executionVal, const T exitVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:242
This class implements a simple scope guard class.
Definition util.hh:193
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1327
constexpr bool is_stl_container_v
A type trait to helper determine if a type is an STL container.
Definition util.hh:371
void stringtok(Container &container, std::string const &in, const char *const delimiters=" \t\n\r")
stringtok is a replacement for C's strtok() function.
Definition util.hh:97
typename voider< T0toN... >::type void_t
Define a void_t equivalent.
Definition util.hh:349
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
Type trait returning true of the specified type is NOT a STL container.
Definition util.hh:355