2#ifndef PODIO_GENERICPARAMETERS_H
3#define PODIO_GENERICPARAMETERS_H 1
15#define DEPRECATED_ACCESS [[deprecated("Use templated access functionality")]]
24static constexpr bool isSupportedGenericDataType = detail::isAnyOrVectorOf<T, SupportedGenericDataTypes>;
43 using type =
const std::string&;
49 using type =
const std::vector<T>&;
77 using MapType = std::map<std::string, std::vector<T>>;
85 using MutexPtr = std::unique_ptr<std::mutex>;
104 template <
typename T,
typename = EnableIfVal
idGenericDataType<T>>
108 template <
typename T,
typename = EnableIfVal
idGenericDataType<T>>
109 void setValue(
const std::string& key, T value);
112 void setValue(
const std::string& key, std::string value) {
113 setValue<std::string>(key, std::move(value));
117 void setValue(
const std::string& key, std::vector<std::string> values) {
118 setValue<std::vector<std::string>>(key, std::move(values));
122 template <
typename T,
typename = std::enable_if_t<detail::isInTuple<T, SupportedGenericDataTypes>>>
123 void setValue(
const std::string& key, std::initializer_list<T>&& values) {
124 setValue<std::vector<T>>(key, std::move(values));
128 template <
typename T,
typename = EnableIfVal
idGenericDataType<T>>
129 size_t getN(
const std::string& key)
const;
132 template <
typename T,
typename = EnableIfVal
idGenericDataType<T>>
133 std::vector<std::string>
getKeys()
const;
205 void print(std::ostream& os = std::cout,
bool flush =
true);
209 return _intMap.empty() && _floatMap.empty() && _stringMap.empty();
216 return getMap<int>();
219 return getMap<int>();
226 return getMap<float>();
229 return getMap<float>();
236 return getMap<double>();
239 return getMap<double>();
246 return getMap<std::string>();
249 return getMap<std::string>();
254 template <
typename T>
255 const MapType<detail::GetVectorType<T>>& getMap()
const {
256 if constexpr (std::is_same_v<detail::GetVectorType<T>,
int>) {
258 }
else if constexpr (std::is_same_v<detail::GetVectorType<T>,
float>) {
260 }
else if constexpr (std::is_same_v<detail::GetVectorType<T>,
double>) {
268 template <
typename T>
269 MapType<detail::GetVectorType<T>>& getMap() {
270 if constexpr (std::is_same_v<detail::GetVectorType<T>,
int>) {
272 }
else if constexpr (std::is_same_v<detail::GetVectorType<T>,
float>) {
274 }
else if constexpr (std::is_same_v<detail::GetVectorType<T>,
double>) {
282 template <
typename T>
283 std::mutex& getMutex()
const {
284 if constexpr (std::is_same_v<detail::GetVectorType<T>,
int>) {
285 return *(m_intMtx.get());
286 }
else if constexpr (std::is_same_v<detail::GetVectorType<T>,
float>) {
287 return *(m_floatMtx.get());
288 }
else if constexpr (std::is_same_v<detail::GetVectorType<T>,
double>) {
289 return *(m_doubleMtx.get());
291 return *(m_stringMtx.get());
297 mutable MutexPtr m_intMtx{
nullptr};
298 FloatMap _floatMap{};
299 mutable MutexPtr m_floatMtx{
nullptr};
300 StringMap _stringMap{};
301 mutable MutexPtr m_stringMtx{
nullptr};
302 DoubleMap _doubleMap{};
303 mutable MutexPtr m_doubleMtx{
nullptr};
306template <
typename T,
typename>
308 const auto& map = getMap<T>();
309 auto& mtx = getMutex<T>();
310 std::lock_guard lock{mtx};
311 const auto it = map.find(key);
314 if (it == map.end()) {
315 static const auto empty = T{};
320 if constexpr (detail::isVector<T>) {
323 const auto& iv = it->second;
328template <
typename T,
typename>
330 auto& map = getMap<T>();
331 auto& mtx = getMutex<T>();
333 if constexpr (detail::isVector<T>) {
334 std::lock_guard lock{mtx};
335 map.insert_or_assign(key, std::move(value));
338 std::vector<T> v = {value};
339 std::lock_guard lock{mtx};
340 map.insert_or_assign(key, std::move(v));
344template <
typename T,
typename>
346 const auto& map = getMap<T>();
347 auto& mtx = getMutex<T>();
348 std::lock_guard lock{mtx};
349 if (
const auto it = map.find(key); it != map.end()) {
350 return it->second.size();
355template <
typename T,
typename>
357 std::vector<std::string> keys;
358 const auto& map = getMap<T>();
359 keys.reserve(map.size());
361 auto& mtx = getMutex<T>();
362 std::lock_guard lock{mtx};
363 std::transform(map.begin(), map.end(), std::back_inserter(keys), [](
const auto& pair) { return pair.first; });
#define DEPRECATED_ACCESS
DEPRECATED_ACCESS int getIntVal(const std::string &key) const
GenericParameters(GenericParameters &&)=default
GenericParameters are default moveable.
DEPRECATED_ACCESS IntVec & getIntVals(const std::string &key, IntVec &values) const
GenericDataReturnType< T > getValue(const std::string &) const
DEPRECATED_ACCESS int getNString(const std::string &key) const
StringMap & getStringMap()
~GenericParameters()=default
void setValue(const std::string &key, std::initializer_list< T > &&values)
Overload for catching initializer list setting for vector values.
std::vector< std::string > getKeys() const
Get all available keys for a given type.
void setValue(const std::string &key, std::vector< std::string > values)
Overlaod for catching initializer list setting of string vector values.
void setValue(const std::string &key, std::string value)
Overload for catching const char* setting for string values.
bool empty() const
Check if no parameter is stored (i.e. if all internal maps are empty)
void setValue(const std::string &key, T value)
Store (a copy of) the passed value under the given key.
DEPRECATED_ACCESS void setValues(const std::string &key, const IntVec &values)
GenericParameters & operator=(const GenericParameters &)=delete
DEPRECATED_ACCESS FloatVec & getFloatVals(const std::string &key, FloatVec &values) const
DEPRECATED_ACCESS const std::string & getStringVal(const std::string &key) const
const StringMap & getStringMap() const
void clear()
erase all elements
DEPRECATED_ACCESS int getNFloat(const std::string &key) const
DEPRECATED_ACCESS int getNInt(const std::string &key) const
const IntMap & getIntMap() const
std::map< std::string, std::vector< T > > MapType
DEPRECATED_ACCESS StringVec & getStringVals(const std::string &key, StringVec &values) const
const FloatMap & getFloatMap() const
DEPRECATED_ACCESS float getFloatVal(const std::string &key) const
void print(std::ostream &os=std::cout, bool flush=true)
DoubleMap & getDoubleMap()
GenericParameters & operator=(GenericParameters &&)=default
size_t getN(const std::string &key) const
Get the number of elements stored under the given key for a type.
const DoubleMap & getDoubleMap() const
DEPRECATED_ACCESS const StringVec & getIntKeys(StringVec &keys) const
DEPRECATED_ACCESS const StringVec & getFloatKeys(StringVec &keys) const
DEPRECATED_ACCESS const StringVec & getStringKeys(StringVec &keys) const
std::vector< float > FloatVec
std::vector< int > IntVec
typename detail::GenericDataReturnTypeHelper< T >::type GenericDataReturnType
std::tuple< int, float, std::string, double > SupportedGenericDataTypes
The types which are supported in the GenericParameters.
std::vector< std::string > StringVec
typename std::enable_if_t< isSupportedGenericDataType< T > > EnableIfValidGenericDataType
const std::vector< T > & type