added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / parser / query_builder.hpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #ifndef REALM_QUERY_BUILDER_HPP
20 #define REALM_QUERY_BUILDER_HPP
21
22 #include <string>
23 #include <memory>
24 #include <vector>
25
26 #include <realm/binary_data.hpp>
27 #include <realm/null.hpp>
28 #include <realm/string_data.hpp>
29 #include <realm/timestamp.hpp>
30 #include <realm/table.hpp>
31 #include <realm/util/any.hpp>
32 #include <realm/util/string_buffer.hpp>
33
34 namespace realm {
35 class Query;
36 class Realm;
37 class Table;
38 template<typename> class BasicRowExpr;
39 using RowExpr = BasicRowExpr<Table>;
40
41 namespace parser {
42     struct Predicate;
43 }
44
45 namespace query_builder {
46 class Arguments;
47
48 void apply_predicate(Query& query, const parser::Predicate& predicate, Arguments& arguments);
49
50 void apply_predicate(Query& query, const parser::Predicate& predicate); // zero out of string args version
51
52
53 struct AnyContext
54 {
55     template<typename T>
56     T unbox(const util::Any& wrapper) {
57         return util::any_cast<T>(wrapper);
58     }
59     bool is_null(const util::Any& wrapper) {
60         if (!wrapper.has_value()) {
61             return true;
62         }
63         if (wrapper.type() == typeid(realm::null)) {
64             return true;
65         }
66         return false;
67     }
68 };
69
70 class Arguments {
71 public:
72     virtual bool bool_for_argument(size_t argument_index) = 0;
73     virtual long long long_for_argument(size_t argument_index) = 0;
74     virtual float float_for_argument(size_t argument_index) = 0;
75     virtual double double_for_argument(size_t argument_index) = 0;
76     virtual StringData string_for_argument(size_t argument_index) = 0;
77     virtual BinaryData binary_for_argument(size_t argument_index) = 0;
78     virtual Timestamp timestamp_for_argument(size_t argument_index) = 0;
79     virtual size_t object_index_for_argument(size_t argument_index) = 0;
80     virtual bool is_argument_null(size_t argument_index) = 0;
81     util::StringBuffer buffer_space; // dynamic conversion space with lifetime tied to this
82 };
83
84 template<typename ValueType, typename ContextType>
85 class ArgumentConverter : public Arguments {
86 public:
87     ArgumentConverter(ContextType& context, const ValueType* arguments, size_t count)
88     : m_ctx(context)
89     , m_arguments(arguments)
90     , m_count(count)
91     {}
92
93     bool bool_for_argument(size_t i) override { return get<bool>(i); }
94     long long long_for_argument(size_t i) override { return get<int64_t>(i); }
95     float float_for_argument(size_t i) override { return get<float>(i); }
96     double double_for_argument(size_t i) override { return get<double>(i); }
97     StringData string_for_argument(size_t i) override { return get<StringData>(i); }
98     BinaryData binary_for_argument(size_t i) override { return get<BinaryData>(i); }
99     Timestamp timestamp_for_argument(size_t i) override { return get<Timestamp>(i); }
100     size_t object_index_for_argument(size_t i) override { return get<RowExpr>(i).get_index(); }
101     bool is_argument_null(size_t i) override { return m_ctx.is_null(at(i)); }
102
103 private:
104     ContextType& m_ctx;
105     const ValueType* m_arguments;
106     size_t m_count;
107
108     const ValueType& at(size_t index) const
109     {
110         if (index >= m_count)
111             throw std::out_of_range("vector");
112         return m_arguments[index];
113     }
114
115     template<typename T>
116     T get(size_t index) const
117     {
118         return m_ctx.template unbox<T>(at(index));
119     }
120 };
121 } // namespace query_builder
122 } // namespace realm
123
124 #endif // REALM_QUERY_BUILDER_HPP