libvisiontransfer  10.8.0
parameterset.h
1 /*******************************************************************************
2  * Copyright (c) 2024 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_PARAMETERSET_H
16 #define VISIONTRANSFER_PARAMETERSET_H
17 
18 #include <string>
19 #include <vector>
20 #include <map>
21 
22 #include <cstring>
23 #include <sstream>
24 #include <iomanip>
25 #include <limits>
26 #include <memory>
27 
28 #include <visiontransfer/common.h>
29 #include <visiontransfer/parameter.h>
30 
31 namespace visiontransfer {
32 namespace param {
33 
35 class ParameterSet: public std::map<std::string, Parameter> {
36 private:
37  // We follow the pimpl idiom
38  class Pimpl;
39  Pimpl* pimpl; // Note: reserved for now, for forward compatibility
40 
41 public:
42  typedef std::shared_ptr<ParameterSet> ptr;
44  VT_EXPORT Parameter& get(const std::string& uid) {
45  auto it = find(uid);
46  if (it==end()) throw std::runtime_error(std::string("Attempted to get nonexistent parameter ") + uid);
47  return it->second;
48  }
49  VT_EXPORT bool add(const Parameter& param) { operator[](param.getUid()) = param; return true; }
51  template<typename T>
52  VT_EXPORT T getCurrentOrFallback(const std::string& key, T&& fallback) {
53  auto it = find(key);
54  if (it!=end()) return it->second.getCurrent<T>();
55  else return (T) fallback;
56  }
58  template<typename T>
59  VT_EXPORT T getCurrent(const std::string& key) {
60  auto it = find(key);
61  if (it!=end()) return it->second.getCurrent<T>();
62  else throw std::runtime_error(std::string("Parameter not found in the parameter set: ") + key);
63  }
64  // Convenience functions for quickly adding (internal) parameter values; scalars with no initial metadata
65  VT_EXPORT Parameter& setOrCreateSimpleScalar(const std::string& uid, int value);
66  VT_EXPORT Parameter& setOrCreateSimpleScalar(const std::string& uid, bool value);
67  VT_EXPORT Parameter& setOrCreateSimpleScalar(const std::string& uid, double value);
68  VT_EXPORT Parameter& setOrCreateSimpleScalar(const std::string& uid, const std::string& value);
69 };
70 
71 } // namespace param
72 } // namespace visiontransfer
73 
74 #endif
75 
visiontransfer::param::ParameterSet::getCurrentOrFallback
VT_EXPORT T getCurrentOrFallback(const std::string &key, T &&fallback)
Convenience function for safe bulk parameter access (fallback for invalid UIDs). Will return any defa...
Definition: parameterset.h:88
visiontransfer::param::ParameterSet::getCurrent
VT_EXPORT T getCurrent(const std::string &key)
Convenience function for safe bulk parameter access (throws for invalid UIDs). Will return any defaul...
Definition: parameterset.h:95
visiontransfer::param::Parameter
Definition: parameter.h:71
visiontransfer::param::ParameterSet::get
VT_EXPORT Parameter & get(const std::string &uid)
Checked parameter getter for clients to avoid instantiation of incomplete ones.
Definition: parameterset.h:80
Allied Vision