forked from jzplp/Cpp-Primer-Answer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query.h
82 lines (71 loc) · 2.25 KB
/
Query.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef QUERY_H
#define QUERY_H
#include<string>
#include<memory>
#include "TextQuery.h"
class Query_base
{
friend class Query;
protected:
using line_no = TextQuery::line_no;
virtual ~Query_base() = default;
private:
virtual QueryResult eval(const TextQuery &) const = 0;
virtual std::string rep() const = 0;
};
class Query
{
friend Query operator~(const Query &);
friend Query operator|(const Query &, const Query &);
friend Query operator&(const Query &, const Query &);
public:
Query(const std::string &);
QueryResult eval(const TextQuery &t) const { return q->eval(t); }
std::string rep() const { return q->rep(); }
private:
Query(std::shared_ptr<Query_base> query) : q(query) { }
std::shared_ptr<Query_base> q;
};
class WordQuery : public Query_base
{
friend class Query;
WordQuery(const std::string &s) : query_word(s) { }
QueryResult eval(const TextQuery &t) const { return t.query(query_word); }
std::string rep() const { return query_word; }
std::string query_word;
};
class NotQuery : public Query_base
{
friend Query operator~(const Query &);
NotQuery(const Query &q) : query(q) { }
std::string rep() const { return "~(" + query.rep() + ")"; }
//todo Test substitution function
QueryResult eval(const TextQuery &t) const { return t.query(query.rep()); }
Query query;
};
class BinaryQuery : public Query_base
{
protected:
BinaryQuery(const Query &l, const Query &r, std::string s) : lhs(l), rhs(r), opSym(s) { }
std::string rep() const { return "(" + lhs.rep() + " " + opSym + " " + rhs.rep() + ")"; }
Query lhs, rhs;
std::string opSym;
};
class AndQuery : public BinaryQuery
{
friend Query operator&(const Query &, const Query &);
AndQuery(const Query &left, const Query &right) : BinaryQuery(left, right, "&") { }
//todo Test substitution function
QueryResult eval(const TextQuery &t) const { return t.query("temp"); }
};
class OrQuery : public BinaryQuery
{
friend Query operator|(const Query &, const Query &);
OrQuery(const Query &left, const Query &right) : BinaryQuery(left, right, "|") { }
//todo Test substitution function
QueryResult eval(const TextQuery &t) const { return t.query("temp"); }
};
Query operator~(const Query &);
Query operator|(const Query &, const Query &);
Query operator&(const Query &, const Query &);
#endif