value.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. * This header defines a set of generic classes that wrap up "values". In
  33. * essence, a Value<T> object is just something that contains a value of type T
  34. * and can provide it when requested. The usefulness stems from composing
  35. * values together with calculations. This enables very clear dependency
  36. * mapping and a way to know clearly how every value was arrived at. This could
  37. * be used to, for example, automatically generate commentary for plots that
  38. * explain the exect calculation used to create it. Or easily making a series
  39. * of plots contrasting different values that have been composed slightly
  40. * differently.
  41. */
  42. #ifndef value_hpp
  43. #define value_hpp
  44. #include <iomanip>
  45. #include <iostream>
  46. #include <sstream>
  47. #include <utility>
  48. #include <algorithm>
  49. #include <map>
  50. #include <vector>
  51. #include <tuple>
  52. #include <initializer_list>
  53. #include <functional>
  54. #include "log.hpp"
  55. /**
  56. * The namespace containing all filval classes and functions.
  57. */
  58. namespace fv{
  59. bool in_register_function = false;
  60. template<typename> class Function; // undefined
  61. /**
  62. * Parent class to all Function classes. Holds a class-level collection of all
  63. * created function objects.
  64. */
  65. class GenFunction {
  66. private:
  67. std::string name;
  68. std::string impl;
  69. public:
  70. /**
  71. * Static mapping of functions from their name to the object wrapper of
  72. * the function.
  73. */
  74. inline static std::map<const std::string, GenFunction*> function_registry;
  75. GenFunction(const std::string& name, const std::string& impl)
  76. :impl(impl),
  77. name(name){
  78. }
  79. virtual ~GenFunction() { };
  80. std::string& get_name(){
  81. return name;
  82. }
  83. /**
  84. * Attempt to invoke clang-format for the purpose of printing out
  85. * nicely formatted functions to the log file. If clang-format is not
  86. * present, this function just passes through the code unmodified.
  87. */
  88. static std::string format_code(const std::string& code){
  89. std::stringstream code_out("");
  90. std::string command("echo \""+code+"\" | clang-format");
  91. char buffer[255];
  92. FILE *stream = popen(command.c_str(), "r");
  93. while (fgets(buffer, 255, stream) != NULL)
  94. code_out << buffer;
  95. if (pclose(stream) == 0)
  96. return code_out.str();
  97. else
  98. return code;
  99. }
  100. static std::string summary(){
  101. std::stringstream ss;
  102. ss << "The following functions have been registered" << std::endl;
  103. for(auto p : function_registry){
  104. if (p.second == nullptr) continue;
  105. ss << "-->" << p.second->name << std::endl;
  106. ss << format_code(p.second->impl);
  107. }
  108. return ss.str();
  109. }
  110. template <typename T>
  111. static Function<T>& register_function(const std::string& name, std::function<T> f, const std::string& impl){
  112. in_register_function = true;
  113. Function<T>* func;
  114. if (GenFunction::function_registry[name] != nullptr){
  115. func = dynamic_cast<Function<T>*>(GenFunction::function_registry[name]);
  116. if (func == nullptr){
  117. ERROR("Trying to register function which has already been registerd with a different type");
  118. }
  119. } else {
  120. func = new Function<T>(name, impl, f);
  121. GenFunction::function_registry[name] = func;
  122. }
  123. in_register_function = false;
  124. return *func;
  125. }
  126. };
  127. /**
  128. * In order to enable proper provenance tracking, and at the same time keep
  129. * the ability to embed functions into values, the Function class should be
  130. * used. It is simply a wrapper around a std::function that also has a name.
  131. * This name is used when generating the name of values that use the function.
  132. * A function name is automatically prepended with "func::" to explicitly state
  133. * that the value is the result of a computation encoded within the function
  134. * object, and not from some other Value object. Unfortunately, it is up to the
  135. * user to find where that function is defined in the source code to inspect
  136. * what it is doing. But hopefully this isn't too onerous by just using grep.
  137. */
  138. template <typename R, typename... ArgTypes>
  139. class Function<R(ArgTypes...)> : public GenFunction {
  140. private:
  141. std::function<R(ArgTypes...)> f;
  142. public:
  143. Function(const std::string& name, const std::string& impl, std::function<R(ArgTypes...)> f)
  144. :GenFunction(name, impl), f(f){
  145. if (!in_register_function) {
  146. WARNING("Don't instantiate Function objects directly! Use register_function instead.");
  147. }
  148. }
  149. Function(const std::string& name, std::function<R(ArgTypes...)> f)
  150. :Function(name, "N/A", f){ }
  151. ~Function() { }
  152. R operator()(ArgTypes ...args){
  153. return f(args...);
  154. }
  155. };
  156. #define FUNC(f) f, #f
  157. /**
  158. * A type-agnostic value.
  159. * It is necessary to create a type-agnostic parent class to Value so that
  160. * it is possible to handle collections of them. GenValue also provides the
  161. * rest of the type-independent interface to Value.
  162. */
  163. class GenValue;
  164. typedef std::map<std::string, GenValue*> ValueSet;
  165. class GenValue{
  166. private:
  167. /**
  168. * The name of the value.
  169. * This is used to allow for dynamic lookup of
  170. * values based on their name via GenValue::get_value.
  171. */
  172. std::string name;
  173. protected:
  174. /**
  175. * Mark the internal value as invalid. This is needed for DerivedValue
  176. * to force a recalculation of the internal value when a new
  177. * observation is loaded into memory. It is called automatically for
  178. * all GenValue objects when reset is called.
  179. */
  180. virtual void _reset() = 0;
  181. /**
  182. * A static mapping containing all created Value objects.
  183. * Every value object must have a unique name, and this name is used as
  184. * a key in values to that object. This is used to enable more dynamic
  185. * creation of objects as well as avoiding the uneccesary passing of
  186. * pointers.
  187. */
  188. inline static std::map<const std::string, GenValue*> values;
  189. inline static std::map<const std::string, GenValue*> aliases;
  190. public:
  191. GenValue(const std::string& name)
  192. :name(name){
  193. values[name] = this;
  194. }
  195. const std::string& get_name(){
  196. return name;
  197. }
  198. static void reset(){
  199. for (auto val : values){
  200. val.second->_reset();
  201. }
  202. }
  203. static GenValue* get_value(const std::string& name){
  204. if (aliases[name] != nullptr)
  205. return aliases[name];
  206. else if (values[name] != nullptr)
  207. return values[name];
  208. else{
  209. ERROR("Could not find alias or value \"" << name << "\". I'll tell you the ones I know about." << std::endl
  210. << summary());
  211. CRITICAL("Aborting... :(", -1);
  212. }
  213. }
  214. static void alias(const std::string& name, GenValue* value){
  215. if (aliases[name] != nullptr){
  216. WARNING("WARNING: alias \"" << name << "\" overrides previous entry.");
  217. }
  218. aliases[name] = value;
  219. }
  220. static GenValue* alias(const std::string& name){
  221. if (values[name] != nullptr){
  222. WARNING("Alias \"" << name << "\" does not exist.");
  223. }
  224. return aliases[name];
  225. }
  226. static std::string summary(){
  227. std::stringstream ss;
  228. ss << "The following values have been created: " << std::endl;
  229. for (auto value : values){
  230. if (value.second == nullptr) continue;
  231. ss << "\t\"" << value.first << "\" at address " << value.second << std::endl;
  232. }
  233. ss << "And these aliases:" << std::endl;
  234. for (auto alias : aliases){
  235. std::string orig("VOID");
  236. if (alias.second == nullptr) continue;
  237. for (auto value : values){
  238. if (alias.second == value.second){
  239. orig = value.second->get_name();
  240. break;
  241. }
  242. }
  243. ss << "\t\"" << alias.first << "\" referring to \"" << orig << "\"" << std::endl;
  244. }
  245. return ss.str();
  246. }
  247. friend std::ostream& operator<<(std::ostream& os, const GenValue& gv);
  248. };
  249. std::ostream& operator<<(std::ostream& os, GenValue& gv){
  250. os << gv.get_name();
  251. return os;
  252. }
  253. /**
  254. * A generic value.
  255. * In order to facilitate run-time creation of analysis routines, it is
  256. * necessary to have some ability to get and store *values*. Values can either
  257. * be directly taken from some original data source (i.e. ObservedValue), or
  258. * they can be a function of some other set of values (i.e. DerivedValue). They
  259. * template class T of Value<T> is the type of thing that is returned upon
  260. * calling get_value().
  261. */
  262. template <typename T>
  263. class Value : public GenValue{
  264. public:
  265. Value(const std::string& name)
  266. :GenValue(name){ }
  267. /** Calculate, if necessary, and return the value held by this object.
  268. */
  269. virtual T& get_value() = 0;
  270. };
  271. /**
  272. * A generic, observed, value.
  273. * An ObservedValue is the interface to your dataset. Upon creation, an
  274. * ObservedValue is given a pointer to an object of type T. When an observation
  275. * is loaded into memory, the value at the location referenced by that pointer
  276. * must be updated with the associated data from that observation. This is the
  277. * responsibility of whatever DataSet implementation is being used. This object
  278. * then will read that data and return it when requested.
  279. */
  280. template <typename T>
  281. class ObservedValue : public Value<T>{
  282. private:
  283. T *val_ref;
  284. void _reset(){ }
  285. public:
  286. ObservedValue(const std::string& name, T* val_ref)
  287. :Value<T>(name),
  288. val_ref(val_ref){ }
  289. T& get_value(){
  290. return *val_ref;
  291. }
  292. };
  293. /**
  294. * A generic, derived, value.
  295. * A DerivedValue is generally defined as some function of other Value objects.
  296. * For example, a Pair is a function of two other Value objects that makes a
  297. * pair of them. Note that these other Value objects are free to be either
  298. * ObservedValues or other DerivedValues.
  299. *
  300. * It is desireable from a performance standpoint that each DerivedValue be
  301. * calculated no more than once per observation. Therefore, when a get_value is
  302. * called on a DerivedValue, it first checks whether the value that it holds is
  303. * **valid**, meaning it has already been calculated for this observation. If
  304. * so, it simply returns the value. If not, the update_value function is called
  305. * to calculate the value. and then the newly calculated value is marked as
  306. * valid and returned.
  307. */
  308. template <typename T>
  309. class DerivedValue : public Value<T>{
  310. private:
  311. void _reset(){
  312. value_valid = false;
  313. }
  314. protected:
  315. T value;
  316. bool value_valid;
  317. /**
  318. * Updates the internal value.
  319. * This function should be overridden by any child class to do the
  320. * actual work of updating value based on whatever rules the class
  321. * chooses. Normally, this consists of geting the values from some
  322. * associated Value objects, doing some calculation on them, and
  323. * storing the result in value.
  324. */
  325. virtual void update_value() = 0;
  326. public:
  327. DerivedValue(const std::string& name)
  328. :Value<T>(name),
  329. value_valid(false) { }
  330. T& get_value(){
  331. if (!value_valid){
  332. update_value();
  333. value_valid = true;
  334. }
  335. return value;
  336. }
  337. };
  338. /**
  339. * A std::vector wrapper around a C-style array.
  340. * In order to make some of the higher-level Value types easier to work with,
  341. * it is a good idea to wrap all arrays in the original data source with
  342. * std::vector objects. To do this, it is necessary to supply both a Value
  343. * object containing the array itself as well as another Value object
  344. * containing the size of that array. Currently, update_value will simply copy
  345. * the contents of the array into the interally held vector.
  346. * \todo avoid an unneccessary copy and set the vectors data directly.
  347. */
  348. template <typename T>
  349. class WrapperVector : public DerivedValue<std::vector<T> >{
  350. private:
  351. Value<int>* size;
  352. Value<T*>* data;
  353. void update_value(){
  354. int n = size->get_value();
  355. T* data_ref = data->get_value();
  356. this->value.resize(n);
  357. for (int i=0; i<n; i++){
  358. this->value[i] = *(data_ref+i);
  359. }
  360. }
  361. public:
  362. WrapperVector(Value<int>* size, Value<T*>* data)
  363. :DerivedValue<std::vector<T> >("vectorOf("+size->get_name()+","+data->get_name()+")"),
  364. size(size), data(data){ }
  365. WrapperVector(const std::string &label_size, const std::string &label_data)
  366. :WrapperVector(dynamic_cast<Value<int>*>(GenValue::get_value(label_size)),
  367. dynamic_cast<Value<T*>*>(GenValue::get_value(label_data))) { }
  368. };
  369. /**
  370. * Creates a std::pair type from a two other Value objects.
  371. */
  372. template <typename T1, typename T2>
  373. class Pair : public DerivedValue<std::pair<T1, T2> >{
  374. protected:
  375. std::pair<Value<T1>*, Value<T2>* > value_pair;
  376. void update_value(){
  377. this->value.first = value_pair.first->get_value();
  378. this->value.second = value_pair.second->get_value();
  379. }
  380. public:
  381. Pair(Value<T1> *value1, Value<T2> *value2)
  382. :DerivedValue<std::pair<T1, T2> >("pair("+value1->get_name()+","+value2->get_name()+")"),
  383. value_pair(value1, value2){ }
  384. Pair(const std::string& label1, const std::string& label2)
  385. :Pair(dynamic_cast<Value<T1>*>(GenValue::values.at(label1)),
  386. dynamic_cast<Value<T1>*>(GenValue::values.at(label2))){ }
  387. };
  388. /**
  389. * Takes a set of four Value<std::vector<T> > objects and a function of four Ts
  390. * and returns a std::vector<R>. This is used in, for instance, calculating the
  391. * energy of a set of particles when one has separate arrays containing pt,
  392. * eta, phi, and mass. These arrays are first wrapped up in VectorWrappers and
  393. * then passes along with a function to calculate the energy into a ZipMapFour.
  394. * The result of this calculation is a new vector containing the energy for
  395. * each particle. Note that if the input vectors are not all the same size,
  396. * calculations are only performed up to the size of the shortest.
  397. * \see MiniTreeDataSet
  398. * \todo find way to implement for arbitrary number(and possibly type) of
  399. * vector inputs.
  400. */
  401. template <typename R, typename T>
  402. class ZipMapFour : public DerivedValue<std::vector<R> >{
  403. private:
  404. Function<R(T, T, T, T)>& f;
  405. Value<std::vector<T> >* v1;
  406. Value<std::vector<T> >* v2;
  407. Value<std::vector<T> >* v3;
  408. Value<std::vector<T> >* v4;
  409. void update_value(){
  410. std::vector<T> v1_val = v1->get_value();
  411. std::vector<T> v2_val = v2->get_value();
  412. std::vector<T> v3_val = v3->get_value();
  413. std::vector<T> v4_val = v4->get_value();
  414. int n;
  415. std::tie(n, std::ignore) = std::minmax({v1_val.size(), v2_val.size(), v3_val.size(), v4_val.size()});
  416. this->value.resize(n);
  417. for (int i=0; i<n; i++){
  418. this->value[i] = f(v1_val[i], v2_val[i], v3_val[i], v4_val[i]);
  419. }
  420. }
  421. public:
  422. ZipMapFour(Function<R(T, T, T, T)>& f,
  423. Value<std::vector<T> >* v1, Value<std::vector<T> >* v2,
  424. Value<std::vector<T> >* v3, Value<std::vector<T> >* v4)
  425. :DerivedValue<std::vector<R> >("zipmap("+f.get_name()+":"+v1->get_name()+","+v2->get_name()+","+v3->get_name()+","+v4->get_name()+")"),
  426. f(f), v1(v1), v2(v2), v3(v3), v4(v4) { }
  427. ZipMapFour(Function<R(T, T, T, T)>* f,
  428. const std::string &label1, const std::string &label2,
  429. const std::string &label3, const std::string &label4)
  430. :ZipMapFour(f,
  431. dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label1)),
  432. dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label2)),
  433. dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label3)),
  434. dynamic_cast<Value<std::vector<T> >*>(GenValue::values.at(label4))){ }
  435. };
  436. /**
  437. * Reduce a Value of type vector<T> to just a T.
  438. * This is useful functionality to model, for instance, calculating the maximum
  439. * element of a vector, or a the mean. See child classes for specific
  440. * implementations.
  441. */
  442. template <typename T>
  443. class Reduce : public DerivedValue<T>{
  444. private:
  445. Function<T(std::vector<T>)>& reduce;
  446. Value<std::vector<T> >* v;
  447. void update_value(){
  448. this->value = reduce(v->get_value());
  449. }
  450. public:
  451. Reduce(Function<T(std::vector<T>)>& reduce, Value<std::vector<T> >* v)
  452. :DerivedValue<T>("reduceWith("+reduce.get_name()+":"+v->get_name()+")"),
  453. reduce(reduce), v(v) { }
  454. Reduce(Function<T(std::vector<T>)>& reduce, const std::string& v_name)
  455. :Reduce(reduce, dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(v_name))) { }
  456. };
  457. /**
  458. * Find and return the maximum value of a vector.
  459. */
  460. template <typename T>
  461. class Max : public Reduce<T>{
  462. public:
  463. Max(const std::string& v_name)
  464. :Reduce<T>(GenFunction::register_function<T(std::vector<T>)>("max",
  465. FUNC(([](std::vector<T> vec){
  466. return *std::max_element(vec.begin(), vec.end());}))),
  467. v_name) { }
  468. };
  469. /**
  470. * Find and return the minimum value of a vector.
  471. */
  472. template <typename T>
  473. class Min : public Reduce<T>{
  474. public:
  475. Min(const std::string& v_name)
  476. :Reduce<T>(new Function<T(std::vector<T>)>("min", [](std::vector<T> vec){
  477. return *std::min_element(vec.begin(), vec.end());}),
  478. v_name) { }
  479. };
  480. /**
  481. * Calculate the mean value of a vector.
  482. */
  483. template <typename T>
  484. class Mean : public Reduce<T>{
  485. public:
  486. Mean(const std::string& v_name)
  487. :Reduce<T>(new Function<T(std::vector<T>)>("mean", [](std::vector<T> vec){
  488. int n = 0; T sum = 0;
  489. for (T e : vec){ n++; sum += e; }
  490. return n>0 ? sum / n : 0; }),
  491. v_name) { }
  492. };
  493. /**
  494. * Extract the element at a specific index from a vector.
  495. */
  496. template <typename T>
  497. class ElementOf : public Reduce<T>{
  498. public:
  499. ElementOf(Value<int>* index, const std::string& v_name)
  500. :Reduce<T>(new Function<T(std::vector<T>)>("elementOf", [index](std::vector<T> vec){return vec[index->get_value()];}),
  501. v_name) { }
  502. ElementOf(const std::string& name, int index, const std::string& v_name)
  503. :Reduce<T>(name, [index](std::vector<T> vec){return vec[index];}, v_name) { }
  504. };
  505. /**
  506. * Similar to Reduce, but returns a pair of a T and an int.
  507. * This is useful if you need to know where in the vector exists the element
  508. * being returned.
  509. */
  510. template <typename T>
  511. class ReduceIndex : public DerivedValue<std::pair<T, int> >{
  512. private:
  513. Function<std::pair<T,int>(std::vector<T>)>& reduce;
  514. Value<std::vector<T> >* v;
  515. void update_value(){
  516. this->value = reduce(v->get_value());
  517. }
  518. public:
  519. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>& reduce, Value<std::vector<T> >* v)
  520. :DerivedValue<T>("reduceIndexWith("+reduce.get_name()+":"+v->get_name()+")"),
  521. reduce(reduce), v(v) { }
  522. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>& reduce, const std::string& v_name)
  523. :ReduceIndex(reduce, dynamic_cast<Value<std::vector<T> >*>(GenValue::get_value(v_name))) { }
  524. };
  525. /**
  526. * Find and return the maximum value of a vector and its index.
  527. */
  528. template <typename T>
  529. class MaxIndex : public ReduceIndex<T>{
  530. public:
  531. MaxIndex(const std::string& v_name)
  532. :ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>("maxIndex",
  533. FUNC(([](std::vector<T> vec){
  534. auto elptr = std::max_element(vec.begin(), vec.end());
  535. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
  536. ))), v_name) { }
  537. };
  538. /**
  539. * Find and return the minimum value of a vector and its index.
  540. */
  541. template <typename T>
  542. class MinIndex : public ReduceIndex<T>{
  543. public:
  544. MinIndex(const std::string& v_name)
  545. :ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>("minIndex",
  546. FUNC(([](std::vector<T> vec){
  547. auto elptr = std::min_element(vec.begin(), vec.end());
  548. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
  549. ))), v_name) { }
  550. };
  551. /**
  552. * A generic value owning only a function object.
  553. * All necessary values upon which this value depends must be bound to the
  554. * function object.
  555. */
  556. template <typename T>
  557. class BoundValue : public DerivedValue<T>{
  558. protected:
  559. Function<T()>& f;
  560. void update_value(){
  561. this->value = f();
  562. }
  563. public:
  564. BoundValue(Function<T()>& f)
  565. :DerivedValue<T>(f.get_name()+"(<bound>)"),
  566. f(f) { }
  567. };
  568. /**
  569. * A Value of a pointer. The pointer is constant, however the data the pointer
  570. * points to is variable.
  571. */
  572. template <typename T>
  573. class PointerValue : public DerivedValue<T*>{
  574. protected:
  575. void update_value(){ }
  576. public:
  577. PointerValue(const std::string& name, T* ptr)
  578. :DerivedValue<T*>(name){
  579. this->value = ptr;
  580. }
  581. };
  582. /**
  583. * A Value which always returns the same value, supplied in the constructor.
  584. */
  585. template <typename T>
  586. class ConstantValue : public DerivedValue<T>{
  587. protected:
  588. T const_value;
  589. void update_value(){
  590. this->value = const_value;
  591. }
  592. public:
  593. ConstantValue(const std::string& name, T const_value)
  594. :DerivedValue<T>("const::"+name),
  595. const_value(const_value) { }
  596. };
  597. }
  598. #endif // value_hpp