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
15/*
16 * History:
17 12-Jan-2017 - TJF - Original version.
18
19 */
20
21#include <drama.hh>
22#include <future>
23#include <typeinfo>
24
25namespace drama {
26 /* Used internally by Future - not for user access */
27 extern bool _futureDebugGlobalLogDetails;
28
29
72 template <class T>
73 class Future : public std::future<T> {
74 using BaseFuture=std::future<T>;
75 private:
76 std::string _name; // Name used in messages.
77 std::string _type;
78 bool _logDetails = false; // Object specific log flag.
79 public:
80
86 void LogOn() {
87 _logDetails = true;
88 }
94 static void GlobalLogOn() {
95 _futureDebugGlobalLogDetails = true;
96 }
103 void LogOff() {
104 _logDetails = false;
105 }
114 static void GlobalLogOff() {
115 _futureDebugGlobalLogDetails = false;
116 }
125 static bool GetGlobalLogState() {
126 return _futureDebugGlobalLogDetails;
127 }
135 bool GetLogState() const {
136 return _logDetails;
137 }
142 void SetName(const std::string &newName) {
143 _name = newName;
144 }
145
156 Future(const std::string &name="unnamed",
157 bool logOn=false) noexcept : _name(name), _logDetails(logOn) {
158 T a;
159 _type = typeid(a).name();
160 }
161
166 Future(const Future&) = delete;
167
168
174 Future& operator=(Future &&rhs) noexcept {
175 *this = std::move(rhs);
176 return *this;
177 }
178
191 Future& operator=( BaseFuture &&rhs) noexcept {
192 *((BaseFuture *)(this)) = std::move(rhs);
193 if ((_logDetails)||( _futureDebugGlobalLogDetails))
194 {
195 std::cerr << fmt::format("drama::Future<{0}> \"{1}\" assigned",
196 _type, _name) << std::endl;
197 }
198 return *this;
199 }
200
204 Future& operator=(const Future &) = delete;
205
219 T get() {
220 if ((_logDetails)||( _futureDebugGlobalLogDetails))
221 {
222 std::cerr << fmt::format("drama::Future<{0}> \"{1}\" get() invoked",
223 _type, _name) << std::endl;
224 }
225 /*
226 * Run get() on the base class. This may throw and we
227 * want to log that.
228 */
229 T result;
230 try
232 result = ((BaseFuture *)(this))->get();
233 }
234 catch (...)
235 {
236 if ((_logDetails)||( _futureDebugGlobalLogDetails))
237 {
238 std::cerr << fmt::format("drama::Future<{0}> \"{1}\" get() threw",
239 _type, _name) << std::endl;
240 }
241 throw;
242 }
243 /*
244 * did not throw.
245 */
246 if ((_logDetails)||( _futureDebugGlobalLogDetails))
247 {
248 std::cerr << fmt::format("drama::Future<{0}> \"{1}\" get() returning",
249 _type, _name) << std::endl;
250 }
251 return result;
252 }
253
268 ~Future() {
269 if ((_logDetails)||( _futureDebugGlobalLogDetails))
270 {
271 std::cerr << fmt::format("drama::Future<{0}> \"{1}\" destroyed",
272 _type, _name) << std::endl;
273 }
274 }
275
276 };
277
278
317 template <>
318 class Future<void> : public std::future<void> {
319 using BaseFuture = std::future<void>;
320 private:
321 std::string _name; // Name used in messages.
322 bool _logDetails = false; // Object specific log flag.
323 public:
324
330 void LogOn() {
331 _logDetails = true;
332 }
338 static void GlobalLogOn() {
339 _futureDebugGlobalLogDetails = true;
340 }
347 void LogOff() {
348 _logDetails = false;
349 }
358 static void GlobalLogOff() {
359 _futureDebugGlobalLogDetails = false;
360 }
369 static bool GetGlobalLogState() {
370 return _futureDebugGlobalLogDetails;
371 }
379 bool GetLogState() const {
380 return _logDetails;
381 }
386 void SetName(const std::string &newName) {
387 _name = newName;
388 }
389
400 Future(const std::string &name="unnamed", bool logOn=false) noexcept : _name(name), _logDetails(logOn) {}
405 Future(const Future&) = delete;
412 Future& operator=(Future &&rhs) noexcept {
413 _name = std::move(rhs._name);
414 _logDetails = rhs._logDetails;
415 return *this;
416 }
417
430 Future& operator=(BaseFuture &&rhs) noexcept {
431 *((BaseFuture *)(this)) = std::move(rhs);
432 if ((_logDetails)||( _futureDebugGlobalLogDetails))
433 {
434 std::cerr << fmt::format("drama::Future<void> \"{0}\" assigned", _name) << std::endl;
435 }
436 return *this;
437 }
440 Future& operator=(const Future &) = delete;
441
455 void get() {
456 if ((_logDetails)||( _futureDebugGlobalLogDetails))
458 std::cerr << fmt::format("drama::Future<void> \"{0}\" get() invoked", _name) << std::endl;
459 }
460 /*
461 * Run get() on the base class. This may throw and we
462 * want to log that.
463 */
464 try
465 {
466 ((BaseFuture *)(this))->get();
468 catch (...)
469 {
470 if ((_logDetails)||( _futureDebugGlobalLogDetails))
471 {
472 std::cerr << fmt::format("drama::Future<void> \"{0}\" get() threw", _name) << std::endl;
473 }
474 throw;
475 }
476 /*
477 * did not throw.
478 */
479 if ((_logDetails)||( _futureDebugGlobalLogDetails))
480 {
481 std::cerr << fmt::format("drama::Future<void> \"{0}\" get() returning", _name) << std::endl;
483 }
498 ~Future() {
499 if ((_logDetails)||( _futureDebugGlobalLogDetails))
500 {
501 if (valid())
502 {
503 std::cerr << fmt::format("drama::Future<void> \"{0}\" destroyed - but is still valid (which could be a problem)!!", _name) << std::endl;
504 }
505 else
506 {
507 std::cerr << fmt::format("drama::Future<void> \"%\" destroyed - was not valid (which is ok).", _name) << std::endl;
508 }
509 }
510 }
511
512 };
513
514
515
516} // namespace drama
517
518#endif /* defined _DRAMA2_SDP_INC */
void get()
Get value of future.
Definition debugfuture.hh:482
static void GlobalLogOff()
Turn logging off - global.
Definition debugfuture.hh:385
static bool GetGlobalLogState()
Return the state of the global logging flag.
Definition debugfuture.hh:396
Future & operator=(const Future &)=delete
Assignment operator (non-move) - deleted.
void LogOn()
Turn logging on.
Definition debugfuture.hh:357
Future(const Future &)=delete
Copy constructor = deleted.
void LogOff()
Turn logging off.
Definition debugfuture.hh:374
void SetName(const std::string &newName)
Set the name of this object - as used in logging messages.
Definition debugfuture.hh:413
static void GlobalLogOn()
Turn logging on - global.
Definition debugfuture.hh:365
Future(const std::string &name="unnamed", bool logOn=false) noexcept
Construct a drama::Future<void> object.
Definition debugfuture.hh:427
~Future()
Destructor.
Definition debugfuture.hh:525
Future & operator=(Future &&rhs) noexcept
Move assignment operator.
Definition debugfuture.hh:439
Future & operator=(BaseFuture &&rhs) noexcept
Move assignment operator - source std::future<void>
Definition debugfuture.hh:457
bool GetLogState() const
Return the state of the object specific logging flag.
Definition debugfuture.hh:406
Future(const std::string &name="unnamed", bool logOn=false) noexcept
Construct a drama::Future object.
Definition debugfuture.hh:183
void LogOff()
Turn logging off.
Definition debugfuture.hh:130
static void GlobalLogOff()
Turn logging off - global.
Definition debugfuture.hh:141
T get()
Get value of future.
Definition debugfuture.hh:246
bool GetLogState() const
Return the state of the object specific logging flag.
Definition debugfuture.hh:162
Future & operator=(BaseFuture &&rhs) noexcept
Move assignment operator - source std::future<T>
Definition debugfuture.hh:218
static bool GetGlobalLogState()
Return the state of the global logging flag.
Definition debugfuture.hh:152
static void GlobalLogOn()
Turn logging on - global.
Definition debugfuture.hh:121
void LogOn()
Turn logging on.
Definition debugfuture.hh:113
~Future()
Destructor.
Definition debugfuture.hh:295
Future & operator=(Future &&rhs) noexcept
Move assignment operator.
Definition debugfuture.hh:201
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:169
A class which wraps a std::future<T> and provides debugging output to stderr.
Definition debugfuture.hh:100
DRAMA 2 main include file.
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1339
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:99