AAO DRAMA/DRAMA2 C++ Interface
DRAMA C++11 and later interface
debugfuture.hh
Go to the documentation of this file.
1#ifndef _DRAMA2_DEBUGFUTURE_INC
2#define _DRAMA2_DEBUGFUTURE_INC
21/*
22 * History:
23 12-Jan-2017 - TJF - Original version.
24
25 * The above ID is for Doxygen, this one has the format ACMM is looking for.
26 * "@(#) $Id$"
27 */
28
29#include <drama.hh>
30
31#include <future>
32#include <typeinfo>
33
34namespace drama {
35 /* Used internally by Future - not for user access */
36 extern bool _futureDebugGlobalLogDetails;
37
38
81 template <class T>
82 class Future : public std::future<T> {
83 using BaseFuture=std::future<T>;
84 private:
85 std::string _name; // Name used in messages.
86 std::string _type;
87 bool _logDetails = false; // Object specific log flag.
88 public:
89
95 void LogOn() {
96 _logDetails = true;
97 }
103 static void GlobalLogOn() {
104 _futureDebugGlobalLogDetails = true;
105 }
112 void LogOff() {
113 _logDetails = false;
114 }
123 static void GlobalLogOff() {
124 _futureDebugGlobalLogDetails = false;
125 }
134 static bool GetGlobalLogState() {
135 return _futureDebugGlobalLogDetails;
136 }
144 bool GetLogState() const {
145 return _logDetails;
146 }
151 void SetName(const std::string &newName) {
152 _name = newName;
153 }
154
165 Future(const std::string &name="unnamed",
166 bool logOn=false) noexcept : _name(name), _logDetails(logOn) {
167 T a;
168 _type = typeid(a).name();
169 }
170
175 Future(const Future&) = delete;
176
177
183 Future& operator=(Future &&rhs) noexcept {
184 *this = std::move(rhs);
185 return *this;
186 }
187
200 Future& operator=( BaseFuture &&rhs) noexcept {
201 *((BaseFuture *)(this)) = std::move(rhs);
202 if ((_logDetails)||( _futureDebugGlobalLogDetails))
203 drama::TSafePrintf(std::cerr, "drama::Future<%> \"%\" assigned\n",
204 _type, _name);
205 return *this;
206 }
207
208
211 Future& operator=(const Future &) = delete;
212
226 T get() {
227 if ((_logDetails)||( _futureDebugGlobalLogDetails))
228 drama::TSafePrintf(std::cerr,
229 "drama::Future<%> \"%\" get() invoked\n",
230 _type, _name);
231 /*
232 * Run get() on the base class. This may throw and we
233 * want to log that.
234 */
235 T result;
236 try
237 {
238 result = ((BaseFuture *)(this))->get();
239 }
240 catch (...)
241 {
242 if ((_logDetails)||( _futureDebugGlobalLogDetails))
243 drama::TSafePrintf(std::cerr,
244 "drama::Future<%> \"%\" get() threw\n",
245 _type, _name);
246 throw;
247 }
248 /*
249 * did not throw.
250 */
251 if ((_logDetails)||( _futureDebugGlobalLogDetails))
252 drama::TSafePrintf(std::cerr,
253 "drama::Future<%> \"%\" get() returning\n",
254 _type, _name);
255 return result;
256 }
257
272 ~Future() {
273 if ((_logDetails)||( _futureDebugGlobalLogDetails))
274 drama::TSafePrintf(std::cerr,
275 "drama::Future<%> \"%\" destroyed\n",
276 _type, _name);
277 }
278
279 };
280
281
320 template <>
321 class Future<void> : public std::future<void> {
322 using BaseFuture = std::future<void>;
323 private:
324 std::string _name; // Name used in messages.
325 bool _logDetails = false; // Object specific log flag.
326 public:
327
333 void LogOn() {
334 _logDetails = true;
335 }
341 static void GlobalLogOn() {
342 _futureDebugGlobalLogDetails = true;
343 }
350 void LogOff() {
351 _logDetails = false;
352 }
361 static void GlobalLogOff() {
362 _futureDebugGlobalLogDetails = false;
363 }
372 static bool GetGlobalLogState() {
373 return _futureDebugGlobalLogDetails;
374 }
382 bool GetLogState() const {
383 return _logDetails;
384 }
389 void SetName(const std::string &newName) {
390 _name = newName;
391 }
392
403 Future(const std::string &name="unnamed", bool logOn=false) noexcept : _name(name), _logDetails(logOn) {}
408 Future(const Future&) = delete;
415 Future& operator=(Future &&rhs) noexcept {
416 _name = std::move(rhs._name);
417 _logDetails = rhs._logDetails;
418 return *this;
419 }
420
433 Future& operator=(BaseFuture &&rhs) noexcept {
434 *((BaseFuture *)(this)) = std::move(rhs);
435 if ((_logDetails)||( _futureDebugGlobalLogDetails))
436 drama::TSafePrintf(std::cerr, "drama::Future<void> \"%\" assigned\n", _name);
437 return *this;
438 }
441 Future& operator=(const Future &) = delete;
456 void get() {
457 if ((_logDetails)||( _futureDebugGlobalLogDetails))
458 drama::TSafePrintf(std::cerr, "drama::Future<void> \"%\" get() invoked\n", _name);
459 /*
460 * Run get() on the base class. This may throw and we
461 * want to log that.
462 */
463 try
464 {
465 ((BaseFuture *)(this))->get();
466 }
467 catch (...)
469 if ((_logDetails)||( _futureDebugGlobalLogDetails))
470 drama::TSafePrintf(std::cerr, "drama::Future<void> \"%\" get() threw\n", _name);
471 throw;
472 }
473 /*
474 * did not throw.
475 */
476 if ((_logDetails)||( _futureDebugGlobalLogDetails))
477 drama::TSafePrintf(std::cerr, "drama::Future<void> \"%\" get() returning\n", _name);
478 }
493 ~Future() {
494 if ((_logDetails)||( _futureDebugGlobalLogDetails))
495 {
496 if (valid())
497 drama::TSafePrintf(std::cerr, "drama::Future<void> \"%\" destroyed - but is still valid (which could be a problem)!!\n", _name);
498 else
499 drama::TSafePrintf(std::cerr, "drama::Future<void> \"%\" destroyed - was not valid (which is ok).\n", _name);
500 }
501 }
502
503 };
504
505
506
507} // namespace drama
508
509#endif /* defined _DRAMA2_SDP_INC */
void get()
Get value of future.
Definition debugfuture.hh:483
static void GlobalLogOff()
Turn logging off - global.
Definition debugfuture.hh:388
static bool GetGlobalLogState()
Return the state of the global logging flag.
Definition debugfuture.hh:399
Future & operator=(const Future &)=delete
Assignment operator (non-move) - deleted.
void LogOn()
Turn logging on.
Definition debugfuture.hh:360
Future(const Future &)=delete
Copy constructor = deleted.
void LogOff()
Turn logging off.
Definition debugfuture.hh:377
void SetName(const std::string &newName)
Set the name of this object - as used in logging messages.
Definition debugfuture.hh:416
static void GlobalLogOn()
Turn logging on - global.
Definition debugfuture.hh:368
Future(const std::string &name="unnamed", bool logOn=false) noexcept
Construct a drama::Future<void> object.
Definition debugfuture.hh:430
~Future()
Destructor.
Definition debugfuture.hh:520
Future & operator=(Future &&rhs) noexcept
Move assignment operator.
Definition debugfuture.hh:442
Future & operator=(BaseFuture &&rhs) noexcept
Move assignment operator - source std::future<void>
Definition debugfuture.hh:460
bool GetLogState() const
Return the state of the object specific logging flag.
Definition debugfuture.hh:409
Future(const std::string &name="unnamed", bool logOn=false) noexcept
Construct a drama::Future object.
Definition debugfuture.hh:192
void LogOff()
Turn logging off.
Definition debugfuture.hh:139
static void GlobalLogOff()
Turn logging off - global.
Definition debugfuture.hh:150
T get()
Get value of future.
Definition debugfuture.hh:253
bool GetLogState() const
Return the state of the object specific logging flag.
Definition debugfuture.hh:171
Future & operator=(BaseFuture &&rhs) noexcept
Move assignment operator - source std::future<T>
Definition debugfuture.hh:227
static bool GetGlobalLogState()
Return the state of the global logging flag.
Definition debugfuture.hh:161
static void GlobalLogOn()
Turn logging on - global.
Definition debugfuture.hh:130
void LogOn()
Turn logging on.
Definition debugfuture.hh:122
~Future()
Destructor.
Definition debugfuture.hh:299
Future & operator=(Future &&rhs) noexcept
Move assignment operator.
Definition debugfuture.hh:210
Future & operator=(const Future &)=delete
Assignment operator (non-move) - deleted.
Future(const Future &)=delete
Copy constructor = deleted.
void SetName(const std::string &newName)
Set the name of this object - as used in logging messages.
Definition debugfuture.hh:178
A class which wraps a std::future<T> and provides debugging output to stderr.
Definition debugfuture.hh:109
DRAMA 2 main include file.
void TSafePrintf(std::ostream &ostream, const char *format, Types... args)
Safe formated write to a stream, thread compatible.
Definition drama.hh:329
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1322
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:93