libdballe  6.8
match.h
1 /*
2  * memdb/match - Record-by-record match
3  *
4  * Copyright (C) 2013 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_MEMDB_MATCH_H
23 #define DBA_MEMDB_MATCH_H
24 
25 #include <dballe/core/stlutils.h>
26 #include <wreport/varinfo.h>
27 #include <vector>
28 
29 namespace dballe {
30 class Varmatch;
31 
32 namespace memdb {
33 
35 template<typename T>
36 struct Match
37 {
38  virtual ~Match() {}
39 
40  virtual bool operator()(const T&) const = 0;
41 };
42 
43 namespace match {
44 
45 template<typename T>
46 class And : public Match<T>
47 {
48 protected:
49  std::vector<Match<T>*> matches;
50 
51 public:
52  And() {}
53  And(Match<T>* f1, Match<T>* f2) { add(f1); add(f2); }
54  ~And();
55 
57  void add(Match<T>* m) { matches.push_back(m); }
58 
59  bool operator()(const T& val) const;
60 
61 private:
62  And(const And<T>&);
63  And<T>& operator=(const And<T>&);
64 };
65 
67 template<typename T>
69 {
70  Match<T>* filter;
71  And<T>* is_and; // When nonzero, it points to the same as 'filter'
72 
73  FilterBuilder() : filter(0), is_and(0) {}
74  ~FilterBuilder()
75  {
76  if (filter) delete filter;
77  }
78 
79  const Match<T>* get() const { return filter; }
80  const Match<T>& operator*() const { return *filter; }
81 
82  void add(Match<T>* f);
83 };
84 
85 template<typename T>
86 struct Varcode : public Match<T>
87 {
88  wreport::Varcode code;
89 
90  Varcode(wreport::Varcode code) : code(code) {}
91  virtual bool operator()(const T& val) const
92  {
93  return val.var->code() == code;
94  }
95 };
96 
97 template<typename T>
98 struct Varcodes : public Match<T>
99 {
100  std::set<wreport::Varcode> codes;
101 
102  Varcodes(std::set<wreport::Varcode> codes);
103  virtual bool operator()(const T& val) const;
104 };
105 
106 template<typename T>
107 struct DataFilter : public Match<T>
108 {
109  Varmatch* match;
110 
111  DataFilter(const std::string& expr);
112  ~DataFilter();
113 
114  virtual bool operator()(const T& val) const;
115 
116 private:
117  DataFilter(const DataFilter&);
118  DataFilter& operator=(const DataFilter&);
119 };
120 
121 template<typename T>
122 struct AttrFilter : public Match<T>
123 {
124  Varmatch* match;
125 
126  AttrFilter(const std::string& expr);
127  ~AttrFilter();
128 
129  virtual bool operator()(const T& val) const;
130 
131 private:
132  AttrFilter(const AttrFilter&);
133  AttrFilter& operator=(const AttrFilter&);
134 };
135 
136 }
137 }
138 }
139 
140 #endif
141 
142 
143 
Definition: match.h:107
Definition: varmatch.h:30
Definition: match.h:98
Definition: cmdline.h:34
Definition: match.h:46
Build an And of filters step by step.
Definition: match.h:68
Base class for match functors.
Definition: match.h:36
void add(Match< T > *m)
Add a match to and, taking ownership of its memory management.
Definition: match.h:57
Definition: match.h:86
Definition: match.h:122