53 #include <initializer_list> 63 template <
typename F,
typename Tuple,
bool Done,
int Total,
int... N>
66 static auto call(F f, Tuple && t)
68 return call_impl<F, Tuple, Total == 1 +
sizeof...(N), Total, N...,
sizeof...(N)>::call(f, std::forward<Tuple>(t));
72 template <
typename F,
typename Tuple,
int Total,
int... N>
75 static auto call(F f, Tuple && t)
77 return f(std::get<N>(std::forward<Tuple>(t))...);
85 template <
typename F,
typename Tuple>
88 typedef typename std::decay<Tuple>::type ttype;
102 inline static bool in_register_function=
false;
111 GenFunction(
const std::string& name,
const std::string& impl)
117 std::string& get_name(){
127 std::stringstream code_out(
"");
128 std::string command(
"echo \""+code+
"\" | clang-format");
130 FILE *stream = popen(command.c_str(),
"r");
131 while (fgets(buffer, 255, stream) != NULL)
133 if (pclose(stream) == 0)
134 return code_out.str();
139 static std::string summary(){
140 std::stringstream ss;
141 ss <<
"The following functions have been registered" << std::endl;
142 for(
auto p : function_registry){
143 if (p.second ==
nullptr)
continue;
144 ss <<
"-->" << p.second->name << std::endl;
145 ss << format_code(p.second->impl);
150 template <
typename T>
151 static Function<T>& register_function(
const std::string& name, std::function<T> f,
const std::string& impl){
152 in_register_function =
true;
156 if (func ==
nullptr){
157 ERROR(
"Trying to register function which has already been registered with a different type");
163 in_register_function =
false;
180 template <
typename R,
typename... ArgTypes>
183 std::function<R(ArgTypes...)> f;
186 Function(
const std::string& name,
const std::string& impl, std::function<R(ArgTypes...)> f)
188 if (!in_register_function) {
189 WARNING(
"Don't instantiate Function objects directly! Use GenFunction::register_function instead.");
192 Function(
const std::string& name, std::function<R(ArgTypes...)> f)
196 R operator()(ArgTypes ...args){
203 #define FUNC(f) f, #f 212 typedef std::map<std::string, GenValue*> ValueSet;
229 virtual void _reset() = 0;
237 inline static std::map<const std::string, GenValue*>
values;
245 inline static std::map<const std::string, GenValue*>
aliases;
248 GenValue(
const std::string& name,
const std::string& alias)
252 GenValue::alias(alias,
this);
255 const std::string& get_name(){
259 void set_name(
const std::string& new_name){
260 values[name] =
nullptr;
266 for (
auto val : values){
267 if (val.second !=
nullptr){
268 val.second->_reset();
273 static GenValue* get_value(
const std::string& name){
274 if (aliases[name] !=
nullptr)
275 return aliases[name];
276 else if (values[name] !=
nullptr)
279 ERROR(
"Could not find alias or value \"" << name <<
"\". I'll tell you the ones I know about." << std::endl
281 CRITICAL(
"Aborting... :(",-1);
285 static void alias(
const std::string& name,
GenValue* value){
286 if (aliases[name] !=
nullptr){
287 WARNING(
"WARNING: alias \"" << name <<
"\" overrides previous entry.");
289 aliases[name] = value;
292 static GenValue* alias(
const std::string& name){
293 if (values[name] !=
nullptr){
294 WARNING(
"Alias \"" << name <<
"\" does not exist.");
296 return aliases[name];
299 static std::string summary(){
300 std::stringstream ss;
301 ss <<
"The following values have been created: " << std::endl;
302 for (
auto value : values){
303 if (value.second ==
nullptr)
continue;
304 ss <<
"\t\"" << value.first <<
"\" at address " << value.second << std::endl;
306 ss <<
"And these aliases:" << std::endl;
307 for (
auto alias : aliases){
308 std::string orig(
"VOID");
309 if (alias.second ==
nullptr)
continue;
310 for (
auto value : values){
311 if (alias.second == value.second){
312 orig = value.second->get_name();
316 ss <<
"\t\"" << alias.first <<
"\" referring to \"" << orig <<
"\"" << std::endl;
320 friend std::ostream& operator<<(std::ostream& os,
const GenValue& gv);
322 std::ostream& operator<<(std::ostream& os,
GenValue& gv){
337 template <
typename T>
340 Value(
const std::string& name,
const std::string& alias=
"")
344 virtual T& get_value() = 0;
357 template <
typename T>
364 ObservedValue(
const std::string& name, T* val_ref,
const std::string& alias=
"")
388 template <
typename T>
406 virtual void update_value() = 0;
408 DerivedValue(
const std::string& name,
const std::string& alias=
"")
410 value_valid(
false) { }
432 template <
typename T>
441 this->value.assign(data_ref, data_ref+n);
447 size(size), data(data){ }
453 template <
typename T1,
typename T2>
456 std::pair<Value<T1>*,
Value<T2>* > value_pair;
458 this->value.first = value_pair.first->get_value();
459 this->value.second = value_pair.second->get_value();
465 value_pair(value1, value2){ }
468 template<
typename... T>
class _Zip;
474 return std::numeric_limits<int>::max();
477 std::tuple<> _get_at(
int idx){
478 return std::make_tuple();
481 bool _verify_integrity() {
485 std::string _get_name(){
493 template<
typename Head,
typename... Tail>
494 class _Zip<Head, Tail...> :
private _Zip<Tail...> {
499 int this_size = head->
get_value().size();
501 return std::min(this_size, rest_size);
504 typename std::tuple<Head,Tail...> _get_at(
int idx){
506 return std::tuple_cat(std::make_tuple(head->
get_value()[idx]),tail_tuple);
509 bool _verify_integrity() {
513 std::string _get_name(){
520 _Zip(
Value<std::vector<Head>>* head,
Value<std::vector<Tail>>*... tail)
521 :
_Zip<Tail...>(tail...),
540 template <
typename... ArgTypes>
542 private _Zip<ArgTypes...>{
547 for(
int i=0; i<size; i++){
552 std::string _get_name(){
557 Zip(
Value<std::vector<ArgTypes>>*... args,
const std::string& alias)
558 :
DerivedValue<std::vector<std::tuple<ArgTypes...>>>(
"", alias),
560 this->set_name(_get_name());
564 template<
typename>
class Map;
565 template <
typename Ret,
typename... ArgTypes>
569 Zip<ArgTypes...>* arg;
573 for(
auto tup : arg->get_value()){
574 this->value.push_back(
call(fn,tup));
598 template <
typename R,
typename T>
614 std::tie(n, std::ignore) = std::minmax({v1_val.size(), v2_val.size(), v3_val.size(), v4_val.size()});
615 this->value.resize(n);
616 for (
int i=0; i<n; i++){
617 this->value[i] = f(v1_val[i], v2_val[i], v3_val[i], v4_val[i]);
623 Value<std::vector<T> >* v1,
Value<std::vector<T> >* v2,
624 Value<std::vector<T> >* v3,
Value<std::vector<T> >* v4,
const std::string alias)
626 v3->get_name()+
","+v4->get_name()+
")", alias),
627 f(f), v1(v1), v2(v2), v3(v3), v4(v4) { }
630 const std::string& label1,
const std::string& label2,
631 const std::string& label3,
const std::string& label4,
const std::string alias)
660 selector(selector), v(v) { }
670 template <
typename T>
676 this->value = reduce(v->get_value());
683 Reduce(
Function<T(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias)
684 :
DerivedValue<T>(
"reduceWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
685 reduce(reduce), v(v) { }
691 template <
typename T>
694 Max(
Value<std::vector<T>>* v,
const std::string alias)
695 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"max",
696 FUNC(([](std::vector<T> vec){
697 return *std::max_element(vec.begin(), vec.end());}))),
704 template <
typename T>
707 Min(
Value<std::vector<T>>* v,
const std::string alias)
708 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"min",
709 FUNC(([](std::vector<T> vec){
710 return *std::min_element(vec.begin(), vec.end());}))),
717 template <
typename T>
720 Mean(
Value<std::vector<T>>* v,
const std::string alias)
721 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"mean",
722 FUNC(([](std::vector<T> vec){
723 int n = 0; T sum = 0;
724 for (T e : vec){ n++; sum += e; }
725 return n>0 ? sum / n : 0; }))),
732 template <
typename T>
735 Range(
Value<std::vector<T>>* v,
const std::string alias)
736 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"range",
737 FUNC(([](std::vector<T> vec){
738 auto minmax = std::minmax_element(vec.begin(), vec.end());
739 return (*minmax.second) - (*minmax.first); }))),
746 template <
typename T>
750 :
Reduce<T>(GenFunction::register_function<T(std::vector<T>)>(
"elementOf",
751 FUNC(([index](std::vector<T> vec){
return vec[index->
get_value()];}))),
760 template <
typename T>
767 this->value = reduce(v->get_value());
771 ReduceIndex(
Function<std::pair<T,int>(std::vector<T>)>& reduce,
Value<std::vector<T> >* v,
const std::string alias=
"")
772 :
DerivedValue<T>(
"reduceIndexWith("+reduce.get_name()+
":"+v->get_name()+
")", alias),
773 reduce(reduce), v(v) { }
779 template <
typename T>
782 MaxIndex(
Value<std::vector<T>>* v,
const std::string alias=
"")
783 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"maxIndex",
784 FUNC(([](std::vector<T> vec){
785 auto elptr = std::max_element(vec.begin(), vec.end());
786 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
793 template <
typename T>
796 MinIndex(
Value<std::vector<T>>* v,
const std::string alias=
"")
797 :
ReduceIndex<T>(GenFunction::register_function<T(std::vector<T>)>(
"minIndex",
798 FUNC(([](std::vector<T> vec){
799 auto elptr = std::min_element(vec.begin(), vec.end());
800 return std::pair<T,int>(*elptr, int(elptr-vec.begin())); }))),
809 template <
typename T>
826 template <
typename T>
832 PointerValue(
const std::string& name, T* ptr,
const std::string alias=
"")
841 template <
typename T>
847 ConstantValue(
const std::string& name, T const_value,
const std::string alias=
"")
Definition: value.hpp:213
void update_value()
Updates the internal value.
Definition: value.hpp:813
Find and return the maximum value of a vector.
Definition: value.hpp:692
Zips a series of vectors together.
Definition: value.hpp:541
Reduce a Value of type vector<T> to just a T.
Definition: value.hpp:671
void update_value()
Updates the internal value.
Definition: value.hpp:649
void update_value()
Updates the internal value.
Definition: value.hpp:457
Find and return the minimum value of a vector and its index.
Definition: value.hpp:794
Definition: value.hpp:468
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:391
A generic value owning only a function object.
Definition: value.hpp:810
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:412
Calculate the range of the values in a vector.
Definition: value.hpp:733
A std::vector wrapper around a C-style array.
Definition: value.hpp:433
Similar to Reduce, but returns a pair of a T and an int.
Definition: value.hpp:761
std::string name
The name of the value.
Definition: value.hpp:220
void update_value()
Updates the internal value.
Definition: value.hpp:607
Returns the count of elements in the input vector passing a test function.
Definition: value.hpp:644
Find and return the maximum value of a vector and its index.
Definition: value.hpp:780
void update_value()
Updates the internal value.
Definition: value.hpp:675
Parent class to all Function classes.
Definition: value.hpp:97
void _reset()
Mark the internal value as invalid.
Definition: value.hpp:361
Takes a set of four Value<std::vector<T> > objects and a function of four Ts and returns a std::vecto...
Definition: value.hpp:599
static std::map< const std::string, GenValue * > aliases
Composite value names are typically nested.
Definition: value.hpp:245
A generic, observed, value.
Definition: value.hpp:358
Find and return the minimum value of a vector.
Definition: value.hpp:705
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:454
T & get_value()
Calculate, if necessary, and return the value held by this object.
Definition: value.hpp:367
void update_value()
Updates the internal value.
Definition: value.hpp:766
void update_value()
Updates the internal value.
Definition: value.hpp:571
void update_value()
Updates the internal value.
Definition: value.hpp:844
The namespace containing all filval classes and functions.
Definition: api.hpp:6
Extract the element at a specific index from a vector.
Definition: value.hpp:747
Calculate the mean value of a vector.
Definition: value.hpp:718
void update_value()
Updates the internal value.
Definition: value.hpp:438
A generic value.
Definition: value.hpp:338
void update_value()
Updates the internal value.
Definition: value.hpp:544
static std::map< const std::string, GenValue * > values
A static mapping containing all created Value objects.
Definition: value.hpp:237
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:126
Definition: value.hpp:564
A generic, derived, value.
Definition: value.hpp:389
void update_value()
Updates the internal value.
Definition: value.hpp:829
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:842
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:109
A Value of a pointer.
Definition: value.hpp:827
auto call(F f, Tuple &&t)
This calls a function of type F with the contents of the tuple as arguments.
Definition: value.hpp:86