MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
data_view.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 "mongo/config.h"
19
20#include <cstring>
21
22#include "mongo/platform/endian.h"
23
24#if __cplusplus >= 201103L
25#include <type_traits>
26#endif
27
28namespace mongo {
29
31public:
32 typedef const char* bytes_type;
33
34 ConstDataView(bytes_type bytes) : _bytes(bytes) {}
35
36 bytes_type view(std::size_t offset = 0) const {
37 return _bytes + offset;
38 }
39
40 template <typename T>
41 const ConstDataView& readNative(T* t, size_t offset = 0) const {
42#if MONGO_HAVE_STD_IS_TRIVIALLY_COPYABLE
43 static_assert(std::is_trivially_copyable<T>::value,
44 "Type for DataView::readNative must be trivially copyable");
45#endif
46 std::memcpy(t, view(offset), sizeof(*t));
47 return *this;
48 }
49
50 template <typename T>
51 T readNative(std::size_t offset = 0) const {
52 T t;
53 readNative(&t, offset);
54 return t;
55 }
56
57 template <typename T>
58 T readLE(std::size_t offset = 0) const {
59 return endian::littleToNative(readNative<T>(offset));
60 }
61
62 template <typename T>
63 T readBE(std::size_t offset = 0) const {
64 return endian::bigToNative(readNative<T>(offset));
65 }
66
67private:
68 bytes_type _bytes;
69};
70
71class DataView : public ConstDataView {
72public:
73 typedef char* bytes_type;
74
75 DataView(bytes_type bytes) : ConstDataView(bytes) {}
76
77 bytes_type view(std::size_t offset = 0) const {
78 // It is safe to cast away const here since the pointer stored in our base class was
79 // originally non-const by way of our constructor.
80 return const_cast<bytes_type>(ConstDataView::view(offset));
81 }
82
83 template <typename T>
84 DataView& writeNative(const T& value, std::size_t offset = 0) {
85#if MONGO_HAVE_STD_IS_TRIVIALLY_COPYABLE
86 static_assert(std::is_trivially_copyable<T>::value,
87 "Type for DataView::writeNative must be trivially copyable");
88#endif
89 std::memcpy(view(offset), &value, sizeof(value));
90 return *this;
91 }
92
93 template <typename T>
94 DataView& writeLE(const T& value, std::size_t offset = 0) {
95 return writeNative(endian::nativeToLittle(value), offset);
96 }
97
98 template <typename T>
99 DataView& writeBE(const T& value, std::size_t offset = 0) {
100 return writeNative(endian::nativeToBig(value), offset);
101 }
102};
103
104} // namespace mongo
Definition data_view.h:30
Definition data_view.h:71
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20