MarlinMT  0.1.0
Condition.cc
Go to the documentation of this file.
2 
3 // -- std includes
4 #include <iostream>
5 #include <string>
6 
7 // -- MarlinBook includes
9 
10 namespace marlinmt {
11  namespace book {
12 
13  bool rgxEvaluation( const std::string_view &match,
14  const std::string_view &str ) {
15  return match == str ;
16  }
17 
18  //--------------------------------------------------------------------------
19 
20  bool rgxEvaluation( const std::basic_regex< char > &rgx,
21  const std::string_view & str ) {
22  return std::regex_search( str.begin(), str.end(), rgx ) ;
23  }
24 
25  //--------------------------------------------------------------------------
26 
28  : _fiterFn{[]( const EntryKey & /*key*/) { return true; }} {}
29 
30  //--------------------------------------------------------------------------
31 
32  Condition::Condition( FilterFn_t filterFn ) : _fiterFn{std::move(filterFn)} {}
33 
34  //--------------------------------------------------------------------------
35 
37  _fiterFn = filterFn;
38  return *this;
39  }
40 
41  //--------------------------------------------------------------------------
42 
43  ConditionBuilder::operator Condition() const { return condition(); }
44 
45  //--------------------------------------------------------------------------
46 
47  // TODO: check performance
49  typename Condition::FilterFn_t fn
50  = []( const EntryKey & /*key*/) { return true; } ;
51 
52  if ( _name ) {
53  fn = [fn, rgx = _name.value()]( const EntryKey &e ) {
54  return fn( e ) && rgxEvaluation( rgx, e.path.filename().string() ) ;
55  } ;
56  } else {
57  fn = [fn, rgx = _rgxName]( const EntryKey &e ) {
58  return fn( e ) && rgxEvaluation( rgx, e.path.filename().string() ) ;
59  } ;
60  }
61 
62  if ( _path ) {
63  fn = [fn, rgx = _path.value()]( const EntryKey &e ) {
64  return fn( e ) && rgxEvaluation( rgx, e.path.parent_path().string() + "/" ) ;
65  } ;
66  } else {
67  fn = [fn, rgx = _rgxPath]( const EntryKey &e ) {
68  return fn( e ) && rgxEvaluation( rgx, e.path.parent_path().string() + "/" ) ;
69  } ;
70  }
71 
72  if ( _type ) {
73  fn = [fn, type = _type.value()]( const EntryKey &e ) -> bool{
74  return fn( e ) && type == e.type ;
75  } ;
76  }
77 
78  return Condition( fn ) ;
79  }
80 
81  //--------------------------------------------------------------------------
82 
84  ConditionBuilder::setName( const std::string_view &name ) {
85  _name = std::optional< std::string >( name ) ;
86  return *this ;
87  }
88 
89  //--------------------------------------------------------------------------
90 
92  ConditionBuilder::setName( const std::basic_regex< char > &rgx ) {
93  _rgxName = rgx ;
94  return *this ;
95  }
96 
97  //--------------------------------------------------------------------------
98 
100  ConditionBuilder::setPath( const std::string_view &path ) {
101  _path = std::optional< std::string >( path ) ;
102  return *this ;
103  }
104 
105  //--------------------------------------------------------------------------
106 
108  ConditionBuilder::setPath( const std::basic_regex< char > &rgx ) {
109  _rgxPath = rgx ;
110  return *this ;
111  }
112 
113  //--------------------------------------------------------------------------
114 
115  ConditionBuilder &ConditionBuilder::setType( const std::type_index &type ) {
116  _type = std::optional< std::type_index >( type ) ;
117  return *this ;
118  }
119 
120  } // end namespace book
121 } // 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
ConditionBuilder & setName(const std::string_view &name)
only accept when key.name is perfect match to name.
Definition: Condition.cc:84
FilterFn_t _fiterFn
actually filter function.
Definition: Condition.h:86
Condition & operator=(const FilterFn_t &filterFn)
Definition: Condition.cc:36
Condition condition() const
construct new Condition.
Definition: Condition.cc:48
bool rgxEvaluation(const std::string_view &match, const std::string_view &str)
Definition: Condition.cc:13
helper to create a Condition.
Definition: Condition.h:92
ConditionBuilder & setPath(const std::string_view &path)
only accept when key.path is perfect match to path.
Definition: Condition.cc:100
Condition()
default constructor.
Definition: Condition.cc:27
wrapper class for an Entry filter function.
Definition: Condition.h:21