The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
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>
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
24namespace 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>
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 ) {}
122
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 ) {}
125
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;
146 void add( WeakPropertyRef* ref ) {
147 if ( ref ) m_weakReferences.insert( ref );
148 }
149 void remove( WeakPropertyRef* ref ) { m_weakReferences.erase( ref ); }
150 };
151
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:
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
#define GAUDI_API
Definition Kernel.h:49
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
const std::type_info * m_typeinfo
property type
PropertyBase & declareReadHandler(void(HT::*MF)(PropertyBase &), HT *instance)
virtual PropertyBase * clone() const =0
clones the current property
virtual bool load(PropertyBase &dest) const =0
export the property value to the destination
PropertyBase(const std::type_info &type, std::string name="", std::string doc="", std::string semantics="")
constructor from the property name and the type
virtual PropertyBase & declareReadHandler(std::function< void(PropertyBase &)> fun)=0
set new callback for reading
void setSemantics(std::string value)
set the semantics string
virtual bool useUpdateHandler()=0
manual trigger for callback for update
virtual const std::function< void(PropertyBase &)> readCallBack() const =0
get a reference to the readCallBack
void remove(WeakPropertyRef *ref)
const std::type_info * type_info() const
property type-info
void setOwnerType(const std::type_info &ownerType)
set the type of the owner class (used for documentation)
void setDocumentation(std::string value)
set the documentation string
void setOwnerType()
set the type of the owner class (used for documentation)
std::string documentation() const
property documentation
virtual std::string toString() const =0
value -> string
const std::type_info * m_ownerType
type of owner of the property (if defined)
void setName(std::string value)
set the new value for the property name
virtual void toStream(std::ostream &out) const =0
value -> stream
std::string type() const
property type
virtual bool assign(const PropertyBase &source)=0
import the property value form the source
std::string_view m_semantics
property semantics
virtual StatusCode fromString(const std::string &value)=0
string -> value
PropertyBase(std::string name, const std::type_info &type)
constructor from the property name and the type
PropertyBase & operator=(const PropertyBase &)=default
assignment operator
std::set< WeakPropertyRef * > m_weakReferences
void add(WeakPropertyRef *ref)
virtual ~PropertyBase()
virtual destructor
const std::type_info * ownerType() const
get the type of the owner class (used for documentation)
std::string semantics() const
property semantics
std::string_view m_name
property name
PropertyBase & declareUpdateHandler(void(HT::*MF)(PropertyBase &), HT *instance)
std::string_view m_documentation
property doc string
static std::string_view to_view(std::string str)
helper to map a string to a reliable std::string_view
Definition Property.cpp:41
std::string ownerTypeName() const
get the string for the type of the owner class (used for documentation)
virtual std::ostream & fillStream(std::ostream &) const
the printout of the property value
Definition Property.cpp:45
virtual PropertyBase & declareUpdateHandler(std::function< void(PropertyBase &)> fun)=0
set new callback for update
const std::string name() const
property name
virtual const std::function< void(PropertyBase &)> updateCallBack() const =0
get a reference to the updateCallBack
Optional reference to a property that can be used to refer to a sting or to the string representation...
WeakPropertyRef & operator=(WeakPropertyRef &&other)
WeakPropertyRef & operator=(const std::string &value)
WeakPropertyRef(std::string value)
WeakPropertyRef(const WeakPropertyRef &other)=delete
WeakPropertyRef(PropertyBase &property)
WeakPropertyRef(WeakPropertyRef &&other)
WeakPropertyRef & operator=(PropertyBase &value)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
std::ostream & operator<<(std::ostream &stream, const PropertyBase &prop)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:260
STL namespace.