52 #include <initializer_list> 73 inline static bool in_register_function=
false;
82 GenFunction(
const std::string& name,
const std::string& impl)
83 :impl(impl), name(name){ }
87 std::string& get_name(){
97 std::stringstream code_out(
"");
98 std::string command(
"echo \""+code+
"\" | clang-format");
100 FILE *stream = popen(command.c_str(),
"r");
101 while (fgets(buffer, 255, stream) != NULL)
103 if (pclose(stream) == 0)
104 return code_out.str();
109 static std::string summary(){
110 std::stringstream ss;
111 ss <<
"The following functions have been registered" << std::endl;
112 for(
auto p : function_registry){
113 if (p.second ==
nullptr)
continue;
114 ss <<
"-->" << p.second->name << std::endl;
120 template <
typename T>
121 static Function<T>& register_function(
const std::string& name, std::function<T> f,
const std::string& impl){
122 in_register_function =
true;
126 if (func ==
nullptr){
127 ERROR(
"Trying to register function which has already been registered with a different type");
133 in_register_function =
false;
150 template <
typename R,
typename... ArgTypes>
153 std::function<R(ArgTypes...)> f;
156 Function(
const std::string& name,
const std::string& impl, std::function<R(ArgTypes...)> f)
158 if (!in_register_function) {
159 WARNING(
"Don't instantiate Function objects directly! Use GenFunction::register_function instead.");
162 Function(
const std::string& name, std::function<R(ArgTypes...)> f)
166 R operator()(ArgTypes ...args){
173 #define FUNC(f) f, #f 182 typedef std::map<std::string, GenValue*> ValueSet;
199 virtual void _reset() = 0;
207 inline static std::map<const std::string, GenValue*>
values;
215 inline static std::map<const std::string, GenValue*>
aliases;
225 virtual void verify_integrity() = 0;
228 GenValue(
const std::string& name,
const std::string& alias)
232 GenValue::alias(alias,
this);
235 const std::string& get_name(){
240 for (
auto val : values){
241 val.second->_reset();
245 static GenValue* get_value(
const std::string& name){
246 if (aliases[name] !=
nullptr)
247 return aliases[name];
248 else if (values[name] !=
nullptr)
251 ERROR(
"Could not find alias or value \"" << name <<
"\". I'll tell you the ones I know about." << std::endl
253 CRITICAL(
"Aborting... :(",-1);
257 static void alias(
const std::string& name,
GenValue* value){
258 if (aliases[name] !=
nullptr){
259 WARNING(
"WARNING: alias \"" << name <<
"\" overrides previous entry.");
261 aliases[name] = value;
264 static GenValue* alias(
const std::string& name){
265 if (values[name] !=
nullptr){
266 WARNING(
"Alias \"" << name <<
"\" does not exist.");
268 return aliases[name];
271 static std::string summary(){
272 std::stringstream ss;
273 ss <<
"The following values have been created: " << std::endl;
274 for (
auto value : values){
275 if (value.second ==
nullptr)
continue;
276 ss <<
"\t\"" << value.first <<
"\" at address " << value.second << std::endl;
278 ss <<
"And these aliases:" << std::endl;
279 for (
auto alias : aliases){
280 std::string orig(
"VOID");
281 if (alias.second ==
nullptr)
continue;
282 for (
auto value : values){
283 if (alias.second == value.second){
284 orig = value.second->get_name();
288 ss <<
"\t\"" << alias.first <<
"\" referring to \"" << orig <<
"\"" << std::endl;
292 friend std::ostream& operator<<(std::ostream& os,
const GenValue& gv);
294 std::ostream& operator<<(std::ostream& os,
GenValue& gv){
309 template <
typename T>
312 Value(
const std::string& name,
const std::string& alias=
"")
316 virtual T& get_value() = 0;
329 template <
typename T>
336 if (val_ref ==
nullptr)
337 CRITICAL(
"ObservedValue " << this->get_name() <<
" created with null pointer",-1);
341 ObservedValue(
const std::string& name, T* val_ref,
const std::string& alias=
"")
365 template <
typename T>
383 virtual void update_value() = 0;
385 DerivedValue(
const std::string& name,
const std::string& alias=
"")
387 value_valid(
false) { }
408 template <
typename T>
417 this->value.assign(data_ref, data_ref+n);
422 CRITICAL(
"WrapperVector " << this->get_name() <<
" created with invalid size.",-1);
424 CRITICAL(
"WrapperVector " << this->get_name() <<
" created with invalid value.",-1);
430 size(size), data(data){ }
432 WrapperVector(
const std::string &label_size,
const std::string &label_data,
const std::string& alias=
"")
434 dynamic_cast<Value<T*>*
>(GenValue::get_value(label_data)), alias) { }
440 template <
typename T1,
typename T2>
443 std::pair<Value<T1>*,
Value<T2>* > value_pair;
445 this->value.first = value_pair.first->get_value();
446 this->value.second = value_pair.second->get_value();
450 if (value_pair.first ==
nullptr)
451 CRITICAL(
"Pair " << this->get_name() <<
" created with invalid first value.",-1);
452 if (value_pair.second ==
nullptr)
453 CRITICAL(
"Pair " << this->get_name() <<
" created with invalid second value.",-1);
459 value_pair(value1, value2){ }
460 Pair(
const std::string& label1,
const std::string& label2,
const std::string alias=
"")
462 dynamic_cast<Value<T1>*
>(GenValue::get_value(label2)),
479 template <
typename R,
typename T>
495 std::tie(n, std::ignore) = std::minmax({v1_val.size(), v2_val.size(), v3_val.size(), v4_val.size()});
496 this->value.resize(n);
497 for (
int i=0; i<n; i++){
498 this->value[i] = f(v1_val[i], v2_val[i], v3_val[i], v4_val[i]);
504 CRITICAL(
"ZipMapFour " << this->get_name() <<
" created with invalid first value.",-1);
506 CRITICAL(
"ZipMapFour " << this->get_name() <<
" created with invalid second value.",-1);
508 CRITICAL(
"ZipMapFour " << this->get_name() <<
" created with invalid third value.",-1);
510 CRITICAL(
"ZipMapFour " << this->get_name() <<
" created with invalid fourth value.",-1);
515 Value<std::vector<T> >* v1,
Value<std::vector<T> >* v2,
516 Value<std::vector<T> >* v3,
Value<std::vector<T> >* v4,
const std::string alias=
"")
518 v3->get_name()+
","+v4->get_name()+
")", alias),
519 f(f), v1(v1), v2(v2), v3(v3), v4(v4) { }
522 const std::string& label1,
const std::string& label2,
523 const std::string& label3,
const std::string& label4,
const std::string alias=
"")
551 CRITICAL(
"Count " << this->get_name() <<
" created with invalid value.",-1);
555 Count(
Function<
bool(T)>& selector,
Value<std::vector<T>>* v,
const std::string alias=
"")
557 selector(selector), v(v) { }
559 Count(
Function<
bool(T)>& selector,
const std::string& v_name,
const std::string alias=
"")
570 template <
typename T>
576 this->value = reduce(v->get_value());
581 CRITICAL(
"Reduce " << this->get_name() <<
" created with invalid value.",-1);
588 Reduce(
Function<T(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias=
"")
589 :
DerivedValue<T>(
"reduceWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
590 reduce(reduce), v(v) { }
592 Reduce(
Function<T(std::vector<T>)>& reduce,
const std::string& v_name,
const std::string alias=
"")
599 template <
typename T>
603 if (this->v ==
nullptr)
604 CRITICAL(
"Max " << this->get_name() <<
" created with invalid value.",-1);
607 Max(
const std::string& v_name,
const std::string alias=
"")
608 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"max",
609 FUNC(([](std::vector<T> vec){
610 return *std::max_element(vec.begin(), vec.end());}))),
617 template <
typename T>
621 if (this->v ==
nullptr)
622 CRITICAL(
"Min " << this->get_name() <<
" created with invalid value.",-1);
625 Min(
const std::string& v_name,
const std::string alias=
"")
626 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"min",
627 FUNC(([](std::vector<T> vec){
628 return *std::min_element(vec.begin(), vec.end());}))),
635 template <
typename T>
639 if (this->v ==
nullptr)
640 CRITICAL(
"Mean " << this->get_name() <<
" created with invalid value.",-1);
644 Mean(
const std::string& v_name,
const std::string alias=
"")
645 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"mean",
646 FUNC(([](std::vector<T> vec){
647 int n = 0; T sum = 0;
648 for (T e : vec){ n++; sum += e; }
649 return n>0 ? sum / n : 0; }))),
656 template <
typename T>
660 if (this->v ==
nullptr)
661 CRITICAL(
"Range " << this->get_name() <<
" created with invalid value.",-1);
665 Range(
const std::string& v_name,
const std::string alias=
"")
666 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"range",
667 FUNC(([](std::vector<T> vec){
668 auto minmax = std::minmax_element(vec.begin(), vec.end());
669 return (*minmax.second) - (*minmax.first); }))),
676 template <
typename T>
680 if (this->v ==
nullptr)
681 CRITICAL(
"ElementOf " << this->get_name() <<
" created with invalid value.",-1);
686 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"elementOf",
687 FUNC(([index](std::vector<T> vec){
return vec[index->
get_value()];}))),
689 ElementOf(
const std::string& name,
int index,
const std::string& v_name,
const std::string alias=
"")
690 :
Reduce<T>(name, [index](std::vector<T> vec){
return vec[index];}, v_name, alias) { }
698 template <
typename T>
705 this->value = reduce(v->get_value());
710 CRITICAL(
"ReduceIndex " << this->get_name() <<
" created with invalid value.",-1);
714 ReduceIndex(
Function<std::pair<T,int>(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias=
"")
715 :
DerivedValue<T>(
"reduceIndexWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
716 reduce(reduce), v(v) { }
718 ReduceIndex(
Function<std::pair<T,int>(std::vector<T>)>& reduce,
const std::string& v_name,
const std::string alias=
"")
725 template <
typename T>
729 if (this->v ==
nullptr)
730 CRITICAL(
"MaxIndex " << this->get_name() <<
" created with invalid value.",-1);
734 MaxIndex(
const std::string& v_name,
const std::string alias=
"")
735 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"maxIndex",
736 FUNC(([](std::vector<T> vec){
737 auto elptr = std::max_element(vec.begin(), vec.end());
738 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
739 ))), v_name, alias) { }
745 template <
typename T>
749 if (this->v ==
nullptr)
750 CRITICAL(
"MinIndex " << this->get_name() <<
" created with invalid value.",-1);
754 MinIndex(
const std::string& v_name,
const std::string alias=
"")
755 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"minIndex",
756 FUNC(([](std::vector<T> vec){
757 auto elptr = std::min_element(vec.begin(), vec.end());
758 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
759 ))), v_name, alias) { }
767 template <
typename T>
786 template <
typename T>
790 if(this->value ==
nullptr)
791 CRITICAL(
"PointerValue " << this->get_name() <<
" created with null pointer",-1);
798 PointerValue(
const std::string& name, T* ptr,
const std::string alias=
"")
807 template <
typename T>
816 ConstantValue(
const std::string& name, T const_value,
const std::string alias=
"")
Definition: value.hpp:183
void update_value()
Updates the internal value.
Definition: value.hpp:773
Find and return the maximum value of a vector.
Definition: value.hpp:600
Reduce a Value of type vector<T> to just a T.
Definition: value.hpp:571
void update_value()
Updates the internal value.
Definition: value.hpp:541
void update_value()
Updates the internal value.
Definition: value.hpp:444
Find and return the minimum value of a vector and its index.
Definition: value.hpp:746
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:368
A generic value owning only a function object.
Definition: value.hpp:768
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:389
virtual void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:579
Calculate the range of the values in a vector.
Definition: value.hpp:657
A std::vector wrapper around a C-style array.
Definition: value.hpp:409
Similar to Reduce, but returns a pair of a T and an int.
Definition: value.hpp:699
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:728
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:602
std::string name
The name of the value.
Definition: value.hpp:190
void update_value()
Updates the internal value.
Definition: value.hpp:488
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:789
Returns the count of elements in the input vector passing a test function.
Definition: value.hpp:536
Find and return the maximum value of a vector and its index.
Definition: value.hpp:726
void update_value()
Updates the internal value.
Definition: value.hpp:575
Parent class to all Function classes.
Definition: value.hpp:68
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:333
Takes a set of four Value<std::vector<T> > objects and a function of four Ts and returns a std::vecto...
Definition: value.hpp:480
static std::map< const std::string, GenValue * > aliases
Composite value names are typically nested.
Definition: value.hpp:215
A generic, observed, value.
Definition: value.hpp:330
Find and return the minimum value of a vector.
Definition: value.hpp:618
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:441
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:420
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:335
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:344
void update_value()
Updates the internal value.
Definition: value.hpp:704
void update_value()
Updates the internal value.
Definition: value.hpp:813
The namespace containing all filval classes and functions.
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:502
Extract the element at a specific index from a vector.
Definition: value.hpp:677
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:748
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:679
Calculate the mean value of a vector.
Definition: value.hpp:636
void update_value()
Updates the internal value.
Definition: value.hpp:414
A generic value.
Definition: value.hpp:310
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:638
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:620
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:659
static std::map< const std::string, GenValue * > values
A static mapping containing all created Value objects.
Definition: value.hpp:207
static std::string format_code(const std::string &code)
Attempt to invoke clang-format for the purpose of printing out nicely formatted functions to the log ...
Definition: value.hpp:96
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:770
A generic, derived, value.
Definition: value.hpp:366
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:449
virtual void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:708
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:810
void update_value()
Updates the internal value.
Definition: value.hpp:795
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
void verify_integrity()
This function serves to check that this Value has been created with real, i.e.
Definition: value.hpp:549
A Value which always returns the same value, supplied in the constructor.
Definition: value.hpp:808
static std::map< const std::string, GenFunction * > function_registry
Static mapping of functions from their name to the object wrapper of the function.
Definition: value.hpp:80
A Value of a pointer.
Definition: value.hpp:787