PCB Environment: /home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Parameter.hpp Source File

PCB Environment 2

Loading...
Searching...
No Matches
Parameter.hpp
1#ifndef GYM_PCB_PARAMETER_H
2#define GYM_PCB_PARAMETER_H
3
4#include <set>
5#include <variant>
6#include "Py.hpp"
7
8 class Parameter
9{
10public:
11 Parameter(const char *name, const std::function<void(const Parameter&)> &commit);
12 const std::string& name() const { return mName; }
13 char dtype() const;
14 void set(double);
15 void set(int64_t);
16 void set(const std::string&);
17 void set(const char *s) { set(std::string(s)); }
18 void set(bool);
19 void set(PyObject *);
20 void setLimits(double def, double min, double max);
21 void setLimits(int64_t def, int64_t min, int64_t max);
22 void addEnum(const std::string &s) { mEnum.insert(s); }
23 void setVisible(bool b) { mVisible = b; }
24 bool visible() const { return mVisible; }
25 void init(bool b) { mValue = b; }
26 void init(const std::string &s) { mValue = s; }
27 void init(const char *s) { init(std::string(s)); }
28 void setNotifier(const std::function<void()> &notify) { mNotify = notify; }
29 double min() const { return mLimitMin; }
30 double max() const { return mLimitMax; }
31 double d() const;
32 bool b() const;
33 const std::string& s() const;
34 const std::set<std::string>& getEnum() const { return mEnum; }
35 int64_t i() const;
36 int64_t as_i() const;
37 double as_d() const;
38 std::string as_s() const;
39 bool as_b() const;
40 template<typename T> bool is_a() const { return std::holds_alternative<T>(mValue); }
41private:
42 const std::string mName;
43 std::variant<double, int64_t, std::string, bool> mValue;
44 std::set<std::string> mEnum;
45 double mLimitMin{-std::numeric_limits<double>::infinity()};
46 double mLimitMax{+std::numeric_limits<double>::infinity()};
47 const std::function<void(const Parameter&)> mCommit;
48 bool mVisible{true};
49 std::function<void()> mNotify;
50};
51
52inline char Parameter::dtype() const
53{
54 assert(mValue.index() >= 0 && mValue.index() < 4);
55 return "disb"[mValue.index()];
56}
57inline double Parameter::d() const
58{
59 return std::get<double>(mValue);
60}
61inline int64_t Parameter::i() const
62{
63 return std::get<int64_t>(mValue);
64}
65inline const std::string& Parameter::s() const
66{
67 return std::get<std::string>(mValue);
68}
69inline bool Parameter::b() const
70{
71 return std::get<bool>(mValue);
72}
73
74#endif // GYM_PCB_PARAMETER_H