value.hpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. bool logging_enabled;
  282. public:
  283. GenValue(const std::type_index&& ti, const std::string& name, const std::string& alias)
  284. :name(name), value_valid(false), logging_enabled(false){
  285. if (alias != "")
  286. INFO("Registered value: \"" << name << "\" with alias: \"" << alias << "\"");
  287. else
  288. INFO("Registered value: \"" << name);
  289. values[std::make_pair(ti,name)] = this;
  290. if (alias != "")
  291. GenValue::alias(ti, alias, this);
  292. }
  293. const std::string& get_name(){
  294. return name;
  295. }
  296. /**
  297. * If logging is enabled for this value, this function should be
  298. * implemented to format the value to a string and place it as an INFO
  299. * entry in the log file. Useful for debugging, but may produce a lot of
  300. * output.
  301. */
  302. virtual void log() = 0;
  303. static void reset(){
  304. for (auto val : values){
  305. if (val.second != nullptr){
  306. val.second->_reset();
  307. }
  308. }
  309. }
  310. template<typename T>
  311. static Value<T>* get_value(const std::string& name){
  312. const std::type_index& ti = typeid(T);
  313. auto lookup_id = std::make_pair(ti,name);
  314. if (aliases[lookup_id] != nullptr)
  315. return (Value<T>*)aliases[lookup_id];
  316. else
  317. return (Value<T>*)values[lookup_id];
  318. }
  319. static void alias(const std::type_index& ti, const std::string& name, GenValue* value){
  320. auto lookup_id = std::make_pair(ti,name);
  321. if (aliases[lookup_id] != nullptr){
  322. WARNING("WARNING: alias \"" << name << "\" overrides previous entry.");
  323. }
  324. aliases[lookup_id] = value;
  325. }
  326. template<typename T>
  327. static void alias(const std::string& name, Value<T>* value){
  328. alias(typeid(T), name, value);
  329. }
  330. static std::string summary(){
  331. std::stringstream ss;
  332. ss << "The following values have been created:" << std::endl;
  333. for (auto item : values){
  334. auto& key = item.first;
  335. auto& value = item.second;
  336. if (value == nullptr) continue;
  337. ss << "\tVALUE::\"" << key.second << "\" at address " << value << std::endl;
  338. }
  339. ss << "And these aliases:" << std::endl;
  340. for (auto item : aliases){
  341. auto& key = item.first;
  342. auto& value = item.second;
  343. std::string orig("VOID");
  344. if (value == nullptr) continue;
  345. for (auto v_item : values){
  346. auto& v_value = v_item.second;
  347. if (v_value == value){
  348. orig = v_value->get_name();
  349. break;
  350. }
  351. }
  352. ss << "\tALIAS::\"" << key.second << "\" referring to \"" << orig << "\"" << std::endl;
  353. }
  354. return ss.str();
  355. }
  356. friend std::ostream& operator<<(std::ostream& os, const GenValue& gv);
  357. };
  358. std::ostream& operator<<(std::ostream& os, GenValue& gv){
  359. os << gv.get_name();
  360. return os;
  361. }
  362. /**
  363. * A templated value.
  364. * In order to facilitate run-time creation of analysis routines, it is
  365. * necessary to have some ability to get and store *values*. Values can either
  366. * be directly taken from some original data source (i.e. ObservedValue), or
  367. * they can be a function of some other set of values (i.e. DerivedValue). They
  368. * template class T of Value<T> is the type of thing that is returned upon
  369. * calling get_value().
  370. */
  371. template <typename T>
  372. class Value : public GenValue{
  373. protected:
  374. std::function<std::string(T)> value_to_string;
  375. public:
  376. Value(const std::string& name, const std::string& alias="")
  377. :value_to_string([](T){return "";}),
  378. GenValue(typeid(T), name, alias){ }
  379. /** Calculate, if necessary, and return the value held by this object.
  380. */
  381. virtual T& get_value() = 0;
  382. void enable_logging(const std::function<std::string(T)>& value_to_string = [](T){return "";}){
  383. logging_enabled = true;
  384. this->value_to_string = value_to_string;
  385. }
  386. void disable_logging(){
  387. logging_enabled = false;
  388. }
  389. };
  390. /**
  391. * A value supplied by the dataset, not derived.
  392. * An ObservedValue is the interface to your dataset. Upon creation, an
  393. * ObservedValue is given a pointer to an object of type T. When an observation
  394. * is loaded into memory, the value at the location referenced by that pointer
  395. * must be updated with the associated data from that observation. This is the
  396. * responsibility of whatever DataSet implementation is being used. This object
  397. * then will read that data and return it when requested.
  398. */
  399. template <typename T>
  400. class ObservedValue : public Value<T>{
  401. private:
  402. T *val_ref;
  403. public:
  404. ObservedValue(const std::string& name, T* val_ref, const std::string& alias="")
  405. :Value<T>(name, alias),
  406. val_ref(val_ref){ }
  407. void log(){
  408. if (util::debug_on) {
  409. std::cout << "Calculating Value: " << this->get_name() << std::endl;
  410. }
  411. if(this->logging_enabled){
  412. INFO(this->get_name() << ": " << this->value_to_string(*val_ref));
  413. }
  414. }
  415. static std::string fmt_name(const std::string& name){
  416. return name;
  417. }
  418. T& get_value(){
  419. if (!this->value_valid){
  420. this->value_valid = true;
  421. this->log();
  422. }
  423. return *val_ref;
  424. }
  425. };
  426. /**
  427. * A Value derived from some other Values, not directly from the dataset.
  428. * A DerivedValue is generally defined as some function of other Value objects.
  429. * For example, a Pair is a function of two other Value objects that makes a
  430. * pair of them. Note that these other Value objects are free to be either
  431. * ObservedValues or other DerivedValues.
  432. *
  433. * It is desireable from a performance standpoint that each DerivedValue be
  434. * calculated no more than once per observation. Therefore, when a get_value is
  435. * called on a DerivedValue, it first checks whether the value that it holds is
  436. * **valid**, meaning it has already been calculated for this observation. If
  437. * so, it simply returns the value. If not, the update_value function is called
  438. * to calculate the value. and then the newly calculated value is marked as
  439. * valid and returned.
  440. */
  441. template <typename T>
  442. class DerivedValue : public Value<T>{
  443. protected:
  444. T value;
  445. /**
  446. * Updates the internal value.
  447. * This function should be overridden by any child class to do the
  448. * actual work of updating value based on whatever rules the class
  449. * chooses. Normally, this consists of geting the values from some
  450. * associated Value objects, doing some calculation on them, and
  451. * storing the result in value.
  452. */
  453. virtual void update_value() = 0;
  454. public:
  455. DerivedValue(const std::string& name, const std::string& alias="")
  456. :Value<T>(name, alias){ }
  457. void log(){
  458. if (util::debug_on) {
  459. std::cout << "Calculating Value: " << this->get_name() << std::endl;
  460. }
  461. if(this->logging_enabled){
  462. INFO(this->get_name() << ": " << this->value_to_string(value));
  463. }
  464. }
  465. T& get_value(){
  466. DEBUG("Getting Value: " << this->get_name());
  467. if (!this->value_valid){
  468. update_value();
  469. this->value_valid = true;
  470. this->log();
  471. }
  472. return value;
  473. }
  474. };
  475. /**
  476. * A std::vector wrapper around a C-style array.
  477. * In order to make some of the higher-level Value types easier to work with,
  478. * it is a good idea to wrap all arrays in the original data source with
  479. * std::vector objects. To do this, it is necessary to supply both a Value
  480. * object containing the array itself as well as another Value object
  481. * containing the size of that array. Currently, update_value will simply copy
  482. * the contents of the array into the interally held vector.
  483. */
  484. template <typename T>
  485. class WrapperVector : public DerivedValue<std::vector<T> >{
  486. private:
  487. Value<int>* size;
  488. Value<T*>* data;
  489. void update_value(){
  490. int n = size->get_value();
  491. T* data_ref = data->get_value();
  492. this->value.assign(data_ref, data_ref+n);
  493. }
  494. public:
  495. static std::string fmt_name(Value<int>* size, Value<T*>* data){
  496. return "wrapper_vector("+size->get_name()+","+data->get_name()+")";
  497. }
  498. WrapperVector(Value<int>* size, Value<T*>* data, const std::string& alias="")
  499. :DerivedValue<std::vector<T> >(fmt_name(size,data), alias),
  500. size(size), data(data){ }
  501. };
  502. /**
  503. * Creates a std::pair type from a two other Value objects.
  504. */
  505. template <typename T1, typename T2>
  506. class Pair : public DerivedValue<std::pair<T1, T2> >{
  507. protected:
  508. std::pair<Value<T1>*, Value<T2>* > value_pair;
  509. void update_value(){
  510. this->value.first = value_pair.first->get_value();
  511. this->value.second = value_pair.second->get_value();
  512. }
  513. public:
  514. static std::string fmt_name(Value<T1> *value1, Value<T2> *value2){
  515. return "pair("+value1->get_name()+","+value2->get_name()+")";
  516. }
  517. Pair(Value<T1> *value1, Value<T2> *value2, const std::string alias="")
  518. :DerivedValue<std::pair<T1, T2> >(fmt_name(value1, value2), alias),
  519. value_pair(value1, value2){ }
  520. };
  521. template<typename... T> class _Zip;
  522. template<>
  523. class _Zip<> {
  524. protected:
  525. int _get_size(){
  526. return std::numeric_limits<int>::max();
  527. }
  528. std::tuple<> _get_at(int){
  529. return std::make_tuple();
  530. }
  531. std::string _get_name(){
  532. return "";
  533. }
  534. public:
  535. _Zip() { }
  536. };
  537. template<typename Head, typename... Tail>
  538. class _Zip<Head, Tail...> : private _Zip<Tail...> {
  539. protected:
  540. Value<std::vector<Head>>* head;
  541. int _get_size(){
  542. int this_size = head->get_value().size();
  543. int rest_size = _Zip<Tail...>::_get_size();
  544. return std::min(this_size, rest_size);
  545. }
  546. typename std::tuple<Head,Tail...> _get_at(int idx){
  547. auto tail_tuple = _Zip<Tail...>::_get_at(idx);
  548. return std::tuple_cat(std::make_tuple(head->get_value()[idx]),tail_tuple);
  549. }
  550. std::string _get_name(){
  551. return head->get_name()+","+_Zip<Tail...>::_get_name();
  552. }
  553. public:
  554. _Zip() { }
  555. _Zip(Value<std::vector<Head>>* head, Value<std::vector<Tail>>*... tail)
  556. : _Zip<Tail...>(tail...),
  557. head(head) { }
  558. };
  559. namespace impl {
  560. std::string zip_fmt_name(){
  561. return "";
  562. }
  563. template<typename Head>
  564. std::string zip_fmt_name(Value<std::vector<Head>>* head){
  565. return head->get_name();
  566. }
  567. template<typename Head1, typename Head2, typename... Tail>
  568. std::string zip_fmt_name(Value<std::vector<Head1>>* head1, Value<std::vector<Head2>>* head2, Value<std::vector<Tail>>*... tail){
  569. return head1->get_name() + "," + zip_fmt_name<Head2, Tail...>(head2, tail...);
  570. }
  571. }
  572. /**
  573. * Zips a series of vectors together. Can be combined with Map to
  574. * yield a Value whose elements are individually a function of the
  575. * corresponding elements of the vectors that were zipped together. For those
  576. * familiar with python, it accompilishes the same thing as
  577. * @code{.py}
  578. * xs = [1,2,3,4]
  579. * ys = [10,20,30,40]
  580. * print(list(map(lambda t:t[0]+t[1],zip(xs,ys))))
  581. * @endcode
  582. * which outputs
  583. * @code
  584. * [11, 22, 33, 44]
  585. * @endcode
  586. */
  587. template <typename... ArgTypes>
  588. class Zip : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>,
  589. private _Zip<ArgTypes...>{
  590. protected:
  591. void update_value(){
  592. this->value.clear();
  593. int size = _Zip<ArgTypes...>::_get_size();
  594. for(int i=0; i<size; i++){
  595. this->value.push_back(_Zip<ArgTypes...>::_get_at(i));
  596. }
  597. }
  598. public:
  599. static std::string fmt_name(Value<std::vector<ArgTypes>>*... args){
  600. return "zip("+zip_fmt_name(args...)+")";
  601. }
  602. Zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias)
  603. :DerivedValue<std::vector<std::tuple<ArgTypes...>>>(fmt_name(args...), alias),
  604. _Zip<ArgTypes...>(args...) { }
  605. };
  606. template<typename> class Map; // undefined
  607. /**
  608. * Maps a function over an input vector. The input vector must be a vector of
  609. * tuples, where the the elements of the tuple match the arguments of the
  610. * function. For example if the function takes two floats as arguments, the
  611. * tuple should contain two floats. The Value object required by Map will
  612. * typically be created as a Zip.
  613. */
  614. template <typename Ret, typename ArgType>
  615. class Map<Ret(ArgType)> : public DerivedValue<std::vector<Ret>>{
  616. private:
  617. typedef Value<std::vector<ArgType>> arg_type;
  618. Function<Ret(ArgType)>* fn;
  619. arg_type* arg;
  620. void update_value(){
  621. this->value.clear();
  622. for(auto v : arg->get_value()){
  623. this->value.push_back(fn(v));
  624. }
  625. }
  626. public:
  627. static std::string fmt_name(Function<Ret(ArgType)>* fn, arg_type* arg){
  628. return "map("+fn->get_name()+":"+arg->get_name()+")";
  629. }
  630. Map(Function<Ret(ArgType)>* fn, arg_type* arg, const std::string& alias)
  631. :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
  632. fn(fn), arg(arg){ }
  633. };
  634. template<typename> class TupMap; // undefined
  635. /**
  636. * Maps a function over an input vector. The input vector must be a vector of
  637. * tuples, where the the elements of the tuple match the arguments of the
  638. * function. For example if the function takes two floats as arguments, the
  639. * tuple should contain two floats. The Value object required by Map will
  640. * typically be created as a Zip.
  641. */
  642. template <typename Ret, typename... ArgTypes>
  643. class TupMap<Ret(ArgTypes...)> : public DerivedValue<std::vector<Ret>>{
  644. private:
  645. typedef Value<std::vector<std::tuple<ArgTypes...>>> arg_type;
  646. Function<Ret(ArgTypes...)>* fn;
  647. arg_type* arg;
  648. void update_value(){
  649. this->value.clear();
  650. for(auto tup : arg->get_value()){
  651. this->value.push_back(call(fn,tup));
  652. }
  653. }
  654. public:
  655. static std::string fmt_name(Function<Ret(ArgTypes...)>* fn, arg_type* arg){
  656. return "tup_map("+fn->get_name()+":"+arg->get_name()+")";
  657. }
  658. TupMap(Function<Ret(ArgTypes...)>* fn, arg_type* arg, const std::string& alias)
  659. :DerivedValue<std::vector<Ret>>(fmt_name(fn, arg), alias),
  660. fn(fn), arg(arg){ }
  661. };
  662. template<typename... T> class _Tuple;
  663. template<>
  664. class _Tuple<> {
  665. protected:
  666. std::tuple<> _get_value(){
  667. return std::make_tuple();
  668. }
  669. public:
  670. _Tuple() { }
  671. };
  672. template<typename Head, typename... Tail>
  673. class _Tuple<Head, Tail...> : private _Tuple<Tail...> {
  674. protected:
  675. Value<Head>* head;
  676. typename std::tuple<Head,Tail...> _get_value(){
  677. auto tail_tuple = _Tuple<Tail...>::_get_value();
  678. return std::tuple_cat(std::make_tuple(head->get_value()),tail_tuple);
  679. }
  680. public:
  681. _Tuple() { }
  682. _Tuple(Value<Head>* head, Value<Tail>*... tail)
  683. : _Tuple<Tail...>(tail...),
  684. head(head) { }
  685. };
  686. namespace impl {
  687. std::string tuple_fmt_name(){
  688. return "";
  689. }
  690. template<typename Head>
  691. std::string tuple_fmt_name(Value<Head>* head){
  692. return head->get_name();
  693. }
  694. template<typename Head1, typename Head2, typename... Tail>
  695. std::string tuple_fmt_name(Value<Head1>* head1, Value<Head2>* head2, Value<Tail>*... tail){
  696. return head1->get_name() + "," + tuple_fmt_name<Head2, Tail...>(head2, tail...);
  697. }
  698. }
  699. /**
  700. * Takes a series of Value objects and bundles them together into a std::tuple
  701. * object. Typically, this is most usefull when one wants to apply a function
  702. * to a few values and store the result. This class can be used in conjunction
  703. * with Apply to achieve this.
  704. */
  705. template <typename... ArgTypes>
  706. class Tuple : public DerivedValue<std::tuple<ArgTypes...>>,
  707. private _Tuple<ArgTypes...>{
  708. protected:
  709. void update_value(){
  710. this->value = _Tuple<ArgTypes...>::_get_value();
  711. }
  712. public:
  713. static std::string fmt_name(Value<ArgTypes>*... args){
  714. return "tuple("+impl::tuple_fmt_name(args...)+")";
  715. }
  716. Tuple(Value<ArgTypes>*... args, const std::string& alias)
  717. :DerivedValue<std::tuple<ArgTypes...>>(fmt_name(args...), alias),
  718. _Tuple<ArgTypes...>(args...) { }
  719. };
  720. /**
  721. * Gets the Nth element from a tuple value
  722. */
  723. template <size_t N, typename... ArgTypes>
  724. class DeTup : public DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>{
  725. Value<std::tuple<ArgTypes...>> tup;
  726. protected:
  727. void update_value(){
  728. this->value = std::get<N>(tup->get_value());
  729. }
  730. public:
  731. static std::string fmt_name(Value<std::tuple<ArgTypes...>>* tup){
  732. return "detup("+tup->get_name()+")";
  733. }
  734. DeTup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias)
  735. :DerivedValue<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>(fmt_name(tup), alias),
  736. tup(tup) { }
  737. };
  738. /**
  739. * Creates a vector of extracting the Nth value from each entry in a vector of
  740. * tuples.
  741. */
  742. template <size_t N, typename... ArgTypes>
  743. class DeTupVector : public DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>{
  744. Value<std::vector<std::tuple<ArgTypes...>>>* tup;
  745. protected:
  746. void update_value(){
  747. this->value.clear();
  748. for( auto& t : tup->get_value()){
  749. this->value.push_back(std::get<N>(t));
  750. }
  751. }
  752. public:
  753. static std::string fmt_name(Value<std::vector<std::tuple<ArgTypes...>>>* tup){
  754. return "detup_vec("+tup->get_name()+")";
  755. }
  756. DeTupVector(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias)
  757. :DerivedValue<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>(fmt_name(tup), alias),
  758. tup(tup) { }
  759. };
  760. template<typename> class Apply; // undefined
  761. /**
  762. * Applies a function to a tuple of values and returns a value. This will
  763. * typically be called with a Tuple object as an argument.
  764. */
  765. template <typename Ret, typename ArgType>
  766. class Apply<Ret(ArgType)> : public DerivedValue<Ret>{
  767. private:
  768. Function<Ret(ArgType)>* fn;
  769. Value<ArgType>* arg;
  770. void update_value(){
  771. this->value = (*fn)(arg->get_value());
  772. }
  773. public:
  774. static std::string fmt_name(Function<Ret(ArgType)>* fn, Value<ArgType>* arg){
  775. return "apply("+fn->get_name()+":"+arg->get_name()+")";
  776. }
  777. Apply(Function<Ret(ArgType)>* fn, Value<ArgType>* arg, const std::string& alias)
  778. :DerivedValue<Ret>(fmt_name(fn,arg), alias),
  779. fn(fn), arg(arg){ }
  780. };
  781. template<typename> class TupApply; // undefined
  782. /**
  783. * Applies a function to a tuple of values and returns a value. This will
  784. * typically be called with a Tuple object as an argument.
  785. */
  786. template <typename Ret, typename... ArgTypes>
  787. class TupApply<Ret(ArgTypes...)> : public DerivedValue<Ret>{
  788. private:
  789. Function<Ret(ArgTypes...)>* fn;
  790. Value<std::tuple<ArgTypes...>>* arg;
  791. void update_value(){
  792. auto &tup = arg->get_value();
  793. this->value = call(fn, tup);
  794. }
  795. public:
  796. static std::string fmt_name(Function<Ret(ArgTypes...)>* fn, Value<std::tuple<ArgTypes...>>* arg){
  797. return "tup_apply("+fn->get_name()+":"+arg->get_name()+")";
  798. }
  799. TupApply(Function<Ret(ArgTypes...)>* fn, Value<std::tuple<ArgTypes...>>* arg, const std::string& alias)
  800. :DerivedValue<Ret>(fmt_name(fn,arg), alias),
  801. fn(fn), arg(arg){ }
  802. };
  803. /**
  804. * Returns the count of elements in the input vector passing a test function.
  805. */
  806. template<typename T>
  807. class Count : public DerivedValue<int>{
  808. private:
  809. Function<bool(T)>* selector;
  810. Value<std::vector<T> >* v;
  811. void update_value(){
  812. value = 0;
  813. for(auto val : v->get_value()){
  814. if(selector(val))
  815. value++;
  816. }
  817. }
  818. public:
  819. static std::string fmt_name(Function<bool(T)>* selector, Value<std::vector<T>>* v){
  820. return "count("+selector->get_name()+":"+v->get_name()+")";
  821. }
  822. Count(Function<bool(T)>* selector, Value<std::vector<T>>* v, const std::string alias)
  823. :DerivedValue<int>(fmt_name(selector,v), alias),
  824. selector(selector), v(v) { }
  825. };
  826. /**
  827. * Returns the elements in a vector that pass a test function.
  828. */
  829. template<typename T>
  830. class Filter : public DerivedValue<std::vector<T>>{
  831. private:
  832. Function<bool(T)>* filter;
  833. Value<std::vector<T> >* v;
  834. void update_value(){
  835. this->value.clear();
  836. for(auto val : v->get_value()){
  837. if(this->filter(val))
  838. this->value.push_back(val);
  839. }
  840. }
  841. public:
  842. static std::string fmt_name(Function<bool(T)>* filter, Value<std::vector<T>>* v){
  843. return "filter("+filter->get_name()+":"+v->get_name()+")";
  844. }
  845. Filter(Function<bool(T)>* filter, Value<std::vector<T>>* v, const std::string alias)
  846. :DerivedValue<std::vector<T>>(fmt_name(filter,v), alias),
  847. filter(filter), v(v) { }
  848. };
  849. /**
  850. * Returns the elements in a vector that pass a test function. The elements on
  851. * the vector must be tuples. Typically this will be used in conjunction with
  852. * Zip and Map.
  853. */
  854. template<typename... ArgTypes>
  855. class TupFilter : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>{
  856. private:
  857. typedef std::vector<std::tuple<ArgTypes...>> value_type;
  858. Function<bool(ArgTypes...)>* filter;
  859. Value<value_type>* arg;
  860. void update_value(){
  861. this->value.clear();
  862. for(auto val : arg->get_value()){
  863. if(call(filter,val))
  864. this->value.push_back(val);
  865. }
  866. }
  867. public:
  868. static std::string fmt_name(Function<bool(ArgTypes...)>* filter, Value<value_type>* arg){
  869. return "tup_filter("+filter->get_name()+":"+arg->get_name()+")";
  870. }
  871. TupFilter(Function<bool(ArgTypes...)>* filter, Value<value_type>* arg, const std::string alias)
  872. :DerivedValue<value_type>(fmt_name(filter, arg), alias),
  873. filter(filter), arg(arg) { }
  874. };
  875. /**
  876. * Reduce a Value of type vector<T> to just a T.
  877. * This is useful functionality to model, for instance, calculating the maximum
  878. * element of a vector, or a the mean. See child classes for specific
  879. * implementations.
  880. */
  881. template <typename T>
  882. class Reduce : public DerivedValue<T>{
  883. private:
  884. Function<T(std::vector<T>)>* reduce_fn;
  885. void update_value(){
  886. this->value = reduce_fn(v->get_value());
  887. }
  888. protected:
  889. Value<std::vector<T> >* v;
  890. public:
  891. static std::string fmt_name(Function<T(std::vector<T>)>* reduce_fn, Value<std::vector<T>>* v){
  892. return "reduce("+reduce_fn->get_name()+":"+v->get_name()+")";
  893. }
  894. Reduce(Function<T(std::vector<T>)>* reduce_fn, Value<std::vector<T> >* v, const std::string alias)
  895. :DerivedValue<T>(fmt_name(reduce_fn, v), alias),
  896. reduce_fn(reduce_fn), v(v) { }
  897. };
  898. /**
  899. * Find and return the maximum value of a vector.
  900. */
  901. template <typename T>
  902. class Max : public Reduce<T>{
  903. public:
  904. static std::string fmt_name(Value<std::vector<T>>* v){
  905. return "max("+v->get_name()+")";
  906. }
  907. Max(Value<std::vector<T>>* v, const std::string alias)
  908. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("max",
  909. FUNC(([](std::vector<T> vec){
  910. return *std::max_element(vec.begin(), vec.end());}))),
  911. v, alias) { }
  912. };
  913. /**
  914. * Find and return the minimum value of a vector.
  915. */
  916. template <typename T>
  917. class Min : public Reduce<T>{
  918. public:
  919. static std::string fmt_name(Value<std::vector<T>>* v){
  920. return "min("+v->get_name()+")";
  921. }
  922. Min(Value<std::vector<T>>* v, const std::string alias)
  923. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("min",
  924. FUNC(([](std::vector<T> vec){
  925. return *std::min_element(vec.begin(), vec.end());}))),
  926. v, alias) { }
  927. };
  928. /**
  929. * Calculate the mean value of a vector.
  930. */
  931. template <typename T>
  932. class Mean : public Reduce<T>{
  933. public:
  934. static std::string fmt_name(Value<std::vector<T>>* v){
  935. return "mean("+v->get_name()+")";
  936. }
  937. Mean(Value<std::vector<T>>* v, const std::string alias)
  938. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("mean",
  939. FUNC(([](std::vector<T> vec){
  940. int n = 0; T sum = 0;
  941. for (T e : vec){ n++; sum += e; }
  942. return n>0 ? sum / n : 0; }))),
  943. v, alias) { }
  944. };
  945. /**
  946. * Calculate the range of the values in a vector
  947. */
  948. template <typename T>
  949. class Range : public Reduce<T>{
  950. public:
  951. static std::string fmt_name(Value<std::vector<T>>* v){
  952. return "range("+v->get_name()+")";
  953. }
  954. Range(Value<std::vector<T>>* v, const std::string alias)
  955. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("range",
  956. FUNC(([](std::vector<T> vec){
  957. auto minmax = std::minmax_element(vec.begin(), vec.end());
  958. return (*minmax.second) - (*minmax.first); }))),
  959. v, alias) { }
  960. };
  961. /**
  962. * Extract the element at a specific index from a vector.
  963. */
  964. template <typename T>
  965. class ElementOf : public Reduce<T>{
  966. public:
  967. ElementOf(Value<int>* index, Value<std::vector<T>>* v, const std::string alias)
  968. :Reduce<T>(GenFunction::reg_func<T(std::vector<T>)>("elementOf",
  969. FUNC(([index](std::vector<T> vec){return vec[index->get_value()];}))),
  970. v, alias) { }
  971. };
  972. /**
  973. * Similar to Reduce, but returns a pair of a T and an int.
  974. * This is useful if you need to know where in the vector exists the element
  975. * being returned.
  976. */
  977. template <typename T>
  978. class ReduceIndex : public DerivedValue<std::pair<T, int> >{
  979. private:
  980. Function<std::pair<T,int>(std::vector<T>)>* reduce;
  981. Value<std::vector<T> >* v;
  982. void update_value(){
  983. this->value = reduce(v->get_value());
  984. }
  985. public:
  986. ReduceIndex(Function<std::pair<T,int>(std::vector<T>)>* reduce, Value<std::vector<T> >* v, const std::string alias="")
  987. :DerivedValue<T>("reduceIndexWith("+reduce->get_name()+":"+v->get_name()+")", alias),
  988. reduce(reduce), v(v) { }
  989. };
  990. /**
  991. * Find and return the maximum value of a vector and its index.
  992. */
  993. template <typename T>
  994. class MaxIndex : public ReduceIndex<T>{
  995. public:
  996. MaxIndex(Value<std::vector<T>>* v, const std::string alias="")
  997. :ReduceIndex<T>(GenFunction::reg_func<T(std::vector<T>)>("maxIndex",
  998. FUNC(([](std::vector<T> vec){
  999. auto elptr = std::max_element(vec.begin(), vec.end());
  1000. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  1001. v, alias) { }
  1002. };
  1003. /**
  1004. * Find and return the minimum value of a vector and its index.
  1005. */
  1006. template <typename T>
  1007. class MinIndex : public ReduceIndex<T>{
  1008. public:
  1009. MinIndex(Value<std::vector<T>>* v, const std::string alias="")
  1010. :ReduceIndex<T>(GenFunction::reg_func<T(std::vector<T>)>("minIndex",
  1011. FUNC(([](std::vector<T> vec){
  1012. auto elptr = std::min_element(vec.begin(), vec.end());
  1013. return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
  1014. v, alias) { }
  1015. };
  1016. /**
  1017. * Find combinations of items from an input vector
  1018. */
  1019. template <typename T, int Size>
  1020. class Combinations : public DerivedValue<std::vector<typename HomoTuple<T,Size>::type>>{
  1021. private:
  1022. Value<std::vector<T>>* val;
  1023. typedef typename HomoTuple<T,Size>::type tuple_type;
  1024. void update_value(){
  1025. auto& v = val->get_value();
  1026. int data_size = v.size();
  1027. this->value.clear();
  1028. std::vector<bool> selector(data_size);
  1029. std::fill(selector.begin(), selector.begin()+std::min({Size,data_size}), true);
  1030. do {
  1031. std::array<T, Size> perm;
  1032. int idx = 0;
  1033. for (int i=0; i<data_size; i++){
  1034. if (selector[i]){
  1035. perm[idx] = v[i];
  1036. idx++;
  1037. if (idx == Size) break;
  1038. }
  1039. }
  1040. this->value.push_back(a2t(perm)); //!!!
  1041. } while(std::prev_permutation(selector.begin(), selector.end()));
  1042. }
  1043. public:
  1044. static std::string fmt_name(Value<std::vector<T>>* val){
  1045. std::stringstream ss;
  1046. ss << "combinations(" << Size << "," << val->get_name() << ")";
  1047. return ss.str();
  1048. }
  1049. Combinations(Value<std::vector<T>>* val, const std::string alias="")
  1050. :DerivedValue<std::vector<tuple_type>>(fmt_name(val), alias),
  1051. val(val) { }
  1052. };
  1053. /**
  1054. * Calculate the cartesian product of two input vectors
  1055. */
  1056. template <typename FST, typename SND>
  1057. class CartProduct : public DerivedValue<std::vector<std::tuple<FST,SND>>>{
  1058. private:
  1059. Value<std::vector<FST>>* val1;
  1060. Value<std::vector<SND>>* val2;
  1061. void update_value(){
  1062. this->value.clear();
  1063. auto& v1 = val1->get_value();
  1064. auto& v2 = val2->get_value();
  1065. for(int i=0; i<v1.size(); i++){
  1066. for(int j=0; j<v2.size(); j++){
  1067. this->value.push_back(std::tuple<FST,SND>(v1[i],v2[j]));
  1068. }
  1069. }
  1070. }
  1071. static std::string calc_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1072. std::stringstream ss;
  1073. ss << "cartProduct("
  1074. << val1->get_name() << ", " << val2->get_name()
  1075. << ")";
  1076. return ss.str();
  1077. }
  1078. public:
  1079. static std::string fmt_name(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2){
  1080. return "cartProduct("+val1->get_name()+", "+val2->get_name()+")";
  1081. }
  1082. CartProduct(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string alias="")
  1083. :DerivedValue<std::vector<std::tuple<FST,SND>>>(calc_name(val1, val2), alias),
  1084. val1(val1), val2(val2) { }
  1085. };
  1086. /**
  1087. * A generic value owning only a function object.
  1088. * All necessary values upon which this value depends must be bound to the
  1089. * function object.
  1090. */
  1091. template <typename T>
  1092. class BoundValue : public DerivedValue<T>{
  1093. protected:
  1094. Function<T()>* f;
  1095. void update_value(){
  1096. this->value = (*f)();
  1097. }
  1098. public:
  1099. static std::string fmt_name(Function<T()>* f){
  1100. return f->get_name()+"(<bound>)";
  1101. }
  1102. BoundValue(Function<T()>* f, const std::string alias="")
  1103. :DerivedValue<T>(fmt_name(f), alias),
  1104. f(f) { }
  1105. };
  1106. /**
  1107. * A Value of a pointer. The pointer is constant, however the data the pointer
  1108. * points to is variable.
  1109. */
  1110. template <typename T>
  1111. class PointerValue : public DerivedValue<T*>{
  1112. protected:
  1113. void update_value(){ }
  1114. public:
  1115. PointerValue(const std::string& name, T* ptr, const std::string alias="")
  1116. :DerivedValue<T*>(name, alias){
  1117. this->value = ptr;
  1118. }
  1119. };
  1120. /**
  1121. * Used for when the value is an object whose location in memory is changing,
  1122. * but one has a pointer to a pointer that points to the always updated
  1123. * location. (Mainly when reading objects such as stl containers from a root
  1124. * TTree)
  1125. */
  1126. template <typename T>
  1127. class ObjectValue : public DerivedValue<T>{
  1128. protected:
  1129. T** obj_pointer;
  1130. void update_value(){
  1131. this->value = **obj_pointer;
  1132. }
  1133. public:
  1134. ObjectValue(const std::string& name, T **ptr, const std::string alias="")
  1135. :DerivedValue<T>(name, alias),
  1136. obj_pointer(ptr){ }
  1137. };
  1138. /**
  1139. * A Value which always returns the same value, supplied in the constructor.
  1140. */
  1141. template <typename T>
  1142. class ConstantValue : public DerivedValue<T>{
  1143. protected:
  1144. void update_value(){ }
  1145. public:
  1146. static std::string fmt_name(const std::string& name){
  1147. return "const::"+name;
  1148. }
  1149. ConstantValue(const std::string& name, T const_value, const std::string alias="")
  1150. :DerivedValue<T>(fmt_name(name), alias) {
  1151. this->value = const_value;
  1152. }
  1153. };
  1154. /**
  1155. * A Value which is the result of calling the specified function which takes
  1156. * no arguments.
  1157. */
  1158. template <typename T>
  1159. class FunctionValue : public DerivedValue<T>{
  1160. Function<T()>* fn;
  1161. protected:
  1162. void update_value(){
  1163. this->value = (*fn)();
  1164. }
  1165. public:
  1166. static std::string fmt_name(const std::string& name){
  1167. return name;
  1168. }
  1169. FunctionValue(const std::string& name, Function<T()>* fn, const std::string alias="")
  1170. :DerivedValue<T>(fmt_name(name), alias) {
  1171. this->fn = fn;
  1172. }
  1173. };
  1174. }
  1175. #endif // value_hpp