Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:09:54

0001 #ifndef ATOOLS_Org_ScopedSettings_H
0002 #define ATOOLS_Org_ScopedSettings_H
0003 
0004 #include "ATOOLS/Org/Settings.H"
0005 
0006 #include <cassert>
0007 
0008 namespace ATOOLS {
0009 
0010   class Scoped_Settings {
0011 
0012     friend Settings;
0013 
0014   public:
0015 
0016     Scoped_Settings();
0017     Scoped_Settings(const std::string& yamlstring);
0018     Scoped_Settings(const Scoped_Settings&);
0019 
0020     Scoped_Settings& operator=(Scoped_Settings);
0021 
0022     Scoped_Settings operator[](const std::string& scope) const;
0023     Scoped_Settings operator[](size_t scope) const;
0024 
0025     std::vector<std::string> GetKeys();
0026 
0027     void DeclareVectorSettingsWithEmptyDefault(const std::vector<std::string>& keys);
0028     void DeclareMatrixSettingsWithEmptyDefault(const std::vector<std::string>& keys);
0029 
0030     template <typename T>
0031     Scoped_Settings& SetDefault(const T&);
0032     template <typename T>
0033     Scoped_Settings& SetDefault(const std::vector<T>&);
0034     template <typename T>
0035     Scoped_Settings& SetDefault(std::initializer_list<T>);
0036     template <typename T>
0037     Scoped_Settings& SetDefault(const std::vector<std::vector<T>>&);
0038     template <typename T>
0039     Scoped_Settings& SetDefault(std::initializer_list<std::initializer_list<T>>);
0040 
0041     bool HasDefault() const;
0042     Scoped_Settings& ResetDefault();
0043 
0044     template <typename T>
0045     Scoped_Settings& SetReplacementList(const std::map<std::string, T>& list);
0046 
0047     Scoped_Settings& SetDefaultSynonyms(const String_Vector&);
0048     Scoped_Settings& SetSynonyms(const String_Vector&);
0049 
0050     /// set a common replacement list: [Off, false, 0, no] -> None
0051     Scoped_Settings& UseNoneReplacements();
0052 
0053     /// set a common replacement list: [None] -> max double
0054     Scoped_Settings& UseMaxDoubleReplacements();
0055 
0056     /// set a common replacement list: [None] -> 0
0057     Scoped_Settings& UseZeroReplacements();
0058 
0059     bool IsSetExplicitly();
0060 
0061     void SetInterpreterEnabled(bool b) { m_interpreterenabled = b; }
0062     template <typename T>
0063     T Interprete(const std::string&);
0064 
0065     template <typename T>
0066     void OverrideScalar(const T&);
0067     template <typename T>
0068     void OverrideVector(const std::vector<T>&);
0069     template <typename T>
0070     void OverrideMatrix(const std::vector<std::vector<T>>&);
0071 
0072     template <typename T>
0073     T Get(); // alias for GetScalar
0074     template <typename T>
0075     T GetScalar();
0076     template <typename T>
0077     std::vector<T> GetVector();
0078     template <typename T>
0079     std::vector<std::vector<T>> GetMatrix();
0080 
0081     template <typename T>
0082     T GetScalarWithOtherDefault(const T& otherdefault);
0083 
0084     /**
0085      * convenience method to read two-valued settings
0086      *
0087      * this is often useful in the context of beam-specific settings
0088      * @param expandsinglevalues If true and only a single value is
0089      * encountered, this value is automatically repeated to return a two-valued
0090      * vector.
0091      */
0092     template <typename T>
0093     std::vector<T> GetTwoVector(bool expandsinglevalues=true);
0094 
0095     void ReplaceTags(std::string& s) { return m_rootsettings->ReplaceTags(s); }
0096 
0097     std::string GetPath() { return m_rootsettings->GetPath(); }
0098 
0099     bool IsScalar() const;
0100     bool IsList() const;
0101     bool IsMap() const;
0102     std::vector<Scoped_Settings> GetItems() const;
0103     size_t GetItemsCount();
0104     Scoped_Settings GetItemAtIndex(const size_t&) const;
0105 
0106     /**
0107      * obtain the index of the current item
0108      *
0109      * If the scope does not point to a sequence of sub-settings, an exception
0110      * is thrown.
0111      */
0112     size_t GetIndex() const;
0113 
0114   private:
0115 
0116     // use this if it is not otherwise guaranteed that rootsettings is kept
0117     // alive
0118     std::shared_ptr<Settings> m_ownedsettings;
0119     Settings* m_rootsettings;
0120     Settings_Keys m_scopes;
0121 
0122     Scoped_Settings(Settings &rootsettings, const std::string& scope);
0123     Scoped_Settings Scoped(const Setting_Key& scope) const;
0124     void AppendScope(const Setting_Key& scope);
0125 
0126     bool m_interpreterenabled{ true };
0127 
0128   };
0129 
0130   template <typename T>
0131   Scoped_Settings& Scoped_Settings::SetDefault(const T& value)
0132   {
0133     m_rootsettings->SetDefault(m_scopes, value);
0134     return *this;
0135   }
0136 
0137   template <typename T>
0138   Scoped_Settings& Scoped_Settings::SetDefault(const std::vector<T>& values)
0139   {
0140     m_rootsettings->SetDefault<T>(m_scopes, values);
0141     return *this;
0142   }
0143 
0144   template <typename T>
0145   Scoped_Settings& Scoped_Settings::SetDefault(std::initializer_list<T> values)
0146   {
0147     m_rootsettings->SetDefault<T>(m_scopes, std::vector<T>(values));
0148     return *this;
0149   }
0150 
0151   template <typename T>
0152   Scoped_Settings& Scoped_Settings::SetDefault(
0153       const std::vector<std::vector<T>>& values)
0154   {
0155     m_rootsettings->SetDefaultMatrix<T>(m_scopes, values);
0156     return *this;
0157   }
0158 
0159   template <typename T>
0160   Scoped_Settings& Scoped_Settings::SetDefault(std::initializer_list<std::initializer_list<T>> l)
0161   {
0162     std::vector<std::vector<T>> m;
0163     for (const auto& row : l)
0164       m.push_back(row);
0165     m_rootsettings->SetDefaultMatrix<T>(m_scopes, m);
0166     return *this;
0167   }
0168 
0169   template <typename T>
0170   Scoped_Settings& Scoped_Settings::SetReplacementList(
0171       const std::map<std::string, T>& list)
0172   {
0173     m_rootsettings->SetReplacementList(m_scopes, list);
0174     return *this;
0175   }
0176 
0177   template <typename T>
0178   void Scoped_Settings::OverrideScalar(const T& value)
0179   {
0180     m_rootsettings->OverrideScalar<T>(m_scopes, value);
0181   }
0182 
0183   template <typename T>
0184   void Scoped_Settings::OverrideVector(const std::vector<T>& values)
0185   {
0186     m_rootsettings->OverrideVector<T>(m_scopes, values);
0187   }
0188 
0189   template <typename T>
0190   void Scoped_Settings::OverrideMatrix(
0191       const std::vector<std::vector<T>>& values)
0192   {
0193     m_rootsettings->OverrideMatrix<T>(m_scopes, values);
0194   }
0195 
0196   template <typename T>
0197   T Scoped_Settings::Get()
0198   {
0199     return GetScalar<T>();
0200   }
0201 
0202   template <typename T>
0203   T Scoped_Settings::GetScalar()
0204   {
0205     auto wasenabled = m_rootsettings->SetInterpreterEnabled(m_interpreterenabled);
0206     const auto r = m_rootsettings->GetScalar<T>(m_scopes);
0207     m_rootsettings->SetInterpreterEnabled(wasenabled);
0208     return r;
0209   }
0210 
0211   template <typename T>
0212   std::vector<T> Scoped_Settings::GetVector()
0213   {
0214     auto wasenabled = m_rootsettings->SetInterpreterEnabled(m_interpreterenabled);
0215     const auto r = m_rootsettings->GetVector<T>(m_scopes);
0216     m_rootsettings->SetInterpreterEnabled(wasenabled);
0217     return r;
0218   }
0219 
0220   template <typename T>
0221   std::vector<std::vector<T>> Scoped_Settings::GetMatrix()
0222   {
0223     auto wasenabled = m_rootsettings->SetInterpreterEnabled(m_interpreterenabled);
0224     const auto r = m_rootsettings->GetMatrix<T>(m_scopes);
0225     m_rootsettings->SetInterpreterEnabled(wasenabled);
0226     return r;
0227   }
0228 
0229   template <typename T>
0230   T Scoped_Settings::GetScalarWithOtherDefault(const T& otherdefault)
0231   {
0232     auto wasenabled = m_rootsettings->SetInterpreterEnabled(m_interpreterenabled);
0233     const auto r =
0234       m_rootsettings->GetScalarWithOtherDefault<T>(m_scopes, otherdefault);
0235     m_rootsettings->SetInterpreterEnabled(wasenabled);
0236     return r;
0237   }
0238 
0239   template <typename T>
0240   std::vector<T> Scoped_Settings::GetTwoVector(bool expandsinglevalues)
0241   {
0242     auto values = GetVector<T>();
0243     if (values.size() == 1 && expandsinglevalues) {
0244       values.push_back(values[0]);
0245     } else if (values.size() != 2) {
0246       THROW(fatal_error,
0247             "The setting " + m_scopes.Name() + " must hold "
0248             + (expandsinglevalues ? "one or two" : "two")
0249             + " values.");
0250     }
0251     assert(values.size() == 2);
0252     return values;
0253   }
0254 
0255   template <typename T>
0256   T Scoped_Settings::Interprete(const std::string& value)
0257   {
0258     return m_rootsettings->Interprete<T>(value);
0259   }
0260 
0261 }
0262 
0263 #endif