summaryrefslogtreecommitdiff
path: root/src/mongo/base/data_view.h
diff options
context:
space:
mode:
authorJason Carey (hanumantmk) <jcarey@argv.me>2015-03-19 16:37:54 -0400
committerJason Carey (hanumantmk) <jcarey@argv.me>2015-04-14 14:32:49 -0400
commit67829601c0e4516f70a59140c8971f5111372b8b (patch)
tree34a85a1f43b16c5e29d47767315d5dd0d04cbf08 /src/mongo/base/data_view.h
parent8bd99fa7bd4387abeb5f6ddf0907b36f67d9c702 (diff)
downloadmongo-67829601c0e4516f70a59140c8971f5111372b8b.tar.gz
SERVER-17629 Implemented Bounded Data View/Cursor
Implements DataRange and DataRangeCursor, bounded Status returning variants of DataView and DataCursor. Implements DataType::Handler<> to allow for specialization by type. * Endian specializations * BSONObj specialization Fix for endian conversions for floats and doubles to avoid some signaling bit patterns.
Diffstat (limited to 'src/mongo/base/data_view.h')
-rw-r--r--src/mongo/base/data_view.h47
1 files changed, 12 insertions, 35 deletions
diff --git a/src/mongo/base/data_view.h b/src/mongo/base/data_view.h
index bb19b561fae..3de3237065d 100644
--- a/src/mongo/base/data_view.h
+++ b/src/mongo/base/data_view.h
@@ -31,7 +31,8 @@
#include <type_traits>
#include "mongo/config.h"
-#include "mongo/platform/endian.h"
+
+#include "mongo/base/data_type.h"
namespace mongo {
@@ -49,30 +50,19 @@ namespace mongo {
}
template<typename T>
- const ConstDataView& readNative(T* t, size_t offset = 0) const {
-#if MONGO_CONFIG_HAVE_STD_IS_TRIVIALLY_COPYABLE
- static_assert(std::is_trivially_copyable<T>::value,
- "Type for DataView::readNative must be trivially copyable");
-#endif
- std::memcpy(t, view(offset), sizeof(*t));
+ const ConstDataView& read(T* t, size_t offset = 0) const {
+ DataType::unsafeLoad(t, view(offset), nullptr);
+
return *this;
}
template<typename T>
- T readNative(std::size_t offset = 0) const {
- T t;
- readNative(&t, offset);
- return t;
- }
+ T read(std::size_t offset = 0) const {
+ T t(DataType::defaultConstruct<T>());
- template<typename T>
- T readLE(std::size_t offset = 0) const {
- return endian::littleToNative(readNative<T>(offset));
- }
+ read(&t, offset);
- template<typename T>
- T readBE(std::size_t offset = 0) const {
- return endian::bigToNative(readNative<T>(offset));
+ return t;
}
private:
@@ -95,23 +85,10 @@ namespace mongo {
}
template<typename T>
- DataView& writeNative(const T& value, std::size_t offset = 0) {
-#if MONGO_CONFIG_HAVE_STD_IS_TRIVIALLY_COPYABLE
- static_assert(std::is_trivially_copyable<T>::value,
- "Type for DataView::writeNative must be trivially copyable");
-#endif
- std::memcpy(view(offset), &value, sizeof(value));
- return *this;
- }
+ DataView& write(const T& value, std::size_t offset = 0) {
+ DataType::unsafeStore(value, view(offset), nullptr);
- template<typename T>
- DataView& writeLE(const T& value, std::size_t offset = 0) {
- return writeNative(endian::nativeToLittle(value), offset);
- }
-
- template<typename T>
- DataView& writeBE(const T& value, std::size_t offset = 0) {
- return writeNative(endian::nativeToBig(value), offset);
+ return *this;
}
};