Skip to content

Commit 9a0200c

Browse files
committed
template specialization for int64_t and wchar_t*
1 parent 507d9b3 commit 9a0200c

4 files changed

Lines changed: 65 additions & 27 deletions

File tree

include/query_predicates.h

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,16 @@ namespace sqlite_reflection {
117117
{
118118
public:
119119
template <typename T, typename R>
120-
Equal(R T::* fn, R value)
120+
explicit Equal(R T::* fn, R value)
121121
: QueryPredicate(fn, value, "=") {}
122+
123+
template <typename T>
124+
explicit Equal(int64_t T::* fn, int value)
125+
: Equal(fn, (int64_t)value) {}
126+
127+
template <typename T>
128+
explicit Equal(std::wstring T::* fn, const wchar_t* value)
129+
: Equal(fn, std::wstring(value)) {}
122130
};
123131

124132
/// A wrapper for an inequality predicate, for which the value of the
@@ -127,8 +135,16 @@ namespace sqlite_reflection {
127135
{
128136
public:
129137
template <typename T, typename R>
130-
Unequal(R T::* fn, R value)
138+
explicit Unequal(R T::* fn, R value)
131139
: QueryPredicate(fn, value, "!=") {}
140+
141+
template <typename T>
142+
explicit Unequal(int64_t T::* fn, int value)
143+
: Unequal(fn, (int64_t)value) {}
144+
145+
template <typename T>
146+
explicit Unequal(std::wstring T::* fn, const wchar_t* value)
147+
: Unequal(fn, std::wstring(value)) {}
132148
};
133149

134150
/// A wrapper for a similarity predicate, for which the value of the
@@ -137,10 +153,18 @@ namespace sqlite_reflection {
137153
{
138154
public:
139155
template <typename T, typename R>
140-
Like(R T::* fn, R value)
156+
explicit Like(R T::* fn, R value)
141157
: QueryPredicate(fn, value, "LIKE", [&](void* v, SqliteStorageClass storage_class){
142158
return GetStringForValue(v, storage_class);
143159
}) {}
160+
161+
template <typename T>
162+
explicit Like(int64_t T::* fn, int value)
163+
: Like(fn, (int64_t)value) {}
164+
165+
template <typename T>
166+
explicit Like(std::wstring T::* fn, const wchar_t* value)
167+
: Like(fn, std::wstring(value)) {}
144168

145169
protected:
146170
std::string GetStringForValue(void* v, SqliteStorageClass storage_class) const override;
@@ -153,11 +177,15 @@ namespace sqlite_reflection {
153177
{
154178
public:
155179
template <typename T>
156-
GreaterThan(int64_t T::* fn, int64_t value)
180+
explicit GreaterThan(int64_t T::* fn, int64_t value)
157181
: QueryPredicate(fn, value, ">") {}
182+
183+
template <typename T>
184+
explicit GreaterThan(int64_t T::* fn, int value)
185+
: QueryPredicate(fn, (int64_t)value, ">") {}
158186

159187
template <typename T>
160-
GreaterThan(double T::* fn, double value)
188+
explicit GreaterThan(double T::* fn, double value)
161189
: QueryPredicate(fn, value, ">") {}
162190
};
163191

@@ -167,11 +195,15 @@ namespace sqlite_reflection {
167195
{
168196
public:
169197
template <typename T>
170-
GreaterThanOrEqual(int64_t T::* fn, int64_t value)
198+
explicit GreaterThanOrEqual(int64_t T::* fn, int64_t value)
171199
: QueryPredicate(fn, value, ">=") {}
200+
201+
template <typename T>
202+
explicit GreaterThanOrEqual(int64_t T::* fn, int value)
203+
: QueryPredicate(fn, (int64_t)value, ">=") {}
172204

173205
template <typename T>
174-
GreaterThanOrEqual(double T::* fn, double value)
206+
explicit GreaterThanOrEqual(double T::* fn, double value)
175207
: QueryPredicate(fn, value, ">=") {}
176208
};
177209

@@ -181,11 +213,15 @@ namespace sqlite_reflection {
181213
{
182214
public:
183215
template <typename T>
184-
SmallerThan(int64_t T::* fn, int64_t value)
216+
explicit SmallerThan(int64_t T::* fn, int64_t value)
185217
: QueryPredicate(fn, value, "<") {}
218+
219+
template <typename T>
220+
explicit SmallerThan(int64_t T::* fn, int value)
221+
: QueryPredicate(fn, (int64_t)value, "<") {}
186222

187223
template <typename T>
188-
SmallerThan(double T::* fn, double value)
224+
explicit SmallerThan(double T::* fn, double value)
189225
: QueryPredicate(fn, value, "<") {}
190226
};
191227

@@ -195,11 +231,15 @@ namespace sqlite_reflection {
195231
{
196232
public:
197233
template <typename T>
198-
SmallerThanOrEqual(int64_t T::* fn, int64_t value)
234+
explicit SmallerThanOrEqual(int64_t T::* fn, int64_t value)
199235
: QueryPredicate(fn, value, "<=") {}
236+
237+
template <typename T>
238+
explicit SmallerThanOrEqual(int64_t T::* fn, int value)
239+
: QueryPredicate(fn, (int64_t)value, "<=") {}
200240

201241
template <typename T>
202-
SmallerThanOrEqual(double T::* fn, double value)
242+
explicit SmallerThanOrEqual(double T::* fn, double value)
203243
: QueryPredicate(fn, value, "<=") {}
204244
};
205245

include/reflection.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
#include <stddef.h>
3636
#endif
3737

38-
// todo: template specialization for int64_t and wchar_t*
39-
4038
// todo: bool member type
4139
// todo: delete with custom predicate
4240

tests/database_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ TEST_F(DatabaseTest, FetchWithSimilarPredicateString) {
251251

252252
db.Save(company);
253253

254-
auto fetch_condition = Like(&Company::address, std::wstring(L"-"));
254+
auto fetch_condition = Like(&Company::address, L"-");
255255

256256
auto fetched_persons = db.Fetch<Company>(&fetch_condition);
257257
EXPECT_EQ(2, fetched_persons.size());
@@ -329,7 +329,7 @@ TEST_F(DatabaseTest, FetchWithSimilarPredicateInt) {
329329

330330
db.Save(company);
331331

332-
const auto fetch_condition = Like(&Company::age, (int64_t)7);
332+
const auto fetch_condition = Like(&Company::age, 7);
333333

334334
const auto fetched_persons = db.Fetch<Company>(&fetch_condition);
335335
EXPECT_EQ(1, fetched_persons.size());
@@ -356,7 +356,7 @@ TEST_F(DatabaseTest, FetchWithPredicateChaining) {
356356

357357
const auto fetch_condition = GreaterThanOrEqual(&Person::id, 2)
358358
.And(SmallerThan(&Person::id, 5))
359-
.And(Equal(&Person::first_name, std::wstring(L"john")));
359+
.And(Equal(&Person::first_name, L"john"));
360360

361361
const auto fetched_persons = db.Fetch<Person>(&fetch_condition);
362362
EXPECT_EQ(2, fetched_persons.size());

tests/query_predicates_test.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
using namespace sqlite_reflection;
3030

3131
TEST(QueryPredicatesTest, EqualityInt) {
32-
const Equal condition(&Person::id, (int64_t)65);
32+
const Equal condition(&Person::id, 65);
3333
const auto evalution = condition.Evaluate();
3434
EXPECT_EQ(0, strcmp(evalution.data(), "id = 65"));
3535
}
3636

3737
TEST(QueryPredicatesTest, EqualityString) {
38-
const Equal condition(&Person::first_name, std::wstring(L"john"));
38+
const Equal condition(&Person::first_name, L"john");
3939
const auto evalution = condition.Evaluate();
4040
EXPECT_EQ(0, strcmp(evalution.data(), "first_name = 'john'"));
4141
}
@@ -47,13 +47,13 @@ TEST(QueryPredicatesTest, EqualityDouble) {
4747
}
4848

4949
TEST(QueryPredicatesTest, InequalityInt) {
50-
const Unequal condition(&Person::id, (int64_t)65);
50+
const Unequal condition(&Person::id, 65);
5151
const auto evalution = condition.Evaluate();
5252
EXPECT_EQ(0, strcmp(evalution.data(), "id != 65"));
5353
}
5454

5555
TEST(QueryPredicatesTest, InequalityString) {
56-
const Unequal condition(&Person::first_name, std::wstring(L"john"));
56+
const Unequal condition(&Person::first_name, L"john");
5757
const auto evalution = condition.Evaluate();
5858
EXPECT_EQ(0, strcmp(evalution.data(), "first_name != 'john'"));
5959
}
@@ -113,25 +113,25 @@ TEST(QueryPredicatesTest, SmallerThanOrEqualDouble) {
113113
}
114114

115115
TEST(QueryPredicatesTest, And) {
116-
const Unequal c1(&Person::id, (int64_t)65);
117-
const Equal c2(&Person::first_name, std::wstring(L"john"));
116+
const Unequal c1(&Person::id, 65);
117+
const Equal c2(&Person::first_name, L"john");
118118
const AndPredicate c3(c1, c2);
119119
const auto evalution = c3.Evaluate();
120120
EXPECT_EQ(0, strcmp(evalution.data(), "(id != 65 AND first_name = 'john')"));
121121
}
122122

123123
TEST(QueryPredicatesTest, Or) {
124-
const Unequal c1(&Person::id, (int64_t)65);
125-
const Equal c2(&Person::first_name, std::wstring(L"john"));
124+
const Unequal c1(&Person::id, 65);
125+
const Equal c2(&Person::first_name, L"john");
126126
const OrPredicate c3(c1, c2);
127127
const auto evalution = c3.Evaluate();
128128
EXPECT_EQ(0, strcmp(evalution.data(), "(id != 65 OR first_name = 'john')"));
129129
}
130130

131131
TEST(QueryPredicatesTest, PredicateChaining) {
132-
const auto predicate = Equal(&Person::id, (int64_t)65)
133-
.Or(Equal(&Person::first_name, std::wstring(L"john")))
134-
.And(Unequal(&Person::last_name, std::wstring(L"appleseed")));
132+
const auto predicate = Equal(&Person::id, 65)
133+
.Or(Equal(&Person::first_name, L"john"))
134+
.And(Unequal(&Person::last_name, L"appleseed"));
135135

136136
const auto evaluation = predicate.Evaluate();
137137

0 commit comments

Comments
 (0)