Scope
helpers.h
Go to the documentation of this file.
1 #pragma once
2 
8 #ifdef max // If this was defined, e.g. in windef.h without NOMINMAX undefine it!
9 #define max
10 #endif
11 #ifdef min
12 #define min
13 #endif
14 
16 template<class T> T Signum(const T& _v) { return (std::abs(_v) == _v)?1:0; }
17 
19 #define LODWORD32(l) ((DWORD32)(((ULONG64)(l)) & 0xffffffff))
20 
21 #define HIDWORD32(l) ((DWORD32)((((ULONG64)(l)) >> 32) & 0xffffffff))
22 
24 template<class Interface>
25 void SafeRelease(Interface **ppInterfaceToRelease) {
26  if (*ppInterfaceToRelease != nullptr) {
27  (*ppInterfaceToRelease)->Release();
28  (*ppInterfaceToRelease) = nullptr;
29  }
30 }
31 
35 inline double round(const double& v) { return floor(v+0.5); }
36 inline uint64_t round2ui64(const double& v) { return static_cast<uint64_t>(floor(v+0.5)); }
37 inline uint32_t round2ui32(const double& v) { return static_cast<uint32_t>(floor(v+0.5)); }
38 inline uint16_t round2ui16(const double& v) { return static_cast<uint16_t>(floor(v+0.5)); }
39 inline uint8_t round2ui8(const double& v) { return static_cast<uint8_t>(floor(v+0.5)); }
40 inline int64_t round2i64(const double& v) { return static_cast<int64_t>(floor(v+0.5)); }
41 inline int32_t round2i32(const double& v) { return static_cast<int32_t>(floor(v+0.5)); }
42 inline int16_t round2i16(const double& v) { return static_cast<int16_t>(floor(v+0.5)); }
43 inline int8_t round2i8(const double& v) { return static_cast<int8_t>(floor(v+0.5)); }
44 
45 template<class T>
46 T round2(const double& v) { return static_cast<T>(floor(v+0.5)); }
54 std::wstring GetCurrentDateString();
55 
58 std::wstring GetCurrentTimeString(const bool& _filenamecompatible = true);
62 enum class ScopeMessageTag { nothing, abort };
63 
65 template<class T>
66 class ScopeMessage {
67 public:
70 
72  T cargo;
73 
74  ScopeMessage()
75  : tag(ScopeMessageTag::nothing)
76  , cargo() {
77  }
78 
80  ScopeMessage(const ScopeMessageTag& _tag, const T& _cargo)
81  : tag(_tag)
82  , cargo(_cargo) {
83  }
84 };
85 
88 
89 private:
91  mutable std::atomic_bool stop;
92 
93 private:
96 
99 
100 public:
102  StopCondition(const bool& _init = false) { stop.store(_init); }
103 
105  bool IsSet() const { return stop; }
106 
108  void Set(const bool& _a = true) { stop.store(_a); }
109 };
110 
112 template<class T>
113 struct Scaler {
115  Scaler(const double& _minin, const double& _maxin)
116  : minin(_minin)
117  , maxin(_maxin)
118  , maxout(static_cast<double>(std::numeric_limits<T>::max()))
119  , minout(static_cast<double>(std::numeric_limits<T>::min()))
120  , rangerelation( static_cast<double>(std::numeric_limits<T>::max()-std::numeric_limits<T>::min()) / (_maxin-_minin) ) {
121  assert(std::numeric_limits<T>::is_specialized);
122  assert(std::numeric_limits<T>::is_integer);
123  }
124 
126  T operator() (const double& v) const { return round2<T>((v-minin)*rangerelation+minout); }
127 
129  const double minin;
130 
132  const double maxin;
133 
135  const double maxout;
136 
138  const double minout;
139 
141  const double rangerelation;
142 };
143 
145 #ifdef _DEBUG
146 #define DBOUT(s) \
147 { \
148 std::wostringstream os_; \
149 os_ << s << std::endl; \
150 ATLTRACE2( os_.str().c_str() ); \
151 }
152 #else
153 #define DBOUT(s)
154 #endif
155 
StopCondition(const bool &_init=false)
Initialize as false.
Definition: helpers.h:102
const double minout
minimum value of type T
Definition: helpers.h:138
Thread-safe lock-free bool to signal a requested stop to the worker function currently executed in th...
Definition: helpers.h:87
const double minin
minimum of range
Definition: helpers.h:129
const double maxin
maximum of range
Definition: helpers.h:132
const double maxout
maximum value of type T
Definition: helpers.h:135
StopCondition(StopCondition &ac)
disable copy
STL namespace.
ScopeMessageTag
Tag for ScopeMessage.
Definition: helpers.h:62
Class for scaling to the full range of a datatype.
Definition: helpers.h:113
ScopeMessageTag tag
the tag of the message, at the moment only nothing or abort
Definition: helpers.h:69
Message with tag and cargo for SyncQueues between controllers.
Definition: DaqController.h:8
void SafeRelease(Interface **ppInterfaceToRelease)
A safe release for COM objects.
Definition: helpers.h:25
T operator()(const double &v) const
Definition: helpers.h:126
std::atomic_bool stop
the stop signal
Definition: helpers.h:91
T Signum(const T &_v)
Definition: helpers.h:16
Scaler(const double &_minin, const double &_maxin)
Definition: helpers.h:115
T cargo
the cargo
Definition: helpers.h:72
std::wstring GetCurrentDateString()
Definition: helpers.cpp:4
ScopeMessage(const ScopeMessageTag &_tag, const T &_cargo)
Construct.
Definition: helpers.h:80
const double rangerelation
scaler between (maxin-minin) and (maxout - minout)
Definition: helpers.h:141
StopCondition & operator=(StopCondition &ac)
disable assignment
void Set(const bool &_a=true)
Definition: helpers.h:108
std::wstring GetCurrentTimeString(const bool &_filenamecompatible=true)
Definition: helpers.cpp:12
bool IsSet() const
Definition: helpers.h:105