MongoDB C++ Driver  legacy-1.1.2
data_cursor.h
1 /* Copyright 2014 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 
16 #pragma once
17 
18 #include <cstddef>
19 #include <cstring>
20 
21 #include "mongo/base/data_view.h"
22 #include "mongo/platform/endian.h"
23 
24 namespace mongo {
25 
27 public:
28  typedef ConstDataView view_type;
29 
30  ConstDataCursor(ConstDataView::bytes_type bytes) : ConstDataView(bytes) {}
31 
32  ConstDataCursor operator+(std::size_t s) const {
33  return view() + s;
34  }
35 
36  ConstDataCursor& operator+=(std::size_t s) {
37  *this = view() + s;
38  return *this;
39  }
40 
41  ConstDataCursor operator-(std::size_t s) const {
42  return view() - s;
43  }
44 
45  ConstDataCursor& operator-=(std::size_t s) {
46  *this = view() - s;
47  return *this;
48  }
49 
50  ConstDataCursor& operator++() {
51  return operator+=(1);
52  }
53 
54  ConstDataCursor operator++(int) {
55  ConstDataCursor tmp = *this;
56  operator++();
57  return tmp;
58  }
59 
60  ConstDataCursor& operator--() {
61  return operator-=(1);
62  }
63 
64  ConstDataCursor operator--(int) {
65  ConstDataCursor tmp = *this;
66  operator--();
67  return tmp;
68  }
69 
70  template <typename T>
71  ConstDataCursor& skip() {
72  *this = view() + sizeof(T);
73  return *this;
74  }
75 
76  template <typename T>
77  ConstDataCursor& readNativeAndAdvance(T* t) {
78  readNative(t);
79  skip<T>();
80  return *this;
81  }
82 
83  template <typename T>
84  T readNativeAndAdvance() {
85  T out;
86  readNativeAndAdvance(&out);
87  return out;
88  }
89 
90  template <typename T>
91  T readLEAndAdvance() {
92  return endian::littleToNative(readNativeAndAdvance<T>());
93  }
94 
95  template <typename T>
96  T readBEAndAdvance() {
97  return endian::bigToNative(readNativeAndAdvance<T>());
98  }
99 };
100 
101 class DataCursor : public DataView {
102 public:
103  typedef DataView view_type;
104 
105  DataCursor(DataView::bytes_type bytes) : DataView(bytes) {}
106 
107  operator ConstDataCursor() const {
108  return view();
109  }
110 
111  DataCursor operator+(std::size_t s) const {
112  return view() + s;
113  }
114 
115  DataCursor& operator+=(std::size_t s) {
116  *this = view() + s;
117  return *this;
118  }
119 
120  DataCursor operator-(std::size_t s) const {
121  return view() - s;
122  }
123 
124  DataCursor& operator-=(std::size_t s) {
125  *this = view() - s;
126  return *this;
127  }
128 
129  DataCursor& operator++() {
130  return operator+=(1);
131  }
132 
133  DataCursor operator++(int) {
134  DataCursor tmp = *this;
135  operator++();
136  return tmp;
137  }
138 
139  DataCursor& operator--() {
140  return operator-=(1);
141  }
142 
143  DataCursor operator--(int) {
144  DataCursor tmp = *this;
145  operator--();
146  return tmp;
147  }
148 
149  template <typename T>
150  DataCursor& skip() {
151  *this = view() + sizeof(T);
152  return *this;
153  }
154 
155  template <typename T>
156  DataCursor& readNativeAndAdvance(T* t) {
157  readNative(t);
158  skip<T>();
159  return *this;
160  }
161 
162  template <typename T>
163  T readNativeAndAdvance() {
164  T out;
165  readNativeAndAdvance(&out);
166  return out;
167  }
168 
169  template <typename T>
170  T readLEAndAdvance() {
171  return endian::littleToNative(readNativeAndAdvance<T>());
172  }
173 
174  template <typename T>
175  T readBEAndAdvance() {
176  return endian::bigToNative(readNativeAndAdvance<T>());
177  }
178 
179  template <typename T>
180  DataCursor& writeNativeAndAdvance(const T& value) {
181  writeNative(value);
182  skip<T>();
183  return *this;
184  }
185 
186  template <typename T>
187  DataCursor& writeLEAndAdvance(const T& value) {
188  return writeNativeAndAdvance(endian::nativeToLittle(value));
189  }
190 
191  template <typename T>
192  DataCursor& writeBEAndAdvance(const T& value) {
193  return writeNativeAndAdvance(endian::nativeToBig(value));
194  }
195 };
196 
197 } // namespace mongo
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Definition: data_view.h:30
Definition: data_cursor.h:26
Definition: data_cursor.h:101
Definition: data_view.h:71