TTTT Analysis  0.1
api.hpp
Go to the documentation of this file.
1 
41 #ifndef API_HPP
42 #define API_HPP
43 #include <string>
44 #include <vector>
45 #include "filval/value.hpp"
46 namespace fv{
47 
48  template<typename T>
49  Value<T>* lookup(const std::string& name){
50  Value<T>* tv = GenValue::get_value<T>(name);
51  if (tv == nullptr){
52  CRITICAL("Could not find alias or value \"" << name << "\"."
53  <<" I'll tell you the ones I know about." << std::endl
54  << GenValue::summary(), -1);
55  }
56  return tv;
57  }
58 
59  template<typename T>
60  bool check_exists(const std::string name){
61  Value<T>* tv = GenValue::get_value<T>(name);
62  return tv != nullptr;
63  }
64 
65  ObsFilter* lookup_obs_filter(const std::string& name){
66  ObsFilter* f = dynamic_cast<ObsFilter*>(GenValue::get_value<bool>(name));
67  if(f == nullptr){
68  CRITICAL("ObsFilter: "+f->get_name() + "has improper type.",-1);
69  }
70  return f;
71  }
72 
73  template <typename T>
74  decltype(auto)
75  wrapper_vector(Value<int>* size, Value<T*>* data, const std::string& alias=""){
76  typedef std::vector<T> type;
77  const std::string& name = WrapperVector<T>::fmt_name(size, data);
78  if (check_exists<type>(name))
79  return lookup<type>(name);
80  else
81  return (Value<type>*)new WrapperVector<T>(size, data, alias);
82  }
83 
84  template <typename... ArgTypes>
85  decltype(auto)
86  zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias=""){
87  typedef std::vector<std::tuple<ArgTypes...>> type;
88  std::string& name = Zip<ArgTypes...>::fmt_name(args...);
89  if (check_exists<type>(name))
90  return lookup<type>(name);
91  else
92  return (Value<type>*)new Zip<ArgTypes...>(args..., alias);
93  }
94 
95  template <typename Ret, typename... ArgTypes>
96  decltype(auto)
97  map(Function<Ret(ArgTypes...)>& fn, Value<std::vector<std::tuple<ArgTypes...>>>* arg, const std::string& alias=""){
98  typedef std::vector<Ret> type;
99  const std::string& name = Map<Ret(ArgTypes...)>::fmt_name(fn, arg);
100  if (check_exists<type>(name))
101  return lookup<type>(name);
102  else
103  return (Value<type>*)new Map<Ret(ArgTypes...)>(fn, arg, alias);
104  }
105 
106  template <typename... ArgTypes>
107  decltype(auto)
108  tuple(Value<ArgTypes>*... args){
109  typedef std::tuple<ArgTypes...> type;
110  const std::string& name = Tuple<ArgTypes...>::fmt_name(args...);
111  if (check_exists<type>(name))
112  return lookup<type>(name);
113  else
114  return (Value<type>*)new Tuple<ArgTypes...>(args..., "");
115  }
116 
117  template <size_t N, typename... ArgTypes>
118  decltype(auto)
119  detup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias=""){
120  typedef typename std::tuple_element<N, std::tuple<ArgTypes...>>::type type;
121  const std::string& name = DeTup<N, ArgTypes...>::fmt_name(tup);
122  if (check_exists<type>(name))
123  return lookup<type>(name);
124  else
125  return (Value<type>*)new DeTup<N, ArgTypes...>(tup, alias);
126  }
127 
128  template <size_t N, typename... ArgTypes>
129  decltype(auto)
130  detup_vec(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias=""){
131  typedef std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type> type;
132  const std::string& name = DeTupVector<N, ArgTypes...>::fmt_name(tup);
133  if (check_exists<type>(name))
134  return lookup<type>(name);
135  else
136  return (Value<type>*)new DeTupVector<N, ArgTypes...>(tup, alias);
137  }
138 
139  template <typename Ret, typename... ArgTypes>
140  decltype(auto)
141  apply(Function<Ret(ArgTypes...)>& fn, Value<std::tuple<ArgTypes...>>* arg, const std::string& alias=""){
142  typedef Ret type;
143  const std::string& name = Apply<Ret(ArgTypes...)>::fmt_name(fn, arg);
144  if (check_exists<type>(name))
145  return lookup<type>(name);
146  else
147  return (Value<type>*)new Apply<Ret(ArgTypes...)>(fn, arg, alias);
148  }
149 
150  template <typename T1, typename T2>
151  decltype(auto)
152  pair(Value<T1>* val1, Value<T2>* val2, const std::string& alias=""){
153  typedef std::pair<T1,T2> type;
154  const std::string& name = Pair<T1,T2>::fmt_name(val1, val2);
155  if (check_exists<type>(name))
156  return lookup<type>(name);
157  else
158  return (Value<type>*)new Pair<T1,T2>(val1, val2, alias);
159  }
160 
161  template <typename T1, typename T2>
162  decltype(auto)
163  pair(const std::string& name1, const std::string& name2, const std::string& alias=""){
164  return pair<T1,T2>(lookup<T1>(name1), lookup<T2>(name2), alias);
165  }
166 
167  template <typename T>
168  decltype(auto)
169  reduce(Function<T(std::vector<T>)>& fn, Value<std::vector<T>>* v, const std::string& alias=""){
170  typedef T type;
171  const std::string& name = Reduce<T>::fmt_name(fn, v);
172  if (check_exists<type>(name))
173  return lookup<type>(name);
174  else
175  return (Value<type>*)new Reduce<T>(fn, v, alias);
176  }
177 
178  template <typename T>
179  decltype(auto)
180  max(Value<std::vector<T>>* v, const std::string& alias=""){
181  typedef T type;
182  const std::string& name = Max<T>::fmt_name(v);
183  if (check_exists<type>(name))
184  return lookup<type>(name);
185  else
186  return (Value<type>*)new Max<T>(v, alias);
187  }
188 
189  template <typename T>
190  decltype(auto)
191  max(const std::string& v_name, const std::string& alias=""){
192  return max(lookup<std::vector<T>>(v_name), alias);
193  }
194 
195  template <typename T>
196  decltype(auto)
197  min(Value<std::vector<T>>* v, const std::string& alias=""){
198  typedef T type;
199  const std::string& name = Min<T>::fmt_name(v);
200  if (check_exists<type>(name))
201  return lookup<type>(name);
202  else
203  return (Value<type>*)new Min<T>(v, alias);
204  }
205 
206  template <typename T>
207  decltype(auto)
208  min(const std::string& v_name, const std::string& alias=""){
209  return min(lookup<std::vector<T>>(v_name), alias);
210  }
211 
212  template <typename T>
213  decltype(auto)
214  range(Value<std::vector<T>>* v, const std::string& alias=""){
215  typedef T type;
216  const std::string& name = Range<T>::fmt_name(v);
217  if (check_exists<type>(name))
218  return lookup<type>(name);
219  else
220  return (Value<type>*)new Range<T>(v, alias);
221  }
222 
223  template <typename T>
224  decltype(auto)
225  range(const std::string& v_name, const std::string& alias=""){
226  return range(lookup<std::vector<T>>(v_name), alias);
227  }
228 
229  template <typename T>
230  decltype(auto)
231  mean(Value<std::vector<T>>* v, const std::string& alias=""){
232  typedef T type;
233  const std::string& name = Mean<T>::fmt_name(v);
234  if (check_exists<type>(name))
235  return lookup<type>(name);
236  else
237  return (Value<type>*)new Mean<T>(v, alias);
238  }
239 
240  template <typename T>
241  decltype(auto)
242  mean(const std::string& v_name, const std::string& alias=""){
243  return mean(lookup<std::vector<T>>(v_name), alias);
244  }
245 
246  template <typename T>
247  decltype(auto)
248  count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string& alias=""){
249  typedef int type;
250  const std::string& name = Count<T>::fmt_name(selector, v);
251  if (check_exists<type>(name))
252  return lookup<type>(name);
253  else
254  return (Value<type>*)new Count<T>(selector, v, alias);
255  }
256 
257  template <typename T>
258  decltype(auto)
259  count(Function<bool(T)>& selector, const std::string& v_name, const std::string& alias=""){
260  return count<T>(selector, lookup<std::vector<T>>(v_name), alias);
261  }
262 
263  template <typename FST, typename SND>
264  decltype(auto)
265  cart_product(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string& alias=""){
266  typedef std::vector<std::tuple<FST,SND>> type;
267  const std::string& name = CartProduct<FST, SND>::fmt_name(val1, val2);
268  if (check_exists<type>(name))
269  return lookup<type>(name);
270  else
271  return (Value<type>*)new CartProduct<FST, SND>(val1, val2, alias);
272  }
273 
274  template <typename FST, typename SND>
275  decltype(auto)
276  cart_product(const std::string& val1_name, const std::string& val2_name, const std::string& alias=""){
277  return cart_product<FST,SND>(lookup<std::vector<FST>>(val1_name), lookup<std::vector<SND>>(val2_name), alias);
278  }
279 
280  template <typename T, int Size>
281  decltype(auto)
282  combinations(Value<std::vector<T>>* val, const std::string& alias=""){
283  typedef std::vector<typename HomoTuple<T,Size>::type> type;
284  const std::string& name = Combinations<T, Size>::fmt_name(val);
285  if (check_exists<type>(name))
286  return lookup<type>(name);
287  else
288  return (Value<type>*)new Combinations<T, Size>(val, alias);
289  }
290 
291  template <typename T, int Size>
292  decltype(auto)
293  combinations(const std::string& val_name, const std::string alias = ""){
294  return combinations<T, Size>(lookup<std::vector<T>>(val_name), alias);
295  }
296 
297  template < typename T>
298  decltype(auto)
299  constant(const std::string name, T const_value, const std::string alias=""){
300  typedef T type;
301  const std::string& val_name = ConstantValue<T>::fmt_name(name);
302  if (check_exists<type>(val_name))
303  return lookup<type>(val_name);
304  else
305  return (Value<type>*)new ConstantValue<T>(val_name, const_value, alias);
306  }
307 
308  template <typename T>
309  decltype(auto)
310  bound(Function<T()>& f, const std::string alias=""){
311  typedef T type;
312  const std::string& name = BoundValue<T>::fmt_name(f);
313  if (check_exists<type>(name))
314  return lookup<type>(name);
315  else
316  return (Value<type>*)new BoundValue<T>(f, alias);
317  }
318 
319  template <typename T>
320  decltype(auto)
321  filter(Function<bool(T)>& filter, Value<std::vector<T>>* val, const std::string alias=""){
322  typedef std::vector<T> type;
323  const std::string& name = Filter<T>::fmt_name(filter, val);
324  if (check_exists<type>(name))
325  return lookup<type>(name);
326  else
327  return (Value<type>*)new Filter<T>(filter, val, alias);
328  }
329 
330  template <typename T>
331  decltype(auto)
332  filter(Function<bool(T)>& filter_func, const std::string& val_name, const std::string alias=""){
333  return filter<T>(filter_func, lookup<std::vector<T>>(val_name), alias);
334  }
335 
336  template <typename... ArgTypes>
337  decltype(auto)
338  tup_filter(Function<bool(ArgTypes...)>& filter, Value<std::vector<std::tuple<ArgTypes...>>>* val, const std::string alias=""){
339  typedef std::vector<std::tuple<ArgTypes...>> type;
340  const std::string& name = TupFilter<ArgTypes...>::fmt_name(filter, val);
341  if (check_exists<type>(name))
342  return lookup<type>(name);
343  else
344  return (Value<type>*)new TupFilter<ArgTypes...>(filter, val, alias);
345  }
346 
347  template <typename... ArgTypes>
348  decltype(auto)
349  tup_filter(Function<bool(ArgTypes...)>& filter, const std::string& val_name, const std::string alias=""){
350  return tup_filter<ArgTypes...>(filter, lookup<std::vector<std::tuple<ArgTypes...>>>(val_name), alias);
351  }
352 
353  ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
354  return new ObsFilter(name, filter_function, impl);
355  }
356 }
357 #endif // API_HPP
Find and return the maximum value of a vector.
Definition: value.hpp:976
Zips a series of vectors together.
Definition: value.hpp:662
Reduce a Value of type vector<T> to just a T.
Definition: value.hpp:951
Find combinations of items from an input vector.
Definition: value.hpp:1108
A generic value owning only a function object.
Definition: value.hpp:1191
Calculate the range of the values in a vector.
Definition: value.hpp:1029
Takes a series of Value objects and bundles them together into a std::tuple object.
Definition: value.hpp:770
A std::vector wrapper around a C-style array.
Definition: value.hpp:539
Returns the count of elements in the input vector passing a test function.
Definition: value.hpp:864
Returns the elements in a vector that pass a test function.
Definition: value.hpp:891
Gets the Nth element from a tuple value.
Definition: value.hpp:791
Find and return the minimum value of a vector.
Definition: value.hpp:993
Creates a vector of extracting the Nth value from each entry in a vector of tuples.
Definition: value.hpp:813
Creates a std::pair type from a two other Value objects.
Definition: value.hpp:564
The namespace containing all filval classes and functions.
Definition: api.hpp:46
Returns the elements in a vector that pass a test function.
Definition: value.hpp:920
Calculate the mean value of a vector.
Definition: value.hpp:1010
A templated value.
Definition: value.hpp:265
Calculate the cartesian product of two input vectors.
Definition: value.hpp:1151
A Value which always returns the same value, supplied in the constructor.
Definition: value.hpp:1228