ATLAS Offline Software
Loading...
Searching...
No Matches
asg::MessagePrinterOverlay Class Referencefinal

an object to change the IMessagePrinter temporarily More...

#include <MessagePrinterOverlay.h>

Collaboration diagram for asg::MessagePrinterOverlay:

Public Member Functions

 MessagePrinterOverlay (IMessagePrinter *val_printer) noexcept
 overlay with the given message printer
 ~MessagePrinterOverlay () noexcept
 remove the currently overlaid printer

Static Public Member Functions

static IMessagePrinter & current () noexcept
 get the current IMessagePrinter

Private Member Functions

 MessagePrinterOverlay (MessagePrinterOverlay &)=delete
MessagePrinterOverlay operator= (MessagePrinterOverlay &)=delete

Static Private Member Functions

static IMessagePrinter *& getCurrent () noexcept
 get a reference to the pointer to the current IMessagePrinter

Private Attributes

IMessagePrinter * m_saved = nullptr
 the stored message printer we need to restore
IMessagePrinter * m_current = nullptr
 the current message printer we should have when we restore

Detailed Description

an object to change the IMessagePrinter temporarily

The idea is to use this in unit tests to replace the standard printer with a mock object. The idea is to place this on the stack and it will then replace the standard object for as long as it is on the stack.

Note that this is per se not thread-safe, but given the intended purpose that ought to be fine, i.e. there is only one MessagePrinter for all threads, so if it is replaced in a multi-threaded manner it will cause other problems.

Definition at line 28 of file MessagePrinterOverlay.h.

Constructor & Destructor Documentation

◆ MessagePrinterOverlay() [1/2]

asg::MessagePrinterOverlay::MessagePrinterOverlay ( MessagePrinterOverlay & )
privatedelete

◆ MessagePrinterOverlay() [2/2]

asg::MessagePrinterOverlay::MessagePrinterOverlay ( IMessagePrinter * val_printer)
noexcept

overlay with the given message printer

Guarantee
no-fail
Precondition
val_printer != nullptr

Definition at line 25 of file MessagePrinterOverlay.cxx.

27 : m_saved (getCurrent()), m_current (val_printer)
28 {
29 using namespace msgAsgMessaging;
30
31 if (val_printer == nullptr)
32 {
33 ANA_MSG_FATAL ("nullptr passed illegally");
34 std::abort ();
35 }
36 getCurrent() = val_printer;
37 }
#define ANA_MSG_FATAL(xmsg)
Macro printing fatal messages.
IMessagePrinter * m_current
the current message printer we should have when we restore
IMessagePrinter * m_saved
the stored message printer we need to restore
static IMessagePrinter *& getCurrent() noexcept
get a reference to the pointer to the current IMessagePrinter

◆ ~MessagePrinterOverlay()

asg::MessagePrinterOverlay::~MessagePrinterOverlay ( )
noexcept

remove the currently overlaid printer

Guarantee
no-fail

Definition at line 41 of file MessagePrinterOverlay.cxx.

43 {
44 using namespace msgAsgMessaging;
45
46 if (getCurrent() != m_current)
47 {
48 ANA_MSG_FATAL ("the overlay message printer changed underhand this should never happen");
49 std::abort ();
50 }
52 }

Member Function Documentation

◆ current()

IMessagePrinter & asg::MessagePrinterOverlay::current ( )
staticnoexcept

get the current IMessagePrinter

Guarantee
no-fail

Definition at line 56 of file MessagePrinterOverlay.cxx.

58 {
59 return *getCurrent();
60 }

◆ getCurrent()

IMessagePrinter *& asg::MessagePrinterOverlay::getCurrent ( )
staticprivatenoexcept

get a reference to the pointer to the current IMessagePrinter

Guarantee
no-fail
Postcondition
result != nullptr

Definition at line 64 of file MessagePrinterOverlay.cxx.

66 {
67 static MessagePrinter defaultPrinter;
68 static IMessagePrinter *pointer = &defaultPrinter;
69 return pointer;
70 }

◆ operator=()

MessagePrinterOverlay asg::MessagePrinterOverlay::operator= ( MessagePrinterOverlay & )
privatedelete

Member Data Documentation

◆ m_current

IMessagePrinter* asg::MessagePrinterOverlay::m_current = nullptr
private

the current message printer we should have when we restore

Definition at line 77 of file MessagePrinterOverlay.h.

◆ m_saved

IMessagePrinter* asg::MessagePrinterOverlay::m_saved = nullptr
private

the stored message printer we need to restore

Definition at line 73 of file MessagePrinterOverlay.h.


The documentation for this class was generated from the following files: