00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_SERIALIZER_HPP_)
00012 #define _XRB_SERIALIZER_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include <stdio.h>
00017 #include <string>
00018
00019 #include "xrb_bitarray.hpp"
00020 #include "xrb_bitcache.hpp"
00021 #include "xrb_color.hpp"
00022 #include "xrb_enums.hpp"
00023 #include "xrb_matrix2.hpp"
00024 #include "xrb_screencoord.hpp"
00025 #include "xrb_simpletransform2.hpp"
00026 #include "xrb_transform2.hpp"
00027 #include "xrb_vector.hpp"
00028
00029 namespace Xrb
00030 {
00031
00032
00033
00039 class Serializer
00040 {
00041 protected:
00042
00043 enum
00044 {
00045
00046 MAX_SUPPORTED_STRING_BUFFER_SIZE = 65536,
00047 };
00048
00049 public:
00050
00051 enum
00052 {
00053
00054 MAX_SUPPORTED_STRING_LENGTH = MAX_SUPPORTED_STRING_BUFFER_SIZE - 1
00055 };
00056
00057 Serializer ();
00058 virtual ~Serializer ();
00059
00060
00061
00062
00063
00068 inline bool IsOpen () const
00069 {
00070 return m_is_open;
00071 }
00078 inline IODirection GetIODirection () const
00079 {
00080 return m_io_direction;
00081 }
00086 virtual bool IsAtEnd () const = 0;
00093 virtual bool HasFewerThan8BitsLeft () const = 0;
00098 inline IOError Error () const
00099 {
00100 return m_error;
00101 }
00106 inline char const *ErrorString () const
00107 {
00108 return Util::IOErrorString(Error());
00109 }
00110
00111
00112
00113
00114
00128 virtual Sint32 ReadSignedBits (Uint32 bit_count) = 0;
00140 virtual void WriteSignedBits (Sint32 value, Uint32 bit_count) = 0;
00141
00150 virtual Uint32 ReadUnsignedBits (Uint32 bit_count) = 0;
00159 virtual void WriteUnsignedBits (Uint32 value, Uint32 bit_count) = 0;
00160
00167 virtual bool ReadBool () = 0;
00174 virtual void WriteBool (bool value) = 0;
00175
00185 template <Uint32 bit_count>
00186 BitArray<bit_count> ReadBitArray ();
00194 template <Uint32 bit_count>
00195 void WriteBitArray (BitArray<bit_count> const &value);
00196
00211 template <Uint32 bit_count>
00212 BitArray<bit_count> ReadBitArray (Uint32 bits_to_read);
00226 template <Uint32 bit_count>
00227 void WriteBitArray (BitArray<bit_count> const &value, Uint32 bits_to_write);
00228
00235 virtual Sint8 ReadSint8 () = 0;
00242 virtual void WriteSint8 (Sint8 value) = 0;
00243
00250 virtual Uint8 ReadUint8 () = 0;
00257 virtual void WriteUint8 (Uint8 value) = 0;
00258
00265 virtual Sint16 ReadSint16 () = 0;
00272 virtual void WriteSint16 (Sint16 value) = 0;
00273
00280 virtual Uint16 ReadUint16 () = 0;
00287 virtual void WriteUint16 (Uint16 value) = 0;
00288
00295 virtual Sint32 ReadSint32 () = 0;
00302 virtual void WriteSint32 (Sint32 value) = 0;
00303
00310 virtual Uint32 ReadUint32 () = 0;
00317 virtual void WriteUint32 (Uint32 value) = 0;
00318
00325
00326 ScreenCoord ReadScreenCoord () { return ReadSint32(); }
00333
00334 void WriteScreenCoord (ScreenCoord value) { WriteSint32(value); }
00335
00345 virtual void ReadFloat (float *destination) = 0;
00354 virtual void WriteFloat (float value) = 0;
00355
00365 virtual void ReadFloat (double *destination) = 0;
00374 virtual void WriteFloat (double value) = 0;
00375
00402 virtual Uint32 ReadBufferString (char *destination, Uint32 destination_size) = 0;
00430 virtual Uint32 WriteBufferString (char const *source, Uint32 source_size) = 0;
00431
00448 virtual Uint32 ReadString (char **destination);
00457 virtual Uint32 WriteString (char const *source);
00458
00470 inline std::string ReadStdString (Uint32 *string_length = NULL)
00471 {
00472 std::string retval;
00473 ReadStdString(&retval, string_length);
00474 return retval;
00475 }
00488 void ReadStdString (std::string *destination, Uint32 *string_length = NULL);
00500 void WriteStdString (std::string const &source, Uint32 *string_length = NULL);
00501
00508 inline Color ReadColor ()
00509 {
00510 Color retval;
00511 ReadColor(&retval);
00512 return retval;
00513 }
00520 virtual void ReadColor (Color *destination) = 0;
00527 virtual void WriteColor (Color const &value) = 0;
00528
00535 inline ScreenCoordVector2 ReadScreenCoordVector2 ()
00536 {
00537 ScreenCoordVector2 retval;
00538 ReadScreenCoordVector2(&retval);
00539 return retval;
00540 }
00547 virtual void ReadScreenCoordVector2 (ScreenCoordVector2 *destination) = 0;
00554 virtual void WriteScreenCoordVector2 (ScreenCoordVector2 const &value) = 0;
00555
00562 inline FloatVector2 ReadFloatVector2 ()
00563 {
00564 FloatVector2 retval;
00565 ReadFloatVector2(&retval);
00566 return retval;
00567 }
00574 virtual void ReadFloatVector2 (FloatVector2 *destination) = 0;
00581 virtual void WriteFloatVector2 (FloatVector2 const &value) = 0;
00582
00589 inline FloatSimpleTransform2 ReadFloatSimpleTransform2 ()
00590 {
00591 FloatSimpleTransform2 retval;
00592 ReadFloatSimpleTransform2(&retval);
00593 return retval;
00594 }
00602 virtual void ReadFloatSimpleTransform2 (FloatSimpleTransform2 *destination) = 0;
00609 virtual void WriteFloatSimpleTransform2 (FloatSimpleTransform2 const &value) = 0;
00610
00617 inline FloatMatrix2 ReadFloatMatrix2 ()
00618 {
00619 FloatMatrix2 retval;
00620 ReadFloatMatrix2(&retval);
00621 return retval;
00622 }
00629 virtual void ReadFloatMatrix2 (FloatMatrix2 *destination) = 0;
00636 virtual void WriteFloatMatrix2 (FloatMatrix2 const &value) = 0;
00637
00644 inline FloatTransform2 ReadFloatTransform2 ()
00645 {
00646 FloatTransform2 retval(true);
00647 ReadFloatTransform2(&retval);
00648 return retval;
00649 }
00657 virtual void ReadFloatTransform2 (FloatTransform2 *destination) = 0;
00664 virtual void WriteFloatTransform2 (FloatTransform2 const &value) = 0;
00665
00666 protected:
00667
00668
00669
00670
00671
00676 inline void SetIsOpen (bool is_open)
00677 {
00678 m_is_open = is_open;
00679 }
00684 inline void SetIODirection (IODirection io_direction)
00685 {
00686 m_io_direction = io_direction;
00687 }
00691 inline void SetError (IOError error) const
00692 {
00693 m_error = error;
00694 }
00695
00696
00697
00698
00699
00707 virtual void FlushWriteCache () = 0;
00708
00710 bool m_is_open;
00712 IODirection m_io_direction;
00714 mutable IOError m_error;
00715 };
00716
00717 template <Uint32 bit_count>
00718 BitArray<bit_count> Serializer::ReadBitArray ()
00719 {
00720 ASSERT1(bit_count <= MAX_SUPPORTED_STRING_BUFFER_SIZE * 8);
00721
00722 BitArray<bit_count> retval;
00723
00724 for (Uint32 current_word = BitArray<bit_count>::HIGHEST_WORD_INDEX;
00725 current_word <= BitArray<bit_count>::HIGHEST_WORD_INDEX;
00726 --current_word)
00727 {
00728 Uint32 bits_to_read_for_current_word =
00729 current_word == BitArray<bit_count>::HIGHEST_WORD_INDEX ?
00730 bit_count % BitArray<bit_count>::WORD_SIZE_IN_BITS :
00731 BitArray<bit_count>::WORD_SIZE_IN_BITS;
00732 if (bits_to_read_for_current_word == 0)
00733 bits_to_read_for_current_word = BitArray<bit_count>::WORD_SIZE_IN_BITS;
00734
00735 retval.SetWord(
00736 current_word,
00737 ReadUnsignedBits(bits_to_read_for_current_word));
00738 if (Error() != IOE_NONE)
00739 return retval;
00740 }
00741
00742 return retval;
00743 }
00744
00745 template <Uint32 bit_count>
00746 void Serializer::WriteBitArray (BitArray<bit_count> const &value)
00747 {
00748 ASSERT1(bit_count <= MAX_SUPPORTED_STRING_BUFFER_SIZE * 8);
00749
00750 for (Uint32 current_word = BitArray<bit_count>::HIGHEST_WORD_INDEX;
00751 current_word <= BitArray<bit_count>::HIGHEST_WORD_INDEX;
00752 --current_word)
00753 {
00754 Uint32 bits_to_write_for_current_word =
00755 current_word == BitArray<bit_count>::HIGHEST_WORD_INDEX ?
00756 bit_count % BitArray<bit_count>::WORD_SIZE_IN_BITS :
00757 BitArray<bit_count>::WORD_SIZE_IN_BITS;
00758 if (bits_to_write_for_current_word == 0)
00759 bits_to_write_for_current_word = BitArray<bit_count>::WORD_SIZE_IN_BITS;
00760
00761 WriteUnsignedBits(
00762 value.Word(current_word),
00763 bits_to_write_for_current_word);
00764 if (Error() != IOE_NONE)
00765 return;
00766 }
00767 }
00768
00769 template <Uint32 bit_count>
00770 BitArray<bit_count> Serializer::ReadBitArray (Uint32 const bits_to_read)
00771 {
00772 ASSERT1(bit_count <= MAX_SUPPORTED_STRING_BUFFER_SIZE * 8);
00773 ASSERT1(bits_to_read > 0);
00774 ASSERT1(bits_to_read <= bit_count);
00775
00776 BitArray<bit_count> retval;
00777 Uint32 highest_word_to_read = bits_to_read / BitArray<bit_count>::WORD_SIZE_IN_BITS;
00778
00779 for (Uint32 current_word = highest_word_to_read;
00780 current_word <= highest_word_to_read;
00781 --current_word)
00782 {
00783 Uint32 bits_to_read_for_current_word =
00784 current_word == highest_word_to_read ?
00785 bits_to_read % BitArray<bit_count>::WORD_SIZE_IN_BITS :
00786 BitArray<bit_count>::WORD_SIZE_IN_BITS;
00787 if (bits_to_read_for_current_word == 0)
00788 bits_to_read_for_current_word = BitArray<bit_count>::WORD_SIZE_IN_BITS;
00789
00790 retval.SetWord(
00791 current_word,
00792 ReadUnsignedBits(bits_to_read_for_current_word));
00793 if (Error() != IOE_NONE)
00794 return retval;
00795 }
00796
00797
00798 for (Uint32 current_word = highest_word_to_read + 1;
00799 current_word <= BitArray<bit_count>::HIGHEST_WORD_INDEX;
00800 ++current_word)
00801 {
00802 retval.SetWord(current_word, 0);
00803 }
00804
00805 return retval;
00806 }
00807
00808 template <Uint32 bit_count>
00809 void Serializer::WriteBitArray (
00810 BitArray<bit_count> const &value,
00811 Uint32 const bits_to_write)
00812 {
00813 ASSERT1(bit_count <= MAX_SUPPORTED_STRING_BUFFER_SIZE * 8);
00814 ASSERT1(bits_to_write > 0);
00815 ASSERT1(bits_to_write <= bit_count);
00816
00817 Uint32 highest_word_to_write = bits_to_write / BitArray<bit_count>::WORD_SIZE_IN_BITS;
00818
00819 for (Uint32 current_word = highest_word_to_write;
00820 current_word <= highest_word_to_write;
00821 --current_word)
00822 {
00823 Uint32 bits_to_write_for_current_word =
00824 current_word == highest_word_to_write ?
00825 bits_to_write % BitArray<bit_count>::WORD_SIZE_IN_BITS :
00826 BitArray<bit_count>::WORD_SIZE_IN_BITS;
00827 if (bits_to_write_for_current_word == 0)
00828 bits_to_write_for_current_word = BitArray<bit_count>::WORD_SIZE_IN_BITS;
00829
00830 WriteUnsignedBits(
00831 value.Word(current_word),
00832 bits_to_write_for_current_word);
00833 if (Error() != IOE_NONE)
00834 return;
00835 }
00836 }
00837
00838 }
00839
00840 #endif // !defined(_XRB_SERIALIZER_HPP_)