api.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. */
  33. #ifndef API_HPP
  34. #define API_HPP
  35. #include <string>
  36. #include <vector>
  37. #include "filval/value.hpp"
  38. namespace fv{
  39. template<typename T>
  40. Value<T>* lookup(const std::string& name){
  41. GenValue* gv = GenValue::get_value(name);
  42. Value<T>* tv = dynamic_cast<Value<T>*>(gv);
  43. if(tv == nullptr){
  44. CRITICAL("Value: \""+gv->get_name() + "\" has improper type.",-1);
  45. }
  46. return tv;
  47. }
  48. ObsFilter* lookup_obs_filter(const std::string& name){
  49. ObsFilter* f = dynamic_cast<ObsFilter*>(GenValue::get_value(name));
  50. if(f == nullptr){
  51. CRITICAL("ObsFilter: "+f->get_name() + "has improper type.",-1);
  52. }
  53. return f;
  54. }
  55. template <typename... ArgTypes>
  56. Value<std::vector<std::tuple<ArgTypes...>>>*
  57. zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias=""){
  58. return new Zip<ArgTypes...>(args..., alias);
  59. }
  60. template <typename Ret, typename... ArgTypes>
  61. Value<std::vector<Ret>>*
  62. map(Function<Ret(ArgTypes...)>& fn,
  63. Value<std::vector<std::tuple<ArgTypes...>>>* arg, const std::string& alias=""){
  64. return new Map<Ret(ArgTypes...)>(fn, arg, alias);
  65. }
  66. template <typename... ArgTypes>
  67. Value<std::tuple<ArgTypes...>>*
  68. tuple(Value<ArgTypes>*... args){
  69. return new Tuple<ArgTypes...>(args..., "");
  70. }
  71. template <size_t N, typename... ArgTypes>
  72. Value<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>*
  73. detup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias=""){
  74. return new DeTup<N, ArgTypes...>(tup, alias);
  75. }
  76. template <size_t N, typename... ArgTypes>
  77. Value<std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type>>*
  78. detup_vec(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias=""){
  79. return new DeTupVector<N, ArgTypes...>(tup, alias);
  80. }
  81. template <typename Ret, typename... ArgTypes>
  82. Value<Ret>*
  83. apply(Function<Ret(ArgTypes...)>& fn,
  84. Value<std::tuple<ArgTypes...>>* arg, const std::string& alias=""){
  85. return new Apply<Ret(ArgTypes...)>(fn, arg, alias);
  86. }
  87. template <typename T1, typename T2>
  88. Value<std::pair<T1,T2>>*
  89. pair(Value<T1>* val1, Value<T2>* val2, const std::string& alias=""){
  90. return new Pair<T1,T2>(val1, val2, alias);
  91. }
  92. template <typename T1, typename T2>
  93. Value<std::pair<T1,T2>>*
  94. pair(const std::string& name1, const std::string& name2, const std::string& alias=""){
  95. return pair<T1,T2>(lookup<T1>(name1), lookup<T2>(name2), alias);
  96. }
  97. template <typename T>
  98. Value<T>*
  99. max(Value<std::vector<T>>* v, const std::string& alias=""){
  100. return new Max<T>(v, alias);
  101. }
  102. template <typename T>
  103. Value<T>*
  104. max(const std::string& v_name, const std::string& alias=""){
  105. return max(lookup<std::vector<T>>(v_name), alias);
  106. }
  107. template <typename T>
  108. Value<T>*
  109. min(Value<std::vector<T>>* v, const std::string& alias=""){
  110. return new Min<T>(v, alias);
  111. }
  112. template <typename T>
  113. Value<T>*
  114. min(const std::string& v_name, const std::string& alias=""){
  115. return min(lookup<std::vector<T>>(v_name), alias);
  116. }
  117. template <typename T>
  118. Value<T>*
  119. range(Value<std::vector<T>>* v, const std::string& alias=""){
  120. return new Range<T>(v, alias);
  121. }
  122. template <typename T>
  123. Value<T>*
  124. range(const std::string& v_name, const std::string& alias=""){
  125. return range(lookup<std::vector<T>>(v_name), alias);
  126. }
  127. template <typename T>
  128. Value<T>*
  129. mean(Value<std::vector<T>>* v, const std::string& alias=""){
  130. return new Mean<T>(v, alias);
  131. }
  132. template <typename T>
  133. Value<T>*
  134. mean(const std::string& v_name, const std::string& alias=""){
  135. return mean(lookup<std::vector<T>>(v_name), alias);
  136. }
  137. template <typename T>
  138. Value<int>*
  139. count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string& alias=""){
  140. return new Count<T>(selector, v, alias);
  141. }
  142. template <typename T>
  143. Value<int>*
  144. count(Function<bool(T)>& selector, const std::string& v_name, const std::string& alias=""){
  145. return count<T>(selector, lookup<std::vector<T>>(v_name), alias);
  146. }
  147. template <typename FST, typename SND>
  148. Value<std::vector<std::tuple<FST,SND>>>*
  149. cart_product(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string& alias=""){
  150. return new CartProduct<FST, SND>(val1, val2, alias);
  151. }
  152. template <typename FST, typename SND>
  153. Value<std::vector<std::tuple<FST,SND>>>*
  154. cart_product(const std::string& val1_name, const std::string& val2_name, const std::string& alias=""){
  155. return cart_product<FST,SND>(lookup<std::vector<FST>>(val1_name), lookup<std::vector<SND>>(val2_name), alias);
  156. }
  157. template <typename T, int Size>
  158. Value<std::vector<typename HomoTuple<T,Size>::type>>*
  159. combinations(Value<std::vector<T>>* val, const std::string& alias=""){
  160. return new Combinations<T, Size>(val, alias);
  161. }
  162. template <typename T, int Size>
  163. Value<std::vector<typename HomoTuple<T,Size>::type>>*
  164. combinations(const std::string& val_name, const std::string alias = ""){
  165. return combinations<T, Size>(lookup<std::vector<T>>(val_name), alias);
  166. }
  167. template <typename T>
  168. Value<std::vector<T>>*
  169. filter(Function<bool(T)>& filter, Value<std::vector<T>>* val, const std::string alias=""){
  170. return new Filter<T>(filter, val, alias);
  171. }
  172. template <typename T>
  173. Value<std::vector<T>>*
  174. filter(Function<bool(T)>& filter_func, const std::string& val_name, const std::string alias=""){
  175. return filter<T>(filter_func, lookup<std::vector<T>>(val_name), alias);
  176. }
  177. template <typename... ArgTypes>
  178. Value<std::vector<std::tuple<ArgTypes...>>>*
  179. tup_filter(Function<bool(ArgTypes...)>& filter, Value<std::vector<std::tuple<ArgTypes...>>>* val, const std::string alias=""){
  180. return new TupFilter<ArgTypes...>(filter, val, alias);
  181. }
  182. template <typename... ArgTypes>
  183. Value<std::vector<std::tuple<ArgTypes...>>>*
  184. tup_filter(Function<bool(ArgTypes...)>& filter, const std::string& val_name, const std::string alias=""){
  185. return tup_filter<ArgTypes...>(filter, lookup<std::vector<std::tuple<ArgTypes...>>>(val_name), alias);
  186. }
  187. ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
  188. return new ObsFilter(name, filter_function, impl);
  189. }
  190. }
  191. #endif // API_HPP