]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Packet.cc
more explicit constructors
[chaz/yoink] / src / Moof / Packet.cc
index 8cd45b53baff4e71a9e5ac5a4e3381ebb59972b4..437438effd73803b814123f3bfbea4ab32c45be4 100644 (file)
@@ -100,24 +100,59 @@ Packet::Packet(size_t size) :
        mSize(size),
        mR(0),
        mW(0),
+       mOriginalW(0),
        mBoolR(0),
        mBoolW(0),
        mBoolNumR(0),
-       mBoolNumW(0)
-{
-}
+       mBoolNumW(0) {}
 
 Packet::Packet(const char* data, size_t size) :
        mBuffer((char*)malloc(size)),
        mSize(size),
        mR(0),
        mW(size),
+       mOriginalW(size),
        mBoolR(0),
        mBoolW(0),
        mBoolNumR(0),
        mBoolNumW(0)
 {
-       memcpy(mBuffer, data, size);
+       if (mBuffer) memcpy(mBuffer, data, size);
+       else throw std::length_error("out of memory");
+}
+
+
+Packet::Packet(const Packet& copy) :
+       mBuffer((char*)malloc(copy.mSize)),
+       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)
+{
+       free(mBuffer);
+
+       mBuffer = (char*)malloc(copy.mSize);
+       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;
 }
 
 
@@ -135,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);
@@ -147,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;
 }
 
@@ -198,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;
 }
 
@@ -207,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;
 }
 
@@ -227,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;
 }
@@ -241,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);
@@ -252,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;
 }
@@ -301,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;
 }
@@ -310,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;
 }
@@ -318,11 +389,33 @@ 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;
 }
 
 
+void Packet::clear()
+{
+       mR = 0;
+       mW = 0;
+       mBoolR = 0;
+       mBoolW = 0;
+       mBoolNumR = 0;
+       mBoolNumW = 0;
+}
+
+
+void Packet::reset()
+{
+       mR = 0;
+       mW = mOriginalW;
+       mBoolR = 0;
+       mBoolW = 0;
+       mBoolNumR = 0;
+       mBoolNumW = 0;
+}
+
+
 } // namespace Mf
 
This page took 0.022953 seconds and 4 git commands to generate.