api.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Map<Ret(ArgTypes...)>*
  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. Tuple<ArgTypes...>*
  68. tuple(Value<ArgTypes>*... args, const std::string& alias=""){
  69. return new Tuple<ArgTypes...>(args..., alias);
  70. }
  71. template <typename Ret, typename... ArgTypes>
  72. Apply<Ret(ArgTypes...)>*
  73. apply(Function<Ret(ArgTypes...)>& fn,
  74. Tuple<ArgTypes...>* arg, const std::string& alias=""){
  75. return new Apply<Ret(ArgTypes...)>(fn, arg, alias);
  76. }
  77. template <typename T1, typename T2>
  78. Pair<T1, T2>*
  79. pair(Value<T1>* val1, Value<T2>* val2, const std::string& alias=""){
  80. return new Pair<T1,T2>(val1, val2, alias);
  81. }
  82. template <typename T1, typename T2>
  83. Pair<T1, T2>*
  84. pair(const std::string& name1, const std::string& name2, const std::string& alias=""){
  85. return pair<T1,T2>(lookup<T1>(name1), lookup<T2>(name2), alias);
  86. }
  87. template <typename T>
  88. Max<T>*
  89. max(Value<std::vector<T>>* v, const std::string alias){
  90. return new Max<T>(v, alias);
  91. }
  92. template <typename T>
  93. Max<T>*
  94. max(const std::string& v_name, const std::string alias){
  95. return max(lookup<std::vector<T>>(v_name), alias);
  96. }
  97. template <typename T>
  98. Min<T>*
  99. min(Value<std::vector<T>>* v, const std::string alias){
  100. return new Min<T>(v, alias);
  101. }
  102. template <typename T>
  103. Min<T>*
  104. min(const std::string& v_name, const std::string alias){
  105. return min(lookup<std::vector<T>>(v_name), alias);
  106. }
  107. template <typename T>
  108. Range<T>*
  109. range(Value<std::vector<T>>* v, const std::string alias){
  110. return new Range<T>(v, alias);
  111. }
  112. template <typename T>
  113. Range<T>*
  114. range(const std::string& v_name, const std::string alias){
  115. return range(lookup<std::vector<T>>(v_name), alias);
  116. }
  117. template <typename T>
  118. Mean<T>*
  119. mean(Value<std::vector<T>>* v, const std::string alias){
  120. return new Mean<T>(v, alias);
  121. }
  122. template <typename T>
  123. Mean<T>*
  124. mean(const std::string& v_name, const std::string alias){
  125. return mean(lookup<std::vector<T>>(v_name), alias);
  126. }
  127. template <typename T>
  128. Count<T>*
  129. count(Function<bool(T)>& selector, Value<std::vector<T>>* v, const std::string alias){
  130. return new Count<T>(selector, v, alias);
  131. }
  132. template <typename T>
  133. Count<T>*
  134. count(Function<bool(T)>& selector, const std::string& v_name, const std::string alias){
  135. return count<T>(selector, lookup<std::vector<T>>(v_name), alias);
  136. }
  137. template <typename FST, typename SND>
  138. Value<std::vector<std::tuple<FST,SND>>>*
  139. cart_product(Value<std::vector<FST>>* val1, Value<std::vector<SND>>* val2, const std::string alias){
  140. return new CartProduct<FST, SND>(val1, val2, alias);
  141. }
  142. template <typename FST, typename SND>
  143. Value<std::vector<std::tuple<FST,SND>>>*
  144. cart_product(const std::string& val1_name, const std::string& val2_name, const std::string alias = ""){
  145. return cart_product<FST,SND>(lookup<std::vector<FST>>(val1_name), lookup<std::vector<SND>>(val2_name), alias);
  146. }
  147. ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
  148. return new ObsFilter(name, filter_function, impl);
  149. }
  150. }
  151. #endif // API_HPP