summaryrefslogtreecommitdiff
path: root/src/mongo/base/data_view.h
diff options
context:
space:
mode:
authorJason Carey <hanumantmk@gmail.com>2014-08-11 10:03:42 -0400
committerJason Carey <hanumantmk@gmail.com>2014-08-12 10:07:27 -0400
commit8d0bf7dfbfafd09b9465feec3651cdd31aeb0dcc (patch)
tree5af8d41713b4555d41cf2d4f66180b0da05a9706 /src/mongo/base/data_view.h
parente2a58d5fd4e3f0d64bb5ba10de87ca48365617fc (diff)
downloadmongo-8d0bf7dfbfafd09b9465feec3651cdd31aeb0dcc.tar.gz
SERVER-14852 AAE safe read/write primitives
Alignment, aliasing and endian safe read write primitives in the form of DataView and DataCursor primitives. These primitives provide safe reads and writes with explicit endian variants that funnel through std::memcpy to provide defined behavior. Support for a safe packed struct idiom is also provided in the encoded_value_storage class.
Diffstat (limited to 'src/mongo/base/data_view.h')
-rw-r--r--src/mongo/base/data_view.h101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/mongo/base/data_view.h b/src/mongo/base/data_view.h
new file mode 100644
index 00000000000..f0430d6a504
--- /dev/null
+++ b/src/mongo/base/data_view.h
@@ -0,0 +1,101 @@
+/* Copyright 2014 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects
+ * for all of the code used other than as permitted herein. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you do not
+ * wish to do so, delete this exception statement from your version. If you
+ * delete this exception statement from all source files in the program,
+ * then also delete it in the license file.
+ */
+
+#pragma once
+
+#include <cstring>
+
+#include "mongo/platform/endian.h"
+
+namespace mongo {
+
+ class ConstDataView {
+
+ public:
+ typedef const char* bytes_type;
+
+ ConstDataView(bytes_type bytes)
+ : _bytes(bytes) {
+ }
+
+ bytes_type view(std::size_t offset = 0) const {
+ return _bytes + offset;
+ }
+
+ template<typename T>
+ T readNative(std::size_t offset = 0) const {
+ T t;
+ std::memcpy(&t, view(offset), sizeof(t));
+ return t;
+ }
+
+ template<typename T>
+ T readLE(std::size_t offset = 0) const {
+ return littleToNative(readNative<T>(offset));
+ }
+
+ template<typename T>
+ T readBE(std::size_t offset = 0) const {
+ return bigToNative(readNative<T>(offset));
+ }
+
+ private:
+ bytes_type _bytes;
+ };
+
+ class DataView : public ConstDataView {
+
+ public:
+ typedef char* bytes_type;
+
+ DataView(bytes_type bytes)
+ : ConstDataView(bytes) {
+ }
+
+ bytes_type view(std::size_t offset = 0) const {
+ // It is safe to cast away const here since the pointer stored in our base class was
+ // originally non-const by way of our constructor.
+ return const_cast<bytes_type>(ConstDataView::view(offset));
+ }
+
+ template<typename T>
+ void writeNative(const T& value, std::size_t offset = 0) {
+ std::memcpy(view(offset), &value, sizeof(value));
+ }
+
+ template<typename T>
+ void writeLE(const T& value, std::size_t offset = 0) {
+ return writeNative(nativeToLittle(value), offset);
+ }
+
+ template<typename T>
+ void writeBE(const T& value, std::size_t offset = 0) {
+ return writeNative(nativeToBig(value), offset);
+ }
+ };
+
+} // namespace mongo