value.hpp 44 KB

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