forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.cpp
95 lines (85 loc) · 2.03 KB
/
Query.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "Query.h"
#include "WordQuery.h"
#include "TextQuery.h"
#include "QueryResult.h"
#include "Query_base.h"
#if DEBUG_LEVEL >= 1
#include <iostream>
#endif
Query::~Query() {
#if DEBUG_LEVEL >= 1
std::cout << "Query::~Query()" << std::endl;
#endif
if (--*ref_count == 0) {
delete pq;
delete ref_count;
}
}
Query::Query(const Query &rhs)
: pq(rhs.pq), ref_count(rhs.ref_count) {
#if DEBUG_LEVEL >= 1
std::cout << "Query::Query(const Query &)" << std::endl;
#endif
++*ref_count;
}
Query &Query::operator=(const Query &rhs) {
#if DEBUG_LEVEL >= 1
std::cout << "Query::operator=(const Query &)" << std::endl;
#endif
if (this != &rhs) {
if (--*ref_count == 0) {
delete pq;
delete ref_count;
}
pq = rhs.pq;
ref_count = rhs.ref_count;
++*ref_count;
}
return *this;
}
//Query::Query(Query &&rhs) : pq(rhs.pq), ref_count(rhs.ref_count) {
//#if DEBUG_LEVEL >= 1
// std::cout << "Query::Query(Query &&)" << std::endl;
//#endif
// rhs.pq = nullptr; // error?
// rhs.ref_count = nullptr;
//}
//Query &Query::operator=(Query &&rhs) {
//#if DEBUG_LEVEL >= 1
// std::cout << "Query::operator=(Query &&)" << std::endl;
//#endif
// if (this != &rhs) {
// if (--*ref_count == 0) {
// delete pq;
// delete ref_count;
// }
// pq = rhs.pq;
// ref_count = rhs.ref_count;
// rhs.pq = nullptr; // error?
// rhs.ref_count = nullptr;
// }
// return *this;
//}
// NOTE The following member functions cannot be inline or they must be put
// into header, because they are used in different translation units.
//inline
Query::Query(const std::string &s)
: pq(new WordQuery(s)), ref_count(new std::size_t(1)) {
#if DEBUG_LEVEL >= 1
std::cout << "Query::Query(const std::string &)" << std::endl;
#endif
}
//inline
QueryResult Query::eval(const TextQuery &t) const {
#if DEBUG_LEVEL >= 1
std::cout << "Query::eval" << std::endl;
#endif
return pq->eval(t);
}
//inline
std::string Query::rep() const {
#if DEBUG_LEVEL >= 1
std::cout << "Query::rep" << std::endl;
#endif
return pq->rep();
}