52 #include <initializer_list> 73 inline static bool in_register_function=
false;
82 GenFunction(
const std::string& name,
const std::string& impl)
88 std::string& get_name(){
98 std::stringstream code_out(
"");
99 std::string command(
"echo \""+code+
"\" | clang-format");
101 FILE *stream = popen(command.c_str(),
"r");
102 while (fgets(buffer, 255, stream) != NULL)
104 if (pclose(stream) == 0)
105 return code_out.str();
110 static std::string summary(){
111 std::stringstream ss;
112 ss <<
"The following functions have been registered" << std::endl;
113 for(
auto p : function_registry){
114 if (p.second ==
nullptr)
continue;
115 ss <<
"-->" << p.second->name << std::endl;
121 template <
typename T>
122 static Function<T>& register_function(
const std::string& name, std::function<T> f,
const std::string& impl){
123 in_register_function =
true;
127 if (func ==
nullptr){
128 ERROR(
"Trying to register function which has already been registered with a different type");
134 in_register_function =
false;
151 template <
typename R,
typename... ArgTypes>
154 std::function<R(ArgTypes...)> f;
157 Function(
const std::string& name,
const std::string& impl, std::function<R(ArgTypes...)> f)
159 if (!in_register_function) {
160 WARNING(
"Don't instantiate Function objects directly! Use GenFunction::register_function instead.");
163 Function(
const std::string& name, std::function<R(ArgTypes...)> f)
167 R operator()(ArgTypes ...args){
174 #define FUNC(f) f, #f 183 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;
217 GenValue(
const std::string& name,
const std::string& alias)
221 GenValue::alias(alias,
this);
224 const std::string& get_name(){
229 for (
auto val : values){
230 val.second->_reset();
234 static GenValue* get_value(
const std::string& name){
235 if (aliases[name] !=
nullptr)
236 return aliases[name];
237 else if (values[name] !=
nullptr)
240 ERROR(
"Could not find alias or value \"" << name <<
"\". I'll tell you the ones I know about." << std::endl
242 CRITICAL(
"Aborting... :(", -1);
246 static void alias(
const std::string& name,
GenValue* value){
247 if (aliases[name] !=
nullptr){
248 WARNING(
"WARNING: alias \"" << name <<
"\" overrides previous entry.");
250 aliases[name] = value;
253 static GenValue* alias(
const std::string& name){
254 if (values[name] !=
nullptr){
255 WARNING(
"Alias \"" << name <<
"\" does not exist.");
257 return aliases[name];
260 static std::string summary(){
261 std::stringstream ss;
262 ss <<
"The following values have been created: " << std::endl;
263 for (
auto value : values){
264 if (value.second ==
nullptr)
continue;
265 ss <<
"\t\"" << value.first <<
"\" at address " << value.second << std::endl;
267 ss <<
"And these aliases:" << std::endl;
268 for (
auto alias : aliases){
269 std::string orig(
"VOID");
270 if (alias.second ==
nullptr)
continue;
271 for (
auto value : values){
272 if (alias.second == value.second){
273 orig = value.second->get_name();
277 ss <<
"\t\"" << alias.first <<
"\" referring to \"" << orig <<
"\"" << std::endl;
281 friend std::ostream& operator<<(std::ostream& os,
const GenValue& gv);
283 std::ostream& operator<<(std::ostream& os,
GenValue& gv){
298 template <
typename T>
301 Value(
const std::string& name,
const std::string& alias=
"")
305 virtual T& get_value() = 0;
318 template <
typename T>
324 ObservedValue(
const std::string& name, T* val_ref,
const std::string& alias=
"")
348 template <
typename T>
366 virtual void update_value() = 0;
368 DerivedValue(
const std::string& name,
const std::string& alias=
"")
370 value_valid(
false) { }
391 template <
typename T>
400 this->value.assign(data_ref, data_ref+n);
406 size(size), data(data){ }
408 WrapperVector(
const std::string &label_size,
const std::string &label_data,
const std::string& alias=
"")
410 dynamic_cast<Value<T*>*
>(GenValue::get_value(label_data)), alias) { }
416 template <
typename T1,
typename T2>
419 std::pair<Value<T1>*,
Value<T2>* > value_pair;
421 this->value.first = value_pair.first->get_value();
422 this->value.second = value_pair.second->get_value();
427 value_pair(value1, value2){ }
428 Pair(
const std::string& label1,
const std::string& label2,
const std::string alias=
"")
430 dynamic_cast<Value<T1>*
>(GenValue::get_value(label2)),
447 template <
typename R,
typename T>
463 std::tie(n, std::ignore) = std::minmax({v1_val.size(), v2_val.size(), v3_val.size(), v4_val.size()});
464 this->value.resize(n);
465 for (
int i=0; i<n; i++){
466 this->value[i] = f(v1_val[i], v2_val[i], v3_val[i], v4_val[i]);
472 Value<std::vector<T> >* v1,
Value<std::vector<T> >* v2,
473 Value<std::vector<T> >* v3,
Value<std::vector<T> >* v4,
const std::string alias=
"")
475 v3->get_name()+
","+v4->get_name()+
")", alias),
476 f(f), v1(v1), v2(v2), v3(v3), v4(v4) { }
479 const std::string& label1,
const std::string& label2,
480 const std::string& label3,
const std::string& label4,
const std::string alias=
"")
505 Count(
Function<
bool(T)>& selector,
Value<std::vector<T>>* v,
const std::string alias=
"")
507 selector(selector), v(v) { }
509 Count(
Function<
bool(T)>& selector,
const std::string& v_name,
const std::string alias=
"")
520 template <
typename T>
529 Reduce(
Function<T(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias=
"")
530 :
DerivedValue<T>(
"reduceWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
531 reduce(reduce), v(v) { }
533 Reduce(
Function<T(std::vector<T>)>& reduce,
const std::string& v_name,
const std::string alias=
"")
540 template <
typename T>
543 Max(
const std::string& v_name,
const std::string alias=
"")
544 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"max",
545 FUNC(([](std::vector<T> vec){
546 return *std::max_element(vec.begin(), vec.end());}))),
553 template <
typename T>
556 Min(
const std::string& v_name,
const std::string alias=
"")
557 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"min",
558 FUNC(([](std::vector<T> vec){
559 return *std::min_element(vec.begin(), vec.end());}))),
566 template <
typename T>
569 Mean(
const std::string& v_name,
const std::string alias=
"")
570 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"mean",
571 FUNC(([](std::vector<T> vec){
572 int n = 0; T sum = 0;
573 for (T e : vec){ n++; sum += e; }
574 return n>0 ? sum / n : 0; }))),
581 template <
typename T>
584 Range(
const std::string& v_name,
const std::string alias=
"")
585 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"range",
586 FUNC(([](std::vector<T> vec){
587 auto minmax = std::minmax_element(vec.begin(), vec.end());
588 return (*minmax.second) - (*minmax.first); }))),
595 template <
typename T>
599 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"elementOf",
600 FUNC(([index](std::vector<T> vec){
return vec[index->
get_value()];}))),
602 ElementOf(
const std::string& name,
int index,
const std::string& v_name,
const std::string alias=
"")
603 :
Reduce<T>(name, [index](std::vector<T> vec){
return vec[index];}, v_name, alias) { }
611 template <
typename T>
617 this->value = reduce(v->get_value());
620 ReduceIndex(
Function<std::pair<T,int>(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias=
"")
621 :
DerivedValue<T>(
"reduceIndexWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
622 reduce(reduce), v(v) { }
624 ReduceIndex(
Function<std::pair<T,int>(std::vector<T>)>& reduce,
const std::string& v_name,
const std::string alias=
"")
631 template <
typename T>
634 MaxIndex(
const std::string& v_name,
const std::string alias=
"")
635 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"maxIndex",
636 FUNC(([](std::vector<T> vec){
637 auto elptr = std::max_element(vec.begin(), vec.end());
638 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
639 ))), v_name, alias) { }
645 template <
typename T>
648 MinIndex(
const std::string& v_name,
const std::string alias=
"")
649 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"minIndex",
650 FUNC(([](std::vector<T> vec){
651 auto elptr = std::min_element(vec.begin(), vec.end());
652 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }
653 ))), v_name, alias) { }
661 template <
typename T>
678 template <
typename T>
683 PointerValue(
const std::string& name, T* ptr,
const std::string alias=
"")
692 template <
typename T>
697 this->value = const_value;
700 ConstantValue(
const std::string& name, T const_value,
const std::string alias=
"")
702 const_value(const_value) { }
Definition: value.hpp:184
void update_value()
Updates the internal value.
Definition: value.hpp:665
Find and return the maximum value of a vector.
Definition: value.hpp:541
Reduce a Value of type vector<T> to just a T.
Definition: value.hpp:521
void update_value()
Updates the internal value.
Definition: value.hpp:497
void update_value()
Updates the internal value.
Definition: value.hpp:420
Find and return the minimum value of a vector and its index.
Definition: value.hpp:646
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:351
A generic value owning only a function object.
Definition: value.hpp:662
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:372
Calculate the range of the values in a vector.
Definition: value.hpp:582
A std::vector wrapper around a C-style array.
Definition: value.hpp:392
Similar to Reduce, but returns a pair of a T and an int.
Definition: value.hpp:612
std::string name
The name of the value.
Definition: value.hpp:191
void update_value()
Updates the internal value.
Definition: value.hpp:456
Definition: value.hpp:493
Find and return the maximum value of a vector and its index.
Definition: value.hpp:632
void update_value()
Updates the internal value.
Definition: value.hpp:525
Parent class to all Function classes.
Definition: value.hpp:68
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:322
Takes a set of four Value<std::vector<T> > objects and a function of four Ts and returns a std::vecto...
Definition: value.hpp:448
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:319
Find and return the minimum value of a vector.
Definition: value.hpp:554
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:417
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:327
void update_value()
Updates the internal value.
Definition: value.hpp:616
void update_value()
Updates the internal value.
Definition: value.hpp:696
The namespace containing all filval classes and functions.
Extract the element at a specific index from a vector.
Definition: value.hpp:596
Calculate the mean value of a vector.
Definition: value.hpp:567
void update_value()
Updates the internal value.
Definition: value.hpp:397
A generic value.
Definition: value.hpp:299
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:97
A generic, derived, value.
Definition: value.hpp:349
void update_value()
Updates the internal value.
Definition: value.hpp:681
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
A Value which always returns the same value, supplied in the constructor.
Definition: value.hpp:693
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:679