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 * The D2_FMT_DEPRECATED macro is marking methods that use the old
52 * SafePrintf() style of formatting string but for which we have
53 * a replacement that uses the now preferred fmt::format approach.
54 *
55 * The marking of these methods as depreciated is likely to generate
56 * lots of warning in user code, as there is a lot of code
57 * using the old approach. This impacts MessageUser() methods,
58 * LogS() methods and DramaTHROW_S() calls.
59 *
60 * You can disable the warning by defining the macro
61 * D2_NO_FMT_DEPRECATION_ALERTS before including drama.hh.
62 */
63//#define D2_NO_FMT_DEPRECATION_ALERTS // Define this to disable the format related depreciation warnings
64#if defined(D2_NO_FMT_DEPRECATION_ALERTS)
65#define D2_FMT_DEPRECATED(_a_) /* */
66
67#elif defined(__GNUC__)
68#define D2_FMT_DEPRECATED(_a_) __attribute__((deprecated(_a_)))
69
70#else
71#define D2_FMT_DEPRECATED(_a_) /* _a_ */
72#endif
73
74namespace drama {
75
90 template <typename Container>
91 void
92 stringtok (Container &container, std::string const &in,
93 const char * const delimiters = " \t\n\r")
94 {
95 /*
96 * This was sourced from
97 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_h.txt
98 *
99 * on 25/Jun/03.
100 *
101 * After following a reference on
102 * http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3
103 *
104 * indicating it was suggested by Chris King and tweaked by
105 * Petr Prikryl.
106 *
107 * There is apparently no copyright notice associated with it.
108 */
109
110 const std::string::size_type len = in.length();
111 std::string::size_type i = 0;
112
113 while ( i < len )
114 {
115 // eat leading whitespace
116 i = in.find_first_not_of (delimiters, i);
117 if (i == std::string::npos)
118 return; // nothing left but white space
120 // find the end of the token
121 std::string::size_type j = in.find_first_of (delimiters, i);
122
123 // push token
124 if (j == std::string::npos) {
125 container.push_back (in.substr(i));
126 return;
127 } else
128 container.push_back (in.substr(i, j-i));
129
130 // set up for next loop
131 i = j + 1;
132 }
133 }
134
135
152 std::string FindFile(const std::string &spec, bool impdir=false);
153
154
155
156
168 extern std::string StatusToString(StatusType status);
169
170
187 template <typename T>
188 class ScopeGuard {
189 private:
190 T & _item; // Reference to item.
191 T _exitVal; // Value to set when destroyed.
192 public:
212 ScopeGuard(T &item, const T executionVal) : _item(item) {
213 _exitVal = _item; // Save current value.
214 _item = executionVal;
237 ScopeGuard(T &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
238 _item = executionVal;
240
244 ~ScopeGuard() {
245 _item = _exitVal;
246 }
247
248 };
249
261 template <typename AT, typename T>
262 class ScopeGuardAtomic {
263 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)");
264 private:
265 AT & _item; // Reference to item.
266 T _exitVal; // Value to set when destroyed.
267 public:
290 ScopeGuardAtomic(AT &item, const T executionVal) : _item(item) {
291 _exitVal = _item.load(); // Save current value.
292 _item.store(executionVal);
293 }
319 ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal) : _item(item), _exitVal(exitVal) {
320 _item.store(executionVal);
321 }
326 _item.store(_exitVal);
327 }
328
329
330
331 };
332
333
334 /* Required for void_t definition below */
335 template< class... > struct voider { using type = void; };
344 template< class... T0toN > using void_t = typename voider<T0toN...>::type;
345
350 template <typename, typename = drama::void_t<>> struct is_stl_container : std::false_type{};
356 template <typename T> struct is_stl_container<T, drama::void_t<typename T::iterator>> : std::true_type{};
366 template <typename T> constexpr bool is_stl_container_v = is_stl_container<T>::value;
367
368
376 extern bool ActionExists(const std::string &name);
378} // namespace drama
379
380
381
382#endif
ScopeGuardAtomic(AT &item, const T executionVal, const T exitVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:346
ScopeGuardAtomic(AT &item, const T executionVal)
Construct a ScopeGuardAtomic for the specified item.
Definition util.hh:317
~ScopeGuardAtomic()
Destroy the ScopeGuardAtomic item, restoring its value to the specified exit value.
Definition util.hh:352
This class implements a simple scope guard class with an std::atomic based type as the underlying typ...
Definition util.hh:289
~ScopeGuard()
Destroy the ScopeGuard item, restoring its value to the specified exit value.
Definition util.hh:271
ScopeGuard(T &item, const T executionVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:239
ScopeGuard(T &item, const T executionVal, const T exitVal)
Construct a ScopeGuard for the specified item.
Definition util.hh:264
This class implements a simple scope guard class.
Definition util.hh:215
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1339
bool ActionExists(const std::string &name)
Determines if action exists.
constexpr bool is_stl_container_v
A type trait to helper determine if a type is an STL container.
Definition util.hh:393
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:119
typename voider< T0toN... >::type void_t
Define a void_t equivalent.
Definition util.hh:371
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:99
Type trait returning true of the specified type is NOT a STL container.
Definition util.hh:377