The Gaudi Framework  master (d98a2936)
PropertyBase.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * 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 <Gaudi/PropertyFwd.h>
14 #include <GaudiKernel/StatusCode.h>
15 #include <GaudiKernel/System.h>
16 #include <functional>
17 #include <iostream>
18 #include <set>
19 #include <string>
20 #include <string_view>
21 #include <typeinfo>
22 #include <utility>
23 
24 namespace Gaudi::Details {
25  class WeakPropertyRef;
26 
35 
36  public:
38  const std::string name() const { return std::string{ m_name }; }
40  std::string documentation() const { return std::string{ m_documentation }; }
42  std::string semantics() const { return std::string{ m_semantics }; }
44  const std::type_info* type_info() const { return m_typeinfo; }
46  std::string type() const { return m_typeinfo->name(); }
48  virtual bool load( PropertyBase& dest ) const = 0;
50  virtual bool assign( const PropertyBase& source ) = 0;
51 
52  public:
54  virtual std::string toString() const = 0;
56  virtual void toStream( std::ostream& out ) const = 0;
58  virtual StatusCode fromString( const std::string& value ) = 0;
59 
60  public:
62  virtual PropertyBase& declareReadHandler( std::function<void( PropertyBase& )> fun ) = 0;
64  virtual PropertyBase& declareUpdateHandler( std::function<void( PropertyBase& )> fun ) = 0;
65 
67  virtual const std::function<void( PropertyBase& )> readCallBack() const = 0;
69  virtual const std::function<void( PropertyBase& )> updateCallBack() const = 0;
70 
72  virtual bool useUpdateHandler() = 0;
73 
74  template <class HT>
75  PropertyBase& declareReadHandler( void ( HT::*MF )( PropertyBase& ), HT* instance ) {
76  return declareReadHandler( [=]( PropertyBase& p ) { ( instance->*MF )( p ); } );
77  }
78 
79  template <class HT>
80  PropertyBase& declareUpdateHandler( void ( HT::*MF )( PropertyBase& ), HT* instance ) {
81  return declareUpdateHandler( [=]( PropertyBase& p ) { ( instance->*MF )( p ); } );
82  }
83 
84  public:
86  virtual inline ~PropertyBase();
88  void setName( std::string value ) { m_name = to_view( std::move( value ) ); }
90  void setDocumentation( std::string value ) { m_documentation = to_view( std::move( value ) ); }
92  void setSemantics( std::string value ) { m_semantics = to_view( std::move( value ) ); }
94  virtual std::ostream& fillStream( std::ostream& ) const;
96  virtual PropertyBase* clone() const = 0;
97 
99  void setOwnerType( const std::type_info& ownerType ) { m_ownerType = &ownerType; }
100 
102  template <class OWNER>
103  void setOwnerType() {
104  setOwnerType( typeid( OWNER ) );
105  }
106 
108  const std::type_info* ownerType() const { return m_ownerType; }
109 
111  std::string ownerTypeName() const {
112  return m_ownerType ? System::typeinfoName( *m_ownerType ) : std::string( "unknown owner type" );
113  }
114 
115  protected:
117  PropertyBase( const std::type_info& type, std::string name = "", std::string doc = "", std::string semantics = "" )
118  : m_name( to_view( std::move( name ) ) )
119  , m_documentation( to_view( std::move( doc ) ) )
120  , m_semantics( to_view( std::move( semantics ) ) )
121  , m_typeinfo( &type ) {}
123  PropertyBase( std::string name, const std::type_info& type )
124  : m_name( to_view( std::move( name ) ) ), m_documentation( m_name ), m_typeinfo( &type ) {}
126  PropertyBase( const PropertyBase& ) = default;
128  PropertyBase& operator=( const PropertyBase& ) = default;
129 
130  private:
132  static std::string_view to_view( std::string str );
134  std::string_view m_name;
136  std::string_view m_documentation;
138  std::string_view m_semantics;
140  const std::type_info* m_typeinfo;
142  const std::type_info* m_ownerType = nullptr;
143 
145  std::set<WeakPropertyRef*> m_weakReferences;
147  if ( ref ) m_weakReferences.insert( ref );
148  }
149  void remove( WeakPropertyRef* ref ) { m_weakReferences.erase( ref ); }
150  };
151 
155  friend PropertyBase;
156 
157  public:
158  WeakPropertyRef() = default;
159  WeakPropertyRef( std::string value ) : m_value{ std::move( value ) }, m_unset{ false } {}
160  WeakPropertyRef( PropertyBase& property ) : m_property{ &property } { property.add( this ); }
161  WeakPropertyRef( const WeakPropertyRef& other ) = delete;
163  : m_property{ other.m_property }, m_value{ std::move( other.m_value ) }, m_unset{ other.m_unset } {
164  if ( m_property ) {
165  other.m_property = nullptr;
166  m_property->remove( &other );
167  m_property->add( this );
168  }
169  }
171  if ( m_property ) m_property->remove( this );
172  }
174  if ( this != &other ) {
175  if ( m_property ) m_property->remove( this );
176  m_property = other.m_property;
177  other.m_property = nullptr;
178  if ( m_property ) {
179  m_property->remove( &other );
180  m_property->add( this );
181  }
182  m_value = std::move( other.m_value );
183  m_unset = other.m_unset;
184  }
185  return *this;
186  }
188  if ( m_property != &value ) {
189  if ( m_property ) {
190  m_property->remove( this );
191  if ( !m_unset ) m_value = m_property->toString();
192  }
193  if ( !m_unset ) value.fromString( m_value ).ignore();
194  m_property = &value;
195  value.add( this );
196  }
197  return *this;
198  }
199  WeakPropertyRef& operator=( const std::string& value ) {
200  if ( m_property ) m_property->fromString( value ).ignore();
201  m_value = value;
202  m_unset = false;
203  return *this;
204  }
205  operator std::string() const;
206 
207  inline bool isBound() const { return m_property; }
208  inline bool isSet() const { return !m_unset; }
209 
210  private:
211  PropertyBase* m_property = nullptr;
212  std::string m_value;
213  bool m_unset = true;
214 
215  void detach() { m_property = nullptr; }
216  };
217 
219  for ( auto ref : m_weakReferences ) { ref->detach(); }
220  }
221 
222  inline std::ostream& operator<<( std::ostream& stream, const PropertyBase& prop ) {
223  return prop.fillStream( stream );
224  }
225 } // namespace Gaudi::Details
Gaudi::Details::PropertyBase::declareUpdateHandler
PropertyBase & declareUpdateHandler(void(HT::*MF)(PropertyBase &), HT *instance)
Definition: PropertyBase.h:80
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:34
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef(PropertyBase &property)
Definition: PropertyBase.h:160
Gaudi::Details::PropertyBase::load
virtual bool load(PropertyBase &dest) const =0
export the property value to the destination
Gaudi::Details::WeakPropertyRef::operator=
WeakPropertyRef & operator=(const std::string &value)
Definition: PropertyBase.h:199
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: PropertyBase.h:38
Write.stream
stream
Definition: Write.py:32
Gaudi::Details
Definition: PropertyId.h:20
Gaudi::Details::PropertyBase::operator=
PropertyBase & operator=(const PropertyBase &)=default
assignment operator
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
Gaudi::Details::PropertyBase::type_info
const std::type_info * type_info() const
property type-info
Definition: PropertyBase.h:44
System.h
Gaudi::Details::WeakPropertyRef::operator=
WeakPropertyRef & operator=(PropertyBase &value)
Definition: PropertyBase.h:187
Gaudi::Details::PropertyBase::documentation
std::string documentation() const
property documentation
Definition: PropertyBase.h:40
Gaudi::Details::WeakPropertyRef::isBound
bool isBound() const
Definition: PropertyBase.h:207
Gaudi::Details::PropertyBase::type
std::string type() const
property type
Definition: PropertyBase.h:46
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef(WeakPropertyRef &&other)
Definition: PropertyBase.h:162
Gaudi::Details::WeakPropertyRef::m_value
std::string m_value
Definition: PropertyBase.h:212
Gaudi::Details::PropertyBase::WeakPropertyRef
friend WeakPropertyRef
Definition: PropertyBase.h:144
Gaudi::Details::PropertyBase::remove
void remove(WeakPropertyRef *ref)
Definition: PropertyBase.h:149
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:260
Gaudi::Details::WeakPropertyRef::detach
void detach()
Definition: PropertyBase.h:215
Gaudi::Details::PropertyBase::toStream
virtual void toStream(std::ostream &out) const =0
value -> stream
Gaudi::Details::WeakPropertyRef::PropertyBase
friend PropertyBase
Definition: PropertyBase.h:155
StatusCode.h
Gaudi::Details::PropertyBase::fromString
virtual StatusCode fromString(const std::string &value)=0
string -> value
Gaudi::Details::PropertyBase::declareReadHandler
virtual PropertyBase & declareReadHandler(std::function< void(PropertyBase &)> fun)=0
set new callback for reading
Gaudi::Details::operator<<
std::ostream & operator<<(std::ostream &stream, const PropertyBase &prop)
Definition: PropertyBase.h:222
Gaudi::Details::PropertyBase::ownerType
const std::type_info * ownerType() const
get the type of the owner class (used for documentation)
Definition: PropertyBase.h:108
Gaudi::Details::WeakPropertyRef
Optional reference to a property that can be used to refer to a sting or to the string representation...
Definition: PropertyBase.h:154
Gaudi::Details::WeakPropertyRef::isSet
bool isSet() const
Definition: PropertyBase.h:208
Gaudi::Details::PropertyBase::m_name
std::string_view m_name
property name
Definition: PropertyBase.h:134
Gaudi::Details::WeakPropertyRef::operator=
WeakPropertyRef & operator=(WeakPropertyRef &&other)
Definition: PropertyBase.h:173
Gaudi::Details::PropertyBase::setDocumentation
void setDocumentation(std::string value)
set the documentation string
Definition: PropertyBase.h:90
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef(const WeakPropertyRef &other)=delete
Gaudi::Details::PropertyBase::fillStream
virtual std::ostream & fillStream(std::ostream &) const
the printout of the property value
Definition: Property.cpp:45
StatusCode
Definition: StatusCode.h:64
Gaudi::Details::PropertyBase::updateCallBack
virtual const std::function< void(PropertyBase &)> updateCallBack() const =0
get a reference to the updateCallBack
Gaudi::Details::PropertyBase::add
void add(WeakPropertyRef *ref)
Definition: PropertyBase.h:146
Gaudi::Details::PropertyBase::m_documentation
std::string_view m_documentation
property doc string
Definition: PropertyBase.h:136
PropertyFwd.h
Gaudi::Details::PropertyBase::PropertyBase
PropertyBase(const std::type_info &type, std::string name="", std::string doc="", std::string semantics="")
constructor from the property name and the type
Definition: PropertyBase.h:117
Gaudi::Details::PropertyBase::~PropertyBase
virtual ~PropertyBase()
virtual destructor
Definition: PropertyBase.h:218
Gaudi::Details::PropertyBase::setOwnerType
void setOwnerType(const std::type_info &ownerType)
set the type of the owner class (used for documentation)
Definition: PropertyBase.h:99
Gaudi::Details::PropertyBase::useUpdateHandler
virtual bool useUpdateHandler()=0
manual trigger for callback for update
Gaudi::Details::PropertyBase::semantics
std::string semantics() const
property semantics
Definition: PropertyBase.h:42
Gaudi::Details::PropertyBase::assign
virtual bool assign(const PropertyBase &source)=0
import the property value form the source
Gaudi::Details::PropertyBase::setOwnerType
void setOwnerType()
set the type of the owner class (used for documentation)
Definition: PropertyBase.h:103
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
Gaudi::Details::PropertyBase::declareUpdateHandler
virtual PropertyBase & declareUpdateHandler(std::function< void(PropertyBase &)> fun)=0
set new callback for update
Gaudi::Details::PropertyBase::m_weakReferences
std::set< WeakPropertyRef * > m_weakReferences
Definition: PropertyBase.h:145
gaudirun.dest
dest
Definition: gaudirun.py:224
Gaudi::Details::WeakPropertyRef::m_property
PropertyBase * m_property
Definition: PropertyBase.h:211
gaudirun.type
type
Definition: gaudirun.py:160
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::Details::PropertyBase::m_semantics
std::string_view m_semantics
property semantics
Definition: PropertyBase.h:138
Gaudi::Details::PropertyBase::m_typeinfo
const std::type_info * m_typeinfo
property type
Definition: PropertyBase.h:140
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
Gaudi::Details::WeakPropertyRef::~WeakPropertyRef
~WeakPropertyRef()
Definition: PropertyBase.h:170
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef()=default
Gaudi::Details::PropertyBase::declareReadHandler
PropertyBase & declareReadHandler(void(HT::*MF)(PropertyBase &), HT *instance)
Definition: PropertyBase.h:75
Gaudi::Details::PropertyBase::readCallBack
virtual const std::function< void(PropertyBase &)> readCallBack() const =0
get a reference to the readCallBack
Gaudi::Details::PropertyBase::setName
void setName(std::string value)
set the new value for the property name
Definition: PropertyBase.h:88
compareRootHistos.ref
ref
Definition: compareRootHistos.py:27
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef(std::string value)
Definition: PropertyBase.h:159
Gaudi::Details::PropertyBase::PropertyBase
PropertyBase(const PropertyBase &)=default
copy constructor
Gaudi::Details::PropertyBase::ownerTypeName
std::string ownerTypeName() const
get the string for the type of the owner class (used for documentation)
Definition: PropertyBase.h:111
Gaudi::Details::PropertyBase::clone
virtual PropertyBase * clone() const =0
clones the current property
Gaudi::Details::PropertyBase::setSemantics
void setSemantics(std::string value)
set the semantics string
Definition: PropertyBase.h:92
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Details::PropertyBase::PropertyBase
PropertyBase(std::string name, const std::type_info &type)
constructor from the property name and the type
Definition: PropertyBase.h:123
Gaudi::Functional::details::out
OptOut && out
Definition: details.h:179