MarlinMT  0.1.0
Condition.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std includes
4 #include <functional>
5 #include <optional>
6 #include <regex>
7 #include <string_view>
8 #include <typeindex>
9 
10 // -- MarlinBook includes
12 
13 namespace marlinmt {
14  namespace book {
15  // -- MarlinBook forward declaration
16  struct EntryKey ;
17 
21  class Condition {
22 
23  public:
24  using FilterFn_t = std::function< bool( const EntryKey & ) > ;
25 
27  Condition() ;
28 
29  ~Condition() = default ;
30 
32  explicit Condition( FilterFn_t filterFn ) ;
33  Condition &operator=( const FilterFn_t &filterFn ) ;
34 
36  Condition( Condition && ) = default ;
37  Condition &operator=( Condition && ) = default ;
38 
40  Condition( const Condition & ) = default ;
41  Condition &operator=( const Condition & ) = default ;
42 
47  bool operator()( const EntryKey &key ) const { return _fiterFn( key ); }
48 
55  Condition operator&( const Condition &rhs ) const {
56  return Condition( [lh = _fiterFn, rh = rhs]( const EntryKey &key ) {
57  return lh( key ) && rh( key ) ;
58  } ) ;
59  }
60 
67  Condition operator|( const Condition &rhs ) const {
68  return Condition( [lh = _fiterFn, rh = rhs]( const EntryKey &key ) {
69  return lh( key ) || rh( key ) ;
70  } ) ;
71  }
72 
78  Condition operator!() const {
79  return Condition( [fn = _fiterFn]( const EntryKey &key ) -> bool {
80  return !fn( key ) ;
81  } ) ;
82  }
83 
84  private:
87  } ;
88 
93  public:
95  operator Condition() const ; // NOLINT(hicpp-explicit-conversions, google-explicit-constructor)
96 
98  [[nodiscard]] Condition condition() const ;
99 
105  ConditionBuilder &setName( const std::string_view &name ) ;
106 
113  ConditionBuilder &setName( const std::basic_regex< char > &rgx ) ;
114 
120  ConditionBuilder &setPath( const std::string_view &path ) ;
121 
128  ConditionBuilder &setPath( const std::basic_regex< char > &rgx ) ;
129 
134  ConditionBuilder &setType( const std::type_index &type ) ;
135 
140  template < typename T >
142  return setType( std::type_index( typeid( T ) ) ) ;
143  }
144 
145  private:
147  std::optional< std::string > _name{} ;
149  std::basic_regex< char > _rgxName{".*", std::regex::optimize} ;
151  std::optional< std::string > _path{} ;
153  std::basic_regex< char > _rgxPath{".*", std::regex::optimize} ;
155  std::optional< std::type_index > _type{} ;
156  } ;
157 
158  } // end namespace book
159 } // end namespace marlinmt
Data selection to identify and manage an Entry.
Definition: EntryData.h:21
std::function< bool(const EntryKey &) > FilterFn_t
Definition: Condition.h:24
ConditionBuilder & setType()
only accept when entry type matches.
Definition: Condition.h:141
FilterFn_t _fiterFn
actually filter function.
Definition: Condition.h:86
Condition & operator=(const FilterFn_t &filterFn)
Definition: Condition.cc:36
Condition operator!() const
creates a condition
Definition: Condition.h:78
helper to create a Condition.
Definition: Condition.h:92
Condition operator|(const Condition &rhs) const
creates a composed condition.
Definition: Condition.h:67
bool operator()(const EntryKey &key) const
test Condition on key.
Definition: Condition.h:47
Condition()
default constructor.
Definition: Condition.cc:27
Condition operator &(const Condition &rhs) const
creates a composed condition.
Definition: Condition.h:55
wrapper class for an Entry filter function.
Definition: Condition.h:21