diff -r a76af67a3a4b -r 7b156b764d11 sources/network/bytestream.h --- a/sources/network/bytestream.h Sat Jan 09 02:35:00 2016 +0200 +++ b/sources/network/bytestream.h Sat Jan 09 17:41:21 2016 +0200 @@ -31,10 +31,14 @@ #pragma once #include #include "../main.h" +BEGIN_ZFC_NAMESPACE class String; -enum { MAX_NETWORK_STRING = 0x800 }; +enum +{ + MAX_NETWORK_STRING = 0x800 +}; // TODO: Make able to handle big-endian too class Bytestream @@ -48,10 +52,9 @@ IOError (String message) : m_message (message) {} - inline METHOD - what() const throw() -> const char* + const char* what() const throw() { - return m_message.chars(); + return m_message; } }; @@ -61,37 +64,80 @@ Bytestream (const Bytestream& other); ~Bytestream(); - inline METHOD allocated_size() const -> unsigned long; - inline METHOD bytes_left() const -> unsigned long; - METHOD clear() -> void; - inline METHOD data() -> unsigned char*; - inline METHOD data() const -> const unsigned char*; - METHOD grow_to_fit (unsigned long bytes) -> void; - inline METHOD position() const -> unsigned long; - METHOD read (unsigned char* buffer, unsigned long length) -> void; - METHOD read_byte() -> char; - METHOD read_short() -> short int; - METHOD read_long() -> long int; - METHOD read_string() -> String; - METHOD read_float() -> float; - METHOD resize (unsigned long length) -> void; - inline METHOD rewind() -> void; - inline METHOD seek (unsigned long pos) -> void; - inline METHOD to_vector() const -> Vector; - METHOD write (const unsigned char* val, unsigned int length) -> void; - METHOD write_buffer (const Bytestream& other) -> void; - METHOD write_buffer (const Vector& other) -> void; - METHOD write_byte (char val) -> void; - METHOD write_double (double val) -> void; - METHOD write_float (float val) -> void; - METHOD write_long (long int val) -> void; - METHOD write_short (short int val) -> void; - METHOD write_string (const String& val) -> void; - inline METHOD written_length() const -> unsigned long; + void clear(); + void grow_to_fit (unsigned long bytes); + void read (unsigned char* buffer, unsigned long length); + int8_t read_byte(); + int32_t read_long(); + int16_t read_short(); + String read_string(); + float read_float(); + void resize (unsigned long length); + void write (const unsigned char* val, unsigned int length); + void write_buffer (const Bytestream& other); + void write_buffer (const Vector& other); + void write_byte (int8_t val); + void write_double (double val); + void write_float (float val); + void write_long (int32_t val); + void write_short (int16_t val); + void write_string (const String& val); + + Bytestream& operator= (const Bytestream& other); + + unsigned long allocated_size() const + { + return m_allocatedSize; + } + + unsigned long bytes_left() const + { + return m_writtenLength - (m_cursor - &m_data[0]); + } - inline METHOD operator[] (unsigned long idx) -> unsigned char&; - inline METHOD operator[] (unsigned long idx) const -> unsigned char; - METHOD operator= (const Bytestream& other) -> Bytestream&; + inline unsigned char* data() + { + return m_data; + } + inline const unsigned char* data() const + { + return m_data; + } + + unsigned long position() const + { + return m_cursor - m_data; + } + + void rewind() + { + m_cursor = m_data; + } + + void seek (unsigned long pos) + { + m_cursor = m_data + pos; + } + + Vector to_vector() const + { + return Vector (m_data, m_writtenLength); + } + + unsigned long written_length() const + { + return m_writtenLength; + } + + unsigned char& operator[] (unsigned long idx) + { + return m_data[idx]; + } + + unsigned char operator[] (unsigned long idx) const + { + return m_data[idx]; + } private: unsigned char* m_data; @@ -99,104 +145,14 @@ unsigned long m_allocatedSize; unsigned long m_writtenLength; - METHOD init (const unsigned char* data, unsigned long length) -> void; - METHOD write (unsigned char val) -> void; - METHOD ensure_read_space (unsigned int bytes) -> void; - inline METHOD space_left() const -> unsigned long; + void init (const unsigned char* data, unsigned long length); + void write (unsigned char val); + void ensure_read_space (unsigned int bytes); + + unsigned long space_left() const + { + return m_allocatedSize - m_writtenLength; + } }; -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::allocated_size() const -> unsigned long -{ - return m_allocatedSize; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::written_length() const -> unsigned long -{ - return m_writtenLength; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::operator[] (unsigned long idx) -> unsigned char& -{ - return m_data[idx]; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::operator[] (unsigned long idx) const -> unsigned char -{ - return m_data[idx]; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::position() const -> unsigned long -{ - return m_cursor - m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::seek (unsigned long pos) -> void -{ - m_cursor = m_data + pos; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::rewind() -> void -{ - m_cursor = m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::bytes_left() const -> unsigned long -{ - return (m_writtenLength - (m_cursor - &m_data[0])); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::space_left() const -> unsigned long -{ - return (m_allocatedSize - m_writtenLength); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::data() -> unsigned char* -{ - return m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::data() const -> const unsigned char* -{ - return m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::to_vector() const -> Vector -{ - return Vector (m_data, m_writtenLength); -} +END_ZFC_NAMESPACE