The Gaudi Framework  master (37c0b60a)
PropertyBase.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 
27  // ============================================================================
36 
37  public:
39  const std::string name() const { return std::string{ m_name }; }
41  std::string documentation() const { return std::string{ m_documentation }; }
43  std::string semantics() const { return std::string{ m_semantics }; }
45  const std::type_info* type_info() const { return m_typeinfo; }
47  std::string type() const { return m_typeinfo->name(); }
49  virtual bool load( PropertyBase& dest ) const = 0;
51  virtual bool assign( const PropertyBase& source ) = 0;
52 
53  public:
55  virtual std::string toString() const = 0;
57  virtual void toStream( std::ostream& out ) const = 0;
59  virtual StatusCode fromString( const std::string& value ) = 0;
60 
61  public:
66 
68  virtual const std::function<void( PropertyBase& )> readCallBack() const = 0;
70  virtual const std::function<void( PropertyBase& )> updateCallBack() const = 0;
71 
73  virtual bool useUpdateHandler() = 0;
74 
75  template <class HT>
76  PropertyBase& declareReadHandler( void ( HT::*MF )( PropertyBase& ), HT* instance ) {
77  return declareReadHandler( [=]( PropertyBase& p ) { ( instance->*MF )( p ); } );
78  }
79 
80  template <class HT>
81  PropertyBase& declareUpdateHandler( void ( HT::*MF )( PropertyBase& ), HT* instance ) {
82  return declareUpdateHandler( [=]( PropertyBase& p ) { ( instance->*MF )( p ); } );
83  }
84 
85  public:
87  virtual inline ~PropertyBase();
89  void setName( std::string value ) { m_name = to_view( std::move( value ) ); }
91  void setDocumentation( std::string value ) { m_documentation = to_view( std::move( value ) ); }
93  void setSemantics( std::string value ) { m_semantics = to_view( std::move( value ) ); }
95  virtual std::ostream& fillStream( std::ostream& ) const;
97  virtual PropertyBase* clone() const = 0;
98 
100  void setOwnerType( const std::type_info& ownerType ) { m_ownerType = &ownerType; }
101 
103  template <class OWNER>
104  void setOwnerType() {
105  setOwnerType( typeid( OWNER ) );
106  }
107 
109  const std::type_info* ownerType() const { return m_ownerType; }
110 
113  return m_ownerType ? System::typeinfoName( *m_ownerType ) : std::string( "unknown owner type" );
114  }
115 
116  protected:
118  PropertyBase( const std::type_info& type, std::string name = "", std::string doc = "", std::string semantics = "" )
119  : m_name( to_view( std::move( name ) ) )
120  , m_documentation( to_view( std::move( doc ) ) )
121  , m_semantics( to_view( std::move( semantics ) ) )
122  , m_typeinfo( &type ) {}
125  : m_name( to_view( std::move( name ) ) ), m_documentation( m_name ), m_typeinfo( &type ) {}
127  PropertyBase( const PropertyBase& ) = default;
129  PropertyBase& operator=( const PropertyBase& ) = default;
130 
131  private:
133  static std::string_view to_view( std::string str );
135  std::string_view m_name;
137  std::string_view m_documentation;
139  std::string_view m_semantics;
143  const std::type_info* m_ownerType = nullptr;
144 
148  if ( ref ) m_weakReferences.insert( ref );
149  }
150  void remove( WeakPropertyRef* ref ) { m_weakReferences.erase( ref ); }
151  };
152 
156  friend PropertyBase;
157 
158  public:
159  WeakPropertyRef() = default;
160  WeakPropertyRef( std::string value ) : m_value{ std::move( value ) }, m_unset{ false } {}
161  WeakPropertyRef( PropertyBase& property ) : m_property{ &property } { property.add( this ); }
162  WeakPropertyRef( const WeakPropertyRef& other ) = delete;
164  : m_property{ other.m_property }, m_value{ std::move( other.m_value ) }, m_unset{ other.m_unset } {
165  if ( m_property ) {
166  other.m_property = nullptr;
167  m_property->remove( &other );
168  m_property->add( this );
169  }
170  }
172  if ( m_property ) m_property->remove( this );
173  }
175  if ( this != &other ) {
176  if ( m_property ) m_property->remove( this );
177  m_property = other.m_property;
178  other.m_property = nullptr;
179  if ( m_property ) {
180  m_property->remove( &other );
181  m_property->add( this );
182  }
183  m_value = std::move( other.m_value );
184  m_unset = other.m_unset;
185  }
186  return *this;
187  }
189  if ( m_property != &value ) {
190  if ( m_property ) {
191  m_property->remove( this );
192  if ( !m_unset ) m_value = m_property->toString();
193  }
194  if ( !m_unset ) value.fromString( m_value ).ignore();
195  m_property = &value;
196  value.add( this );
197  }
198  return *this;
199  }
201  if ( m_property ) m_property->fromString( value ).ignore();
202  m_value = value;
203  m_unset = false;
204  return *this;
205  }
206  operator std::string() const;
207 
208  inline bool isBound() const { return m_property; }
209  inline bool isSet() const { return !m_unset; }
210 
211  private:
212  PropertyBase* m_property = nullptr;
214  bool m_unset = true;
215 
216  void detach() { m_property = nullptr; }
217  };
218 
220  for ( auto ref : m_weakReferences ) { ref->detach(); }
221  }
222 
224  return prop.fillStream( stream );
225  }
226 } // namespace Gaudi::Details
Gaudi::Details::PropertyBase::declareUpdateHandler
PropertyBase & declareUpdateHandler(void(HT::*MF)(PropertyBase &), HT *instance)
Definition: PropertyBase.h:81
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef(PropertyBase &property)
Definition: PropertyBase.h:161
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:200
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: PropertyBase.h:39
Write.stream
stream
Definition: Write.py:32
std::string
STL class.
Gaudi::Details
Definition: PropertyId.h:28
std::move
T move(T... args)
Gaudi::Details::PropertyBase::operator=
PropertyBase & operator=(const PropertyBase &)=default
assignment operator
Gaudi::Details::PropertyBase::type_info
const std::type_info * type_info() const
property type-info
Definition: PropertyBase.h:45
System.h
Gaudi::Details::WeakPropertyRef::operator=
WeakPropertyRef & operator=(PropertyBase &value)
Definition: PropertyBase.h:188
Gaudi::Details::PropertyBase::documentation
std::string documentation() const
property documentation
Definition: PropertyBase.h:41
Gaudi::Details::WeakPropertyRef::isBound
bool isBound() const
Definition: PropertyBase.h:208
std::type_info
Gaudi::Details::PropertyBase::type
std::string type() const
property type
Definition: PropertyBase.h:47
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef(WeakPropertyRef &&other)
Definition: PropertyBase.h:163
Gaudi::Details::WeakPropertyRef::m_value
std::string m_value
Definition: PropertyBase.h:213
Gaudi::Details::PropertyBase::WeakPropertyRef
friend WeakPropertyRef
Definition: PropertyBase.h:145
Gaudi::Details::PropertyBase::remove
void remove(WeakPropertyRef *ref)
Definition: PropertyBase.h:150
std::function
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
Gaudi::Details::WeakPropertyRef::detach
void detach()
Definition: PropertyBase.h:216
Gaudi::Details::PropertyBase::toStream
virtual void toStream(std::ostream &out) const =0
value -> stream
Gaudi::Details::WeakPropertyRef::PropertyBase
friend PropertyBase
Definition: PropertyBase.h:156
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:223
Gaudi::Details::PropertyBase::ownerType
const std::type_info * ownerType() const
get the type of the owner class (used for documentation)
Definition: PropertyBase.h:109
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:155
Gaudi::Details::WeakPropertyRef::isSet
bool isSet() const
Definition: PropertyBase.h:209
Gaudi::Details::PropertyBase::m_name
std::string_view m_name
property name
Definition: PropertyBase.h:135
Gaudi::Details::WeakPropertyRef::operator=
WeakPropertyRef & operator=(WeakPropertyRef &&other)
Definition: PropertyBase.h:174
Gaudi::Details::PropertyBase::setDocumentation
void setDocumentation(std::string value)
set the documentation string
Definition: PropertyBase.h:91
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:60
StatusCode
Definition: StatusCode.h:65
Gaudi::Details::PropertyBase::updateCallBack
virtual const std::function< void(PropertyBase &)> updateCallBack() const =0
get a reference to the updateCallBack
std::ostream
STL class.
Gaudi::Details::PropertyBase::add
void add(WeakPropertyRef *ref)
Definition: PropertyBase.h:147
Gaudi::Details::PropertyBase::m_documentation
std::string_view m_documentation
property doc string
Definition: PropertyBase.h:137
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:118
std::set::erase
T erase(T... args)
Gaudi::Details::PropertyBase::~PropertyBase
virtual ~PropertyBase()
virtual destructor
Definition: PropertyBase.h:219
Gaudi::Details::PropertyBase::setOwnerType
void setOwnerType(const std::type_info &ownerType)
set the type of the owner class (used for documentation)
Definition: PropertyBase.h:100
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:43
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:104
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:146
gaudirun.dest
dest
Definition: gaudirun.py:224
Gaudi::Details::WeakPropertyRef::m_property
PropertyBase * m_property
Definition: PropertyBase.h:212
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:139
Gaudi::Details::PropertyBase::m_typeinfo
const std::type_info * m_typeinfo
property type
Definition: PropertyBase.h:141
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
std
STL namespace.
Gaudi::Details::WeakPropertyRef::~WeakPropertyRef
~WeakPropertyRef()
Definition: PropertyBase.h:171
std::set::insert
T insert(T... args)
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef()=default
Gaudi::Details::PropertyBase::declareReadHandler
PropertyBase & declareReadHandler(void(HT::*MF)(PropertyBase &), HT *instance)
Definition: PropertyBase.h:76
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:89
compareRootHistos.ref
ref
Definition: compareRootHistos.py:27
Gaudi::Details::WeakPropertyRef::WeakPropertyRef
WeakPropertyRef(std::string value)
Definition: PropertyBase.h:160
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:112
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:93
std::set
STL class.
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Details::PropertyBase::PropertyBase
PropertyBase(std::string name, const std::type_info &type)
constructor from the property name and the type
Definition: PropertyBase.h:124
PrepareBase.out
out
Definition: PrepareBase.py:20