|
||||
File indexing completed on 2025-01-30 10:07:04
0001 /***********************************************************************************\ 0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations * 0003 * * 0004 * This software is distributed under the terms of the Apache version 2 licence, * 0005 * copied verbatim in the file "COPYING". * 0006 * * 0007 * In applying this licence, CERN does not waive the privileges and immunities * 0008 * granted to it by virtue of its status as an Intergovernmental Organization * 0009 * or submit itself to any jurisdiction. * 0010 \***********************************************************************************/ 0011 #pragma once 0012 0013 #include <GaudiKernel/IInterface.h> 0014 #include <GaudiKernel/TaggedBool.h> 0015 #include <regex> 0016 #include <string> 0017 #include <tuple> 0018 #include <vector> 0019 0020 namespace Gaudi { 0021 namespace Details { 0022 class PropertyBase; 0023 } 0024 namespace Interfaces { 0025 /** Interface for a component that manages application configuration options. 0026 0027 Options are to be recorded as pairs of strings: option id and value. 0028 The value must be the string representation of the a value, as understood by 0029 `Gaudi::Property<T>::fromString()`. 0030 0031 For example, if an Algorithm defines two properties as in 0032 \code 0033 class MyAlg: public Algorithm { 0034 Gaudi::Property<std::string> m_hname{this, "HistoName", "none"}; 0035 Gaudi::Property<float> m_threshold{this, "Threshold", 1.0}; 0036 }; 0037 \endcode 0038 0039 The IOptionsSvc instance should be populated with something like 0040 \code 0041 auto& optsSvc = sericeLocator()->getOptsSvc(); 0042 optsSvc.set("AlgInstanceName.HistoName", "'my_histogram'"); 0043 optsSvc.set("AlgInstanceName.Threshold", "2.345"); 0044 \endcode 0045 */ 0046 struct IOptionsSvc : virtual public IInterface { 0047 /// InterfaceID declaration. 0048 DeclareInterfaceID( IOptionsSvc, 1, 0 ); 0049 0050 /// Set the value of an option, overriding the old value, if any. 0051 virtual void set( const std::string& key, const std::string& value ) = 0; 0052 /// Get the value of an options, returning the specified _default_ value if not found. 0053 virtual std::string get( const std::string& key, const std::string& default_ = {} ) const = 0; 0054 /// Get the value of an options, removing it from the storage, returning the specified _default_ value if not 0055 /// found. 0056 virtual std::string pop( const std::string& key, const std::string& default_ = {} ) = 0; 0057 /// %Test if an option key is available in the catalog. 0058 virtual bool has( const std::string& key ) const = 0; 0059 /// %Test if an option key was explicitly set or not. 0060 virtual bool isSet( const std::string& key ) const = 0; 0061 0062 /// @name Methods to query options. 0063 /// These methods allow to extract the list of know options with their values. 0064 /// @{ 0065 /** Return all known options with their values. */ 0066 virtual std::vector<std::tuple<std::string, std::string>> items() const = 0; 0067 /** Return all known options with their values for which `predicate` evaluates to true. */ 0068 template <class UnaryPredicate> 0069 std::vector<std::tuple<std::string, std::string>> items( UnaryPredicate predicate ) const { 0070 auto v = this->items(); 0071 v.erase( std::remove_if( begin( v ), end( v ), 0072 [&predicate]( const auto& element ) { return !predicate( element ); } ), 0073 v.end() ); 0074 return v; 0075 } 0076 /** Return all known options with their values for which the key matches the given regular expression. */ 0077 std::vector<std::tuple<std::string, std::string>> items( const std::regex& filter ) const { 0078 std::smatch match; 0079 return items( 0080 [&filter, &match]( const auto& element ) { return regex_match( std::get<0>( element ), match, filter ); } ); 0081 } 0082 /// @} 0083 0084 /// Register a Gaudi::Property instance to the option service. 0085 /// 0086 /// The option will be bound to the property with name `<prefix>.<property-name>`, and immediately 0087 /// assigned the value set in the options service, if any. 0088 /// 0089 /// After the binding, setting a property value via IOptionsSvc::set will automatically set the value 0090 /// of the Gaudi::Property instance, and IOptionsSvc::get will return the string representation of the 0091 /// value of the Gaudi::Property instance. 0092 virtual void bind( const std::string& prefix, Gaudi::Details::PropertyBase* property ) = 0; 0093 0094 using OnlyDefaults = Gaudi::tagged_bool<class OnlyDefaults_tag>; 0095 0096 /// Broadcast version of IOptionsSvc::set. 0097 /// 0098 /// With IOptionsSvc::broadcast it is possible to assign one value to all known properties matching a 0099 /// regular expression. By default, only default values are overridden, but explicitly assigned values 0100 /// can also be overridden passing `OnlyDefaults{false}` as third argument to the method. 0101 virtual void broadcast( const std::regex& filter, const std::string& value, 0102 OnlyDefaults defaults = OnlyDefaults{ true } ) = 0; 0103 0104 /// look for file 'file' into search path 'path' 0105 /// and read it to update the options 0106 virtual StatusCode readOptions( std::string_view file, std::string_view path = "" ) = 0; 0107 0108 protected: 0109 virtual ~IOptionsSvc() = default; 0110 }; 0111 } // namespace Interfaces 0112 } // namespace Gaudi
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |