diff options
author | Andrew Morrow <acm@mongodb.com> | 2014-08-22 12:41:06 -0400 |
---|---|---|
committer | Andrew Morrow <acm@mongodb.com> | 2014-09-08 17:44:20 -0400 |
commit | 09878cb3cb2ec6224824b5ebcf8747413e3802e8 (patch) | |
tree | 17096931afd825787d1cd47b3f2eaffff325bedf /src/mongo/base/data_cursor.h | |
parent | de8ad641914eeb56367d06fd465c17ad5e6a3e26 (diff) | |
download | mongo-09878cb3cb2ec6224824b5ebcf8747413e3802e8.tar.gz |
SERVER-15174 Provide some improved semantics for data_cursor and data_view
Diffstat (limited to 'src/mongo/base/data_cursor.h')
-rw-r--r-- | src/mongo/base/data_cursor.h | 172 |
1 files changed, 125 insertions, 47 deletions
diff --git a/src/mongo/base/data_cursor.h b/src/mongo/base/data_cursor.h index 135af3b1a05..f601ea3b740 100644 --- a/src/mongo/base/data_cursor.h +++ b/src/mongo/base/data_cursor.h @@ -35,102 +35,180 @@ namespace mongo { - template <class T> - struct CursorMethods : public T { + class ConstDataCursor : public ConstDataView { + public: + + typedef ConstDataView view_type; - CursorMethods(typename T::bytes_type bytes) : T(bytes) {} + ConstDataCursor(ConstDataView::bytes_type bytes) + : ConstDataView(bytes) { + } - T operator+(std::size_t s) const { - return this->T::view() + s; + ConstDataCursor operator+(std::size_t s) const { + return view() + s; } - T& operator+=(std::size_t s) { - *this = this->T::view() + s; + ConstDataCursor& operator+=(std::size_t s) { + *this = view() + s; return *this; } - T operator-(std::size_t s) const { - return this->T::view() - s; + ConstDataCursor operator-(std::size_t s) const { + return view() - s; } - T& operator-=(std::size_t s) { - *this = this->T::view() - s; + ConstDataCursor& operator-=(std::size_t s) { + *this = view() - s; return *this; } - T& operator++() { - return this->operator+=(1); + ConstDataCursor& operator++() { + return operator+=(1); } - T operator++(int) { - T tmp = *this; + ConstDataCursor operator++(int) { + ConstDataCursor tmp = *this; operator++(); return tmp; } - T& operator--() { - return this->operator-=(1); + ConstDataCursor& operator--() { + return operator-=(1); } - T operator--(int) { - T tmp = *this; + ConstDataCursor operator--(int) { + ConstDataCursor tmp = *this; operator--(); return tmp; } - template <typename U> - void skip() { - *this = this->T::view() + sizeof(U); + template <typename T> + ConstDataCursor& skip() { + *this = view() + sizeof(T); + return *this; } - template <typename U> - U readNativeAndAdvance() { - U out = this->T::template readNative<U>(); - this->skip<U>(); + template <typename T> + ConstDataCursor& readNativeAndAdvance(T* t) { + readNative(t); + skip<T>(); + return *this; + } + + template <typename T> + T readNativeAndAdvance() { + T out; + readNativeAndAdvance(&out); return out; } - template <typename U> - U readLEAndAdvance() { - return littleToNative(readNativeAndAdvance<U>()); + template <typename T> + T readLEAndAdvance() { + return endian::littleToNative(readNativeAndAdvance<T>()); } - template <typename U> - U readBEAndAdvance() { - return bigToNative(readNativeAndAdvance<U>()); + template <typename T> + T readBEAndAdvance() { + return endian::bigToNative(readNativeAndAdvance<T>()); } }; - class ConstDataCursor : public CursorMethods<ConstDataView> { + class DataCursor : public DataView { public: - ConstDataCursor(ConstDataView::bytes_type bytes) : CursorMethods<ConstDataView>(bytes) {} - }; + typedef DataView view_type; - class DataCursor : public CursorMethods<DataView> { - public: + DataCursor(DataView::bytes_type bytes) + : DataView(bytes) {} + + operator ConstDataCursor() const { + return view(); + } + + DataCursor operator+(std::size_t s) const { + return view() + s; + } + + DataCursor& operator+=(std::size_t s) { + *this = view() + s; + return *this; + } + + DataCursor operator-(std::size_t s) const { + return view() - s; + } + + DataCursor& operator-=(std::size_t s) { + *this = view() - s; + return *this; + } + + DataCursor& operator++() { + return operator+=(1); + } + + DataCursor operator++(int) { + DataCursor tmp = *this; + operator++(); + return tmp; + } - DataCursor(DataView::bytes_type bytes) : CursorMethods<DataView>(bytes) {} + DataCursor& operator--() { + return operator-=(1); + } + + DataCursor operator--(int) { + DataCursor tmp = *this; + operator--(); + return tmp; + } template <typename T> - void writeNativeAndAdvance(const T& value) { - this->writeNative(value); - this->skip<T>(); + DataCursor& skip() { + *this = view() + sizeof(T); + return *this; } template <typename T> - void writeLEAndAdvance(const T& value) { - return writeNativeAndAdvance(nativeToLittle(value)); + DataCursor& readNativeAndAdvance(T* t) { + readNative(t); + skip<T>(); + return *this; } template <typename T> - void writeBEAndAdvance(const T& value) { - return writeNativeAndAdvance(nativeToBig(value)); + T readNativeAndAdvance() { + T out; + readNativeAndAdvance(&out); + return out; } - operator ConstDataCursor() const { - return view(); + template <typename T> + T readLEAndAdvance() { + return endian::littleToNative(readNativeAndAdvance<T>()); + } + + template <typename T> + T readBEAndAdvance() { + return endian::bigToNative(readNativeAndAdvance<T>()); + } + + template <typename T> + DataCursor& writeNativeAndAdvance(const T& value) { + writeNative(value); + skip<T>(); + return *this; + } + + template <typename T> + DataCursor& writeLEAndAdvance(const T& value) { + return writeNativeAndAdvance(endian::nativeToLittle(value)); + } + + template <typename T> + DataCursor& writeBEAndAdvance(const T& value) { + return writeNativeAndAdvance(endian::nativeToBig(value)); } }; |