NSL
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Selection.h
Go to the documentation of this file.
1 #ifndef NSL_SELECTIONS_H
2 #define NSL_SELECTIONS_H
3 
4 // our headers
5 #include "NSL/matchers.h"
6 
7 // c++ headers
8 #include <type_traits>
9 
10 namespace NSL {
11 
12 class Selection {
13  // Generic definition of logical operators that takes uses perfect forwarding for both arguments
14  template <typename T, typename U,
15  std::enable_if_t<std::is_base_of<Selection, std::remove_reference_t<T>>::value, bool> = true,
16  std::enable_if_t<std::is_base_of<Selection, std::remove_reference_t<U>>::value, bool> = true>
17  friend inline Selection operator&&(T &&lhs, U &&rhs) {
18  Selection result;
19  result.m_matcher = std::make_shared<andMatcher>(std::forward<decltype(lhs.m_matcher)>(lhs.m_matcher),
20  std::forward<decltype(rhs.m_matcher)>(rhs.m_matcher));
21  return result;
22  }
23 
24  template <typename T, typename U,
25  std::enable_if_t<std::is_base_of<Selection, std::remove_reference_t<T>>::value, bool> = true,
26  std::enable_if_t<std::is_base_of<Selection, std::remove_reference_t<U>>::value, bool> = true>
27  friend inline Selection operator||(T &&lhs, U &&rhs) {
28  Selection result;
29  result.m_matcher = std::make_shared<orMatcher>(std::forward<decltype(lhs.m_matcher)>(lhs.m_matcher),
30  std::forward<decltype(rhs.m_matcher)>(rhs.m_matcher));
31 
32  return result;
33  }
34 
35  template <typename T, std::enable_if_t<std::is_base_of<Selection, std::remove_reference_t<T>>::value, bool> = true>
36  friend inline Selection operator!(T &&lhs) {
37  Selection result;
38  result.m_matcher = std::make_shared<notMatcher>(std::forward<decltype(lhs.m_matcher)>(lhs.m_matcher));
39 
40  return result;
41  }
42 
43 public:
44  template <class Hook, std::enable_if_t<std::is_convertible<Hook, matcher::hookType>::value, bool> = true>
45  Selection &AddPreHook(Hook &&preHook) {
46  m_matcher->AddPreHook(std::forward<decltype(preHook)>(preHook));
47  return *this;
48  }
49 
50  template <class Hook, std::enable_if_t<std::is_convertible<Hook, matcher::hookType>::value, bool> = true>
52  m_matcher->AddPostHook(std::forward<decltype(preHook)>(preHook), condition);
53  return *this;
54  }
55 
56  virtual bool operator()(Event &ev) { return (*m_matcher)(ev); };
57 
58  template <typename T, std::enable_if_t<std::is_base_of<Selection, std::remove_reference_t<T>>::value, bool> = true>
59  Selection &operator&=(T &&rhs) {
60  m_matcher = std::make_shared<andMatcher>(m_matcher, std::forward<decltype(rhs.m_matcher)>(rhs.m_matcher));
61 
62  return *this;
63  };
64 
65  template <typename T, std::enable_if_t<std::is_base_of<Selection, std::remove_reference_t<T>>::value, bool> = true>
66  Selection &operator|=(T &&rhs) {
67  m_matcher = std::make_shared<orMatcher>(m_matcher, std::forward<decltype(rhs.m_matcher)>(rhs.m_matcher));
68 
69  return *this;
70  };
71 
72 protected:
73  RefCMatcher m_matcher = std::make_shared<boolMatcher>([](Event &ev) { return true; });
74 };
75 
76 } // namespace Selections
77 
78 #endif
Selection & operator|=(T &&rhs)
Definition: Selection.h:66
virtual bool operator()(Event &ev)
Definition: Selection.h:56
friend Selection operator||(T &&lhs, U &&rhs)
Definition: Selection.h:27
NAIA::Event Event
Definition: matchers.h:11
Selection & AddPreHook(Hook &&preHook)
Definition: Selection.h:45
Selection & AddPostHook(Hook &&preHook, PostHookCondition condition=PostHookCondition::OnSuccess)
Definition: Selection.h:51
PostHookCondition
Definition: matchers.h:15
friend Selection operator&&(T &&lhs, U &&rhs)
Definition: Selection.h:17
Selection & operator&=(T &&rhs)
Definition: Selection.h:59
std::shared_ptr< matcher > RefCMatcher
Definition: matchers.h:56
RefCMatcher m_matcher
Definition: Selection.h:73
friend Selection operator!(T &&lhs)
Definition: Selection.h:36