summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorAndrew Morrow <acm@mongodb.com>2014-08-22 12:41:06 -0400
committerAndrew Morrow <acm@mongodb.com>2014-09-08 17:44:20 -0400
commit09878cb3cb2ec6224824b5ebcf8747413e3802e8 (patch)
tree17096931afd825787d1cd47b3f2eaffff325bedf /src/mongo
parentde8ad641914eeb56367d06fd465c17ad5e6a3e26 (diff)
downloadmongo-09878cb3cb2ec6224824b5ebcf8747413e3802e8.tar.gz
SERVER-15174 Provide some improved semantics for data_cursor and data_view
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/base/data_cursor.h172
-rw-r--r--src/mongo/base/data_view.h23
-rw-r--r--src/mongo/base/data_view_test.cpp4
-rw-r--r--src/mongo/db/dbmessage.h8
-rw-r--r--src/mongo/platform/endian.h3
-rw-r--r--src/mongo/platform/endian_test.cpp2
-rw-r--r--src/mongo/util/net/message.h8
7 files changed, 153 insertions, 67 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));
}
};
diff --git a/src/mongo/base/data_view.h b/src/mongo/base/data_view.h
index f0430d6a504..9b4d43762e8 100644
--- a/src/mongo/base/data_view.h
+++ b/src/mongo/base/data_view.h
@@ -47,20 +47,26 @@ namespace mongo {
}
template<typename T>
+ const ConstDataView& readNative(T* t, size_t offset = 0) const {
+ std::memcpy(t, view(offset), sizeof(*t));
+ return *this;
+ }
+
+ template<typename T>
T readNative(std::size_t offset = 0) const {
T t;
- std::memcpy(&t, view(offset), sizeof(t));
+ readNative(&t, offset);
return t;
}
template<typename T>
T readLE(std::size_t offset = 0) const {
- return littleToNative(readNative<T>(offset));
+ return endian::littleToNative(readNative<T>(offset));
}
template<typename T>
T readBE(std::size_t offset = 0) const {
- return bigToNative(readNative<T>(offset));
+ return endian::bigToNative(readNative<T>(offset));
}
private:
@@ -83,18 +89,19 @@ namespace mongo {
}
template<typename T>
- void writeNative(const T& value, std::size_t offset = 0) {
+ DataView& writeNative(const T& value, std::size_t offset = 0) {
std::memcpy(view(offset), &value, sizeof(value));
+ return *this;
}
template<typename T>
- void writeLE(const T& value, std::size_t offset = 0) {
- return writeNative(nativeToLittle(value), offset);
+ DataView& writeLE(const T& value, std::size_t offset = 0) {
+ return writeNative(endian::nativeToLittle(value), offset);
}
template<typename T>
- void writeBE(const T& value, std::size_t offset = 0) {
- return writeNative(nativeToBig(value), offset);
+ DataView& writeBE(const T& value, std::size_t offset = 0) {
+ return writeNative(endian::nativeToBig(value), offset);
}
};
diff --git a/src/mongo/base/data_view_test.cpp b/src/mongo/base/data_view_test.cpp
index 2f224d5a71b..288d44f0f8e 100644
--- a/src/mongo/base/data_view_test.cpp
+++ b/src/mongo/base/data_view_test.cpp
@@ -38,8 +38,8 @@ namespace mongo {
TEST(DataView, ConstDataView) {
char buf[sizeof(uint32_t) * 3];
uint32_t native = 1234;
- uint32_t le = nativeToLittle(native);
- uint32_t be = nativeToBig(native);
+ uint32_t le = endian::nativeToLittle(native);
+ uint32_t be = endian::nativeToBig(native);
std::memcpy(buf, &native, sizeof(uint32_t));
std::memcpy(buf + sizeof(uint32_t), &le, sizeof(uint32_t));
diff --git a/src/mongo/db/dbmessage.h b/src/mongo/db/dbmessage.h
index 1b15a58030e..85d4ed731b4 100644
--- a/src/mongo/db/dbmessage.h
+++ b/src/mongo/db/dbmessage.h
@@ -155,15 +155,15 @@ namespace mongo {
}
void setCursorId(int64_t value) {
- return storage().writeLE(value, offsetof(Layout, cursorId));
+ storage().writeLE(value, offsetof(Layout, cursorId));
}
void setStartingFrom(int32_t value) {
- return storage().writeLE(value, offsetof(Layout, startingFrom));
+ storage().writeLE(value, offsetof(Layout, startingFrom));
}
void setNReturned(int32_t value) {
- return storage().writeLE(value, offsetof(Layout, nReturned));
+ storage().writeLE(value, offsetof(Layout, nReturned));
}
int32_t getResultFlags() {
@@ -171,7 +171,7 @@ namespace mongo {
}
void setResultFlags(int32_t value) {
- return DataView(msgdata().data()).writeLE(value);
+ DataView(msgdata().data()).writeLE(value);
}
void setResultFlagsToOk() {
diff --git a/src/mongo/platform/endian.h b/src/mongo/platform/endian.h
index fed9ca95c69..0d644d6b512 100644
--- a/src/mongo/platform/endian.h
+++ b/src/mongo/platform/endian.h
@@ -171,8 +171,6 @@ namespace endian {
((v & 0xFF00000000000000ULL) >> 56);
}
-} // namespace endian
-
template<typename T>
struct ByteOrderConverter;
@@ -452,6 +450,7 @@ namespace endian {
return ByteOrderConverter<T>::littleToNative(t);
}
+} // namespace endian
} // namespace mongo
#undef MONGO_UINT16_SWAB
diff --git a/src/mongo/platform/endian_test.cpp b/src/mongo/platform/endian_test.cpp
index 733f9c69b0c..ab12b8c3fa3 100644
--- a/src/mongo/platform/endian_test.cpp
+++ b/src/mongo/platform/endian_test.cpp
@@ -33,6 +33,8 @@
namespace mongo {
+ using namespace endian;
+
TEST( EndianTest, TestSlow16 ) {
uint8_t le_buf[] = { 0x01, 0x02 };
uint8_t be_buf[] = { 0x02, 0x01 };
diff --git a/src/mongo/util/net/message.h b/src/mongo/util/net/message.h
index 0fd7ac22312..7fdd7ebf91d 100644
--- a/src/mongo/util/net/message.h
+++ b/src/mongo/util/net/message.h
@@ -169,19 +169,19 @@ namespace mongo {
}
void setMessageLength(int32_t value) {
- return data().writeLE(value, offsetof(Layout, messageLength));
+ data().writeLE(value, offsetof(Layout, messageLength));
}
void setRequestID(int32_t value) {
- return data().writeLE(value, offsetof(Layout, requestID));
+ data().writeLE(value, offsetof(Layout, requestID));
}
void setResponseTo(int32_t value) {
- return data().writeLE(value, offsetof(Layout, responseTo));
+ data().writeLE(value, offsetof(Layout, responseTo));
}
void setOpCode(int32_t value) {
- return data().writeLE(value, offsetof(Layout, opCode));
+ data().writeLE(value, offsetof(Layout, opCode));
}
private: