Skip to content

Instantly share code, notes, and snippets.

@Naios
Created April 26, 2015 20:10
Show Gist options
  • Save Naios/75f6952c61c2b9324eb2 to your computer and use it in GitHub Desktop.
Save Naios/75f6952c61c2b9324eb2 to your computer and use it in GitHub Desktop.
diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h
index 0fa500c..caa162f 100644
--- a/src/server/shared/Packets/ByteBuffer.h
+++ b/src/server/shared/Packets/ByteBuffer.h
@@ -23,6 +23,7 @@
#include "Errors.h"
#include "ByteConverter.h"
#include "Util.h"
+#include "Common.h"
#include <exception>
#include <list>
@@ -341,6 +342,83 @@ class ByteBuffer
return *this;
}
+ ByteBuffer &operator<<(Optional<uint8> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<uint16> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<uint32> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<uint64> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<int8> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<int16> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<int32> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<int64> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<float> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<double> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
+ ByteBuffer &operator<<(Optional<std::string> const& value)
+ {
+ if (value)
+ (*this) << (*value);
+ return *this;
+ }
+
ByteBuffer &operator>>(bool &value)
{
value = read<char>() > 0 ? true : false;
@@ -425,6 +503,85 @@ class ByteBuffer
return *this;
}
+ ByteBuffer &operator>>(Optional<bool> &value)
+ {
+ value = read<char>() > 0 ? true : false;
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<uint8> &value)
+ {
+ value = read<uint8>();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<uint16> &value)
+ {
+ value = read<uint16>();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<uint32> &value)
+ {
+ value = read<uint32>();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<uint64> &value)
+ {
+ value = read<uint64>();
+ return *this;
+ }
+
+ //signed as in 2e complement
+ ByteBuffer &operator>>(Optional<int8> &value)
+ {
+ value = read<int8>();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<int16> &value)
+ {
+ value = read<int16>();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<int32> &value)
+ {
+ value = read<int32>();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<int64> &value)
+ {
+ value = read<int64>();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<float>& value)
+ {
+ value = read<float>();
+ if (!std::isfinite(*value))
+ throw ByteBufferException();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<double>& value)
+ {
+ value = read<double>();
+ if (!std::isfinite(*value))
+ throw ByteBufferException();
+ return *this;
+ }
+
+ ByteBuffer &operator>>(Optional<std::string>& value)
+ {
+ std::string str;
+ (*this) >> str;
+ value = str;
+ return *this;
+ }
+
uint8& operator[](size_t const pos)
{
if (pos >= size())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment