The Gaudi Framework  v33r1 (b1225454)
CArrayAsProperty.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 "LICENSE". *
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 #ifndef NO_C_ARRAY_AS_PROPERTY_WARNING
12 # warning deprecated header (will be removed in Gaudi v29r0), think about using std::array<T,N> instead of T[N]
13 #endif
14 #ifndef GAUDIKERNEL_CARRAYASPROPERTY_H
15 # define GAUDIKERNEL_CARRAYASPROPERTY_H
16 # include "GaudiKernel/Property.h"
17 
18 namespace Gaudi {
19  template <class TYPE, size_t N, class VERIFIER, class HANDLERS>
20  class Property<TYPE[N], VERIFIER, HANDLERS> : public Details::PropertyBase {
21  public:
22  // ==========================================================================
24  using StorageType = TYPE ( & )[N];
26  using VerifierType = VERIFIER;
27  using HandlersType = HANDLERS;
28 
29  private:
36  template <class T>
37  static inline constexpr bool is_this_type_v = std::is_same_v<Property, typename std::remove_reference_t<T>>;
38  template <class T>
41  public:
42  // ==========================================================================
45  : PropertyBase( typeid( ValueType ), std::move( name ), std::move( doc ) ), m_value( value ) {
47  }
48 
49  using PropertyBase::declareReadHandler;
50  using PropertyBase::declareUpdateHandler;
51 
54  m_handlers.setReadHandler( std::move( fun ) );
55  return *this;
56  }
59  m_handlers.setUpdateHandler( std::move( fun ) );
60  return *this;
61  }
62 
64  const std::function<void( PropertyBase& )> readCallBack() const override { return m_handlers.getReadHandler(); }
66  const std::function<void( PropertyBase& )> updateCallBack() const override { return m_handlers.getUpdateHandler(); }
67 
69  bool useUpdateHandler() override {
70  m_handlers.useUpdateHandler( *this );
71  return true;
72  }
73 
75  operator const ValueType&() const {
76  m_handlers.useReadHandler( *this );
77  return m_value;
78  }
79  // /// Automatic conversion to value (reference).
80  // operator ValueType& () {
81  // useReadHandler();
82  // return m_value;
83  // }
84 
86  Property& operator=( const ValueType& v ) {
87  m_verifier( v );
88  for ( size_t i = 0; i != N; ++i ) { m_value[i] = v[i]; }
89  m_handlers.useUpdateHandler( *this );
90  return *this;
91  }
92 
94  // Property(const Property& other):
95  // PropertyBase(other), value(other.m_value) {}
96 
98  const VerifierType& verifier() const { return m_verifier; }
101 
104  const ValueType& value() const { return *this; }
105  ValueType& value() { return const_cast<ValueType&>( (const ValueType&)*this ); }
106  bool setValue( const ValueType& v ) {
107  *this = v;
108  return true;
109  }
110  bool set( const ValueType& v ) {
111  *this = v;
112  return true;
113  }
114  PropertyBase* clone() const override { return new Property( *this ); }
116 
120  inline size_t size() const { return N; }
121  inline bool empty() const { return false; }
122  template <class T = const ValueType>
123  inline decltype( std::declval<T>()[typename T::key_type{}] ) operator[]( const typename T::key_type& key ) const {
124  return value()[key];
125  }
126  template <class T = ValueType>
127  inline decltype( std::declval<T>()[typename T::key_type{}] ) operator[]( const typename T::key_type& key ) {
128  return value()[key];
129  }
131  // ==========================================================================
132  public:
134  bool assign( const PropertyBase& source ) override {
135  // Is the property of "the same" type?
136  const Property* p = dynamic_cast<const Property*>( &source );
137  if ( p ) {
138  *this = p->value();
139  } else {
140  this->fromString( source.toString() ).ignore();
141  }
142  return true;
143  }
145  bool load( PropertyBase& dest ) const override {
146  // delegate to the 'opposite' method
147  return dest.assign( *this );
148  }
150  StatusCode fromString( const std::string& source ) override {
151  ValueType tmp;
152  if ( Parsers::parse( tmp, source ).isSuccess() ) {
153  *this = tmp;
154  } else {
155  throw std::invalid_argument( "cannot parse '" + source + "' to " + this->type() );
156  }
157  return StatusCode::SUCCESS;
158  }
160  std::string toString() const override {
161  m_handlers.useReadHandler( *this );
162  return Utils::toString( m_value );
163  }
165  void toStream( std::ostream& out ) const override {
166  m_handlers.useReadHandler( *this );
167  Utils::toStream( m_value, out );
168  }
169  };
170  template <class TYPE, size_t N, class VERIFIER, class HANDLERS>
171  class Property<TYPE ( & )[N], VERIFIER, HANDLERS> : public Property<TYPE[N], VERIFIER, HANDLERS> {
172  public:
174  };
175 } // namespace Gaudi
176 #endif
const ValueType & value() const
Backward compatibility.
StatusCode fromString(const std::string &source) override
string -> value
std::string toString() const override
value -> string
std::ostream & toStream(ITERATOR first, ITERATOR last, std::ostream &s, const std::string &open, const std::string &close, const std::string &delim)
the helper function to print the sequence
Definition: ToStream.h:291
static constexpr bool is_this_type_v
helper typedefs for SFINAE
Definition: Property.h:388
bool useUpdateHandler() override
manual trigger for callback for update
Implementation of property with value of concrete type.
Definition: Property.h:370
void toStream(std::ostream &out) const override
value -> stream
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:341
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
STL namespace.
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
HandlersType m_handlers
Definition: Property.h:384
StorageType m_value
Storage.
Definition: Property.h:382
const std::function< void(PropertyBase &)> readCallBack() const override
get a reference to the readCallBack
const std::function< void(PropertyBase &)> updateCallBack() const override
get a reference to the updateCallBack
const std::string name() const
property name
Definition: Property.h:46
typename std::remove_reference< StorageType >::type ValueType
PropertyBase & declareUpdateHandler(std::function< void(PropertyBase &)> fun) override
set new callback for update
Property()
Construct an anonymous property with default constructed value.
Definition: Property.h:466
STL class.
PropertyBase(const std::type_info &type, std::string name="", std::string doc="", std::string semantics="")
constructor from the property name and the type
Definition: Property.h:125
int N
Definition: IOTest.py:110
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:554
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
T move(T... args)
const VerifierType & verifier() const
Copy constructor.
PropertyBase * clone() const override
clones the current property
std::string type() const
property type
Definition: Property.h:54
bool assign(const PropertyBase &source) override
get the value from another property
Property(std::string name, StorageType value, std::string doc="")
the constructor with property name, value and documentation.
PropertyBase & declareReadHandler(std::function< void(PropertyBase &)> fun) override
set new callback for reading
bool load(PropertyBase &dest) const override
set value to another property
STL class.
Property & operator=(const ValueType &v)
Assignment from value.
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
VerifierType m_verifier
Definition: Property.h:383
VerifierType & verifier()
Accessor to verifier.
StatusCode fromString(const std::string &source) override
string -> value
Definition: Property.h:704