api.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef API_HPP
  2. #define API_HPP
  3. #include <string>
  4. #include <vector>
  5. #include "filval/value.hpp"
  6. namespace fv{
  7. template<typename T>
  8. Value<T>* lookup(const std::string& name){
  9. GenValue* gv = GenValue::get_value(name);
  10. Value<T>* tv = dynamic_cast<Value<T>*>(gv);
  11. if(tv == nullptr){
  12. CRITICAL("Value: \""+gv->get_name() + "\" has improper type.",-1);
  13. }
  14. return tv;
  15. }
  16. ObsFilter* lookup_obs_filter(const std::string& name){
  17. ObsFilter* f = dynamic_cast<ObsFilter*>(GenValue::get_value(name));
  18. if(f == nullptr){
  19. CRITICAL("ObsFilter: "+f->get_name() + "has improper type.",-1);
  20. }
  21. return f;
  22. }
  23. template <typename... ArgTypes>
  24. Value<std::vector<std::tuple<ArgTypes...>>>*
  25. zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias=""){
  26. return new Zip<ArgTypes...>(args..., alias);
  27. }
  28. template <typename Ret, typename... ArgTypes>
  29. Map<Ret(ArgTypes...)>*
  30. map(Function<Ret(ArgTypes...)>& fn,
  31. Value<std::vector<std::tuple<ArgTypes...>>>* arg, const std::string& alias=""){
  32. return new Map<Ret(ArgTypes...)>(fn, arg, alias);
  33. }
  34. template <typename... ArgTypes>
  35. Tuple<ArgTypes...>*
  36. tuple(Value<ArgTypes>*... args, const std::string& alias=""){
  37. return new Tuple<ArgTypes...>(args..., alias);
  38. }
  39. template <typename Ret, typename... ArgTypes>
  40. Apply<Ret(ArgTypes...)>*
  41. apply(Function<Ret(ArgTypes...)>& fn,
  42. Tuple<ArgTypes...>* arg, const std::string& alias=""){
  43. return new Apply<Ret(ArgTypes...)>(fn, arg, alias);
  44. }
  45. template <typename T1, typename T2>
  46. Pair<T1, T2>*
  47. pair(Value<T1>* val1, Value<T2>* val2, const std::string& alias=""){
  48. return new Pair<T1,T2>(val1, val2, alias);
  49. }
  50. template <typename T1, typename T2>
  51. Pair<T1, T2>*
  52. pair(const std::string& name1, const std::string& name2, const std::string& alias=""){
  53. return pair<T1,T2>(lookup<T1>(name1), lookup<T2>(name2), alias);
  54. }
  55. template <typename T>
  56. Max<T>*
  57. max(Value<std::vector<T>>* v, const std::string alias){
  58. return new Max<T>(v, alias);
  59. }
  60. template <typename T>
  61. Max<T>*
  62. max(const std::string& v_name, const std::string alias){
  63. return max(lookup<std::vector<T>>(v_name), alias);
  64. }
  65. template <typename T>
  66. Min<T>*
  67. min(Value<std::vector<T>>* v, const std::string alias){
  68. return new Min<T>(v, alias);
  69. }
  70. template <typename T>
  71. Min<T>*
  72. min(const std::string& v_name, const std::string alias){
  73. return min(lookup<std::vector<T>>(v_name), alias);
  74. }
  75. template <typename T>
  76. Range<T>*
  77. range(Value<std::vector<T>>* v, const std::string alias){
  78. return new Range<T>(v, alias);
  79. }
  80. template <typename T>
  81. Range<T>*
  82. range(const std::string& v_name, const std::string alias){
  83. return range(lookup<std::vector<T>>(v_name), alias);
  84. }
  85. template <typename T>
  86. Mean<T>*
  87. mean(Value<std::vector<T>>* v, const std::string alias){
  88. return new Mean<T>(v, alias);
  89. }
  90. template <typename T>
  91. Mean<T>*
  92. mean(const std::string& v_name, const std::string alias){
  93. return mean(lookup<std::vector<T>>(v_name), alias);
  94. }
  95. template <typename T>
  96. Count<T>*
  97. count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string alias){
  98. return new Count<T>(selector, v, alias);
  99. }
  100. template <typename T>
  101. Count<T>*
  102. count(Function<bool(T)>& selector, const std::string& v_name, const std::string alias){
  103. return count<T>(selector, lookup<std::vector<T>>(v_name), alias);
  104. }
  105. template <typename FST, typename SND>
  106. Value<std::vector<std::tuple<FST,SND>>>*
  107. cart_product(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string alias){
  108. return new CartProduct<FST, SND>(val1, val2, alias);
  109. }
  110. template <typename FST, typename SND>
  111. Value<std::vector<std::tuple<FST,SND>>>*
  112. cart_product(const std::string& val1_name, const std::string& val2_name, const std::string alias = ""){
  113. return cart_product<FST,SND>(lookup<std::vector<FST>>(val1_name), lookup<std::vector<SND>>(val2_name), alias);
  114. }
  115. ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
  116. return new ObsFilter(name, filter_function, impl);
  117. }
  118. }
  119. #endif // API_HPP