Mangrove
The C++ Object Document Mapper for MongoDB
expression_syntax.hpp
1 // Copyright 2016 MongoDB Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <mangrove/config/prelude.hpp>
18 
19 #include <cstddef>
20 #include <stdexcept>
21 #include <tuple>
22 #include <type_traits>
23 
24 #include <bsoncxx/types.hpp>
25 
26 #include <mangrove/macros.hpp>
27 #include <mangrove/util.hpp>
28 
29 namespace mangrove {
30 MANGROVE_INLINE_NAMESPACE_BEGIN
31 
32 // Enum of possible expression types.
33 enum struct expression_category { none, query, update, sort };
34 
35 // Forward declarations for Expression type trait structs
36 template <expression_category, typename>
38 
39 /* Type traits structs for various expressions */
40 template <expression_category expr_type>
42  static constexpr expression_category value = expr_type;
43 };
44 
45 /* forward declarations of expression classes */
46 
47 template <typename NvpT>
48 class sort_expr;
49 
50 template <expression_category list_type, typename... Args>
52 
53 template <typename NvpT, typename U>
55 
56 template <typename NvpT, typename U>
58 
59 template <typename NvpT>
60 class mod_expr;
61 
62 template <typename Expr>
63 class not_expr;
64 
65 class text_search_expr;
66 
67 template <typename Expr1, typename Expr2>
69 
70 template <typename List>
72 
73 template <typename NvpT, typename U>
75 
76 template <typename NvpT, typename U>
78 
79 template <typename NvpT>
80 class unset_expr;
81 
82 template <typename NvpT>
84 
85 template <typename NvpT, typename U>
87 
88 template <typename NvpT, typename U, typename Sort = int>
90 
91 template <typename NvpT, typename Integer>
93 
94 namespace details {
95 
96 /* type traits for classifying expressions by their category. */
97 
98 using expression_none_t = expression_category_t<expression_category::none>;
99 
100 using expression_sort_t = expression_category_t<expression_category::sort>;
101 
102 using expression_query_t = expression_category_t<expression_category::query>;
103 
104 using expression_update_t = expression_category_t<expression_category::update>;
105 
106 // Type trait containing the expression category of a given type.
107 
108 template <typename>
110 
111 template <typename T>
112 constexpr expression_category expression_type_v = expression_type<T>::value;
113 
114 template <typename NvpT>
115 struct expression_type<sort_expr<NvpT>> : public expression_sort_t {};
116 
117 template <expression_category list_type, typename... Args>
118 struct expression_type<expression_list<list_type, Args...>>
119  : public expression_category_t<list_type> {};
120 
121 template <typename NvpT, typename U>
123 
124 template <typename NvpT, typename U>
126 
127 template <typename NvpT>
128 struct expression_type<mod_expr<NvpT>> : public expression_query_t {};
129 
130 template <typename Expr>
131 struct expression_type<not_expr<Expr>> : public expression_query_t {};
132 
133 template <>
135 
136 template <typename Expr1, typename Expr2>
137 struct expression_type<boolean_expr<Expr1, Expr2>> : public expression_query_t {};
138 
139 template <typename List>
141 
142 template <typename NvpT, typename U>
143 struct expression_type<update_expr<NvpT, U>> : public expression_update_t {};
144 
145 template <typename NvpT, typename U>
147 
148 template <typename NvpT>
150 
151 template <typename NvpT>
153 
154 template <typename NvpT, typename U>
156 
157 template <typename NvpT, typename U, typename Sort>
158 struct expression_type<push_update_expr<NvpT, U, Sort>> : public expression_update_t {};
159 
160 template <typename NvpT, typename Integer>
161 struct expression_type<bit_update_expr<NvpT, Integer>> : public expression_update_t {};
162 
163 // type trait struct for checking if a type has a certain expression category.
164 template <expression_category type, typename T>
165 struct is_expression_type : public std::integral_constant<bool, type == expression_type_v<T>> {};
166 
167 template <typename T>
168 struct isnt_expression : public is_expression_type<expression_category::none, T> {};
169 
170 template <typename T>
171 constexpr bool isnt_expression_v = isnt_expression<T>::value;
172 
173 template <typename T>
174 struct is_sort_expression : public is_expression_type<expression_category::sort, T> {};
175 
176 template <typename T>
177 constexpr bool is_sort_expression_v = is_sort_expression<T>::value;
178 
179 template <typename T>
180 struct is_query_expression : public is_expression_type<expression_category::query, T> {};
181 
182 template <typename T>
183 constexpr bool is_query_expression_v = is_query_expression<T>::value;
184 
185 template <typename T>
186 struct is_update_expression : public is_expression_type<expression_category::update, T> {};
187 
188 template <typename T>
189 constexpr bool is_update_expression_v = is_update_expression<T>::value;
190 
191 } // namespace details
192 MANGROVE_INLINE_NAMESPACE_END
193 } // namespace mangrove
194 
195 #include <mangrove/config/postlude.hpp>
Represents an array update epression that uses the $push operator.
Definition: expression_syntax.hpp:89
Creates an expression that uses the $currentDate operator.
Definition: expression_syntax.hpp:83
Represents a query that performs a text search with the $text operator.
Definition: query_builder.hpp:335
This class represents a query expression using the $mod operator, that checks the modulus of a certai...
Definition: expression_syntax.hpp:60
Definition: expression_syntax.hpp:165
An expression that uses the $addToSet operator to add unique elements to an array.
Definition: expression_syntax.hpp:86
Definition: expression_syntax.hpp:37
This represents a boolean expression with two arguments.
Definition: expression_syntax.hpp:68
Represents an expresion that uses the $unset operator.
Definition: expression_syntax.hpp:80
Definition: expression_syntax.hpp:174
Definition: expression_syntax.hpp:41
Represents an update operator that modifies a certain elements.
Definition: expression_syntax.hpp:74
Expression that updates field using the $bit operator, which does bitwise operations using a mask...
Definition: expression_syntax.hpp:92
Definition: collection_wrapper.hpp:28
Definition: expression_syntax.hpp:180
This represents an expression with the $not operator, which wraps a comparison expression and negates...
Definition: expression_syntax.hpp:63
Represents a comparison expression as above, but stores a value instead of a reference.
Definition: expression_syntax.hpp:57
Definition: expression_syntax.hpp:109
This class represents a boolean expression over an array of arguments.
Definition: expression_syntax.hpp:71
Definition: expression_syntax.hpp:77
Definition: expression_syntax.hpp:168
An expression that represents a sorting order.
Definition: expression_syntax.hpp:48
Represents a query expression with the syntax "key: {$op: value}".
Definition: expression_syntax.hpp:54
This represents a list of expressions.
Definition: expression_syntax.hpp:51
Definition: expression_syntax.hpp:186