compare.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* nodes/compare.cpp - comparator nodes
  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 <experimental/type_traits>
  18. #include <core/node_info.h>
  19. #include <core/node/proxy_node.h>
  20. #include <core/node/property.h>
  21. #include <core/all_types.h>
  22. namespace rainynite::core::nodes {
  23. namespace detail {
  24. using std::declval;
  25. using std::experimental::is_detected_v;
  26. template <class C>
  27. using operator_eq_t = decltype(declval<C&>() == declval<C&>());
  28. template <class C>
  29. constexpr bool has_operator_eq = is_detected_v<operator_eq_t, C>;
  30. }
  31. template <typename T>
  32. class Equal : public Node<bool> {
  33. DOC_STRING(
  34. "Node that compares its arguments."
  35. )
  36. public:
  37. Equal() {
  38. this->template init<T>(a, {});
  39. this->template init<T>(b, {});
  40. }
  41. public:
  42. bool get(shared_ptr<Context> ctx) const override {
  43. if constexpr (detail::has_operator_eq<T>) {
  44. return get_a()->value(ctx) == get_b()->value(ctx);
  45. } else {
  46. throw std::logic_error("Comparing incomparable type");
  47. }
  48. }
  49. private:
  50. NODE_PROPERTY(a, T);
  51. NODE_PROPERTY(b, T);
  52. };
  53. NODE_INFO_TEMPLATE(Equal, Equal<T>, T);
  54. TYPE_INSTANCES(EqualNodeInfo)
  55. } // namespace rainynite::core::nodes