Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r7 (7f57a304)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 "GaudiKernel/detected.h"
14 
15 #include <deque>
16 #include <functional>
17 #include <nlohmann/json.hpp>
18 #include <string>
19 #include <typeindex>
20 #include <typeinfo>
21 
22 namespace Gaudi::Monitoring {
23 
24  namespace details {
25 
26  template <typename T>
27  using has_merge_and_reset_ = decltype( std::declval<T>().mergeAndReset( std::declval<T&&>() ) );
28  template <typename T>
29  inline constexpr bool has_merge_and_reset_v = Gaudi::cpp17::is_detected_v<has_merge_and_reset_, T>;
30 
32  virtual ~MergeAndResetBase() = default;
33  virtual void operator()( void*, void* ) const = 0;
34  };
35 
36  using MergeAndReset_t = void ( * )( void*, void* );
37 
38  template <typename T>
40  if constexpr ( has_merge_and_reset_v<T> ) {
41  return []( void* ptr, void* other ) {
42  reinterpret_cast<T*>( ptr )->mergeAndReset( std::move( *reinterpret_cast<T*>( other ) ) );
43  };
44  } else {
45  return []( void*, void* ) {};
46  }
47  }
48 
49  } // namespace details
50 
55  struct Hub {
57 
71  class Entity {
72  public:
73  template <typename T>
76  , name{ std::move( name ) }
77  , type{ std::move( type ) }
78  , m_ptr{ &ent }
79  , m_typeIndex{ []( const void* ptr ) {
80  return std::type_index( typeid( *reinterpret_cast<const T*>( ptr ) ) );
81  } }
82  , m_reset{ []( void* ptr ) { reinterpret_cast<T*>( ptr )->reset(); } }
83  , m_mergeAndReset{ details::makeMergeAndResetFor<T>() }
84  , m_getJSON{ []( const void* ptr ) { return reinterpret_cast<const T*>( ptr )->toJSON(); } } {}
92  json toJSON() const { return ( *m_getJSON )( m_ptr ); }
94  std::type_index typeIndex() const { return ( *m_typeIndex )( m_ptr ); }
96  void reset() { return ( *m_reset )( m_ptr ); }
97  // The following function does not protect against usage with entities with different internal types
98  // The user should ensure that entities are compatible before calling this function
100  void mergeAndReset( Entity const& ent ) {
101  if ( typeIndex() != ent.typeIndex() ) {
102  throw std::runtime_error( std::string( "Entity: mergeAndReset called on different types: " ) +
103  typeIndex().name() + " and " + ent.typeIndex().name() );
104  }
105  return ( *m_mergeAndReset )( m_ptr, ent.m_ptr );
106  }
108  bool operator==( void* ent ) { return m_ptr == ent; }
110  bool operator==( Entity const& ent ) { return m_ptr == ent.m_ptr; }
111 
112  private:
114  void* m_ptr{ nullptr };
115  // The next 4 members are needed for type erasure
116  // indeed, their implementation is internal type dependant
117  // (see Constructor above and the usage of T in the reinterpret_cast)
119  std::type_index ( *m_typeIndex )( const void* );
121  void ( *m_reset )( void* );
125  json ( *m_getJSON )( const void* );
126  };
127 
129  struct Sink {
130  virtual void registerEntity( Entity ent ) = 0;
131  virtual void removeEntity( Entity const& ent ) = 0;
132  virtual ~Sink() = default;
133  };
134 
135  template <typename T>
137  registerEntity( { std::move( c ), std::move( n ), std::move( t ), ent } );
138  }
139  void registerEntity( Entity ent ) {
140  std::for_each( begin( m_sinks ), end( m_sinks ), [ent]( auto sink ) { sink->registerEntity( ent ); } );
141  m_entities.emplace_back( std::move( ent ) );
142  }
143  template <typename T>
144  void removeEntity( T& ent ) {
145  auto it = std::find( begin( m_entities ), end( m_entities ), &ent );
146  if ( it != m_entities.end() ) {
147  std::for_each( begin( m_sinks ), end( m_sinks ), [&it]( auto sink ) { sink->removeEntity( *it ); } );
148  m_entities.erase( it );
149  }
150  }
151 
152  void addSink( Sink* sink ) {
154  [sink]( Entity ent ) { sink->registerEntity( std::move( ent ) ); } );
155  m_sinks.push_back( sink );
156  }
157  void removeSink( Sink* sink ) {
158  auto it = std::find( begin( m_sinks ), end( m_sinks ), sink );
159  if ( it != m_sinks.end() ) m_sinks.erase( it );
160  }
161 
162  private:
165  };
166 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:129
Gaudi::Monitoring::details::MergeAndResetBase
Definition: MonitoringHub.h:31
std::for_each
T for_each(T... args)
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:88
Gaudi::Monitoring::Hub::Entity::operator==
bool operator==(Entity const &ent)
operator== for comparison with an entity
Definition: MonitoringHub.h:110
Gaudi::Monitoring::Hub::Entity::operator==
bool operator==(void *ent)
operator== for comparison with raw pointer
Definition: MonitoringHub.h:108
std::string
STL class.
Gaudi::Monitoring::Hub::m_sinks
std::deque< Sink * > m_sinks
Definition: MonitoringHub.h:163
std::move
T move(T... args)
Gaudi::Monitoring::details::MergeAndResetBase::~MergeAndResetBase
virtual ~MergeAndResetBase()=default
std::find
T find(T... args)
std::type_index::name
T name(T... args)
std::type_index
Gaudi::Monitoring::Hub::m_entities
std::deque< Entity > m_entities
Definition: MonitoringHub.h:164
Gaudi::Monitoring
Definition: JSONSink.cpp:21
gaudirun.c
c
Definition: gaudirun.py:525
Gaudi::Monitoring::Hub::addSink
void addSink(Sink *sink)
Definition: MonitoringHub.h:152
Gaudi::Monitoring::Hub::Sink::removeEntity
virtual void removeEntity(Entity const &ent)=0
jsonFromLHCbLog.json
dictionary json
Definition: jsonFromLHCbLog.py:87
bug_34121.t
t
Definition: bug_34121.py:30
Gaudi::Monitoring::details::has_merge_and_reset_
decltype(std::declval< T >().mergeAndReset(std::declval< T && >())) has_merge_and_reset_
Definition: MonitoringHub.h:27
Gaudi::Monitoring::Hub::Sink::registerEntity
virtual void registerEntity(Entity ent)=0
details
Definition: AnyDataWrapper.h:18
Gaudi::Monitoring::Hub::Entity::m_typeIndex
std::type_index(* m_typeIndex)(const void *)
function to get internal type.
Definition: MonitoringHub.h:119
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::Monitoring::Hub::removeSink
void removeSink(Sink *sink)
Definition: MonitoringHub.h:157
Gaudi::Monitoring::Hub::Entity::reset
void reset()
function resetting internal data
Definition: MonitoringHub.h:96
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(std::string c, std::string n, std::string t, T &ent)
Definition: MonitoringHub.h:136
Gaudi::Monitoring::Hub::json
nlohmann::json json
Definition: MonitoringHub.h:56
std::runtime_error
STL class.
std::deque
STL class.
Gaudi::Monitoring::Hub::Entity::typeIndex
std::type_index typeIndex() const
function to get internal type
Definition: MonitoringHub.h:94
Gaudi::Monitoring::Hub::removeEntity
void removeEntity(T &ent)
Definition: MonitoringHub.h:144
Gaudi::Monitoring::details::has_merge_and_reset_v
constexpr bool has_merge_and_reset_v
Definition: MonitoringHub.h:29
IOTest.end
def end
Definition: IOTest.py:128
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(Entity ent)
Definition: MonitoringHub.h:139
Gaudi::Monitoring::Hub::Entity::m_mergeAndReset
details::MergeAndReset_t m_mergeAndReset
function calling merge and reset on internal data with the internal data of another entity
Definition: MonitoringHub.h:123
Gaudi::Monitoring::Hub::Entity::m_getJSON
json(* m_getJSON)(const void *)
function converting the internal data to json.
Definition: MonitoringHub.h:125
Gaudi::Monitoring::Hub::Entity::m_ptr
void * m_ptr
pointer to the actual data inside this Entity
Definition: MonitoringHub.h:114
Gaudi::Monitoring::Hub::Entity::toJSON
json toJSON() const
function giving access to internal data in json format
Definition: MonitoringHub.h:92
Gaudi::Monitoring::details::MergeAndResetBase::operator()
virtual void operator()(void *, void *) const =0
Gaudi::Monitoring::Hub::Entity::Entity
Entity(std::string component, std::string name, std::string type, T &ent)
Definition: MonitoringHub.h:74
detected.h
Gaudi::Monitoring::details::makeMergeAndResetFor
MergeAndReset_t makeMergeAndResetFor()
Definition: MonitoringHub.h:39
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:100
Gaudi::Monitoring::Hub
Central entity in a Gaudi application that manages monitoring objects (i.e.
Definition: MonitoringHub.h:55
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:71
Gaudi::Monitoring::Hub::Entity::m_reset
void(* m_reset)(void *)
function reseting internal data.
Definition: MonitoringHub.h:121
Gaudi::Monitoring::Hub::Sink::~Sink
virtual ~Sink()=default
Gaudi::Monitoring::details::MergeAndReset_t
void(*)(void *, void *) MergeAndReset_t
Definition: MonitoringHub.h:36
Gaudi::Monitoring::Hub::Entity::type
std::string type
type of the entity, see comment above concerning its format and usage
Definition: MonitoringHub.h:90
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:86