The Gaudi Framework  v36r1 (3e2fb5a8)
MonitoringHub.h
Go to the documentation of this file.
1 /*****************************************************************************\
2 * (c) Copyright 2020 CERN for the benefit of the LHCb Collaboration *
3 * *
4 * This software is distributed under the terms of the GNU General Public *
5 * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \*****************************************************************************/
11 #pragma once
12 
13 #include <deque>
14 #include <functional>
15 #include <nlohmann/json.hpp>
16 #include <string>
17 #include <typeinfo>
18 
19 namespace Gaudi::Monitoring {
24  struct Hub {
26 
40  class Entity {
41  public:
42  template <typename T>
45  , name{std::move( name )}
46  , type{std::move( type )}
47  , m_ptr{&ent}
48  , m_reset{[]( void* ptr ) { reinterpret_cast<T*>( ptr )->reset(); }}
49  , m_getJSON{[]( const void* ptr ) { return reinterpret_cast<const T*>( ptr )->toJSON(); }} {}
57  json toJSON() const { return ( *m_getJSON )( m_ptr ); }
59  void reset() { return ( *m_reset )( m_ptr ); }
61  bool operator==( void* ent ) { return m_ptr == ent; }
63  bool operator==( Entity const& ent ) { return m_ptr == ent.m_ptr; }
64 
65  private:
67  void* m_ptr{nullptr};
68  // The next 2 members are needed for type erasure
69  // indeed, their implementation is internal type dependant
70  // (see Constructor above and the usage of T in the reinterpret_cast)
72  void ( *m_reset )( void* );
74  json ( *m_getJSON )( const void* );
75  };
76 
78  struct Sink {
79  virtual void registerEntity( Entity ent ) = 0;
80  virtual void removeEntity( Entity const& ent ) = 0;
81  virtual ~Sink() = default;
82  };
83 
84  template <typename T>
86  registerEntity( {std::move( c ), std::move( n ), std::move( t ), ent} );
87  }
88  void registerEntity( Entity ent ) {
90  [ent]( auto sink ) { sink->registerEntity( std::move( ent ) ); } );
91  m_entities.emplace_back( std::move( ent ) );
92  }
93  template <typename T>
94  void removeEntity( T& ent ) {
95  auto it = std::find( begin( m_entities ), end( m_entities ), &ent );
96  if ( it != m_entities.end() ) {
97  std::for_each( begin( m_sinks ), end( m_sinks ), [&it]( auto sink ) { sink->removeEntity( *it ); } );
98  m_entities.erase( it );
99  }
100  }
101 
102  void addSink( Sink* sink ) {
104  [sink]( Entity ent ) { sink->registerEntity( std::move( ent ) ); } );
105  m_sinks.push_back( sink );
106  }
107  void removeSink( Sink* sink ) {
108  auto it = std::find( begin( m_sinks ), end( m_sinks ), sink );
109  if ( it != m_sinks.end() ) m_sinks.erase( it );
110  }
111 
112  private:
115  };
116 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:78
std::for_each
T for_each(T... args)
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:53
Gaudi::Monitoring::Hub::Entity::operator==
bool operator==(Entity const &ent)
operator== for comparison with an entity
Definition: MonitoringHub.h:63
Gaudi::Monitoring::Hub::Entity::operator==
bool operator==(void *ent)
operator== for comparison with raw pointer
Definition: MonitoringHub.h:61
std::string
STL class.
Gaudi::Monitoring::Hub::m_sinks
std::deque< Sink * > m_sinks
Definition: MonitoringHub.h:113
std::move
T move(T... args)
std::find
T find(T... args)
Gaudi::Monitoring::Hub::m_entities
std::deque< Entity > m_entities
Definition: MonitoringHub.h:114
Gaudi::Monitoring
Definition: MessageSvcSink.cpp:147
gaudirun.c
c
Definition: gaudirun.py:509
Gaudi::Monitoring::Hub::addSink
void addSink(Sink *sink)
Definition: MonitoringHub.h:102
Gaudi::Monitoring::Hub::Sink::removeEntity
virtual void removeEntity(Entity const &ent)=0
jsonFromLHCbLog.json
dictionary json
Definition: jsonFromLHCbLog.py:86
bug_34121.t
t
Definition: bug_34121.py:30
Gaudi::Monitoring::Hub::Sink::registerEntity
virtual void registerEntity(Entity ent)=0
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::Monitoring::Hub::removeSink
void removeSink(Sink *sink)
Definition: MonitoringHub.h:107
Gaudi::Monitoring::Hub::Entity::reset
void reset()
function resetting internal data
Definition: MonitoringHub.h:59
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(std::string c, std::string n, std::string t, T &ent)
Definition: MonitoringHub.h:85
Gaudi::Monitoring::Hub::json
nlohmann::json json
Definition: MonitoringHub.h:25
std::deque
STL class.
Gaudi::Monitoring::Hub::removeEntity
void removeEntity(T &ent)
Definition: MonitoringHub.h:94
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:221
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(Entity ent)
Definition: MonitoringHub.h:88
Gaudi::Monitoring::Hub::Entity::m_getJSON
json(* m_getJSON)(const void *)
function converting the internal data to json.
Definition: MonitoringHub.h:74
Gaudi::Monitoring::Hub::Entity::m_ptr
void * m_ptr
pointer to the actual data inside this Entity
Definition: MonitoringHub.h:67
Gaudi::Monitoring::Hub::Entity::toJSON
json toJSON() const
function giving access to internal data in json format
Definition: MonitoringHub.h:57
Gaudi::Monitoring::Hub::Entity::Entity
Entity(std::string component, std::string name, std::string type, T &ent)
Definition: MonitoringHub.h:43
IOTest.end
end
Definition: IOTest.py:123
Gaudi::Monitoring::Hub
Central entity in a Gaudi application that manages monitoring objects (i.e.
Definition: MonitoringHub.h:24
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:40
Gaudi::Monitoring::Hub::Entity::m_reset
void(* m_reset)(void *)
function reseting internal data.
Definition: MonitoringHub.h:72
Gaudi::Monitoring::Hub::Sink::~Sink
virtual ~Sink()=default
Gaudi::Monitoring::Hub::Entity::type
std::string type
type of the entity, see comment above concerning its format and usage
Definition: MonitoringHub.h:55
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:51