value.hpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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 <algorithm>
  45. #include <functional>
  46. #include <initializer_list>
  47. #include <iomanip>
  48. #include <iostream>
  49. #include <limits>
  50. #include <map>
  51. #include <sstream>
  52. #include <tuple>
  53. #include <typeindex>
  54. #include <utility>
  55. #include <vector>
  56. #include "log.hpp"
  57. #include "config.hpp"
  58. /**
  59. * The namespace containing all filval classes and functions.
  60. */
  61. namespace fv{
  62. namespace detail {
  63. template<typename T, int N, bool Done, typename... TYPES>
  64. struct _HomoTuple {
  65. typedef _HomoTuple<T, N, sizeof...(TYPES)+1==N, TYPES..., T> stype;
  66. typedef typename stype::type type;
  67. };
  68. template<typename T, int N, typename... TYPES>
  69. struct _HomoTuple<T, N, true, TYPES...> {
  70. typedef std::tuple<TYPES...> type;
  71. };
  72. }
  73. template<typename T, int N>
  74. struct HomoTuple {
  75. typedef detail::_HomoTuple<T, N, N==0> stype;
  76. typedef typename stype::type type;
  77. };
  78. namespace detail {
  79. // Convert array into a tuple
  80. template<typename Array, std::size_t... I>
  81. decltype(auto) a2t_impl(const Array& a, std::index_sequence<I...>){
  82. return std::make_tuple(a[I]...);
  83. }
  84. }
  85. /**
  86. * Converts a std::array to a std::tuple.
  87. */
  88. template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
  89. decltype(auto) a2t(const std::array<T, N>& a){
  90. return detail::a2t_impl(a, Indices());
  91. }
  92. namespace detail {
  93. // Convert tuple into a vector
  94. template<typename R, typename Tuple, std::size_t... Is>
  95. decltype(auto) t2v_impl(const Tuple& t, std::index_sequence<Is...>){
  96. /* return std::make_tuple(a[I]...); */
  97. return std::vector<R>({std::get<Is>(t)...});
  98. }
  99. }
  100. /**
  101. * Converts a std::tuple to a std::vector.
  102. */
  103. template<typename R, typename... ArgTypes>
  104. std::vector<R> t2v(const std::tuple<ArgTypes...>& t){
  105. return detail::t2v_impl<R, std::tuple<ArgTypes...>>(t, std::index_sequence_for<ArgTypes...>{});
  106. }
  107. namespace detail {
  108. template <class F, class Tuple, std::size_t... I>
  109. constexpr decltype(auto) call_impl(F *f, Tuple &&t, std::index_sequence<I...>){
  110. return (*f)(std::get<I>(std::forward<Tuple>(t))...);
  111. }
  112. }
  113. /**
  114. * Call a function f with the elements of the tuple t as arguments
  115. */
  116. template <class F, class Tuple>
  117. constexpr decltype(auto) call(F *f, Tuple &&t){
  118. return detail::call_impl(
  119. f, std::forward<Tuple>(t),
  120. std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
  121. }
  122. template<typename> class Function; // undefined
  123. /**
  124. * Parent class to all Function classes. Holds a class-level collection of all
  125. * created function objects.
  126. */
  127. class GenFunction {
  128. private:
  129. std::string name;
  130. std::string impl;
  131. protected:
  132. inline static bool in_reg_func=false;
  133. public:
  134. /**
  135. * Static mapping of functions from their name to the object wrapper of
  136. * the function.
  137. */
  138. inline static std::map<const std::string, GenFunction*> function_registry;
  139. GenFunction(const std::string& name, const std::string& impl)
  140. :name(name),
  141. impl(impl){ }
  142. virtual ~GenFunction() { };
  143. std::string& get_name(){
  144. return name;
  145. }
  146. std::string& get_impl(){
  147. return impl;
  148. }
  149. /**
  150. * Attempt to invoke clang-format for the purpose of printing out
  151. * nicely formatted functions to the log file. If clang-format is not
  152. * present, this function just passes through the code unmodified.
  153. */
  154. static std::string format_code(const std::string& code){
  155. std::stringstream code_out("");
  156. std::string command("echo \""+code+"\" | clang-format");
  157. char buffer[255];
  158. FILE *stream = popen(command.c_str(), "r");
  159. while (fgets(buffer, 255, stream) != NULL)
  160. code_out << buffer;
  161. if (pclose(stream) == 0)
  162. return code_out.str();
  163. else
  164. return code;
  165. }
  166. static std::string summary(){
  167. std::stringstream ss;
  168. ss << "The following functions have been registered" << std::endl;
  169. for(auto p : function_registry){
  170. if (p.second == nullptr) continue;
  171. ss << "FUNCTION::" << p.second->name << "@" << p.second << std::endl;
  172. ss << format_code(p.second->impl);
  173. }
  174. return ss.str();
  175. }
  176. template <typename T>
  177. static Function<T>* reg_func(const std::string& name, std::function<T> f, const std::string& impl){
  178. in_reg_func = true;
  179. Function<T>* func;
  180. if (GenFunction::function_registry[name] != nullptr){
  181. func = dynamic_cast<Function<T>*>(GenFunction::function_registry[name]);
  182. if (func == nullptr){
  183. ERROR("Trying to register function which has already been registered with a different type");
  184. }
  185. } else {
  186. func = new Function<T>(name, impl, f);
  187. GenFunction::function_registry[name] = func;
  188. }
  189. in_reg_func = false;
  190. return func;
  191. }
  192. template <typename T>
  193. static Function<T>* lookup_function(const std::string& name){
  194. if (GenFunction::function_registry[name] == nullptr){
  195. CRITICAL("Function \"" << name << "\" not previously registered", -1);
  196. } else {
  197. Function<T>* func = dynamic_cast<Function<T>*>(GenFunction::function_registry[name]);
  198. if (func == nullptr){
  199. CRITICAL("Function \"" << name << "\" request and register have mismatched types", -1);
  200. }
  201. return func;
  202. }
  203. }
  204. };
  205. /**
  206. * In order to enable proper provenance tracking, and at the same time keep
  207. * the ability to embed functions into values, the Function class should be
  208. * used. It is simply a wrapper around a std::function that also has a name.
  209. * This name is used when generating the name of values that use the function.
  210. * A function name is automatically prepended with "func::" to explicitly state
  211. * that the value is the result of a computation encoded within the function
  212. * object, and not from some other Value object. Unfortunately, it is up to the
  213. * user to find where that function is defined in the source code to inspect
  214. * what it is doing. But hopefully this isn't too onerous by just using grep.
  215. */
  216. template <typename R, typename... ArgTypes>
  217. class Function<R(ArgTypes...)> : public GenFunction {
  218. private:
  219. std::function<R(ArgTypes...)> f;
  220. public:
  221. Function(const std::string& name, const std::string& impl, std::function<R(ArgTypes...)> f)
  222. :GenFunction(name, impl), f(f){
  223. if (!in_reg_func) {
  224. WARNING("Don't instantiate Function objects directly! Use GenFunction::reg_func instead.");
  225. }
  226. }
  227. Function(const std::string& name, std::function<R(ArgTypes...)> f)
  228. :Function(name, "N/A", f){ }
  229. ~Function() { }
  230. R operator()(ArgTypes ...args){
  231. DEBUG("Calculating Function: " << this->get_name());
  232. return f(args...);
  233. }
  234. };
  235. #define FUNC(f) f, #f
  236. template <typename T>
  237. class Value;
  238. /**
  239. * A type-agnostic value.
  240. * It is necessary to create a type-agnostic parent class to Value so that
  241. * it is possible to handle collections of them. GenValue also provides the
  242. * rest of the type-independent interface to Value.
  243. */
  244. class GenValue;
  245. typedef std::map<std::string, GenValue*> ValueSet;
  246. class GenValue {
  247. private:
  248. /**
  249. * The name of the value.
  250. * This is used to allow for dynamic lookup of
  251. * values based on their name via GenValue::get_value.
  252. */
  253. std::string name;
  254. protected:
  255. /**
  256. * Mark the internal value as invalid. This is needed for DerivedValue
  257. * to force a recalculation of the internal value when a new
  258. * observation is loaded into memory. It is called automatically for
  259. * all GenValue objects when reset is called.
  260. */
  261. bool value_valid;
  262. void _reset(){
  263. this->value_valid = false;
  264. }
  265. /**
  266. * A static mapping containing all created Value objects.
  267. * Every value object must have a unique name, and this name is used as
  268. * a key in values to that object. This is used to enable more dynamic
  269. * creation of objects as well as avoiding the uneccesary passing of
  270. * pointers.
  271. */
  272. inline static std::map<std::pair<const std::type_index, const std::string>, GenValue*> values;
  273. /**
  274. * Composite value names are typically nested. This makes complex
  275. * values have rather unwieldy names. Therefore, one can declare
  276. * aliases which allow for more human-usable names to be used. When a
  277. * value is requested by name, an alias with that value takes precidence
  278. * over a name with that value.
  279. */
  280. inline static std::map<std::pair<const std::type_index, const std::string>, GenValue*> aliases;
  281. public:
  282. GenValue(const std::type_index&& ti, const std::string& name, const std::string& alias)
  283. :name(name), value_valid(false) {
  284. if (alias != "")
  285. INFO("Registered value: \"" << name << "\" with alias: \"" << alias << "\"");
  286. else
  287. INFO("Registered value: \"" << name);
  288. values[std::make_pair(ti,name)] = this;
  289. if (alias != "")
  290. GenValue::alias(ti, alias, this);
  291. }
  292. const std::string& get_name(){
  293. return name;
  294. }
  295. static void reset(){
  296. for (auto val : values){
  297. if (val.second != nullptr){
  298. val.second->_reset();
  299. }
  300. }
  301. }
  302. template<typename T>
  303. static Value<T>* get_value(const std::string& name){
  304. const std::type_index& ti = typeid(T);
  305. auto lookup_id = std::make_pair(ti,name);
  306. if (aliases[lookup_id] != nullptr)
  307. return (Value<T>*)aliases[lookup_id];
  308. else
  309. return (Value<T>*)values[lookup_id];
  310. }
  311. static void alias(const std::type_index& ti, const std::string& name, GenValue* value){
  312. auto lookup_id = std::make_pair(ti,name);
  313. if (aliases[lookup_id] != nullptr){
  314. WARNING("WARNING: alias \"" << name << "\" overrides previous entry.");
  315. }
  316. aliases[lookup_id] = value;
  317. }
  318. template<typename T>
  319. static void alias(const std::string& name, Value<T>* value){
  320. alias(typeid(T), name, value);
  321. }
  322. static std::string summary(){
  323. std::stringstream ss;
  324. ss << "The following values have been created:" << std::endl;
  325. for (auto item : values){
  326. auto& key = item.first;
  327. auto& value = item.second;
  328. if (value == nullptr) continue;
  329. ss << "\tVALUE::\"" << key.second << "\" at address " << value << std::endl;
  330. }
  331. ss << "And these aliases:" << std::endl;
  332. for (auto item : aliases){
  333. auto& key = item.first;
  334. auto& value = item.second;
  335. std::string orig("VOID");
  336. if (value == nullptr) continue;
  337. for (auto v_item : values){
  338. auto& v_value = v_item.second;
  339. if (v_value == value){
  340. orig = v_value->get_name();
  341. break;
  342. }
  343. }
  344. ss << "\tALIAS::\"" << key.second << "\" referring to \"" << orig << "\"" << std::endl;
  345. }
  346. return ss.str();
  347. }
  348. friend std::ostream& operator<<(std::ostream& os, const GenValue& gv);
  349. };
  350. std::ostream& operator<<(std::ostream& os, GenValue& gv){
  351. os << gv.get_name();
  352. return os;
  353. }
  354. /**
  355. * A templated value.
  356. * In order to facilitate run-time creation of analysis routines, it is
  357. * necessary to have some ability to get and store *values*. Values can either
  358. * be directly taken from some original data source (i.e. ObservedValue), or
  359. * they can be a function of some other set of values (i.e. DerivedValue). They
  360. * template class T of Value<T> is the type of thing that is returned upon
  361. * calling get_value().
  362. */
  363. template <typename T>
  364. class Value : public GenValue {
  365. public:
  366. Value(const std::string& name, const std::string& alias="")
  367. :GenValue(typeid(T), name, alias){ }
  368. /** Calculate, if necessary, and return the value held by this object.
  369. */
  370. virtual T& get_value() = 0;
  371. };
  372. /**
  373. * A value supplied by the dataset, not derived.
  374. * An ObservedValue is the interface to your dataset. Upon creation, an
  375. * ObservedValue is given a pointer to an object of type T. When an observation
  376. * is loaded into memory, the value at the location referenced by that pointer
  377. * must be updated with the associated data from that observation. This is the
  378. * responsibility of whatever DataSet implementation is being used. This object
  379. * then will read that data and return it when requested.
  380. */
  381. template <typename T>
  382. class ObservedValue : public Value<T>{
  383. private:
  384. T *val_ref;
  385. public:
  386. ObservedValue(const std::string& name, T* val_ref, const std::string& alias="")
  387. :Value<T>(name, alias),
  388. val_ref(val_ref){ }
  389. static std::string fmt_name(const std::string& name){
  390. return name;
  391. }
  392. T& get_value(){
  393. if (!this->value_valid){
  394. if (util::debug_on) {
  395. std::cout << "Calculating Value: " << this->get_name() << std::endl;
  396. }
  397. this->value_valid = true;
  398. }
  399. return *val_ref;
  400. }
  401. };
  402. /**
  403. * A Value derived from some other Values, not directly from the dataset.
  404. * A DerivedValue is generally defined as some function of other Value objects.
  405. * For example, a Pair is a function of two other Value objects that makes a
  406. * pair of them. Note that these other Value objects are free to be either
  407. * ObservedValues or other DerivedValues.
  408. *
  409. * It is desireable from a performance standpoint that each DerivedValue be
  410. * calculated no more than once per observation. Therefore, when a get_value is
  411. * called on a DerivedValue, it first checks whether the value that it holds is
  412. * **valid**, meaning it has already been calculated for this observation. If
  413. * so, it simply returns the value. If not, the update_value function is called
  414. * to calculate the value. and then the newly calculated value is marked as
  415. * valid and returned.
  416. */
  417. template <typename T>
  418. class DerivedValue : public Value<T>{
  419. protected:
  420. T value;
  421. /**
  422. * Updates the internal value.
  423. * This function should be overridden by any child class to do the
  424. * actual work of updating value based on whatever rules the class
  425. * chooses. Normally, this consists of geting the values from some
  426. * associated Value objects, doing some calculation on them, and
  427. * storing the result in value.
  428. */
  429. virtual void update_value() = 0;
  430. public:
  431. DerivedValue(const std::string& name, const std::string& alias="")
  432. :Value<T>(name, alias){ }
  433. T& get_value(){
  434. if (!this->value_valid){
  435. if (util::debug_on) {
  436. std::cout << "Calculating Value: " << this->get_name() << std::endl;
  437. }
  438. update_value();
  439. this->value_valid = true;
  440. }
  441. return value;
  442. }
  443. };
  444. /**
  445. * A std::vector wrapper around a C-style array.
  446. * In order to make some of the higher-level Value types easier to work with,
  447. * it is a good idea to wrap all arrays in the original data source with
  448. * std::vector objects. To do this, it is necessary to supply both a Value
  449. * object containing the array itself as well as another Value object
  450. * containing the size of that array. Currently, update_value will simply copy
  451. * the contents of the array into the interally held vector.
  452. */
  453. template <typename T>
  454. class WrapperVector : public DerivedValue<std::vector<T> >{
  455. private:
  456. Value<int>* size;
  457. Value<T*>* data;
  458. void update_value(){
  459. int n = size->get_value();
  460. T* data_ref = data->get_value();
  461. this->value.assign(data_ref, data_ref+n);
  462. }
  463. public:
  464. static std::string fmt_name(Value<int>* size, Value<T*>* data){
  465. return "wrapper_vector("+size->get_name()+","+data->get_name()+")";
  466. }
  467. WrapperVector(Value<int>* size, Value<T*>* data, const std::string& alias="")
  468. :DerivedValue<std::vector<T> >(fmt_name(size,data), alias),
  469. size(size), data(data){ }
  470. };
  471. /**
  472. * Creates a std::pair type from a two other Value objects.
  473. */
  474. template <typename T1, typename T2>
  475. class Pair : public DerivedValue<std::pair<T1, T2> >{
  476. protected:
  477. std::pair<Value<T1>*, Value<T2>* > value_pair;
  478. void update_value(){
  479. this->value.first = value_pair.first->get_value();
  480. this->value.second = value_pair.second->get_value();
  481. }
  482. public:
  483. static std::string fmt_name(Value<T1> *value1, Value<T2> *value2){
  484. return "pair("+value1->get_name()+","+value2->get_name()+")";
  485. }
  486. Pair(Value<T1> *value1, Value<T2> *value2, const std::string alias="")
  487. :DerivedValue<std::pair<T1, T2> >(fmt_name(value1, value2), alias),
  488. value_pair(value1, value2){ }
  489. };
  490. template<typename... T> class _Zip;
  491. template<>
  492. class _Zip<> {
  493. protected:
  494. int _get_size(){
  495. return std::numeric_limits<int>::max();
  496. }
  497. std::tuple<> _get_at(int){
  498. return std::make_tuple();
  499. }
  500. std::string _get_name(){
  501. return "";
  502. }
  503. public:
  504. _Zip() { }
  505. };
  506. template<typename Head, typename... Tail>
  507. class _Zip<Head, Tail...> : private _Zip<Tail...> {
  508. protected:
  509. Value<std::vector<Head>>* head;
  510. int _get_size(){
  511. int this_size = head->get_value().size();
  512. int rest_size = _Zip<Tail...>::_get_size();
  513. return std::min(this_size, rest_size);
  514. }
  515. typename std::tuple<Head,Tail...> _get_at(int idx){
  516. auto tail_tuple = _Zip<Tail...>::_get_at(idx);
  517. return std::tuple_cat(std::make_tuple(head->get_value()[idx]),tail_tuple);
  518. }
  519. std::string _get_name(){
  520. return head->get_name()+","+_Zip<Tail...>::_get_name();
  521. }
  522. public:
  523. _Zip() { }
  524. _Zip(Value<std::vector<Head>>* head, Value<std::vector<Tail>>*... tail)
  525. : _Zip<Tail...>(tail...),
  526. head(head) { }
  527. };
  528. namespace impl {
  529. std::string zip_fmt_name(){
  530. return "";
  531. }
  532. template<typename Head>
  533. std::string zip_fmt_name(Value<std::vector<Head>>* head){
  534. return head->get_name();
  535. }
  536. template<typename Head1, typename Head2, typename... Tail>
  537. std::string zip_fmt_name(Value<std::vector<Head1>>* head1, Value<std::vector<Head2>>* head2, Value<std::vector<Tail>>*... tail){
  538. return head1->get_name() + "," + zip_fmt_name<Head2, Tail...>(head2, tail...);
  539. }
  540. }
  541. /**
  542. * Zips a series of vectors together. Can be combined with Map to
  543. * yield a Value whose elements are individually a function of the
  544. * corresponding elements of the vectors that were zipped together. For those
  545. * familiar with python, it accompilishes the same thing as
  546. * @code{.py}
  547. * xs = [1,2,3,4]
  548. * ys = [10,20,30,40]
  549. * print(list(map(lambda t:t[0]+t[1],zip(xs,ys))))
  550. * @endcode
  551. * which outputs
  552. * @code
  553. * [11, 22, 33, 44]
  554. * @endcode
  555. */
  556. template <typename... ArgTypes>
  557. class Zip : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>,
  558. private _Zip<ArgTypes...>{
  559. protected:
  560. void update_value(){
  561. this->value.clear();
  562. int size = _Zip<ArgTypes...>::_get_size();
  563. for(int i=0; i<size; i++){
  564. this->value.push_back(_Zip<ArgTypes...>::_get_at(i));
  565. }
  566. }
  567. public:
  568. static std::string fmt_name(Value<std::vector<ArgTypes>>*... args){
  569. return "zip("+zip_fmt_name(args...)+")";
  570. }
  571. Zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias)
  572. :DerivedValue<std::vector<std::tuple<ArgTypes...>>>(fmt_name(args...), alias),
  573. _Zip<ArgTypes...>(args...) { }
  574. };
  575. template<typename> class Map; // undefined
  576. /**
  577. * Maps a function over an input vector. The input vector must be a vector of
  578. * tuples, where the the elements of the tuple match the arguments of the
  579. * function. For example if the function takes two floats as arguments, the
  580. * tuple should contain two floats. The Value object required by Map will
  581. * typically be created as a Zip.
  582. */
  583. template <typename Ret, typename ArgType>
  584. class Map<Ret(ArgType)> : public DerivedValue<std::vector<Ret>>{
  585. private:
  586. typedef Value<std::vector<ArgType>> arg_type;
  587. Function<Ret(ArgType)>* fn;
  588. arg_type* arg;
  589. void update_value(){
  590. this->value.clear();
  591. for(auto v : arg->get_value()){
  592. this->value.push_back(fn(v));
  593. }
  594. }
  595. public:
  596. static std::string fmt_name(Function<Ret(ArgType)>* fn, arg_type* arg){
  597. return "map("+fn->get_name()+":"+arg->get_name()+")";
  598. }
  599. Map(Function<Ret(ArgType)>* fn, arg_type* arg, const std::string& alias)
  600. :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
  601. fn(fn), arg(arg){ }
  602. };
  603. template<typename> class TupMap; // undefined
  604. /**
  605. * Maps a function over an input vector. The input vector must be a vector of
  606. * tuples, where the the elements of the tuple match the arguments of the
  607. * function. For example if the function takes two floats as arguments, the
  608. * tuple should contain two floats. The Value object required by Map will
  609. * typically be created as a Zip.
  610. */
  611. template <typename Ret, typename... ArgTypes>
  612. class TupMap<Ret(ArgTypes...)> : public DerivedValue<std::vector<Ret>>{
  613. private:
  614. typedef Value<std::vector<std::tuple<ArgTypes...>>> arg_type;
  615. Function<Ret(ArgTypes...)>* fn;
  616. arg_type* arg;
  617. void update_value(){
  618. this->value.clear();
  619. for(auto tup : arg->get_value()){
  620. this->value.push_back(call(fn,tup));
  621. }
  622. }
  623. public:
  624. static std::string fmt_name(Function<Ret(ArgTypes...)>* fn, arg_type* arg){
  625. return "tup_map("+fn->get_name()+":"+arg->get_name()+")";
  626. }
  627. TupMap(Function<Ret(ArgTypes...)>* fn, arg_type* arg, const std::string& alias)
  628. :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
  629. fn(fn), arg(arg){ }
  630. };
  631. template<typename... T> class _Tuple;
  632. template<>
  633. class _Tuple<> {
  634. protected:
  635. std::tuple<> _get_value(){
  636. return std::make_tuple();
  637. }
  638. public:
  639. _Tuple() { }
  640. };
  641. template<typename Head, typename... Tail>
  642. class _Tuple<Head, Tail...> : private _Tuple<Tail...> {
  643. protected:
  644. Value<Head>* head;
  645. typename std::tuple<Head,Tail...> _get_value(){
  646. auto tail_tuple = _Tuple<Tail...>::_get_value();
  647. return std::tuple_cat(std::make_tuple(head->get_value()),tail_tuple);
  648. }
  649. public:
  650. _Tuple() { }
  651. _Tuple(Value<Head>* head, Value<Tail>*... tail)
  652. : _Tuple<Tail...>(tail...),
  653. head(head) { }
  654. };
  655. namespace impl {
  656. std::string tuple_fmt_name(){
  657. return "";
  658. }
  659. template<typename Head>
  660. std::string tuple_fmt_name(Value<Head>* head){
  661. return head->get_name();
  662. }
  663. template<typename Head1, typename Head2, typename... Tail>
  664. std::string tuple_fmt_name(Value<Head1>* head1, Value<Head2>* head2, Value<Tail>*... tail){
  665. return head1->get_name() + "," + tuple_fmt_name<Head2, Tail...>(head2, tail...);
  666. }
  667. }
  668. /**
  669. * Takes a series of Value objects and bundles them together into a std::tuple
  670. * object. Typically, this is most usefull when one wants to apply a function
  671. * to a few values and store the result. This class can be used in conjunction
  672. * with Apply to achieve this.
  673. */
  674. template <typename... ArgTypes>
  675. class Tuple : public DerivedValue<std::tuple<ArgTypes...>>,
  676. private _Tuple<ArgTypes...>{
  677. protected:
  678. void update_value(){
  679. this->value = _Tuple<ArgTypes...>::_get_value();
  680. }
  681. public:
  682. static std::string fmt_name(Value<ArgTypes>*... args){
  683. return "tuple("+impl::tuple_fmt_name(args...)+")";
  684. }
  685. Tuple(Value<ArgTypes>*... args, const std::string& alias)
  686. :DerivedValue<std::tuple<ArgTypes...>>(fmt_name(args...), alias),
  687. _Tuple<ArgTypes...>(args...) { }
  688. };
  689. /**
  690. * Gets the Nth element from a tuple value
  691. */
  692. template <size_t N, typename... ArgTypes>
  693. class DeTup : public DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>{
  694. Value<std::tuple<ArgTypes...>> tup;
  695. protected:
  696. void update_value(){
  697. this->value = std::get<N>(tup->get_value());
  698. }
  699. public:
  700. static std::string fmt_name(Value<std::tuple<ArgTypes...>>* tup){
  701. return "detup("+tup->get_name()+")";
  702. }
  703. DeTup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias)
  704. :DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>(fmt_name(tup), alias),
  705. tup(tup) { }
  706. };
  707. /**
  708. * Creates a vector of extracting the Nth value from each entry in a vector of
  709. * tuples.
  710. */
  711. template <size_t N, typename... ArgTypes>
  712. class DeTupVector : public DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>{
  713. Value<std::vector<std::tuple<ArgTypes...>>>* tup;
  714. protected:
  715. void update_value(){
  716. this->value.clear();
  717. for( auto& t : tup->get_value()){
  718. this->value.push_back(std::get<N>(t));
  719. }
  720. }
  721. public:
  722. static std::string fmt_name(Value<std::vector<std::tuple<ArgTypes...>>>* tup){
  723. return "detup_vec("+tup->get_name()+")";
  724. }
  725. DeTupVector(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias)
  726. :DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>(fmt_name(tup), alias),
  727. tup(tup) { }
  728. };
  729. template<typename> class Apply; // undefined
  730. /**
  731. * Applies a function to a tuple of values and returns a value. This will
  732. * typically be called with a Tuple object as an argument.
  733. */
  734. template <typename Ret, typename ArgType>
  735. class Apply<Ret(ArgType)> : public DerivedValue<Ret>{
  736. private:
  737. Function<Ret(ArgType)>* fn;
  738. Value<ArgType>* arg;
  739. void update_value(){
  740. this->value = (*fn)(arg->get_value());
  741. }
  742. public:
  743. static std::string fmt_name(Function<Ret(ArgType)>* fn, Value<ArgType>* arg){
  744. return "apply("+fn->get_name()+":"+arg->get_name()+")";
  745. }
  746. Apply(Function<Ret(ArgType)>* fn, Value<ArgType>* arg, const std::string& alias)
  747. :DerivedValue<Ret>(fmt_name(fn,arg), alias),
  748. fn(fn), arg(arg){ }
  749. };
  750. template<typename> class TupApply; // undefined
  751. /**
  752. * Applies a function to a tuple of values and returns a value. This will
  753. * typically be called with a Tuple object as an argument.
  754. */
  755. template <typename Ret, typename... ArgTypes>
  756. class TupApply<Ret(ArgTypes...)> : public DerivedValue<Ret>{
  757. private:
  758. Function<Ret(ArgTypes...)>* fn;
  759. Value<std::tuple<ArgTypes...>>* arg;
  760. void update_value(){
  761. auto &tup = arg->get_value();
  762. this->value = call(fn, tup);
  763. }
  764. public:
  765. static std::string fmt_name(Function<Ret(ArgTypes...)>* fn, Value<std::tuple<ArgTypes...>>* arg){
  766. return "tup_apply("+fn->get_name()+":"+arg->get_name()+")";
  767. }
  768. TupApply(Function<Ret(ArgTypes...)>* fn, Value<std::tuple<ArgTypes...>>* arg, const std::string& alias)
  769. :DerivedValue<Ret>(fmt_name(fn,arg), alias),
  770. fn(fn), arg(arg){ }
  771. };
  772. /**
  773. * Returns the count of elements in the input vector passing a test function.
  774. */
  775. template<typename T>
  776. class Count : public DerivedValue<int>{
  777. private:
  778. Function<bool(T)>* selector;
  779. Value<std::vector<T> >* v;
  780. void update_value(){
  781. value = 0;
  782. for(auto val : v->get_value()){
  783. if(selector(val))
  784. value++;
  785. }
  786. }
  787. public:
  788. static std::string fmt_name(Function<bool(T)>* selector, Value<std::vector<T>>* v){
  789. return "count("+selector->get_name()+":"+v->get_name()+")";
  790. }
  791. Count(Function<bool(T)>* selector, Value<std::vector<T>>* v, const std::string alias)
  792. :DerivedValue<int>(fmt_name(selector,v), alias),
  793. selector(selector), v(v) { }
  794. };
  795. /**
  796. * Returns the elements in a vector that pass a test function.
  797. */
  798. template<typename T>
  799. class Filter : public DerivedValue<std::vector<T>>{
  800. private:
  801. Function<bool(T)>* filter;
  802. Value<std::vector<T> >* v;
  803. void update_value(){
  804. this->value.clear();
  805. for(auto val : v->get_value()){
  806. if(this->filter(val))
  807. this->value.push_back(val);
  808. }
  809. }
  810. public:
  811. static std::string fmt_name(Function<bool(T)>* filter, Value<std::vector<T>>* v){
  812. return "filter("+filter->get_name()+":"+v->get_name()+")";
  813. }
  814. Filter(Function<bool(T)>* filter, Value<std::vector<T>>* v, const std::string alias)
  815. :DerivedValue<std::vector<T>>(fmt_name(filter,v), alias),
  816. filter(filter), v(v) { }
  817. };
  818. /**
  819. * Returns the elements in a vector that pass a test function. The elements on
  820. * the vector must be tuples. Typically this will be used in conjunction with
  821. * Zip and Map.
  822. */
  823. template<typename... ArgTypes>
  824. class TupFilter : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>{
  825. private:
  826. typedef std::vector<std::tuple<ArgTypes...>> value_type;
  827. Function<bool(ArgTypes...)>* filter;
  828. Value<value_type>* arg;
  829. void update_value(){
  830. this->value.clear();
  831. for(auto val : arg->get_value()){
  832. if(call(filter,val))
  833. this->value.push_back(val);
  834. }
  835. }
  836. public:
  837. static std::string fmt_name(Function<bool(ArgTypes...)>* filter, Value<value_type>* arg){
  838. return "tup_filter("+filter->get_name()+":"+arg->get_name()+")";
  839. }
  840. TupFilter(Function<bool(ArgTypes...)>* filter, Value<value_type>* arg, const std::string alias)
  841. :DerivedValue<value_type>(fmt_name(filter, arg), alias),
  842. filter(filter), arg(arg) { }
  843. };
  844. /**
  845. * Reduce a Value of type vector<T> to just a T.
  846. * This is useful functionality to model, for instance, calculating the maximum
  847. * element of a vector, or a the mean. See child classes for specific
  848. * implementations.
  849. */
  850. template <typename T>
  851. class Reduce : public DerivedValue<T>{
  852. private:
  853. Function<T(std::vector<T>)>* reduce_fn;
  854. void update_value(){
  855. this->value = reduce_fn(v->get_value());
  856. }
  857. protected:
  858. Value<std::vector<T> >* v;
  859. public:
  860. static std::string fmt_name(Function<T(std::vector<T>)>* reduce_fn, Value<std::vector<T>>* v){
  861. return "reduce("+reduce_fn->get_name()+":"+v->get_name()+")";
  862. }
  863. Reduce(Function<T(std::vector<T>)>* reduce_fn, Value<std::vector<T> >* v, const std::string alias)
  864. :DerivedValue<T>(fmt_name(reduce_fn, v), alias),
  865. reduce_fn(reduce_fn), v(v) { }
  866. };
  867. /**
  868. * Find and return the maximum value of a vector.
  869. */
  870. template <typename T>
  871. class Max : public Reduce<T>{
  872. public:
  873. static std::string fmt_name(Value<std::vector<T>>* v){
  874. return "max("+v->get_name()+")";
  875. }
  876. Max(Value<std::vector<T>>* v, const std::string alias)
  877. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("max",
  878. FUNC(([](std::vector<T> vec){
  879. return *std::max_element(vec.begin(), vec.end());}))),
  880. v, alias) { }
  881. };
  882. /**
  883. * Find and return the minimum value of a vector.
  884. */
  885. template <typename T>
  886. class Min : public Reduce<T>{
  887. public:
  888. static std::string fmt_name(Value<std::vector<T>>* v){
  889. return "min("+v->get_name()+")";
  890. }
  891. Min(Value<std::vector<T>>* v, const std::string alias)
  892. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("min",
  893. FUNC(([](std::vector<T> vec){
  894. return *std::min_element(vec.begin(), vec.end());}))),
  895. v, alias) { }
  896. };
  897. /**
  898. * Calculate the mean value of a vector.
  899. */
  900. template <typename T>
  901. class Mean : public Reduce<T>{
  902. public:
  903. static std::string fmt_name(Value<std::vector<T>>* v){
  904. return "mean("+v->get_name()+")";
  905. }
  906. Mean(Value<std::vector<T>>* v, const std::string alias)
  907. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("mean",
  908. FUNC(([](std::vector<T> vec){
  909. int n = 0; T sum = 0;
  910. for (T e : vec){ n++; sum += e; }
  911. return n>0 ? sum / n : 0; }))),
  912. v, alias) { }
  913. };
  914. /**
  915. * Calculate the range of the values in a vector
  916. */
  917. template <typename T>
  918. class Range : public Reduce<T>{
  919. public:
  920. static std::string fmt_name(Value<std::vector<T>>* v){
  921. return "range("+v->get_name()+")";
  922. }
  923. Range(Value<std::vector<T>>* v, const std::string alias)
  924. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("range",
  925. FUNC(([](std::vector<T> vec){
  926. auto minmax = std::minmax_element(vec.begin(), vec.end());
  927. return (*minmax.second) - (*minmax.first); }))),
  928. v, alias) { }
  929. };
  930. /**
  931. * Extract the element at a specific index from a vector.
  932. */
  933. template <typename T>
  934. class ElementOf : public Reduce<T>{
  935. public:
  936. ElementOf(Value<int>* index, Value<std::vector<T>>* v, const std::string alias)
  937. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("elementOf",
  938. FUNC(([index](std::vector<T> vec){return vec[index->get_value()];}))),
  939. v, alias) { }
  940. };
  941. /**
  942. * Similar to Reduce, but returns a pair of a T and an int.
  943. * This is useful if you need to know where in the vector exists the element
  944. * being returned.
  945. */
  946. template <typename T>
  947. class ReduceIndex : public DerivedValue<std::pair<T, int> >{
  948. private:
  949. Function<std::pair<T,int>(std::vector<T>)>* reduce;
  950. Value<std::vector<T> >* v;
  951. void update_value(){
  952. this->value = reduce(v->get_value());
  953. }
  954. public:
  955. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>* reduce, Value<std::vector<T> >* v, const std::string alias="")
  956. :DerivedValue<T>("reduceIndexWith("+reduce->get_name()+":"+v->get_name()+")", alias),
  957. reduce(reduce), v(v) { }
  958. };
  959. /**
  960. * Find and return the maximum value of a vector and its index.
  961. */
  962. template <typename T>
  963. class MaxIndex : public ReduceIndex<T>{
  964. public:
  965. MaxIndex(Value<std::vector<T>>* v, const std::string alias="")
  966. :ReduceIndex<T>(GenFunction::reg_func<T(std::vector<T>)>("maxIndex",
  967. FUNC(([](std::vector<T> vec){
  968. auto elptr = std::max_element(vec.begin(), vec.end());
  969. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  970. v, alias) { }
  971. };
  972. /**
  973. * Find and return the minimum value of a vector and its index.
  974. */
  975. template <typename T>
  976. class MinIndex : public ReduceIndex<T>{
  977. public:
  978. MinIndex(Value<std::vector<T>>* v, const std::string alias="")
  979. :ReduceIndex<T>(GenFunction::reg_func<T(std::vector<T>)>("minIndex",
  980. FUNC(([](std::vector<T> vec){
  981. auto elptr = std::min_element(vec.begin(), vec.end());
  982. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  983. v, alias) { }
  984. };
  985. /**
  986. * Find combinations of items from an input vector
  987. */
  988. template <typename T, int Size>
  989. class Combinations : public DerivedValue<std::vector<typename HomoTuple<T,Size>::type>>{
  990. private:
  991. Value<std::vector<T>>* val;
  992. typedef typename HomoTuple<T,Size>::type tuple_type;
  993. void update_value(){
  994. auto& v = val->get_value();
  995. int data_size = v.size();
  996. this->value.clear();
  997. std::vector<bool> selector(data_size);
  998. std::fill(selector.begin(), selector.begin()+std::min({Size,data_size}), true);
  999. do {
  1000. std::array<T, Size> perm;
  1001. int idx = 0;
  1002. for (int i=0; i<data_size; i++){
  1003. if (selector[i]){
  1004. perm[idx] = v[i];
  1005. idx++;
  1006. if (idx == Size) break;
  1007. }
  1008. }
  1009. this->value.push_back(a2t(perm)); //!!!
  1010. } while(std::prev_permutation(selector.begin(), selector.end()));
  1011. }
  1012. public:
  1013. static std::string fmt_name(Value<std::vector<T>>* val){
  1014. std::stringstream ss;
  1015. ss << "combinations(" << Size << "," << val->get_name() << ")";
  1016. return ss.str();
  1017. }
  1018. Combinations(Value<std::vector<T>>* val, const std::string alias="")
  1019. :DerivedValue<std::vector<tuple_type>>(fmt_name(val), alias),
  1020. val(val) { }
  1021. };
  1022. /**
  1023. * Calculate the cartesian product of two input vectors
  1024. */
  1025. template <typename FST, typename SND>
  1026. class CartProduct : public DerivedValue<std::vector<std::tuple<FST,SND>>>{
  1027. private:
  1028. Value<std::vector<FST>>* val1;
  1029. Value<std::vector<SND>>* val2;
  1030. void update_value(){
  1031. this->value.clear();
  1032. auto& v1 = val1->get_value();
  1033. auto& v2 = val2->get_value();
  1034. for(int i=0; i<v1.size(); i++){
  1035. for(int j=0; j<v2.size(); j++){
  1036. this->value.push_back(std::tuple<FST,SND>(v1[i],v2[j]));
  1037. }
  1038. }
  1039. }
  1040. static std::string calc_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1041. std::stringstream ss;
  1042. ss << "cartProduct("
  1043. << val1->get_name() << ", " << val2->get_name()
  1044. << ")";
  1045. return ss.str();
  1046. }
  1047. public:
  1048. static std::string fmt_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1049. return "cartProduct("+val1->get_name()+", "+val2->get_name()+")";
  1050. }
  1051. CartProduct(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string alias="")
  1052. :DerivedValue<std::vector<std::tuple<FST,SND>>>(calc_name(val1, val2), alias),
  1053. val1(val1), val2(val2) { }
  1054. };
  1055. /**
  1056. * A generic value owning only a function object.
  1057. * All necessary values upon which this value depends must be bound to the
  1058. * function object.
  1059. */
  1060. template <typename T>
  1061. class BoundValue : public DerivedValue<T>{
  1062. protected:
  1063. Function<T()>* f;
  1064. void update_value(){
  1065. this->value = (*f)();
  1066. }
  1067. public:
  1068. static std::string fmt_name(Function<T()>* f){
  1069. return f->get_name()+"(<bound>)";
  1070. }
  1071. BoundValue(Function<T()>* f, const std::string alias="")
  1072. :DerivedValue<T>(fmt_name(f), alias),
  1073. f(f) { }
  1074. };
  1075. /**
  1076. * A Value of a pointer. The pointer is constant, however the data the pointer
  1077. * points to is variable.
  1078. */
  1079. template <typename T>
  1080. class PointerValue : public DerivedValue<T*>{
  1081. protected:
  1082. void update_value(){ }
  1083. public:
  1084. PointerValue(const std::string& name, T* ptr, const std::string alias="")
  1085. :DerivedValue<T*>(name, alias){
  1086. this->value = ptr;
  1087. }
  1088. };
  1089. /**
  1090. * Used for when the value is an object whose location in memory is changing,
  1091. * but one has a pointer to a pointer that points to the always updated
  1092. * location. (Mainly when reading objects such as stl containers from a root
  1093. * TTree)
  1094. */
  1095. template <typename T>
  1096. class ObjectValue : public DerivedValue<T>{
  1097. protected:
  1098. T** obj_pointer;
  1099. void update_value(){
  1100. this->value = **obj_pointer;
  1101. }
  1102. public:
  1103. ObjectValue(const std::string& name, T **ptr, const std::string alias="")
  1104. :DerivedValue<T>(name, alias),
  1105. obj_pointer(ptr){ }
  1106. };
  1107. /**
  1108. * A Value which always returns the same value, supplied in the constructor.
  1109. */
  1110. template <typename T>
  1111. class ConstantValue : public DerivedValue<T>{
  1112. protected:
  1113. void update_value(){ }
  1114. public:
  1115. static std::string fmt_name(const std::string& name){
  1116. return "const::"+name;
  1117. }
  1118. ConstantValue(const std::string& name, T const_value, const std::string alias="")
  1119. :DerivedValue<T>(fmt_name(name), alias) {
  1120. this->value = const_value;
  1121. }
  1122. };
  1123. /**
  1124. * A Value which is the result of calling the specified function which takes
  1125. * no arguments.
  1126. */
  1127. template <typename T>
  1128. class FunctionValue : public DerivedValue<T>{
  1129. Function<T()>* fn;
  1130. protected:
  1131. void update_value(){
  1132. this->value = (*fn)();
  1133. }
  1134. public:
  1135. static std::string fmt_name(const std::string& name){
  1136. return name;
  1137. }
  1138. FunctionValue(const std::string& name, Function<T()>* fn, const std::string alias="")
  1139. :DerivedValue<T>(fmt_name(name), alias) {
  1140. this->fn = fn;
  1141. }
  1142. };
  1143. }
  1144. #endif // value_hpp