The Gaudi Framework  v36r2 (27905625)
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_mergeAndReset{[]( void* ptr, void* other ) {
50  reinterpret_cast<T*>( ptr )->mergeAndReset( std::move( *reinterpret_cast<T*>( other ) ) );
51  }}
52  , m_getJSON{[]( const void* ptr ) { return reinterpret_cast<const T*>( ptr )->toJSON(); }} {}
60  json toJSON() const { return ( *m_getJSON )( m_ptr ); }
62  void reset() { return ( *m_reset )( m_ptr ); }
63  // The following function does not protect against usage with entities with different internal types
64  // The user should ensure that entities are compatible before calling this function
66  void mergeAndReset( Entity const& ent ) { return ( *m_mergeAndReset )( m_ptr, ent.m_ptr ); }
68  bool operator==( void* ent ) { return m_ptr == ent; }
70  bool operator==( Entity const& ent ) { return m_ptr == ent.m_ptr; }
71 
72  private:
74  void* m_ptr{nullptr};
75  // The next 3 members are needed for type erasure
76  // indeed, their implementation is internal type dependant
77  // (see Constructor above and the usage of T in the reinterpret_cast)
79  void ( *m_reset )( void* );
81  void ( *m_mergeAndReset )( void*, void* );
83  json ( *m_getJSON )( const void* );
84  };
85 
87  struct Sink {
88  virtual void registerEntity( Entity ent ) = 0;
89  virtual void removeEntity( Entity const& ent ) = 0;
90  virtual ~Sink() = default;
91  };
92 
93  template <typename T>
95  registerEntity( {std::move( c ), std::move( n ), std::move( t ), ent} );
96  }
97  void registerEntity( Entity ent ) {
99  [ent]( auto sink ) { sink->registerEntity( std::move( ent ) ); } );
100  m_entities.emplace_back( std::move( ent ) );
101  }
102  template <typename T>
103  void removeEntity( T& ent ) {
104  auto it = std::find( begin( m_entities ), end( m_entities ), &ent );
105  if ( it != m_entities.end() ) {
106  std::for_each( begin( m_sinks ), end( m_sinks ), [&it]( auto sink ) { sink->removeEntity( *it ); } );
107  m_entities.erase( it );
108  }
109  }
110 
111  void addSink( Sink* sink ) {
113  [sink]( Entity ent ) { sink->registerEntity( std::move( ent ) ); } );
114  m_sinks.push_back( sink );
115  }
116  void removeSink( Sink* sink ) {
117  auto it = std::find( begin( m_sinks ), end( m_sinks ), sink );
118  if ( it != m_sinks.end() ) m_sinks.erase( it );
119  }
120 
121  private:
124  };
125 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:87
std::for_each
T for_each(T... args)
Gaudi::Monitoring::Hub::Entity::m_mergeAndReset
void(* m_mergeAndReset)(void *, void *)
function calling merge and reset on internal data with the internal data of another entity
Definition: MonitoringHub.h:81
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:56
Gaudi::Monitoring::Hub::Entity::operator==
bool operator==(Entity const &ent)
operator== for comparison with an entity
Definition: MonitoringHub.h:70
Gaudi::Monitoring::Hub::Entity::operator==
bool operator==(void *ent)
operator== for comparison with raw pointer
Definition: MonitoringHub.h:68
std::string
STL class.
Gaudi::Monitoring::Hub::m_sinks
std::deque< Sink * > m_sinks
Definition: MonitoringHub.h:122
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:123
Gaudi::Monitoring
Definition: JSONSink.cpp:57
gaudirun.c
c
Definition: gaudirun.py:509
Gaudi::Monitoring::Hub::addSink
void addSink(Sink *sink)
Definition: MonitoringHub.h:111
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:116
Gaudi::Monitoring::Hub::Entity::reset
void reset()
function resetting internal data
Definition: MonitoringHub.h:62
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(std::string c, std::string n, std::string t, T &ent)
Definition: MonitoringHub.h:94
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:103
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:221
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(Entity ent)
Definition: MonitoringHub.h:97
Gaudi::Monitoring::Hub::Entity::m_getJSON
json(* m_getJSON)(const void *)
function converting the internal data to json.
Definition: MonitoringHub.h:83
Gaudi::Monitoring::Hub::Entity::m_ptr
void * m_ptr
pointer to the actual data inside this Entity
Definition: MonitoringHub.h:74
Gaudi::Monitoring::Hub::Entity::toJSON
json toJSON() const
function giving access to internal data in json format
Definition: MonitoringHub.h:60
Gaudi::Monitoring::Hub::Entity::Entity
Entity(std::string component, std::string name, std::string type, T &ent)
Definition: MonitoringHub.h:43
Gaudi::Monitoring::Hub::Entity::mergeAndReset
void mergeAndReset(Entity const &ent)
function calling merge and reset on internal data with the internal data of another entity
Definition: MonitoringHub.h:66
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:79
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:58
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:54