added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / column_type_traits.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 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_COLUMN_TYPE_TRAITS_HPP
20 #define REALM_COLUMN_TYPE_TRAITS_HPP
21
22 #include <realm/column_fwd.hpp>
23 #include <realm/column_type.hpp>
24 #include <realm/data_type.hpp>
25 #include <realm/array.hpp>
26
27 namespace realm {
28
29 class OldDateTime;
30 class ArrayBinary;
31 class ArrayInteger;
32 class ArrayIntNull;
33 template <class>
34 class BasicArray;
35
36 template <class T>
37 struct ColumnTypeTraits;
38
39 template <>
40 struct ColumnTypeTraits<int64_t> {
41     using column_type = Column<int64_t>;
42     using leaf_type = ArrayInteger;
43     using sum_type = int64_t;
44     using minmax_type = int64_t;
45     static const DataType id = type_Int;
46     static const ColumnType column_id = col_type_Int;
47     static const ColumnType real_column_type = col_type_Int;
48 };
49
50 template <>
51 struct ColumnTypeTraits<util::Optional<int64_t>> {
52     using column_type = Column<util::Optional<int64_t>>;
53     using leaf_type = ArrayIntNull;
54     using sum_type = int64_t;
55     using minmax_type = int64_t;
56     static const DataType id = type_Int;
57     static const ColumnType column_id = col_type_Int;
58     static const ColumnType real_column_type = col_type_Int;
59 };
60
61 template <>
62 struct ColumnTypeTraits<bool> : ColumnTypeTraits<int64_t> {
63     static const DataType id = type_Bool;
64     static const ColumnType column_id = col_type_Bool;
65 };
66
67 template <>
68 struct ColumnTypeTraits<util::Optional<bool>> : ColumnTypeTraits<util::Optional<int64_t>> {
69     static const DataType id = type_Bool;
70     static const ColumnType column_id = col_type_Bool;
71 };
72
73 template <>
74 struct ColumnTypeTraits<float> {
75     using column_type = FloatColumn;
76     using leaf_type = BasicArray<float>;
77     using sum_type = double;
78     using minmax_type = float;
79     static const DataType id = type_Float;
80     static const ColumnType column_id = col_type_Float;
81     static const ColumnType real_column_type = col_type_Float;
82 };
83
84 template <>
85 struct ColumnTypeTraits<double> {
86     using column_type = DoubleColumn;
87     using leaf_type = BasicArray<double>;
88     using sum_type = double;
89     using minmax_type = double;
90     static const DataType id = type_Double;
91     static const ColumnType column_id = col_type_Double;
92     static const ColumnType real_column_type = col_type_Double;
93 };
94
95 template <>
96 struct ColumnTypeTraits<OldDateTime> : ColumnTypeTraits<int64_t> {
97     static const DataType id = type_OldDateTime;
98     static const ColumnType column_id = col_type_OldDateTime;
99 };
100
101 template <>
102 struct ColumnTypeTraits<util::Optional<OldDateTime>> : ColumnTypeTraits<util::Optional<int64_t>> {
103     static const DataType id = type_OldDateTime;
104     static const ColumnType column_id = col_type_OldDateTime;
105 };
106
107 template <>
108 struct ColumnTypeTraits<StringData> {
109     static const DataType id = type_String;
110     static const ColumnType column_id = col_type_String;
111 };
112
113 template <>
114 struct ColumnTypeTraits<BinaryData> {
115     using column_type = BinaryColumn;
116     using leaf_type = ArrayBinary;
117     static const DataType id = type_Binary;
118     static const ColumnType column_id = col_type_Binary;
119     static const ColumnType real_column_type = col_type_Binary;
120 };
121
122 template <DataType, bool Nullable>
123 struct GetColumnType;
124 template <>
125 struct GetColumnType<type_Int, false> {
126     using type = IntegerColumn;
127 };
128 template <>
129 struct GetColumnType<type_Int, true> {
130     using type = IntNullColumn;
131 };
132 template <bool N>
133 struct GetColumnType<type_Float, N> {
134     // FIXME: Null definition
135     using type = FloatColumn;
136 };
137 template <bool N>
138 struct GetColumnType<type_Double, N> {
139     // FIXME: Null definition
140     using type = DoubleColumn;
141 };
142
143 // Only purpose is to return 'double' if and only if source column (T) is float and you're doing a sum (A)
144 template <class T, Action A>
145 struct ColumnTypeTraitsSum {
146     typedef T sum_type;
147 };
148
149 template <>
150 struct ColumnTypeTraitsSum<float, act_Sum> {
151     typedef double sum_type;
152 };
153
154 template <Action A>
155 struct ColumnTypeTraitsSum<util::Optional<int64_t>, A> {
156     using sum_type = int64_t;
157 };
158 }
159
160 #endif // REALM_COLUMN_TYPE_TRAITS_HPP