forked from datastax/cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.hpp
147 lines (115 loc) · 3.82 KB
/
collection.hpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright (c) 2014-2016 DataStax
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __CASS_COLLECTION_HPP_INCLUDED__
#define __CASS_COLLECTION_HPP_INCLUDED__
#include "cassandra.h"
#include "data_type.hpp"
#include "encode.hpp"
#include "external.hpp"
#include "buffer.hpp"
#include "ref_counted.hpp"
#include "types.hpp"
#define CASS_COLLECTION_CHECK_TYPE(Value) do { \
CassError rc = check(Value); \
if (rc != CASS_OK) return rc; \
} while(0)
namespace cass {
class UserTypeValue;
class Collection : public RefCounted<Collection> {
public:
Collection(CassCollectionType type,
size_t item_count)
: data_type_(new CollectionType(static_cast<CassValueType>(type), false)) {
items_.reserve(item_count);
}
Collection(const CollectionType::ConstPtr& data_type,
size_t item_count)
: data_type_(data_type) {
items_.reserve(item_count);
}
CassCollectionType type() const {
return static_cast<CassCollectionType>(data_type_->value_type());
}
const CollectionType::ConstPtr& data_type() const { return data_type_; }
const BufferVec& items() const { return items_; }
#define APPEND_TYPE(Type) \
CassError append(const Type value) { \
CASS_COLLECTION_CHECK_TYPE(value); \
items_.push_back(cass::encode(value)); \
return CASS_OK; \
}
APPEND_TYPE(cass_int8_t)
APPEND_TYPE(cass_int16_t)
APPEND_TYPE(cass_int32_t)
APPEND_TYPE(cass_uint32_t)
APPEND_TYPE(cass_int64_t)
APPEND_TYPE(cass_float_t)
APPEND_TYPE(cass_double_t)
APPEND_TYPE(cass_bool_t)
APPEND_TYPE(CassString)
APPEND_TYPE(CassBytes)
APPEND_TYPE(CassCustom)
APPEND_TYPE(CassUuid)
APPEND_TYPE(CassInet)
APPEND_TYPE(CassDecimal)
APPEND_TYPE(CassDuration)
#undef APPEND_TYPE
CassError append(CassNull value);
CassError append(const Collection* value);
CassError append(const Tuple* value);
CassError append(const UserTypeValue* value);
size_t get_items_size(int version) const;
void encode_items(int version, char* buf) const;
size_t get_size_with_length(int version) const;
Buffer encode() const;
Buffer encode_with_length(int version) const;
void clear() {
items_.clear();
}
private:
template <class T>
CassError check(const T value) {
IsValidDataType<T> is_valid_type;
size_t index = items_.size();
switch(type()) {
case CASS_COLLECTION_TYPE_MAP:
if (data_type_->types().size() == 2 &&
!is_valid_type(value, data_type_->types()[index % 2])) {
return CASS_ERROR_LIB_INVALID_VALUE_TYPE;
}
break;
case CASS_COLLECTION_TYPE_LIST:
case CASS_COLLECTION_TYPE_SET:
if (data_type_->types().size() == 1 &&
!is_valid_type(value, data_type_->types()[0])) {
return CASS_ERROR_LIB_INVALID_VALUE_TYPE;
}
break;
}
return CASS_OK;
}
int32_t get_count() const {
return ((type() == CASS_COLLECTION_TYPE_MAP) ? items_.size() / 2 : items_.size());
}
size_t get_items_size(size_t num_bytes_for_size) const;
void encode_items_int32(char* buf) const;
void encode_items_uint16(char* buf) const;
private:
CollectionType::ConstPtr data_type_;
BufferVec items_;
private:
DISALLOW_COPY_AND_ASSIGN(Collection);
};
} // namespace cass
EXTERNAL_TYPE(cass::Collection, CassCollection)
#endif