api.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. if (gv == nullptr){
  43. CRITICAL("Could not find alias or value \"" << name << "\"."
  44. <<" I'll tell you the ones I know about." << std::endl
  45. << GenValue::summary(), -1);
  46. }
  47. Value<T>* tv = dynamic_cast<Value<T>*>(gv);
  48. if(tv == nullptr){
  49. CRITICAL("Value: \""+gv->get_name() + "\" has improper type.",-1);
  50. }
  51. return tv;
  52. }
  53. template<typename T>
  54. bool check_exists(const std::string name){
  55. GenValue* gv = GenValue::get_value(name);
  56. Value<T>* tv = dynamic_cast<Value<T>*>(gv);
  57. return tv != nullptr;
  58. }
  59. ObsFilter* lookup_obs_filter(const std::string& name){
  60. ObsFilter* f = dynamic_cast<ObsFilter*>(GenValue::get_value(name));
  61. if(f == nullptr){
  62. CRITICAL("ObsFilter: "+f->get_name() + "has improper type.",-1);
  63. }
  64. return f;
  65. }
  66. template <typename T>
  67. decltype(auto)
  68. wrapper_vector(Value<int>* size, Value<T*>* data, const std::string& alias=""){
  69. typedef std::vector<T> type;
  70. const std::string& name = WrapperVector<T>::fmt_name(size, data);
  71. if (check_exists<type>(name))
  72. return lookup<type>(name);
  73. else
  74. return (Value<type>*)new WrapperVector<T>(size, data, alias);
  75. }
  76. template <typename... ArgTypes>
  77. decltype(auto)
  78. zip(Value<std::vector<ArgTypes>>*... args, const std::string& alias=""){
  79. typedef std::vector<std::tuple<ArgTypes...>> type;
  80. std::string& name = Zip<ArgTypes...>::fmt_name(args...);
  81. if (check_exists<type>(name))
  82. return lookup<type>(name);
  83. else
  84. return (Value<type>*)new Zip<ArgTypes...>(args..., alias);
  85. }
  86. template <typename Ret, typename... ArgTypes>
  87. decltype(auto)
  88. map(Function<Ret(ArgTypes...)>& fn, Value<std::vector<std::tuple<ArgTypes...>>>* arg, const std::string& alias=""){
  89. typedef std::vector<Ret> type;
  90. const std::string& name = Map<Ret(ArgTypes...)>::fmt_name(fn, arg);
  91. if (check_exists<type>(name))
  92. return lookup<type>(name);
  93. else
  94. return (Value<type>*)new Map<Ret(ArgTypes...)>(fn, arg, alias);
  95. }
  96. template <typename... ArgTypes>
  97. decltype(auto)
  98. tuple(Value<ArgTypes>*... args){
  99. typedef std::tuple<ArgTypes...> type;
  100. const std::string& name = Tuple<ArgTypes...>::fmt_name(args...);
  101. if (check_exists<type>(name))
  102. return lookup<type>(name);
  103. else
  104. return (Value<type>*)new Tuple<ArgTypes...>(args..., "");
  105. }
  106. template <size_t N, typename... ArgTypes>
  107. decltype(auto)
  108. detup(Value<std::tuple<ArgTypes...>>* tup, const std::string& alias=""){
  109. typedef typename std::tuple_element<N, std::tuple<ArgTypes...>>::type type;
  110. const std::string& name = DeTup<N, ArgTypes...>::fmt_name(tup);
  111. if (check_exists<type>(name))
  112. return lookup<type>(name);
  113. else
  114. return (Value<type>*)new DeTup<N, ArgTypes...>(tup, alias);
  115. }
  116. template <size_t N, typename... ArgTypes>
  117. decltype(auto)
  118. detup_vec(Value<std::vector<std::tuple<ArgTypes...>>>* tup, const std::string& alias=""){
  119. typedef std::vector<typename std::tuple_element<N, std::tuple<ArgTypes...>>::type> type;
  120. const std::string& name = DeTupVector<N, ArgTypes...>::fmt_name(tup);
  121. if (check_exists<type>(name))
  122. return lookup<type>(name);
  123. else
  124. return (Value<type>*)new DeTupVector<N, ArgTypes...>(tup, alias);
  125. }
  126. template <typename Ret, typename... ArgTypes>
  127. decltype(auto)
  128. apply(Function<Ret(ArgTypes...)>& fn, Value<std::tuple<ArgTypes...>>* arg, const std::string& alias=""){
  129. typedef Ret type;
  130. const std::string& name = Apply<Ret(ArgTypes...)>::fmt_name(fn, arg);
  131. if (check_exists<type>(name))
  132. return lookup<type>(name);
  133. else
  134. return (Value<type>*)new Apply<Ret(ArgTypes...)>(fn, arg, alias);
  135. }
  136. template <typename T1, typename T2>
  137. decltype(auto)
  138. pair(Value<T1>* val1, Value<T2>* val2, const std::string& alias=""){
  139. typedef std::pair<T1,T2> type;
  140. const std::string& name = Pair<T1,T2>::fmt_name(val1, val2);
  141. if (check_exists<type>(name))
  142. return lookup<type>(name);
  143. else
  144. return (Value<type>*)new Pair<T1,T2>(val1, val2, alias);
  145. }
  146. template <typename T1, typename T2>
  147. decltype(auto)
  148. pair(const std::string& name1, const std::string& name2, const std::string& alias=""){
  149. return pair<T1,T2>(lookup<T1>(name1), lookup<T2>(name2), alias);
  150. }
  151. template <typename T>
  152. decltype(auto)
  153. max(Value<std::vector<T>>* v, const std::string& alias=""){
  154. typedef T type;
  155. const std::string& name = Max<T>::fmt_name(v);
  156. if (check_exists<type>(name))
  157. return lookup<type>(name);
  158. else
  159. return (Value<type>*)new Max<T>(v, alias);
  160. }
  161. template <typename T>
  162. decltype(auto)
  163. max(const std::string& v_name, const std::string& alias=""){
  164. return max(lookup<std::vector<T>>(v_name), alias);
  165. }
  166. template <typename T>
  167. decltype(auto)
  168. min(Value<std::vector<T>>* v, const std::string& alias=""){
  169. typedef T type;
  170. const std::string& name = Min<T>::fmt_name(v);
  171. if (check_exists<type>(name))
  172. return lookup<type>(name);
  173. else
  174. return (Value<type>*)new Min<T>(v, alias);
  175. }
  176. template <typename T>
  177. decltype(auto)
  178. min(const std::string& v_name, const std::string& alias=""){
  179. return min(lookup<std::vector<T>>(v_name), alias);
  180. }
  181. template <typename T>
  182. decltype(auto)
  183. range(Value<std::vector<T>>* v, const std::string& alias=""){
  184. typedef T type;
  185. const std::string& name = Range<T>::fmt_name(v);
  186. if (check_exists<type>(name))
  187. return lookup<type>(name);
  188. else
  189. return (Value<type>*)new Range<T>(v, alias);
  190. }
  191. template <typename T>
  192. decltype(auto)
  193. range(const std::string& v_name, const std::string& alias=""){
  194. return range(lookup<std::vector<T>>(v_name), alias);
  195. }
  196. template <typename T>
  197. decltype(auto)
  198. mean(Value<std::vector<T>>* v, const std::string& alias=""){
  199. typedef T type;
  200. const std::string& name = Mean<T>::fmt_name(v);
  201. if (check_exists<type>(name))
  202. return lookup<type>(name);
  203. else
  204. return (Value<type>*)new Mean<T>(v, alias);
  205. }
  206. template <typename T>
  207. decltype(auto)
  208. mean(const std::string& v_name, const std::string& alias=""){
  209. return mean(lookup<std::vector<T>>(v_name), alias);
  210. }
  211. template <typename T>
  212. decltype(auto)
  213. count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string& alias=""){
  214. typedef int type;
  215. const std::string& name = Count<T>::fmt_name(selector, v);
  216. if (check_exists<type>(name))
  217. return lookup<type>(name);
  218. else
  219. return (Value<type>*)new Count<T>(selector, v, alias);
  220. }
  221. template <typename T>
  222. decltype(auto)
  223. count(Function<bool(T)>& selector, const std::string& v_name, const std::string& alias=""){
  224. return count<T>(selector, lookup<std::vector<T>>(v_name), alias);
  225. }
  226. template <typename FST, typename SND>
  227. decltype(auto)
  228. cart_product(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string& alias=""){
  229. typedef std::vector<std::tuple<FST,SND>> type;
  230. const std::string& name = CartProduct<FST, SND>::fmt_name(val1, val2);
  231. if (check_exists<type>(name))
  232. return lookup<type>(name);
  233. else
  234. return (Value<type>*)new CartProduct<FST, SND>(val1, val2, alias);
  235. }
  236. template <typename FST, typename SND>
  237. decltype(auto)
  238. cart_product(const std::string& val1_name, const std::string& val2_name, const std::string& alias=""){
  239. return cart_product<FST,SND>(lookup<std::vector<FST>>(val1_name), lookup<std::vector<SND>>(val2_name), alias);
  240. }
  241. template <typename T, int Size>
  242. decltype(auto)
  243. combinations(Value<std::vector<T>>* val, const std::string& alias=""){
  244. typedef std::vector<typename HomoTuple<T,Size>::type> type;
  245. const std::string& name = Combinations<T, Size>::fmt_name(val);
  246. if (check_exists<type>(name))
  247. return lookup<type>(name);
  248. else
  249. return (Value<type>*)new Combinations<T, Size>(val, alias);
  250. }
  251. template <typename T, int Size>
  252. decltype(auto)
  253. combinations(const std::string& val_name, const std::string alias = ""){
  254. return combinations<T, Size>(lookup<std::vector<T>>(val_name), alias);
  255. }
  256. template <typename T>
  257. decltype(auto)
  258. filter(Function<bool(T)>& filter, Value<std::vector<T>>* val, const std::string alias=""){
  259. typedef std::vector<T> type;
  260. const std::string& name = Filter<T>::fmt_name(filter, val);
  261. if (check_exists<type>(name))
  262. return lookup<type>(name);
  263. else
  264. return (Value<type>*)new Filter<T>(filter, val, alias);
  265. }
  266. template <typename T>
  267. decltype(auto)
  268. filter(Function<bool(T)>& filter_func, const std::string& val_name, const std::string alias=""){
  269. return filter<T>(filter_func, lookup<std::vector<T>>(val_name), alias);
  270. }
  271. template <typename... ArgTypes>
  272. decltype(auto)
  273. tup_filter(Function<bool(ArgTypes...)>& filter, Value<std::vector<std::tuple<ArgTypes...>>>* val, const std::string alias=""){
  274. typedef std::vector<std::tuple<ArgTypes...>> type;
  275. const std::string& name = TupFilter<ArgTypes...>::fmt_name(filter, val);
  276. if (check_exists<type>(name))
  277. return lookup<type>(name);
  278. else
  279. return (Value<type>*)new TupFilter<ArgTypes...>(filter, val, alias);
  280. }
  281. template <typename... ArgTypes>
  282. decltype(auto)
  283. tup_filter(Function<bool(ArgTypes...)>& filter, const std::string& val_name, const std::string alias=""){
  284. return tup_filter<ArgTypes...>(filter, lookup<std::vector<std::tuple<ArgTypes...>>>(val_name), alias);
  285. }
  286. ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
  287. return new ObsFilter(name, filter_function, impl);
  288. }
  289. }
  290. #endif // API_HPP