ComPWA
Common Partial-Wave-Analysis Framework
Parameter.hpp
Go to the documentation of this file.
1 // Copyright (c) 2013, 2017 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
9 
10 #ifndef _Parameter_HPP_
11 #define _Parameter_HPP_
12 
13 #include <algorithm>
14 #include <complex>
15 #include <fstream>
16 
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
22 
23 #include <boost/serialization/level.hpp>
24 #include <boost/serialization/nvp.hpp>
25 #include <boost/serialization/serialization.hpp>
26 #include <boost/serialization/shared_ptr.hpp>
27 #include <boost/serialization/tracking.hpp>
28 
29 namespace ComPWA {
30 namespace FunctionTree {
31 
34 enum ParType {
35  UNDEFINED = 0,
36  COMPLEX = 1,
37  DOUBLE = 2,
38  INTEGER = 3,
39  MCOMPLEX = 4,
40  MDOUBLE = 5,
42 };
43 
46 static const char *const ParNames[7] = {"UNDEFINED", "COMPLEX", "DOUBLE",
47  "INTEGER", "MCOMPLEX", "MDOUBLE",
48  "MINTEGER"};
49 
51 template <typename T> inline ParType typeName(void) {
52  return ParType::UNDEFINED;
53 }
54 
55 template <> inline ParType typeName<std::vector<std::complex<double>>>(void) {
56  return ParType::MCOMPLEX;
57 }
58 template <> inline ParType typeName<std::vector<double>>(void) {
59  return ParType::MDOUBLE;
60 }
61 template <> inline ParType typeName<std::vector<int>>(void) {
62  return ParType::MINTEGER;
63 }
64 template <> inline ParType typeName<std::complex<double>>(void) {
65  return ParType::COMPLEX;
66 }
67 template <> inline ParType typeName<double>(void) { return ParType::DOUBLE; }
68 template <> inline ParType typeName<int>(void) { return ParType::INTEGER; }
69 
79 class Parameter {
80 public:
83  : Name(name), Type(type) {}
84 
85  virtual ~Parameter() = default;
86 
88  virtual std::string name() const { return Name; }
89 
91  virtual void setName(std::string n) { Name = n; }
92 
94  virtual ParType type() const { return Type; }
95 
97  virtual std::string className() const = 0;
98 
99  virtual bool isParameter() const { return false; }
100 
101  // Observer Pattern Functions
102 
104  void attach(std::weak_ptr<ParObserver> newObserver) {
105  ObservingNodes.push_back(newObserver);
106  }
107 
109  void detachExpired() {
110  ObservingNodes.erase(std::remove_if(ObservingNodes.begin(),
111  ObservingNodes.end(),
112  [](auto x) { return x.expired(); }),
113  ObservingNodes.end());
114  }
115 
117  void notify() {
118  for (auto x : ObservingNodes) {
119  x.lock()->update();
120  }
121  }
122 
123  friend std::ostream &operator<<(std::ostream &out,
124  std::shared_ptr<Parameter> b) {
125  return out << b->to_str();
126  }
127 
128  friend std::ostream &operator<<(std::ostream &out, Parameter &b) {
129  return out << b.to_str();
130  }
131 
133  virtual std::string to_str() const = 0;
134 
136  virtual std::string val_to_str() const = 0;
137 
138  // template <typename T>
139  // std::shared_ptr<T> GetComponent() {
140  // return std::dynamic_pointer_cast<T>(shared_from_this());
141  // }
142 
143 protected:
145  std::string Name;
146 
149 
151  std::vector<std::weak_ptr<ParObserver>> ObservingNodes;
152 
153 private:
154  friend class boost::serialization::access;
155  template <class archive>
156  void serialize(archive &ar, const unsigned int version) {
157  ar &BOOST_SERIALIZATION_NVP(Name);
158  ar &BOOST_SERIALIZATION_NVP(Type);
159  }
160 };
161 } // namespace FunctionTree
162 } // namespace ComPWA
163 
164 BOOST_SERIALIZATION_SHARED_PTR(Parameter);
165 
166 BOOST_CLASS_IMPLEMENTATION(
168  boost::serialization::level_type::object_serializable)
169 
170 #endif
void serialize(archive &ar, const unsigned int version)
Definition: Parameter.hpp:156
ParObserver class.
void attach(std::weak_ptr< ParObserver > newObserver)
Attaches a new TreeNode as Observer.
Definition: Parameter.hpp:104
virtual std::string val_to_str() const =0
A public function returning a string with parameter value.
Base class for internal parameter.
Definition: Parameter.hpp:79
virtual bool isParameter() const
Definition: Parameter.hpp:99
Parameter(std::string name, ParType type=ParType::UNDEFINED)
Constructor with name of parameter and optional type.
Definition: Parameter.hpp:82
virtual std::string name() const
Getter for name of object.
Definition: Parameter.hpp:88
void detachExpired()
Removes TreeNodes not needed as Observer anymore.
Definition: Parameter.hpp:109
ParType typeName(void)
Template functions which return above specified parameter types.
Definition: Parameter.hpp:51
std::string Name
Name of parameter.
Definition: Parameter.hpp:145
friend std::ostream & operator<<(std::ostream &out, Parameter &b)
Definition: Parameter.hpp:128
static const char *const ParNames[7]
Names of the parameter types, should be extended if an new parameter type is added.
Definition: Parameter.hpp:46
ParType typeName< int >(void)
Definition: Parameter.hpp:68
virtual ParType type() const
Getter for type of object.
Definition: Parameter.hpp:94
ParType Type
Type of parameter (e.g. Double, Integer, ...)
Definition: Parameter.hpp:148
friend std::ostream & operator<<(std::ostream &out, std::shared_ptr< Parameter > b)
Definition: Parameter.hpp:123
void notify()
Notify all observing TreeNodes that parameter changed.
Definition: Parameter.hpp:117
ParType typeName< double >(void)
Definition: Parameter.hpp:67
ParType
Enums for the type of the parameter, should be extended if an new parameter type is added...
Definition: Parameter.hpp:34
std::vector< std::weak_ptr< ParObserver > > ObservingNodes
List of observers, e.g. TreeNodes.
Definition: Parameter.hpp:151
virtual std::string to_str() const =0
A public function returning a string with parameter information.
virtual std::string className() const =0
Getter for typename of object, to be defined by the actual implementation.
virtual void setName(std::string n)
Getter for name of object.
Definition: Parameter.hpp:91