-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathLogger.hxx
More file actions
81 lines (71 loc) · 2.53 KB
/
Logger.hxx
File metadata and controls
81 lines (71 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* ExternalPolimorphyism.hxx
*
* Created on: Dec 18, 2021
* Author: <a href="mailto:damirlj@yahoo.com">Damir Ljubic</a>
*/
#ifndef TUTORIAL_TYPE_ERASURE_LOGGER_HXX_
#define TUTORIAL_TYPE_ERASURE_LOGGER_HXX_
#include <string>
#include <memory>
namespace test::ep
{
/**
* Adapter interface, for a classes for which defining the new common ancestor (base class)
* in inheritance tree is not possible: due to, for example, fact that
* these classes are imported from the (different) 3rd party libraries
*/
struct Logging
{
virtual ~Logging() = default;
virtual void log() const = 0;
protected:
Logging() = default;
};
/**
* Wrapper type around the Logging interface
*
* @tparam T The type to be logged
* @tparam Logger The specific logger (medium): Dependency Injection
*/
template <typename T, typename Logger>
class LoggingImpl final : public Logging
{
public:
LoggingImpl(const T& type, const Logger& logger) noexcept :
m_obj(type)
, m_logger(logger)
{}
~LoggingImpl() override = default;
void log() const override
{
/*
* "dump()" is a signature adapter, here as a free function.
*
* Internally, it will call T specific logging function.
* Actually, it's modified (http://www.dre.vanderbilt.edu/~schmidt/PDF/External-Polymorphism.pdf)
* to return the object string representation, and use external logger medium.
*
* Alternatively, one could use function template as signature adapter
*
* template <typename T>
* std::string dump(const T& t)
* {
* return t.toString(); // this would assume that most of the classes have already the same "java-like" signature
* }
*
* and for those types who don't share the same signature - we have overloading specialization
*
* std::string dump<A>(const A& a)
* {
* return a.print();
* }
*/
m_logger.log(dump(m_obj));
}
private:
const T& m_obj; // unmutable object to log
const Logger& m_logger; // unmutable logger itself
};
}
#endif /* TUTORIAL_TYPE_ERASURE_LOGGER_HXX_ */