Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
monitor_factory Class Reference

Monitor factory implementing singleton pattern with DI. More...

Inheritance diagram for monitor_factory:
Inheritance graph
Collaboration diagram for monitor_factory:
Collaboration graph

Public Member Functions

void set_shared_logger (std::shared_ptr< ILogger > logger)
 
std::shared_ptr< IMonitor > get_monitor () override
 
std::shared_ptr< IMonitor > create_monitor (const std::string &name) override
 
std::vector< std::string > list_monitors () const
 
size_t monitor_count () const
 
void reset ()
 

Static Public Member Functions

static std::shared_ptr< monitor_factoryinstance ()
 

Private Member Functions

 monitor_factory ()=default
 

Private Attributes

std::shared_ptr< IMonitor > default_monitor_
 
std::unordered_map< std::string, std::shared_ptr< IMonitor > > named_monitors_
 
std::shared_ptr< ILogger > shared_logger_
 
std::mutex factory_mutex_
 

Static Private Attributes

static std::shared_ptr< monitor_factoryinstance_ = nullptr
 
static std::mutex instance_mutex_
 

Detailed Description

Monitor factory implementing singleton pattern with DI.

Examples
monitor_factory_pattern_example.cpp.

Definition at line 30 of file monitor_factory_pattern_example.cpp.

Constructor & Destructor Documentation

◆ monitor_factory()

monitor_factory::monitor_factory ( )
privatedefault
Examples
monitor_factory_pattern_example.cpp.

Referenced by instance().

Here is the caller graph for this function:

Member Function Documentation

◆ create_monitor()

std::shared_ptr< IMonitor > monitor_factory::create_monitor ( const std::string & name)
inlineoverride
Examples
monitor_factory_pattern_example.cpp.

Definition at line 70 of file monitor_factory_pattern_example.cpp.

70 {
71 std::lock_guard<std::mutex> lock(factory_mutex_);
72
73 auto it = named_monitors_.find(name);
74 if (it != named_monitors_.end()) {
75 return it->second;
76 }
77
78 auto monitor = std::make_shared<performance_monitor>();
79 named_monitors_[name] = monitor;
80 return monitor;
81 }
std::unordered_map< std::string, std::shared_ptr< IMonitor > > named_monitors_

References factory_mutex_, and named_monitors_.

◆ get_monitor()

std::shared_ptr< IMonitor > monitor_factory::get_monitor ( )
inlineoverride
Examples
monitor_factory_pattern_example.cpp.

Definition at line 60 of file monitor_factory_pattern_example.cpp.

60 {
61 std::lock_guard<std::mutex> lock(factory_mutex_);
62
63 if (!default_monitor_) {
64 default_monitor_ = std::make_shared<performance_monitor>();
65 }
66
67 return default_monitor_;
68 }
std::shared_ptr< IMonitor > default_monitor_

References default_monitor_, and factory_mutex_.

◆ instance()

static std::shared_ptr< monitor_factory > monitor_factory::instance ( )
inlinestatic
Examples
monitor_factory_pattern_example.cpp.

Definition at line 43 of file monitor_factory_pattern_example.cpp.

43 {
44 std::lock_guard<std::mutex> lock(instance_mutex_);
45 if (!instance_) {
46 instance_ = std::shared_ptr<monitor_factory>(new monitor_factory());
47 }
48 return instance_;
49 }
static std::shared_ptr< monitor_factory > instance_
monitor_factory()=default

References instance_, instance_mutex_, and monitor_factory().

Referenced by example_1_basic_factory(), example_2_named_monitors(), example_3_factory_with_logger(), example_4_monitor_reuse(), example_5_provider_interface(), example_6_aggregating_pattern(), and example_7_factory_lifecycle().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_monitors()

std::vector< std::string > monitor_factory::list_monitors ( ) const
inline
Examples
monitor_factory_pattern_example.cpp.

Definition at line 83 of file monitor_factory_pattern_example.cpp.

83 {
84 std::lock_guard<std::mutex> lock(factory_mutex_);
85 std::vector<std::string> names;
86 names.reserve(named_monitors_.size());
87
88 for (const auto& [name, _] : named_monitors_) {
89 names.push_back(name);
90 }
91
92 return names;
93 }

References factory_mutex_, and named_monitors_.

◆ monitor_count()

size_t monitor_factory::monitor_count ( ) const
inline
Examples
monitor_factory_pattern_example.cpp.

Definition at line 95 of file monitor_factory_pattern_example.cpp.

95 {
96 std::lock_guard<std::mutex> lock(factory_mutex_);
97 return named_monitors_.size() + (default_monitor_ ? 1 : 0);
98 }

References default_monitor_, factory_mutex_, and named_monitors_.

◆ reset()

void monitor_factory::reset ( )
inline
Examples
monitor_factory_pattern_example.cpp.

Definition at line 100 of file monitor_factory_pattern_example.cpp.

100 {
101 std::lock_guard<std::mutex> lock(factory_mutex_);
102 default_monitor_.reset();
103 named_monitors_.clear();
104 }

References default_monitor_, factory_mutex_, and named_monitors_.

◆ set_shared_logger()

void monitor_factory::set_shared_logger ( std::shared_ptr< ILogger > logger)
inline
Examples
monitor_factory_pattern_example.cpp.

Definition at line 51 of file monitor_factory_pattern_example.cpp.

51 {
52 std::lock_guard<std::mutex> lock(factory_mutex_);
53 shared_logger_ = logger;
54
55 // Note: Logger injection not available in performance_monitor yet
56 // Future enhancement: Add ILogger support to performance_monitor
57 }
std::shared_ptr< ILogger > shared_logger_

References factory_mutex_, and shared_logger_.

Member Data Documentation

◆ default_monitor_

std::shared_ptr<IMonitor> monitor_factory::default_monitor_
private

◆ factory_mutex_

std::mutex monitor_factory::factory_mutex_
mutableprivate

◆ instance_

std::shared_ptr< monitor_factory > monitor_factory::instance_ = nullptr
staticprivate
Examples
monitor_factory_pattern_example.cpp.

Definition at line 32 of file monitor_factory_pattern_example.cpp.

Referenced by instance().

◆ instance_mutex_

std::mutex monitor_factory::instance_mutex_
staticprivate
Examples
monitor_factory_pattern_example.cpp.

Definition at line 33 of file monitor_factory_pattern_example.cpp.

Referenced by instance().

◆ named_monitors_

std::unordered_map<std::string, std::shared_ptr<IMonitor> > monitor_factory::named_monitors_
private

◆ shared_logger_

std::shared_ptr<ILogger> monitor_factory::shared_logger_
private

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