|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | +#include "Framework/ConfigParamRegistry.h" |
| 12 | +#include "Framework/VariantPropertyTreeHelpers.h" |
| 13 | +#include "Framework/Array2D.h" |
| 14 | + |
| 15 | +namespace o2::framework |
| 16 | +{ |
| 17 | + |
| 18 | +ConfigParamRegistry::ConfigParamRegistry(std::unique_ptr<ConfigParamStore> store) |
| 19 | + : mStore{std::move(store)} |
| 20 | +{ |
| 21 | +} |
| 22 | + |
| 23 | +bool ConfigParamRegistry::isSet(const char* key) const |
| 24 | +{ |
| 25 | + return mStore->store().count(key); |
| 26 | +} |
| 27 | + |
| 28 | +bool ConfigParamRegistry::hasOption(const char* key) const |
| 29 | +{ |
| 30 | + return mStore->store().get_child_optional(key).is_initialized(); |
| 31 | +} |
| 32 | + |
| 33 | +bool ConfigParamRegistry::isDefault(const char* key) const |
| 34 | +{ |
| 35 | + return mStore->store().count(key) > 0 && mStore->provenance(key) != "default"; |
| 36 | +} |
| 37 | + |
| 38 | +namespace |
| 39 | +{ |
| 40 | +template <SimpleConfigValueType T> |
| 41 | +T getImpl(boost::property_tree::ptree const& tree, const char* key) |
| 42 | +{ |
| 43 | + return tree.get<T>(key); |
| 44 | +} |
| 45 | + |
| 46 | +template <StringConfigValueType T> |
| 47 | +T getImpl(boost::property_tree::ptree const& tree, const char* key) |
| 48 | +{ |
| 49 | + return tree.get<std::string>(key); |
| 50 | +} |
| 51 | + |
| 52 | +template <typename T> |
| 53 | + requires base_of_template<std::vector, T> |
| 54 | +auto getImpl(boost::property_tree::ptree const& tree, const char* key) |
| 55 | +{ |
| 56 | + return o2::framework::vectorFromBranch<typename T::value_type>(tree.get_child(key)); |
| 57 | +} |
| 58 | + |
| 59 | +template <Array2DLike T> |
| 60 | +auto getImpl(boost::property_tree::ptree& tree, const char* key) |
| 61 | +{ |
| 62 | + return array2DFromBranch<typename T::element_t>(tree.get_child(key)); |
| 63 | +} |
| 64 | + |
| 65 | +template <LabeledArrayLike T> |
| 66 | +auto getImpl(boost::property_tree::ptree& tree, const char* key) |
| 67 | +{ |
| 68 | + return labeledArrayFromBranch<typename T::element_t>(tree.get_child(key)); |
| 69 | +} |
| 70 | +} // namespace |
| 71 | + |
| 72 | +template <ConfigValueType T> |
| 73 | +T ConfigParamRegistry::get(const char* key) const |
| 74 | +{ |
| 75 | + try { |
| 76 | + return getImpl<T>(this->mStore->store(), key); |
| 77 | + } catch (std::exception& e) { |
| 78 | + throw std::invalid_argument(std::string("missing option: ") + key + " (" + e.what() + ")"); |
| 79 | + } catch (...) { |
| 80 | + throw std::invalid_argument(std::string("error parsing option: ") + key); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void ConfigParamRegistry::override(const char* key, ConfigValueType auto const& val) const |
| 85 | +{ |
| 86 | + try { |
| 87 | + mStore->store().put(key, val); |
| 88 | + } catch (std::exception& e) { |
| 89 | + throw std::invalid_argument(std::string("failed to store an option: ") + key + " (" + e.what() + ")"); |
| 90 | + } catch (...) { |
| 91 | + throw std::invalid_argument(std::string("failed to store an option: ") + key); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Load extra parameters discovered while we process data |
| 96 | +void ConfigParamRegistry::loadExtra(std::vector<ConfigParamSpec>& extras) |
| 97 | +{ |
| 98 | + mStore->load(extras); |
| 99 | +} |
| 100 | + |
| 101 | +[[nodiscard]] std::vector<ConfigParamSpec> const& ConfigParamRegistry::specs() const |
| 102 | +{ |
| 103 | + return mStore->specs(); |
| 104 | +} |
| 105 | + |
| 106 | +template int8_t ConfigParamRegistry::get<int8_t>(const char* key) const; |
| 107 | +template short ConfigParamRegistry::get<short>(const char* key) const; |
| 108 | +template int ConfigParamRegistry::get<int>(const char* key) const; |
| 109 | +template long ConfigParamRegistry::get<long>(const char* key) const; |
| 110 | +template long long ConfigParamRegistry::get<long long>(const char* key) const; |
| 111 | +template uint8_t ConfigParamRegistry::get<uint8_t>(const char* key) const; |
| 112 | +template uint16_t ConfigParamRegistry::get<uint16_t>(const char* key) const; |
| 113 | +template uint32_t ConfigParamRegistry::get<uint32_t>(const char* key) const; |
| 114 | +template uint64_t ConfigParamRegistry::get<uint64_t>(const char* key) const; |
| 115 | +template LabeledArray<std::string> ConfigParamRegistry::get<LabeledArray<std::string>>(const char* key) const; |
| 116 | +template LabeledArray<double> ConfigParamRegistry::get<LabeledArray<double>>(const char* key) const; |
| 117 | +template LabeledArray<float> ConfigParamRegistry::get<LabeledArray<float>>(const char* key) const; |
| 118 | +template LabeledArray<int> ConfigParamRegistry::get<LabeledArray<int>>(const char* key) const; |
| 119 | +template Array2D<std::string> ConfigParamRegistry::get<Array2D<std::string>>(const char* key) const; |
| 120 | +template Array2D<double> ConfigParamRegistry::get<Array2D<double>>(const char* key) const; |
| 121 | +template Array2D<float> ConfigParamRegistry::get<Array2D<float>>(const char* key) const; |
| 122 | +template Array2D<int> ConfigParamRegistry::get<Array2D<int>>(const char* key) const; |
| 123 | +template std::vector<std::string> ConfigParamRegistry::get<std::vector<std::string>>(const char* key) const; |
| 124 | +template std::vector<double> ConfigParamRegistry::get<std::vector<double>>(const char* key) const; |
| 125 | +template std::vector<float> ConfigParamRegistry::get<std::vector<float>>(const char* key) const; |
| 126 | +template std::vector<int> ConfigParamRegistry::get<std::vector<int>>(const char* key) const; |
| 127 | +template float ConfigParamRegistry::get<float>(const char* key) const; |
| 128 | +template double ConfigParamRegistry::get<double>(const char* key) const; |
| 129 | +template std::string ConfigParamRegistry::get<std::string>(const char* key) const; |
| 130 | +template bool ConfigParamRegistry::get<bool>(const char* key) const; |
| 131 | + |
| 132 | +template void ConfigParamRegistry::override(const char* key, int8_t const&) const; |
| 133 | +template void ConfigParamRegistry::override(const char* key, int16_t const&) const; |
| 134 | +template void ConfigParamRegistry::override(const char* key, int32_t const&) const; |
| 135 | +template void ConfigParamRegistry::override(const char* key, int64_t const&) const; |
| 136 | +template void ConfigParamRegistry::override(const char* key, uint8_t const&) const; |
| 137 | +template void ConfigParamRegistry::override(const char* key, uint16_t const&) const; |
| 138 | +template void ConfigParamRegistry::override(const char* key, uint32_t const&) const; |
| 139 | +template void ConfigParamRegistry::override(const char* key, uint64_t const&) const; |
| 140 | +template void ConfigParamRegistry::override(const char* key, float const&) const; |
| 141 | +template void ConfigParamRegistry::override(const char* key, double const&) const; |
| 142 | +template void ConfigParamRegistry::override(const char* key, std::string const&) const; |
| 143 | +template void ConfigParamRegistry::override(const char* key, bool const&) const; |
| 144 | + |
| 145 | +//template void ConfigParamRegistry::override(char const* key, LabeledArray<std::string> const&) const; |
| 146 | +//template void ConfigParamRegistry::override(char const* key, LabeledArray<double> const&) const; |
| 147 | +//template void ConfigParamRegistry::override(char const* key, LabeledArray<float> const&) const; |
| 148 | +//template void ConfigParamRegistry::override(char const* key, LabeledArray<int> const&) const; |
| 149 | +//template void ConfigParamRegistry::override(char const* key, Array2D<std::string> const&) const; |
| 150 | +//template void ConfigParamRegistry::override(char const* key, Array2D<double> const&) const; |
| 151 | +//template void ConfigParamRegistry::override(char const* key, Array2D<float> const&) const; |
| 152 | +//template void ConfigParamRegistry::override(char const* key, Array2D<int> const&) const; |
| 153 | +//template void ConfigParamRegistry::override(char const* key, std::vector<std::string> const&) const; |
| 154 | +//template void ConfigParamRegistry::override(char const* key, std::vector<double> const&) const; |
| 155 | +//template void ConfigParamRegistry::override(char const* key, std::vector<float> const&) const; |
| 156 | +//template void ConfigParamRegistry::override(char const* key, std::vector<int> const&) const; |
| 157 | +} // namespace o2::framework |
0 commit comments