C++Instrument Catalog MFC 1.2
Metrolab C++InstrumentCatalog for MFC
Loading...
Searching...
No Matches
Helpers.h
Go to the documentation of this file.
1
4
5#pragma once
6
7//---------------------------------------------------------------------------//
8// Standard includes
9//---------------------------------------------------------------------------//
10#include <functional> // std::function
11#include <mutex> // std::mutex
12#include <iostream> // ostream
13#include <sstream> // std::cerr, std::cout, std::wcout
14#include <cassert> // assert
15#include <thread> // std::thread
16#include <chrono> // std::chrono
17
18//---------------------------------------------------------------------------//
19// String helpers
20//---------------------------------------------------------------------------//
21#define MTL__STR_HELPER__(x) #x
22#define MTL__STR__(x) MTL__STR_HELPER__(x)
23#define MTL__LOCATION_HELPER__ __FILE__ ":" MTL__STR__(__LINE__)
24#define MTL__LOCATION__ (MTL__LOCATION_HELPER__)
25
26//---------------------------------------------------------------------------//
27// Output helpers
28//---------------------------------------------------------------------------//
29namespace MTL
30{ //CERR sink redirection
31 namespace CerrSinkRedir
32 {
33 using tOutputSink = std::function<void(const std::string &)>;
34
36 {
37 static tOutputSink s_Sink;
38 return s_Sink;
39 }
40
41 inline std::mutex & CerrSinkMutex()
42 {
43 static std::mutex s_Mutex;
44 return s_Mutex;
45 }
46
47 inline void WriteCerr(const std::string & rMessage)
48 {
49 tOutputSink l_Sink;
50 {
51 std::lock_guard<std::mutex> l_Lock(CerrSinkMutex());
52 l_Sink = CerrSink();
53 }
54
55 if (l_Sink)
56 l_Sink(rMessage);
57 else
58 std::cerr << rMessage;
59 }
60 } // namespace CerrSinkRedir
61
63 {
64 std::lock_guard<std::mutex> l_Lock(CerrSinkRedir::CerrSinkMutex());
65 auto l_PreviousSink = CerrSinkRedir::CerrSink();
66 CerrSinkRedir::CerrSink() = std::move(rSink);
67 return l_PreviousSink;
68 }
69
70 inline void ResetCerrSink()
71 {
72 std::lock_guard<std::mutex> l_Lock(CerrSinkRedir::CerrSinkMutex());
73 CerrSinkRedir::CerrSink() = nullptr;
74 }
75} // namespace MTL
76
77#ifndef MTL_MULTITHREAD_APPLICATION
78 #ifdef MTL_CERR_SINK_LOG_REDIRECTION
79 #define CERR(__X__) {std::ostringstream mtl_Os; mtl_Os << __X__; MTL::CerrSinkRedir::WriteCerr(mtl_Os.str());} //CERR sink redirection
80 #else
81 #define CERR(__X__) std::cerr << __X__
82 #endif
83#define COUT(__X__) std::cout << __X__
84#define WCOUT(__X__) std::wcout << __X__
85#else
86// "Synchronized" output helpers : allow to define thread safe methods, redirection to files, etc... To be defined in user code
87extern inline void MTLLockOutputStream();
88extern inline void MTLUnlockOutputStream();
89 #ifdef MTL_CERR_SINK_LOG_REDIRECTION
90 #define CERR(__X__) {MTLLockOutputStream(); std::ostringstream mtl_Os; mtl_Os << __X__; MTL::CerrSinkRedir::WriteCerr(mtl_Os.str()); MTLUnlockOutputStream();}
91 #else
92 #define CERR(__X__) {MTLLockOutputStream(); std::cerr << __X__; MTLUnlockOutputStream();}
93 #endif
94#define COUT(__X__) {MTLLockOutputStream(); std::cout << __X__; MTLUnlockOutputStream();}
95#define WCOUT(__X__) {MTLLockOutputStream(); std::wcout << __X__; MTLUnlockOutputStream();}
96#endif
97
98//---------------------------------------------------------------------------//
99// Other helpers
100//---------------------------------------------------------------------------//
101// Definitions
102#define MTL_Assert assert
103#define MTL_StaticAssert static_assert
104#define MTL_SleepMs(_ms_) std::this_thread::sleep_for(std::chrono::milliseconds(_ms_))
105#define MTL_Unused(x) (void)x;
void ResetCerrSink()
Definition Helpers.h:70
CerrSinkRedir::tOutputSink SetCerrSink(CerrSinkRedir::tOutputSink rSink)
Definition Helpers.h:62
std::function< void(const std::string &)> tOutputSink
Definition Helpers.h:33
std::mutex & CerrSinkMutex()
Definition Helpers.h:41
tOutputSink & CerrSink()
Definition Helpers.h:35
void WriteCerr(const std::string &rMessage)
Definition Helpers.h:47