operators.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* operators.cpp - standard c++ operators node templates
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <functional>
  18. #include <core/std/traits.h>
  19. #include <core/node_info.h>
  20. #include <core/node/new_node.h>
  21. #include <core/all_types.h>
  22. #include <core/context.h>
  23. namespace rainynite::core::nodes {
  24. namespace detail {
  25. using std::declval;
  26. template <typename Op, typename A>
  27. using has_operator_t = decltype(Op()(declval<A&>(), declval<A&>()));
  28. }
  29. template <typename Op, typename A>
  30. constexpr bool has_operator = is_detected_v<detail::has_operator_t, Op, A>;
  31. template <class T, template <typename> typename Op>
  32. class BinaryNode :
  33. public NewNode<
  34. BinaryNode<T, Op>,
  35. T,
  36. types::Only<T>,
  37. types::Only<T>
  38. >
  39. {
  40. NODE_PROPERTIES("a", "b")
  41. DEFAULT_VALUES(T{}, T{})
  42. PROPERTY(a)
  43. PROPERTY(b)
  44. T get(shared_ptr<Context> ctx) const override {
  45. if constexpr (has_operator<Op<void>, T>) {
  46. return Op<void>()(a_value<T>(ctx), b_value<T>(ctx));
  47. } else {
  48. throw std::logic_error("Unsupported operator for this type.");
  49. }
  50. }
  51. };
  52. template <typename T>
  53. using Add = BinaryNode<T,std::plus>;
  54. NODE_INFO_INSTANCES(Add, Add<T>, T)
  55. template <typename T>
  56. using Sub = BinaryNode<T,std::minus>;
  57. NODE_INFO_INSTANCES(Sub, Sub<T>, T)
  58. // template <typename T>
  59. // using Mul = BinaryNode<T,std::multiplies>;
  60. // NODE_INFO_INSTANCES(Mul, Mul<T>, T)
  61. //
  62. // template <typename T>
  63. // using Div = BinaryNode<T,std::divides>;
  64. // NODE_INFO_INSTANCES(Div, Div<T>, T)
  65. //
  66. // template <typename T>
  67. // using Mod = BinaryNode<T,std::modulus>;
  68. // NODE_INFO_INSTANCES(Mod, Mod<T>, T)
  69. } // namespace rainynite::core::nodes