]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Packet.cc
more explicit constructors
[chaz/yoink] / src / Moof / Packet.cc
index 6923e7cb06e5d4a507ec1d8cbe2064e784503a4b..437438effd73803b814123f3bfbea4ab32c45be4 100644 (file)
@@ -100,6 +100,7 @@ Packet::Packet(size_t size) :
        mSize(size),
        mR(0),
        mW(0),
+       mOriginalW(0),
        mBoolR(0),
        mBoolW(0),
        mBoolNumR(0),
@@ -110,12 +111,14 @@ Packet::Packet(const char* data, size_t size) :
        mSize(size),
        mR(0),
        mW(size),
+       mOriginalW(size),
        mBoolR(0),
        mBoolW(0),
        mBoolNumR(0),
        mBoolNumW(0)
 {
        if (mBuffer) memcpy(mBuffer, data, size);
+       else throw std::length_error("out of memory");
 }
 
 
@@ -124,12 +127,14 @@ Packet::Packet(const Packet& copy) :
        mSize(copy.mSize),
        mR(copy.mR),
        mW(copy.mW),
+       mOriginalW(copy.mOriginalW),
        mBoolR(copy.mBoolR),
        mBoolW(copy.mBoolW),
        mBoolNumR(copy.mBoolNumR),
        mBoolNumW(copy.mBoolNumW)
 {
        if (mBuffer) memcpy(mBuffer, copy.mBuffer, mSize);
+       else throw std::length_error("out of memory");
 }
 
 Packet& Packet::operator=(const Packet& copy)
@@ -140,10 +145,13 @@ Packet& Packet::operator=(const Packet& copy)
        mSize = copy.mSize;
        mR = copy.mR;
        mW = copy.mW;
+       mOriginalW = copy.mOriginalW;
        mBoolR = copy.mBoolR;
        mBoolW = copy.mBoolW;
        mBoolNumR = copy.mBoolNumR;
        mBoolNumW = copy.mBoolNumW;
+       if (mBuffer) memcpy(mBuffer, copy.mBuffer, mSize);
+       else throw std::length_error("out of memory");
        return *this;
 }
 
@@ -162,7 +170,7 @@ Packet& Packet::operator<<(bool value)
                mBoolW = mW;
 
                unsigned char byte = 0;
-               if (write(&byte, 1) == 0) return *this;
+               if (write(&byte, 1) == 0) throw std::length_error("out of memory");
        }
 
        if (value) mBuffer[mBoolW] |= (1 << bit);
@@ -174,49 +182,61 @@ Packet& Packet::operator<<(bool value)
 
 Packet& Packet::operator<<(int8_t value)
 {
-       return *this << (uint8_t)value;
+       return *this << reinterpret_cast<uint8_t&>(value);
 }
 
 Packet& Packet::operator<<(int16_t value)
 {
-       return *this << (uint16_t)value;
+       return *this << reinterpret_cast<uint16_t&>(value);
 }
 
 Packet& Packet::operator<<(int32_t value)
 {
-       return *this << (uint32_t)value;
+       return *this << reinterpret_cast<uint32_t&>(value);
 }
 
 Packet& Packet::operator<<(int64_t value)
 {
-       return *this << (uint64_t)value;
+       return *this << reinterpret_cast<uint64_t&>(value);
 }
 
 
 Packet& Packet::operator<<(uint8_t value)
 {
-       write(&value, sizeof(value));
+       if (write(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::length_error("out of memory");
+       }
        return *this;
 }
 
 Packet& Packet::operator<<(uint16_t value)
 {
        value = htons(value);
-       write(&value, sizeof(value));
+       if (write(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::length_error("out of memory");
+       }
        return *this;
 }
 
 Packet& Packet::operator<<(uint32_t value)
 {
        value = htonl(value);
-       write(&value, sizeof(value));
+       if (write(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::length_error("out of memory");
+       }
        return *this;
 }
 
 Packet& Packet::operator<<(uint64_t value)
 {
        value = htonll(value);
-       write(&value, sizeof(value));
+       if (write(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::length_error("out of memory");
+       }
        return *this;
 }
 
@@ -225,7 +245,10 @@ Packet& Packet::operator<<(float value)
        // XXX: assumes the ieee-754
        uint32_t* integer = reinterpret_cast<uint32_t*>(&value);
        *integer = htonl(*integer);
-       write(integer, sizeof(value));
+       if (write(integer, sizeof(value)) != sizeof(value))
+       {
+               throw std::length_error("out of memory");
+       }
        return *this;
 }
 
@@ -234,7 +257,10 @@ Packet& Packet::operator<<(double value)
        // XXX: assumes the ieee-754
        uint64_t* integer = reinterpret_cast<uint64_t*>(&value);
        *integer = htonll(*integer);
-       write(integer, sizeof(value));
+       if (write(integer, sizeof(value)) != sizeof(value))
+       {
+               throw std::length_error("out of memory");
+       }
        return *this;
 }
 
@@ -254,7 +280,7 @@ size_t Packet::write(const void* bytes, size_t size)
                }
                if (!mBuffer) return 0;
        }
-       memcpy(&mBuffer[mW], bytes, nBytes);
+       memcpy(mBuffer + mW, bytes, nBytes);
        mW += nBytes;
        return nBytes;
 }
@@ -268,7 +294,7 @@ Packet& Packet::operator>>(bool& value)
                mBoolR = mR;
 
                unsigned char byte = 0;
-               if (read(&byte, 1) == 0) return *this;
+               if (read(&byte, 1) == 0) throw std::out_of_range("end of packet");
        }
 
        value = 1 & (mBuffer[mBoolR] >> bit);
@@ -279,47 +305,59 @@ Packet& Packet::operator>>(bool& value)
 
 Packet& Packet::operator>>(int8_t& value)
 {
-       return *this >> (uint8_t&)value;
+       return *this >> reinterpret_cast<uint8_t&>(value);
 }
 
 Packet& Packet::operator>>(int16_t& value)
 {
-       return *this >> (uint16_t&)value;
+       return *this >> reinterpret_cast<uint16_t&>(value);
 }
 
 Packet& Packet::operator>>(int32_t& value)
 {
-       return *this >> (uint32_t&)value;
+       return *this >> reinterpret_cast<uint32_t&>(value);
 }
 
 Packet& Packet::operator>>(int64_t& value)
 {
-       return *this >> (uint64_t&)value;
+       return *this >> reinterpret_cast<uint64_t&>(value);
 }
 
 Packet& Packet::operator>>(uint8_t& value)
 {
-       read(&value, sizeof(value));
+       if (read(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::out_of_range("end of packet");
+       }
        return *this;
 }
 
 Packet& Packet::operator>>(uint16_t& value)
 {
-       read(&value, sizeof(value));
+       if (read(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::out_of_range("end of packet");
+       }
        value = ntohs(value);
        return *this;
 }
 
 Packet& Packet::operator>>(uint32_t& value)
 {
-       read(&value, sizeof(value));
+       if (read(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::out_of_range("end of packet");
+       }
        value = ntohl(value);
        return *this;
 }
 
 Packet& Packet::operator>>(uint64_t& value)
 {
-       read(&value, sizeof(value));
+       if (read(&value, sizeof(value)) != sizeof(value))
+       {
+               throw std::out_of_range("end of packet");
+       }
        value = ntohll(value);
        return *this;
 }
@@ -328,7 +366,10 @@ Packet& Packet::operator>>(float& value)
 {
        // XXX: assumes the ieee-754
        uint32_t* integer = reinterpret_cast<uint32_t*>(&value);
-       read(integer, sizeof(value));
+       if (read(integer, sizeof(value)) != sizeof(value))
+       {
+               throw std::out_of_range("end of packet");
+       }
        *integer = htonl(*integer);
        return *this;
 }
@@ -337,7 +378,10 @@ Packet& Packet::operator>>(double& value)
 {
        // XXX: assumes the ieee-754
        uint64_t* integer = reinterpret_cast<uint64_t*>(&value);
-       read(integer, sizeof(value));
+       if (read(integer, sizeof(value)) != sizeof(value))
+       {
+               throw std::out_of_range("end of packet");
+       }
        *integer = htonll(*integer);
        return *this;
 }
@@ -345,7 +389,7 @@ Packet& Packet::operator>>(double& value)
 size_t Packet::read(void* bytes, size_t size)
 {
        size_t nBytes = std::min(size, mW - mR);
-       memcpy(bytes, &mBuffer[mR], nBytes);
+       memcpy(bytes, mBuffer + mR, nBytes);
        mR += nBytes;
        return nBytes;
 }
@@ -362,5 +406,16 @@ void Packet::clear()
 }
 
 
+void Packet::reset()
+{
+       mR = 0;
+       mW = mOriginalW;
+       mBoolR = 0;
+       mBoolW = 0;
+       mBoolNumR = 0;
+       mBoolNumW = 0;
+}
+
+
 } // namespace Mf
 
This page took 0.025902 seconds and 4 git commands to generate.