summaryrefslogtreecommitdiff
path: root/src/mongo/base/data_range.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/base/data_range.h')
-rw-r--r--src/mongo/base/data_range.h220
1 files changed, 107 insertions, 113 deletions
diff --git a/src/mongo/base/data_range.h b/src/mongo/base/data_range.h
index 16c7f68ca98..9226e6c133a 100644
--- a/src/mongo/base/data_range.h
+++ b/src/mongo/base/data_range.h
@@ -39,141 +39,135 @@
namespace mongo {
- class ConstDataRange {
-
- public:
- // begin and end should point to the first and one past last bytes in
- // the range you wish to view.
- //
- // debug_offset provides a way to indicate that the ConstDataRange is
- // located at an offset into some larger logical buffer. By setting it
- // to a non-zero value, you'll change the Status messages that are
- // returned on failure to be offset by the amount passed to this
- // constructor.
- ConstDataRange(const char* begin, const char* end, std::ptrdiff_t debug_offset = 0)
- : _begin(begin), _end(end), _debug_offset(debug_offset) {
- invariant(end >= begin);
- }
-
- const char* data() const {
- return _begin;
- }
-
- size_t length() const {
- return _end - _begin;
- }
+class ConstDataRange {
+public:
+ // begin and end should point to the first and one past last bytes in
+ // the range you wish to view.
+ //
+ // debug_offset provides a way to indicate that the ConstDataRange is
+ // located at an offset into some larger logical buffer. By setting it
+ // to a non-zero value, you'll change the Status messages that are
+ // returned on failure to be offset by the amount passed to this
+ // constructor.
+ ConstDataRange(const char* begin, const char* end, std::ptrdiff_t debug_offset = 0)
+ : _begin(begin), _end(end), _debug_offset(debug_offset) {
+ invariant(end >= begin);
+ }
+
+ const char* data() const {
+ return _begin;
+ }
+
+ size_t length() const {
+ return _end - _begin;
+ }
- template<typename T>
- Status read(T* t, size_t offset = 0) const {
- if (offset > length()) {
- return makeOffsetStatus(offset);
- }
-
- return DataType::load(t, _begin + offset, length() - offset, nullptr,
- offset + _debug_offset);
+ template <typename T>
+ Status read(T* t, size_t offset = 0) const {
+ if (offset > length()) {
+ return makeOffsetStatus(offset);
}
- template<typename T>
- StatusWith<T> read(std::size_t offset = 0) const {
- T t(DataType::defaultConstruct<T>());
- Status s = read(&t, offset);
+ return DataType::load(
+ t, _begin + offset, length() - offset, nullptr, offset + _debug_offset);
+ }
- if (s.isOK()) {
- return StatusWith<T>(std::move(t));
- } else {
- return StatusWith<T>(std::move(s));
- }
- }
-
- friend bool operator==(const ConstDataRange& lhs, const ConstDataRange& rhs) {
- return std::tie(lhs._begin, lhs._end) == std::tie(rhs._begin, rhs._end);
+ template <typename T>
+ StatusWith<T> read(std::size_t offset = 0) const {
+ T t(DataType::defaultConstruct<T>());
+ Status s = read(&t, offset);
+
+ if (s.isOK()) {
+ return StatusWith<T>(std::move(t));
+ } else {
+ return StatusWith<T>(std::move(s));
}
+ }
- friend bool operator!=(const ConstDataRange& lhs, const ConstDataRange& rhs) {
- return !(lhs == rhs);
- }
+ friend bool operator==(const ConstDataRange& lhs, const ConstDataRange& rhs) {
+ return std::tie(lhs._begin, lhs._end) == std::tie(rhs._begin, rhs._end);
+ }
+ friend bool operator!=(const ConstDataRange& lhs, const ConstDataRange& rhs) {
+ return !(lhs == rhs);
+ }
- protected:
- const char* _begin;
- const char* _end;
- std::ptrdiff_t _debug_offset;
- Status makeOffsetStatus(size_t offset) const;
+protected:
+ const char* _begin;
+ const char* _end;
+ std::ptrdiff_t _debug_offset;
- };
+ Status makeOffsetStatus(size_t offset) const;
+};
- class DataRange : public ConstDataRange {
+class DataRange : public ConstDataRange {
+public:
+ typedef char* bytes_type;
- public:
- typedef char* bytes_type;
+ DataRange(bytes_type begin, bytes_type end, std::ptrdiff_t debug_offset = 0)
+ : ConstDataRange(begin, end, debug_offset) {}
- DataRange(bytes_type begin, bytes_type end, std::ptrdiff_t debug_offset = 0)
- : ConstDataRange(begin, end, debug_offset) {
+ template <typename T>
+ Status write(const T& value, std::size_t offset = 0) {
+ if (offset > length()) {
+ return makeOffsetStatus(offset);
}
- template<typename T>
- Status write(const T& value, std::size_t offset = 0) {
- if (offset > length()) {
- return makeOffsetStatus(offset);
- }
-
- return DataType::store(value, const_cast<char *>(_begin + offset), length() - offset,
- nullptr, offset + _debug_offset);
+ return DataType::store(value,
+ const_cast<char*>(_begin + offset),
+ length() - offset,
+ nullptr,
+ offset + _debug_offset);
+ }
+};
+
+struct DataRangeTypeHelper {
+ static Status makeStoreStatus(size_t t_length, size_t length, std::ptrdiff_t debug_offset);
+};
+
+// Enable for classes derived from ConstDataRange
+template <typename T>
+struct DataType::Handler<T,
+ typename std::enable_if<std::is_base_of<ConstDataRange, T>::value>::type> {
+ static Status load(
+ T* t, const char* ptr, size_t length, size_t* advanced, std::ptrdiff_t debug_offset) {
+ if (t) {
+ // Assuming you know what you're doing at the read above this
+ // is fine. Either you're reading into a readable buffer, so
+ // ptr started off non-const, or the const_cast will feed back
+ // to const char* taking Const variants. So it'll get tossed
+ // out again.
+ *t = T(const_cast<char*>(ptr), const_cast<char*>(ptr) + length);
}
- };
-
- struct DataRangeTypeHelper {
- static Status makeStoreStatus(size_t t_length, size_t length, std::ptrdiff_t debug_offset);
- };
-
- // Enable for classes derived from ConstDataRange
- template <typename T>
- struct DataType::Handler<T,
- typename std::enable_if<std::is_base_of<ConstDataRange, T>::value>::type> {
-
- static Status load(T* t, const char* ptr, size_t length, size_t* advanced,
- std::ptrdiff_t debug_offset)
- {
- if (t) {
- // Assuming you know what you're doing at the read above this
- // is fine. Either you're reading into a readable buffer, so
- // ptr started off non-const, or the const_cast will feed back
- // to const char* taking Const variants. So it'll get tossed
- // out again.
- *t = T(const_cast<char*>(ptr), const_cast<char*>(ptr) + length);
- }
-
- if (advanced) {
- *advanced = length;
- }
-
- return Status::OK();
+ if (advanced) {
+ *advanced = length;
}
- static Status store(const T& t, char* ptr, size_t length, size_t* advanced,
- std::ptrdiff_t debug_offset)
- {
- if (t.length() > length) {
- return DataRangeTypeHelper::makeStoreStatus(t.length(), length, debug_offset);
- }
-
- if (ptr) {
- std::memcpy(ptr, t.data(), t.length());
- }
+ return Status::OK();
+ }
- if (advanced) {
- *advanced = t.length();
- }
+ static Status store(
+ const T& t, char* ptr, size_t length, size_t* advanced, std::ptrdiff_t debug_offset) {
+ if (t.length() > length) {
+ return DataRangeTypeHelper::makeStoreStatus(t.length(), length, debug_offset);
+ }
- return Status::OK();
+ if (ptr) {
+ std::memcpy(ptr, t.data(), t.length());
}
- static T defaultConstruct()
- {
- return T(nullptr, nullptr);
+ if (advanced) {
+ *advanced = t.length();
}
- };
-} // namespace mongo
+ return Status::OK();
+ }
+
+ static T defaultConstruct() {
+ return T(nullptr, nullptr);
+ }
+};
+
+} // namespace mongo