ovr_sdk

diff LibOVR/Src/Net/OVR_BitStream.h @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LibOVR/Src/Net/OVR_BitStream.h	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,1744 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +PublicHeader:   n/a
     1.7 +Filename    :   OVR_BitStream.h
     1.8 +Content     :   A generic serialization toolkit for packing data to a binary stream.
     1.9 +Created     :   June 10, 2014
    1.10 +Authors     :   Kevin Jenkins
    1.11 +
    1.12 +Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.
    1.13 +
    1.14 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
    1.15 +you may not use the Oculus VR Rift SDK except in compliance with the License, 
    1.16 +which is provided at the time of installation or download, or which 
    1.17 +otherwise accompanies this software in either electronic or hard copy form.
    1.18 +
    1.19 +You may obtain a copy of the License at
    1.20 +
    1.21 +http://www.oculusvr.com/licenses/LICENSE-3.2 
    1.22 +
    1.23 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
    1.24 +distributed under the License is distributed on an "AS IS" BASIS,
    1.25 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.26 +See the License for the specific language governing permissions and
    1.27 +limitations under the License.
    1.28 +
    1.29 +************************************************************************************/
    1.30 +
    1.31 +#ifndef OVR_Bitstream_h
    1.32 +#define OVR_Bitstream_h
    1.33 +
    1.34 +#include <math.h>
    1.35 +#include "../Kernel/OVR_Types.h"
    1.36 +#include "../Kernel/OVR_Std.h"
    1.37 +#include "../Kernel/OVR_String.h"
    1.38 +
    1.39 +namespace OVR { namespace Net {
    1.40 +
    1.41 +typedef uint32_t BitSize_t;
    1.42 +#define BITSTREAM_STACK_ALLOCATION_SIZE 256
    1.43 +#define BITS_TO_BYTES(x) (((x)+7)>>3)
    1.44 +#define BYTES_TO_BITS(x) ((x)<<3)
    1.45 +
    1.46 +
    1.47 +//-----------------------------------------------------------------------------
    1.48 +// BitStream
    1.49 +
    1.50 +// Generic serialization class to binary stream
    1.51 +class BitStream : public NewOverrideBase
    1.52 +{
    1.53 +public:
    1.54 +	/// Default Constructor
    1.55 +	BitStream();
    1.56 +
    1.57 +	/// \brief Create the bitstream, with some number of bytes to immediately allocate.
    1.58 +	/// \details There is no benefit to calling this, unless you know exactly how many bytes you need and it is greater than BITSTREAM_STACK_ALLOCATION_SIZE.
    1.59 +	/// In that case all it does is save you one or more realloc calls.
    1.60 +	/// \param[in] initialBytesToAllocate the number of bytes to pre-allocate.
    1.61 +	BitStream( const unsigned int initialBytesToAllocate );
    1.62 +
    1.63 +	/// \brief Initialize the BitStream, immediately setting the data it contains to a predefined pointer.
    1.64 +	/// \details Set \a _copyData to true if you want to make an internal copy of the data you are passing. Set it to false to just save a pointer to the data.
    1.65 +	/// You shouldn't call Write functions with \a _copyData as false, as this will write to unallocated memory
    1.66 +	/// 99% of the time you will use this function to cast Packet::data to a bitstream for reading, in which case you should write something as follows:
    1.67 +	/// \code
    1.68 +	/// RakNet::BitStream bs(packet->data, packet->length, false);
    1.69 +	/// \endcode
    1.70 +	/// \param[in] _data An array of bytes.
    1.71 +	/// \param[in] lengthInBytes Size of the \a _data.
    1.72 +	/// \param[in] _copyData true or false to make a copy of \a _data or not.
    1.73 +	BitStream( char* _data, const unsigned int lengthInBytes, bool _copyData );
    1.74 +
    1.75 +	// Destructor
    1.76 +	~BitStream();
    1.77 +
    1.78 +public:
    1.79 +	/// Resets the bitstream for reuse.
    1.80 +	void Reset( void );
    1.81 +
    1.82 +	/// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream.  
    1.83 +	/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
    1.84 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
    1.85 +	/// \param[in] inOutTemplateVar The value to write
    1.86 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
    1.87 +	template <class templateType>
    1.88 +	bool Serialize(bool writeToBitstream, templateType &inOutTemplateVar);
    1.89 +
    1.90 +	/// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. 
    1.91 +	/// \details If the current value is different from the last value
    1.92 +	/// the current value will be written.  Otherwise, a single bit will be written
    1.93 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
    1.94 +	/// \param[in] inOutCurrentValue The current value to write
    1.95 +	/// \param[in] lastValue The last value to compare against.  Only used if \a writeToBitstream is true.
    1.96 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
    1.97 +	template <class templateType>
    1.98 +	bool SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue);
    1.99 +
   1.100 +	/// \brief Bidirectional version of SerializeDelta when you don't know what the last value is, or there is no last value.
   1.101 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.102 +	/// \param[in] inOutCurrentValue The current value to write
   1.103 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.104 +	template <class templateType>
   1.105 +	bool SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue);
   1.106 +
   1.107 +	/// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream.
   1.108 +	/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
   1.109 +	/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
   1.110 +	/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
   1.111 +	/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
   1.112 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.113 +	/// \param[in] inOutTemplateVar The value to write
   1.114 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.115 +	template <class templateType>
   1.116 +	bool SerializeCompressed(bool writeToBitstream, templateType &inOutTemplateVar);
   1.117 +
   1.118 +	/// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream.  
   1.119 +	/// \details If the current value is different from the last value
   1.120 +	/// the current value will be written.  Otherwise, a single bit will be written
   1.121 +	/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
   1.122 +	/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
   1.123 +	/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
   1.124 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.125 +	/// \param[in] inOutCurrentValue The current value to write
   1.126 +	/// \param[in] lastValue The last value to compare against.  Only used if \a writeToBitstream is true.
   1.127 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.128 +	template <class templateType>
   1.129 +	bool SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue);
   1.130 +
   1.131 +	/// \brief Save as SerializeCompressedDelta(templateType &currentValue, const templateType &lastValue) when we have an unknown second parameter
   1.132 +	/// \return true on data read. False on insufficient data in bitstream
   1.133 +	template <class templateType>
   1.134 +	bool SerializeCompressedDelta(bool writeToBitstream, templateType &inOutTemplateVar);
   1.135 +
   1.136 +	/// \brief Bidirectional serialize/deserialize an array or casted stream or raw data.  This does NOT do endian swapping.
   1.137 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.138 +	/// \param[in] inOutByteArray a byte buffer
   1.139 +	/// \param[in] numberOfBytes the size of \a input in bytes
   1.140 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.141 +	bool Serialize(bool writeToBitstream,  char* inOutByteArray, const unsigned int numberOfBytes );
   1.142 +
   1.143 +	/// \brief Serialize a float into 2 bytes, spanning the range between \a floatMin and \a floatMax
   1.144 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.145 +	/// \param[in] inOutFloat The float to write
   1.146 +	/// \param[in] floatMin Predetermined minimum value of f
   1.147 +	/// \param[in] floatMax Predetermined maximum value of f
   1.148 +	bool SerializeFloat16(bool writeToBitstream, float &inOutFloat, float floatMin, float floatMax);
   1.149 +
   1.150 +	/// Serialize one type casted to another (smaller) type, to save bandwidth
   1.151 +	/// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t
   1.152 +	/// Example: int num=53; SerializeCasted<uint8_t>(true, num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes)
   1.153 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.154 +	/// \param[in] value The value to serialize
   1.155 +	template <class serializationType, class sourceType >
   1.156 +	bool SerializeCasted( bool writeToBitstream, sourceType &value );
   1.157 +
   1.158 +	/// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range
   1.159 +	/// Then serialize only those bits
   1.160 +	/// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program
   1.161 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.162 +	/// \param[in] value Integer value to write, which should be between \a minimum and \a maximum
   1.163 +	/// \param[in] minimum Minimum value of \a value
   1.164 +	/// \param[in] maximum Maximum value of \a value
   1.165 +	/// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates
   1.166 +	template <class templateType>
   1.167 +	bool SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false );
   1.168 +	/// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum
   1.169 +	template <class templateType>
   1.170 +	bool SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false );
   1.171 +
   1.172 +	/// \brief Bidirectional serialize/deserialize a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes.  
   1.173 +	/// \details Will further compress y or z axis aligned vectors.
   1.174 +	/// Accurate to 1/32767.5.
   1.175 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.176 +	/// \param[in] x x
   1.177 +	/// \param[in] y y
   1.178 +	/// \param[in] z z
   1.179 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.180 +	template <class templateType> // templateType for this function must be a float or double
   1.181 +	bool SerializeNormVector(bool writeToBitstream,  templateType &x, templateType &y, templateType &z );
   1.182 +
   1.183 +	/// \brief Bidirectional serialize/deserialize a vector, using 10 bytes instead of 12.
   1.184 +	/// \details Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.
   1.185 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.186 +	/// \param[in] x x
   1.187 +	/// \param[in] y y
   1.188 +	/// \param[in] z z
   1.189 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.190 +	template <class templateType> // templateType for this function must be a float or double
   1.191 +	bool SerializeVector(bool writeToBitstream,  templateType &x, templateType &y, templateType &z );
   1.192 +
   1.193 +	/// \brief Bidirectional serialize/deserialize a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.
   1.194 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.195 +	/// \param[in] w w
   1.196 +	/// \param[in] x x
   1.197 +	/// \param[in] y y
   1.198 +	/// \param[in] z z
   1.199 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.200 +	template <class templateType> // templateType for this function must be a float or double
   1.201 +	bool SerializeNormQuat(bool writeToBitstream,  templateType &w, templateType &x, templateType &y, templateType &z);
   1.202 +
   1.203 +	/// \brief Bidirectional serialize/deserialize an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each.
   1.204 +	/// \details Use 6 bytes instead of 36
   1.205 +	/// Lossy, although the result is renormalized
   1.206 +	/// \return true on success, false on failure.
   1.207 +	template <class templateType> // templateType for this function must be a float or double
   1.208 +	bool SerializeOrthMatrix(
   1.209 +		bool writeToBitstream,
   1.210 +		templateType &m00, templateType &m01, templateType &m02,
   1.211 +		templateType &m10, templateType &m11, templateType &m12,
   1.212 +		templateType &m20, templateType &m21, templateType &m22 );
   1.213 +
   1.214 +	/// \brief Bidirectional serialize/deserialize numberToSerialize bits to/from the input. 
   1.215 +	/// \details Right aligned data means in the case of a partial byte, the bits are aligned
   1.216 +	/// from the right (bit 0) rather than the left (as in the normal
   1.217 +	/// internal representation) You would set this to true when
   1.218 +	/// writing user data, and false when copying bitstream data, such
   1.219 +	/// as writing one bitstream to another
   1.220 +	/// \param[in] writeToBitstream true to write from your data to this bitstream.  False to read from this bitstream and write to your data
   1.221 +	/// \param[in] inOutByteArray The data
   1.222 +	/// \param[in] numberOfBitsToSerialize The number of bits to write
   1.223 +	/// \param[in] rightAlignedBits if true data will be right aligned
   1.224 +	/// \return true if \a writeToBitstream is true.  true if \a writeToBitstream is false and the read was successful.  false if \a writeToBitstream is false and the read was not successful.
   1.225 +	bool SerializeBits(bool writeToBitstream, unsigned char* inOutByteArray, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits = true );
   1.226 +
   1.227 +	/// \brief Write any integral type to a bitstream.  
   1.228 +	/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
   1.229 +	/// \param[in] inTemplateVar The value to write
   1.230 +	template <class templateType>
   1.231 +	void Write(const templateType &inTemplateVar);
   1.232 +
   1.233 +	/// \brief Write the dereferenced pointer to any integral type to a bitstream.  
   1.234 +	/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
   1.235 +	/// \param[in] inTemplateVar The value to write
   1.236 +	template <class templateType>
   1.237 +	void WritePtr(templateType *inTemplateVar);
   1.238 +
   1.239 +	/// \brief Write any integral type to a bitstream.  
   1.240 +	/// \details If the current value is different from the last value
   1.241 +	/// the current value will be written.  Otherwise, a single bit will be written
   1.242 +	/// \param[in] currentValue The current value to write
   1.243 +	/// \param[in] lastValue The last value to compare against
   1.244 +	template <class templateType>
   1.245 +	void WriteDelta(const templateType &currentValue, const templateType &lastValue);
   1.246 +
   1.247 +	/// \brief WriteDelta when you don't know what the last value is, or there is no last value.
   1.248 +	/// \param[in] currentValue The current value to write
   1.249 +	template <class templateType>
   1.250 +	void WriteDelta(const templateType &currentValue);
   1.251 +
   1.252 +	/// \brief Write any integral type to a bitstream.  
   1.253 +	/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
   1.254 +	/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
   1.255 +	/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
   1.256 +	/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
   1.257 +	/// \param[in] inTemplateVar The value to write
   1.258 +	template <class templateType>
   1.259 +	void WriteCompressed(const templateType &inTemplateVar);
   1.260 +
   1.261 +	/// \brief Write any integral type to a bitstream.  
   1.262 +	/// \details If the current value is different from the last value
   1.263 +	/// the current value will be written.  Otherwise, a single bit will be written
   1.264 +	/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
   1.265 +	/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
   1.266 +	/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
   1.267 +	/// \param[in] currentValue The current value to write
   1.268 +	/// \param[in] lastValue The last value to compare against
   1.269 +	template <class templateType>
   1.270 +	void WriteCompressedDelta(const templateType &currentValue, const templateType &lastValue);
   1.271 +
   1.272 +	/// \brief Save as WriteCompressedDelta(const templateType &currentValue, const templateType &lastValue) when we have an unknown second parameter
   1.273 +	template <class templateType>
   1.274 +	void WriteCompressedDelta(const templateType &currentValue);
   1.275 +
   1.276 +	/// \brief Read any integral type from a bitstream.  
   1.277 +	/// \details Define __BITSTREAM_NATIVE_END if you need endian swapping.
   1.278 +	/// \param[in] outTemplateVar The value to read
   1.279 +	/// \return true on success, false on failure.
   1.280 +	template <class templateType>
   1.281 +	bool Read(templateType &outTemplateVar);
   1.282 +
   1.283 +	/// \brief Read any integral type from a bitstream.  
   1.284 +	/// \details If the written value differed from the value compared against in the write function,
   1.285 +	/// var will be updated.  Otherwise it will retain the current value.
   1.286 +	/// ReadDelta is only valid from a previous call to WriteDelta
   1.287 +	/// \param[in] outTemplateVar The value to read
   1.288 +	/// \return true on success, false on failure.
   1.289 +	template <class templateType>
   1.290 +	bool ReadDelta(templateType &outTemplateVar);
   1.291 +
   1.292 +	/// \brief Read any integral type from a bitstream.  
   1.293 +	/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
   1.294 +	/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
   1.295 +	/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
   1.296 +	/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
   1.297 +	/// \param[in] outTemplateVar The value to read
   1.298 +	/// \return true on success, false on failure.
   1.299 +	template <class templateType>
   1.300 +	bool ReadCompressed(templateType &outTemplateVar);
   1.301 +
   1.302 +	/// \brief Read any integral type from a bitstream.  
   1.303 +	/// \details If the written value differed from the value compared against in the write function,
   1.304 +	/// var will be updated.  Otherwise it will retain the current value.
   1.305 +	/// the current value will be updated.
   1.306 +	/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
   1.307 +	/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
   1.308 +	/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
   1.309 +	/// ReadCompressedDelta is only valid from a previous call to WriteDelta
   1.310 +	/// \param[in] outTemplateVar The value to read
   1.311 +	/// \return true on success, false on failure.
   1.312 +	template <class templateType>
   1.313 +	bool ReadCompressedDelta(templateType &outTemplateVar);
   1.314 +
   1.315 +	/// \brief Read one bitstream to another.
   1.316 +	/// \param[in] numberOfBits bits to read
   1.317 +	/// \param bitStream the bitstream to read into from
   1.318 +	/// \return true on success, false on failure.
   1.319 +	bool Read( BitStream *bitStream, BitSize_t numberOfBits );
   1.320 +	bool Read( BitStream *bitStream );
   1.321 +	bool Read( BitStream &bitStream, BitSize_t numberOfBits );
   1.322 +	bool Read( BitStream &bitStream );
   1.323 +
   1.324 +	/// \brief Write an array or casted stream or raw data.  This does NOT do endian swapping.
   1.325 +	/// \param[in] inputByteArray a byte buffer
   1.326 +	/// \param[in] numberOfBytes the size of \a input in bytes
   1.327 +	void Write( const char* inputByteArray, const unsigned int numberOfBytes );
   1.328 +
   1.329 +	/// \brief Write one bitstream to another.
   1.330 +	/// \param[in] numberOfBits bits to write
   1.331 +	/// \param bitStream the bitstream to copy from
   1.332 +	void Write( BitStream *bitStream, BitSize_t numberOfBits );
   1.333 +	void Write( BitStream *bitStream );
   1.334 +	void Write( BitStream &bitStream, BitSize_t numberOfBits );
   1.335 +	void Write( BitStream &bitStream );\
   1.336 +
   1.337 +	/// \brief Write a float into 2 bytes, spanning the range between \a floatMin and \a floatMax
   1.338 +	/// \param[in] x The float to write
   1.339 +	/// \param[in] floatMin Predetermined minimum value of f
   1.340 +	/// \param[in] floatMax Predetermined maximum value of f
   1.341 +	void WriteFloat16( float x, float floatMin, float floatMax );
   1.342 +
   1.343 +	/// Write one type serialized as another (smaller) type, to save bandwidth
   1.344 +	/// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t
   1.345 +	/// Example: int num=53; WriteCasted<uint8_t>(num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes)
   1.346 +	/// \param[in] value The value to write
   1.347 +	template <class serializationType, class sourceType >
   1.348 +	void WriteCasted( const sourceType &value );
   1.349 +
   1.350 +	/// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range
   1.351 +	/// Then write only those bits
   1.352 +	/// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program
   1.353 +	/// \param[in] value Integer value to write, which should be between \a minimum and \a maximum
   1.354 +	/// \param[in] minimum Minimum value of \a value
   1.355 +	/// \param[in] maximum Maximum value of \a value
   1.356 +	/// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Read().
   1.357 +	template <class templateType>
   1.358 +	void WriteBitsFromIntegerRange( const templateType value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false );
   1.359 +	/// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum
   1.360 +	template <class templateType>
   1.361 +	void WriteBitsFromIntegerRange( const templateType value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false );
   1.362 +
   1.363 +	/// \brief Write a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes.  
   1.364 +	/// \details Will further compress y or z axis aligned vectors.
   1.365 +	/// Accurate to 1/32767.5.
   1.366 +	/// \param[in] x x
   1.367 +	/// \param[in] y y
   1.368 +	/// \param[in] z z
   1.369 +	template <class templateType> // templateType for this function must be a float or double
   1.370 +	void WriteNormVector( templateType x, templateType y, templateType z );
   1.371 +
   1.372 +	/// \brief Write a vector, using 10 bytes instead of 12.
   1.373 +	/// \details Loses accuracy to about 3/10ths and only saves 2 bytes, 
   1.374 +	/// so only use if accuracy is not important.
   1.375 +	/// \param[in] x x
   1.376 +	/// \param[in] y y
   1.377 +	/// \param[in] z z
   1.378 +	template <class templateType> // templateType for this function must be a float or double
   1.379 +	void WriteVector( templateType x, templateType y, templateType z );
   1.380 +
   1.381 +	/// \brief Write a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes.  Slightly lossy.
   1.382 +	/// \param[in] w w
   1.383 +	/// \param[in] x x
   1.384 +	/// \param[in] y y
   1.385 +	/// \param[in] z z
   1.386 +	template <class templateType> // templateType for this function must be a float or double
   1.387 +	void WriteNormQuat( templateType w, templateType x, templateType y, templateType z);
   1.388 +
   1.389 +	/// \brief Write an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each.
   1.390 +	/// \details Use 6 bytes instead of 36
   1.391 +	/// Lossy, although the result is renormalized
   1.392 +	template <class templateType> // templateType for this function must be a float or double
   1.393 +	void WriteOrthMatrix(
   1.394 +		templateType m00, templateType m01, templateType m02,
   1.395 +		templateType m10, templateType m11, templateType m12,
   1.396 +		templateType m20, templateType m21, templateType m22 );
   1.397 +
   1.398 +	/// \brief Read an array or casted stream of byte.
   1.399 +	/// \details The array is raw data. There is no automatic endian conversion with this function
   1.400 +	/// \param[in] output The result byte array. It should be larger than @em numberOfBytes.
   1.401 +	/// \param[in] numberOfBytes The number of byte to read
   1.402 +	/// \return true on success false if there is some missing bytes.
   1.403 +	bool Read( char* output, const unsigned int numberOfBytes );
   1.404 +
   1.405 +	/// \brief Read a float into 2 bytes, spanning the range between \a floatMin and \a floatMax
   1.406 +	/// \param[in] outFloat The float to read
   1.407 +	/// \param[in] floatMin Predetermined minimum value of f
   1.408 +	/// \param[in] floatMax Predetermined maximum value of f
   1.409 +	bool ReadFloat16( float &outFloat, float floatMin, float floatMax );
   1.410 +
   1.411 +	/// Read one type serialized to another (smaller) type, to save bandwidth
   1.412 +	/// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t
   1.413 +	/// Example: int num; ReadCasted<uint8_t>(num); would read 1 bytefrom the stream, and put the value in an integer
   1.414 +	/// \param[in] value The value to write
   1.415 +	template <class serializationType, class sourceType >
   1.416 +	bool ReadCasted( sourceType &value );
   1.417 +
   1.418 +	/// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range
   1.419 +	/// Then read only those bits
   1.420 +	/// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program
   1.421 +	/// \param[in] value Integer value to read, which should be between \a minimum and \a maximum
   1.422 +	/// \param[in] minimum Minimum value of \a value
   1.423 +	/// \param[in] maximum Maximum value of \a value
   1.424 +	/// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Write().
   1.425 +	template <class templateType>
   1.426 +	bool ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false );
   1.427 +	/// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum
   1.428 +	template <class templateType>
   1.429 +	bool ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false );
   1.430 +
   1.431 +	/// \brief Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes.  
   1.432 +	/// \details Will further compress y or z axis aligned vectors.
   1.433 +	/// Accurate to 1/32767.5.
   1.434 +	/// \param[in] x x
   1.435 +	/// \param[in] y y
   1.436 +	/// \param[in] z z
   1.437 +	/// \return true on success, false on failure.
   1.438 +	template <class templateType> // templateType for this function must be a float or double
   1.439 +	bool ReadNormVector( templateType &x, templateType &y, templateType &z );
   1.440 +
   1.441 +	/// \brief Read 3 floats or doubles, using 10 bytes, where those float or doubles comprise a vector.
   1.442 +	/// \details Loses accuracy to about 3/10ths and only saves 2 bytes, 
   1.443 +	/// so only use if accuracy is not important.
   1.444 +	/// \param[in] x x
   1.445 +	/// \param[in] y y
   1.446 +	/// \param[in] z z
   1.447 +	/// \return true on success, false on failure.
   1.448 +	template <class templateType> // templateType for this function must be a float or double
   1.449 +	bool ReadVector( templateType &x, templateType &y, templateType &z );
   1.450 +
   1.451 +	/// \brief Read a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes.
   1.452 +	/// \param[in] w w
   1.453 +	/// \param[in] x x
   1.454 +	/// \param[in] y y
   1.455 +	/// \param[in] z z
   1.456 +	/// \return true on success, false on failure.
   1.457 +	template <class templateType> // templateType for this function must be a float or double
   1.458 +	bool ReadNormQuat( templateType &w, templateType &x, templateType &y, templateType &z);
   1.459 +
   1.460 +	/// \brief Read an orthogonal matrix from a quaternion, reading 3 components of the quaternion in 2 bytes each and extrapolatig the 4th.
   1.461 +	/// \details Use 6 bytes instead of 36
   1.462 +	/// Lossy, although the result is renormalized
   1.463 +	/// \return true on success, false on failure.
   1.464 +	template <class templateType> // templateType for this function must be a float or double
   1.465 +	bool ReadOrthMatrix(
   1.466 +		templateType &m00, templateType &m01, templateType &m02,
   1.467 +		templateType &m10, templateType &m11, templateType &m12,
   1.468 +		templateType &m20, templateType &m21, templateType &m22 );
   1.469 +
   1.470 +	/// \brief Sets the read pointer back to the beginning of your data.
   1.471 +	void ResetReadPointer( void );
   1.472 +
   1.473 +	/// \brief Sets the write pointer back to the beginning of your data.
   1.474 +	void ResetWritePointer( void );
   1.475 +
   1.476 +	/// \brief This is good to call when you are done with the stream to make
   1.477 +	/// sure you didn't leave any data left over void
   1.478 +	void AssertStreamEmpty( void );
   1.479 +
   1.480 +	/// \brief RAKNET_DEBUG_PRINTF the bits in the stream.  Great for debugging.
   1.481 +	void PrintBits( char *out ) const;
   1.482 +	void PrintBits( void ) const;
   1.483 +	void PrintHex( char *out ) const;
   1.484 +	void PrintHex( void ) const;
   1.485 +
   1.486 +	/// \brief Ignore data we don't intend to read
   1.487 +	/// \param[in] numberOfBits The number of bits to ignore
   1.488 +	void IgnoreBits( const BitSize_t numberOfBits );
   1.489 +
   1.490 +	/// \brief Ignore data we don't intend to read
   1.491 +	/// \param[in] numberOfBits The number of bytes to ignore
   1.492 +	void IgnoreBytes( const unsigned int numberOfBytes );
   1.493 +
   1.494 +	/// \brief Move the write pointer to a position on the array.
   1.495 +	/// \param[in] offset the offset from the start of the array.
   1.496 +	/// \attention
   1.497 +	/// \details Dangerous if you don't know what you are doing!
   1.498 +	/// For efficiency reasons you can only write mid-stream if your data is byte aligned.
   1.499 +	void SetWriteOffset( const BitSize_t offset );
   1.500 +
   1.501 +	/// \brief Returns the length in bits of the stream
   1.502 +	inline BitSize_t GetNumberOfBitsUsed( void ) const {return GetWriteOffset();}
   1.503 +	inline BitSize_t GetWriteOffset( void ) const {return numberOfBitsUsed;}
   1.504 +
   1.505 +	/// \brief Returns the length in bytes of the stream
   1.506 +	inline BitSize_t GetNumberOfBytesUsed( void ) const {return BITS_TO_BYTES( numberOfBitsUsed );}
   1.507 +
   1.508 +	/// \brief Returns the number of bits into the stream that we have read
   1.509 +	inline BitSize_t GetReadOffset( void ) const {return readOffset;}
   1.510 +
   1.511 +	/// \brief Sets the read bit index
   1.512 +	void SetReadOffset( const BitSize_t newReadOffset ) {readOffset=newReadOffset;}
   1.513 +
   1.514 +	/// \brief Returns the number of bits left in the stream that haven't been read
   1.515 +	inline BitSize_t GetNumberOfUnreadBits( void ) const {return numberOfBitsUsed - readOffset;}
   1.516 +
   1.517 +	/// \brief Makes a copy of the internal data for you \a _data will point to
   1.518 +	/// the stream. Partial bytes are left aligned.
   1.519 +	/// \param[out] _data The allocated copy of GetData()
   1.520 +	/// \return The length in bits of the stream.
   1.521 +	BitSize_t CopyData( unsigned char** _data ) const;
   1.522 +
   1.523 +	/// \internal
   1.524 +	/// Set the stream to some initial data.
   1.525 +	void SetData( unsigned char *inByteArray );
   1.526 +
   1.527 +	/// Gets the data that BitStream is writing to / reading from.
   1.528 +	/// Partial bytes are left aligned.
   1.529 +	/// \return A pointer to the internal state
   1.530 +	inline char* GetData( void ) const {return (char*) data;}
   1.531 +
   1.532 +	/// \brief Write numberToWrite bits from the input source.
   1.533 +	/// \details Right aligned data means in the case of a partial byte, the bits are aligned
   1.534 +	/// from the right (bit 0) rather than the left (as in the normal
   1.535 +	/// internal representation) You would set this to true when
   1.536 +	/// writing user data, and false when copying bitstream data, such
   1.537 +	/// as writing one bitstream to another.
   1.538 +	/// \param[in] inByteArray The data
   1.539 +	/// \param[in] numberOfBitsToWrite The number of bits to write
   1.540 +	/// \param[in] rightAlignedBits if true data will be right aligned
   1.541 +	void WriteBits( const unsigned char* inByteArray, BitSize_t numberOfBitsToWrite, const bool rightAlignedBits = true );
   1.542 +
   1.543 +	/// \brief Align the bitstream to the byte boundary and then write the
   1.544 +	/// specified number of bits.  
   1.545 +	/// \details This is faster than WriteBits but
   1.546 +	/// wastes the bits to do the alignment and requires you to call
   1.547 +	/// ReadAlignedBits at the corresponding read position.
   1.548 +	/// \param[in] inByteArray The data
   1.549 +	/// \param[in] numberOfBytesToWrite The size of input.
   1.550 +	void WriteAlignedBytes( const unsigned char *inByteArray, const unsigned int numberOfBytesToWrite );
   1.551 +
   1.552 +	// Endian swap bytes already in the bitstream
   1.553 +	void EndianSwapBytes( int byteOffset, int length );
   1.554 +
   1.555 +	/// \brief Aligns the bitstream, writes inputLength, and writes input. Won't write beyond maxBytesToWrite
   1.556 +	/// \param[in] inByteArray The data
   1.557 +	/// \param[in] inputLength The size of input.
   1.558 +	/// \param[in] maxBytesToWrite Max bytes to write
   1.559 +	void WriteAlignedBytesSafe( const char *inByteArray, const unsigned int inputLength, const unsigned int maxBytesToWrite );
   1.560 +
   1.561 +	/// \brief Read bits, starting at the next aligned bits. 
   1.562 +	/// \details Note that the modulus 8 starting offset of the sequence must be the same as
   1.563 +	/// was used with WriteBits. This will be a problem with packet
   1.564 +	/// coalescence unless you byte align the coalesced packets.
   1.565 +	/// \param[in] inOutByteArray The byte array larger than @em numberOfBytesToRead
   1.566 +	/// \param[in] numberOfBytesToRead The number of byte to read from the internal state
   1.567 +	/// \return true if there is enough byte.
   1.568 +	bool ReadAlignedBytes( unsigned char *inOutByteArray, const unsigned int numberOfBytesToRead );
   1.569 +
   1.570 +	/// \brief Reads what was written by WriteAlignedBytesSafe.
   1.571 +	/// \param[in] inOutByteArray The data
   1.572 +	/// \param[in] maxBytesToRead Maximum number of bytes to read
   1.573 +	/// \return true on success, false on failure.
   1.574 +	bool ReadAlignedBytesSafe( char *inOutByteArray, int &inputLength, const int maxBytesToRead );
   1.575 +	bool ReadAlignedBytesSafe( char *inOutByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead );
   1.576 +
   1.577 +	/// \brief Same as ReadAlignedBytesSafe() but allocates the memory for you using new, rather than assuming it is safe to write to
   1.578 +	/// \param[in] outByteArray outByteArray will be deleted if it is not a pointer to 0
   1.579 +	/// \return true on success, false on failure.
   1.580 +	bool ReadAlignedBytesSafeAlloc( char **outByteArray, int &inputLength, const unsigned int maxBytesToRead );
   1.581 +	bool ReadAlignedBytesSafeAlloc( char **outByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead );
   1.582 +
   1.583 +	/// \brief Align the next write and/or read to a byte boundary.  
   1.584 +	/// \details This can be used to 'waste' bits to byte align for efficiency reasons It
   1.585 +	/// can also be used to force coalesced bitstreams to start on byte
   1.586 +	/// boundaries so so WriteAlignedBits and ReadAlignedBits both
   1.587 +	/// calculate the same offset when aligning.
   1.588 +	inline void AlignWriteToByteBoundary( void ) {numberOfBitsUsed += 8 - ( (( numberOfBitsUsed - 1 ) & 7) + 1 );}
   1.589 +
   1.590 +	/// \brief Align the next write and/or read to a byte boundary.  
   1.591 +	/// \details This can be used to 'waste' bits to byte align for efficiency reasons It
   1.592 +	/// can also be used to force coalesced bitstreams to start on byte
   1.593 +	/// boundaries so so WriteAlignedBits and ReadAlignedBits both
   1.594 +	/// calculate the same offset when aligning.
   1.595 +	inline void AlignReadToByteBoundary( void ) {readOffset += 8 - ( (( readOffset - 1 ) & 7 ) + 1 );}
   1.596 +
   1.597 +	/// \brief Read \a numberOfBitsToRead bits to the output source.
   1.598 +	/// \details alignBitsToRight should be set to true to convert internal
   1.599 +	/// bitstream data to userdata. It should be false if you used
   1.600 +	/// WriteBits with rightAlignedBits false
   1.601 +	/// \param[in] inOutByteArray The resulting bits array
   1.602 +	/// \param[in] numberOfBitsToRead The number of bits to read
   1.603 +	/// \param[in] alignBitsToRight if true bits will be right aligned.
   1.604 +	/// \return true if there is enough bits to read
   1.605 +	bool ReadBits( unsigned char *inOutByteArray, BitSize_t numberOfBitsToRead, const bool alignBitsToRight = true );
   1.606 +
   1.607 +	/// \brief Write a 0
   1.608 +	void Write0( void );
   1.609 +
   1.610 +	/// \brief Write a 1
   1.611 +	void Write1( void );
   1.612 +
   1.613 +	/// \brief Reads 1 bit and returns true if that bit is 1 and false if it is 0.
   1.614 +	bool ReadBit( void );
   1.615 +
   1.616 +	/// \brief If we used the constructor version with copy data off, this
   1.617 +	/// *makes sure it is set to on and the data pointed to is copied.
   1.618 +	void AssertCopyData( void );
   1.619 +
   1.620 +	/// \brief Use this if you pass a pointer copy to the constructor
   1.621 +	/// *(_copyData==false) and want to overallocate to prevent
   1.622 +	/// reallocation.
   1.623 +	void SetNumberOfBitsAllocated( const BitSize_t lengthInBits );
   1.624 +
   1.625 +	/// \brief Reallocates (if necessary) in preparation of writing numberOfBitsToWrite
   1.626 +	void AddBitsAndReallocate( const BitSize_t numberOfBitsToWrite );
   1.627 +
   1.628 +	/// \internal
   1.629 +	/// \return How many bits have been allocated internally
   1.630 +	BitSize_t GetNumberOfBitsAllocated(void) const;
   1.631 +
   1.632 +	/// Write zeros until the bitstream is filled up to \a bytes
   1.633 +	void PadWithZeroToByteLength( unsigned int bytes );
   1.634 +
   1.635 +	/// Get the number of leading zeros for a number
   1.636 +	/// \param[in] x Number to test
   1.637 +	static int NumberOfLeadingZeroes( uint8_t x );
   1.638 +	static int NumberOfLeadingZeroes( uint16_t x );
   1.639 +	static int NumberOfLeadingZeroes( uint32_t x );
   1.640 +	static int NumberOfLeadingZeroes( uint64_t x );
   1.641 +	static int NumberOfLeadingZeroes( int8_t x );
   1.642 +	static int NumberOfLeadingZeroes( int16_t x );
   1.643 +	static int NumberOfLeadingZeroes( int32_t x );
   1.644 +	static int NumberOfLeadingZeroes( int64_t x );
   1.645 +
   1.646 +	/// \internal Unrolled inner loop, for when performance is critical
   1.647 +	void WriteAlignedVar8(const char *inByteArray);
   1.648 +	/// \internal Unrolled inner loop, for when performance is critical
   1.649 +	bool ReadAlignedVar8(char *inOutByteArray);
   1.650 +	/// \internal Unrolled inner loop, for when performance is critical
   1.651 +	void WriteAlignedVar16(const char *inByteArray);
   1.652 +	/// \internal Unrolled inner loop, for when performance is critical
   1.653 +	bool ReadAlignedVar16(char *inOutByteArray);
   1.654 +	/// \internal Unrolled inner loop, for when performance is critical
   1.655 +	void WriteAlignedVar32(const char *inByteArray);
   1.656 +	/// \internal Unrolled inner loop, for when performance is critical
   1.657 +	bool ReadAlignedVar32(char *inOutByteArray);
   1.658 +
   1.659 +	inline void Write(const char * const inStringVar)
   1.660 +	{
   1.661 +		uint16_t l = (uint16_t) OVR_strlen(inStringVar);
   1.662 +		Write(l);
   1.663 +		WriteAlignedBytes((const unsigned char*) inStringVar, (const unsigned int) l);
   1.664 +	}
   1.665 +	inline void Write(const unsigned char * const inTemplateVar)
   1.666 +	{
   1.667 +		Write((const char*)inTemplateVar);
   1.668 +	}
   1.669 +	inline void Write(char * const inTemplateVar)
   1.670 +	{
   1.671 +		Write((const char*)inTemplateVar);
   1.672 +	}
   1.673 +	inline void Write(unsigned char * const inTemplateVar)
   1.674 +	{
   1.675 +		Write((const char*)inTemplateVar);
   1.676 +	}
   1.677 +
   1.678 +	/// ---- Member function template specialization declarations ----
   1.679 +	// Used for VC7
   1.680 +#if defined(OVR_CC_MSVC) && _MSC_VER == 1300
   1.681 +	/// Write a bool to a bitstream.
   1.682 +	/// \param[in] var The value to write
   1.683 +	template <>
   1.684 +	void Write(const bool &var);
   1.685 +
   1.686 +	/// Write a RakNetGUID to a bitsteam
   1.687 +	/// \param[in] var The value to write
   1.688 +	template <>
   1.689 +	void Write(const RakNetGuid &var);
   1.690 +
   1.691 +	/// Write a string to a bitstream
   1.692 +	/// \param[in] var The value to write
   1.693 +	template <>
   1.694 +	void Write(const char* const &var);
   1.695 +	template <>
   1.696 +	void Write(const unsigned char* const &var);
   1.697 +	template <>
   1.698 +	void Write(char* const &var);
   1.699 +	template <>
   1.700 +	void Write(unsigned char* const &var);
   1.701 +	template <>
   1.702 +	void Write(const OVR::String &var);
   1.703 +
   1.704 +	/// \brief Write a bool delta.  
   1.705 +	/// \details Same thing as just calling Write
   1.706 +	/// \param[in] currentValue The current value to write
   1.707 +	/// \param[in] lastValue The last value to compare against
   1.708 +	template <>
   1.709 +	void WriteDelta(const bool &currentValue, const bool &lastValue);
   1.710 +
   1.711 +	template <>
   1.712 +	void WriteCompressed(const bool &var);
   1.713 +
   1.714 +	/// For values between -1 and 1
   1.715 +	template <>
   1.716 +	void WriteCompressed(const float &var);
   1.717 +
   1.718 +	/// For values between -1 and 1
   1.719 +	template <>
   1.720 +	void WriteCompressed(const double &var);
   1.721 +	
   1.722 +	/// \brief Write a bool delta.  
   1.723 +	/// \details Same thing as just calling Write
   1.724 +	/// \param[in] currentValue The current value to write
   1.725 +	/// \param[in] lastValue The last value to compare against
   1.726 +	template <>
   1.727 +	void WriteCompressedDelta(const bool &currentValue, const bool &lastValue);
   1.728 +
   1.729 +	/// \brief Save as WriteCompressedDelta(bool currentValue, const templateType &lastValue) 
   1.730 +	/// when we have an unknown second bool
   1.731 +	template <>
   1.732 +	void WriteCompressedDelta(const bool &currentValue);
   1.733 +
   1.734 +	/// \brief Read a bool from a bitstream.
   1.735 +	/// \param[in] var The value to read
   1.736 +	/// \return true on success, false on failure.
   1.737 +	template <>
   1.738 +	bool Read(bool &var);
   1.739 +
   1.740 +	/// \brief Read a String from a bitstream.
   1.741 +	/// \param[in] var The value to read
   1.742 +	/// \return true on success, false on failure.
   1.743 +	template <>
   1.744 +	bool Read(char *&var);
   1.745 +	template <>
   1.746 +	bool Read(wchar_t *&var);
   1.747 +	template <>
   1.748 +	bool Read(unsigned char *&var);
   1.749 +
   1.750 +	/// \brief Read a bool from a bitstream.
   1.751 +	/// \param[in] var The value to read
   1.752 +	/// \return true on success, false on failure.
   1.753 +	template <>
   1.754 +	bool ReadDelta(bool &var);
   1.755 +
   1.756 +	template <>
   1.757 +	bool ReadCompressed(bool &var);
   1.758 +
   1.759 +	template <>
   1.760 +	bool ReadCompressed(float &var);
   1.761 +
   1.762 +	/// For values between -1 and 1
   1.763 +	/// \return true on success, false on failure.
   1.764 +	template <>
   1.765 +	bool ReadCompressed(double &var);
   1.766 +
   1.767 +	template <>
   1.768 +	bool ReadCompressed(char* &var);
   1.769 +	template <>
   1.770 +	bool ReadCompressed(wchar_t* &var);
   1.771 +	template <>
   1.772 +	bool ReadCompressed(unsigned char *&var);
   1.773 +	template <>
   1.774 +	bool ReadCompressed(OVR::String &var);
   1.775 +
   1.776 +	/// \brief Read a bool from a bitstream.
   1.777 +	/// \param[in] var The value to read
   1.778 +	/// \return true on success, false on failure.
   1.779 +	template <>
   1.780 +	bool ReadCompressedDelta(bool &var);
   1.781 +#endif
   1.782 +
   1.783 +	inline static bool DoEndianSwap(void) {
   1.784 +#ifndef __BITSTREAM_NATIVE_END
   1.785 +		return IsNetworkOrder()==false;
   1.786 +#else
   1.787 +		return false;
   1.788 +#endif
   1.789 +	}
   1.790 +	inline static bool IsBigEndian(void)
   1.791 +	{
   1.792 +		return IsNetworkOrder();
   1.793 +	}
   1.794 +	inline static bool IsNetworkOrder(void) {bool r = IsNetworkOrderInternal(); return r;}
   1.795 +	// Not inline, won't compile on PC due to winsock include errors
   1.796 +	static bool IsNetworkOrderInternal(void);
   1.797 +	static void ReverseBytes(unsigned char *inByteArray, unsigned char *inOutByteArray, const unsigned int length);
   1.798 +	static void ReverseBytesInPlace(unsigned char *inOutData,const unsigned int length);
   1.799 +
   1.800 +private:
   1.801 +
   1.802 +	BitStream( const BitStream & /*invalid*/) : numberOfBitsUsed(0), numberOfBitsAllocated(0), readOffset(0),data(NULL), copyData(false) {
   1.803 +		OVR_ASSERT(0);
   1.804 +	}
   1.805 +
   1.806 +	BitStream& operator = ( const BitStream& /*invalid*/ ) {
   1.807 +		OVR_ASSERT(0);
   1.808 +		static BitStream i;
   1.809 +		return i;
   1.810 +	}
   1.811 +
   1.812 +	/// \brief Assume the input source points to a native type, compress and write it.
   1.813 +	void WriteCompressed( const unsigned char* inByteArray, const unsigned int size, const bool unsignedData );
   1.814 +
   1.815 +	/// \brief Assume the input source points to a compressed native type. Decompress and read it.
   1.816 +	bool ReadCompressed( unsigned char* inOutByteArray,	const unsigned int size, const bool unsignedData );
   1.817 +
   1.818 +
   1.819 +	BitSize_t numberOfBitsUsed;
   1.820 +
   1.821 +	BitSize_t numberOfBitsAllocated;
   1.822 +
   1.823 +	BitSize_t readOffset;
   1.824 +
   1.825 +	unsigned char *data;
   1.826 +
   1.827 +	/// true if the internal buffer is copy of the data passed to the constructor
   1.828 +	bool copyData;
   1.829 +
   1.830 +	/// BitStreams that use less than BITSTREAM_STACK_ALLOCATION_SIZE use the stack, rather than the heap to store data.  It switches over if BITSTREAM_STACK_ALLOCATION_SIZE is exceeded
   1.831 +	unsigned char stackData[BITSTREAM_STACK_ALLOCATION_SIZE];
   1.832 +};
   1.833 +
   1.834 +template <class templateType>
   1.835 +inline bool BitStream::Serialize(bool writeToBitstream, templateType &inOutTemplateVar)
   1.836 +{
   1.837 +	if (writeToBitstream)
   1.838 +		Write(inOutTemplateVar);
   1.839 +	else
   1.840 +		return Read(inOutTemplateVar);
   1.841 +	return true;
   1.842 +}
   1.843 +
   1.844 +template <class templateType>
   1.845 +inline bool BitStream::SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue)
   1.846 +{
   1.847 +	if (writeToBitstream)
   1.848 +		WriteDelta(inOutCurrentValue, lastValue);
   1.849 +	else
   1.850 +		return ReadDelta(inOutCurrentValue);
   1.851 +	return true;
   1.852 +}
   1.853 +
   1.854 +template <class templateType>
   1.855 +inline bool BitStream::SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue)
   1.856 +{
   1.857 +	if (writeToBitstream)
   1.858 +		WriteDelta(inOutCurrentValue);
   1.859 +	else
   1.860 +		return ReadDelta(inOutCurrentValue);
   1.861 +	return true;
   1.862 +}
   1.863 +
   1.864 +template <class templateType>
   1.865 +inline bool BitStream::SerializeCompressed(bool writeToBitstream, templateType &inOutTemplateVar)
   1.866 +{
   1.867 +	if (writeToBitstream)
   1.868 +		WriteCompressed(inOutTemplateVar);
   1.869 +	else
   1.870 +		return ReadCompressed(inOutTemplateVar);
   1.871 +	return true;
   1.872 +}
   1.873 +
   1.874 +template <class templateType>
   1.875 +inline bool BitStream::SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue)
   1.876 +{
   1.877 +	if (writeToBitstream)
   1.878 +		WriteCompressedDelta(inOutCurrentValue,lastValue);
   1.879 +	else
   1.880 +		return ReadCompressedDelta(inOutCurrentValue);
   1.881 +	return true;
   1.882 +}
   1.883 +//Stoppedhere
   1.884 +template <class templateType>
   1.885 +inline bool BitStream::SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue)
   1.886 +{
   1.887 +	if (writeToBitstream)
   1.888 +		WriteCompressedDelta(inOutCurrentValue);
   1.889 +	else
   1.890 +		return ReadCompressedDelta(inOutCurrentValue);
   1.891 +	return true;
   1.892 +}
   1.893 +
   1.894 +inline bool BitStream::Serialize(bool writeToBitstream, char* inOutByteArray, const unsigned int numberOfBytes )
   1.895 +{
   1.896 +	if (writeToBitstream)
   1.897 +		Write(inOutByteArray, numberOfBytes);
   1.898 +	else
   1.899 +		return Read(inOutByteArray, numberOfBytes);
   1.900 +	return true;
   1.901 +}
   1.902 +
   1.903 +template <class serializationType, class sourceType >
   1.904 +bool BitStream::SerializeCasted( bool writeToBitstream, sourceType &value )
   1.905 +{
   1.906 +	if (writeToBitstream) WriteCasted<serializationType>(value);
   1.907 +	else return ReadCasted<serializationType>(value);
   1.908 +	return true;
   1.909 +}
   1.910 +
   1.911 +template <class templateType>
   1.912 +bool BitStream::SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange )
   1.913 +{
   1.914 +	int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum));
   1.915 +	return SerializeBitsFromIntegerRange(writeToBitstream,value,minimum,maximum,requiredBits,allowOutsideRange);
   1.916 +}
   1.917 +template <class templateType>
   1.918 +bool BitStream::SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange )
   1.919 +{
   1.920 +	if (writeToBitstream) WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
   1.921 +	else return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
   1.922 +	return true;
   1.923 +}
   1.924 +
   1.925 +template <class templateType>
   1.926 +inline bool BitStream::SerializeNormVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z )
   1.927 +{
   1.928 +	if (writeToBitstream)
   1.929 +		WriteNormVector(x,y,z);
   1.930 +	else
   1.931 +		return ReadNormVector(x,y,z);
   1.932 +	return true;
   1.933 +}
   1.934 +
   1.935 +template <class templateType>
   1.936 +inline bool BitStream::SerializeVector(bool writeToBitstream,  templateType &x, templateType &y, templateType &z )
   1.937 +{
   1.938 +	if (writeToBitstream)
   1.939 +		WriteVector(x,y,z);
   1.940 +	else
   1.941 +		return ReadVector(x,y,z);
   1.942 +	return true;
   1.943 +}
   1.944 +
   1.945 +template <class templateType>
   1.946 +inline bool BitStream::SerializeNormQuat(bool writeToBitstream,  templateType &w, templateType &x, templateType &y, templateType &z)
   1.947 +{
   1.948 +	if (writeToBitstream)
   1.949 +		WriteNormQuat(w,x,y,z);
   1.950 +	else
   1.951 +		return ReadNormQuat(w,x,y,z);
   1.952 +	return true;
   1.953 +}
   1.954 +
   1.955 +template <class templateType>
   1.956 +inline bool BitStream::SerializeOrthMatrix(
   1.957 +	bool writeToBitstream,
   1.958 +	templateType &m00, templateType &m01, templateType &m02,
   1.959 +	templateType &m10, templateType &m11, templateType &m12,
   1.960 +	templateType &m20, templateType &m21, templateType &m22 )
   1.961 +{
   1.962 +	if (writeToBitstream)
   1.963 +		WriteOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22);
   1.964 +	else
   1.965 +		return ReadOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22);
   1.966 +	return true;
   1.967 +}
   1.968 +
   1.969 +inline bool BitStream::SerializeBits(bool writeToBitstream, unsigned char* inOutByteArray, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits )
   1.970 +{
   1.971 +	if (writeToBitstream)
   1.972 +		WriteBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits);
   1.973 +	else
   1.974 +		return ReadBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits);
   1.975 +	return true;
   1.976 +}
   1.977 +
   1.978 +template <class templateType>
   1.979 +inline void BitStream::Write(const templateType &inTemplateVar)
   1.980 +{
   1.981 +#ifdef OVR_CC_MSVC
   1.982 +#pragma warning(disable:4127)   // conditional expression is constant
   1.983 +#endif
   1.984 +	if (sizeof(inTemplateVar)==1)
   1.985 +		WriteBits( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true );
   1.986 +	else
   1.987 +	{
   1.988 +#ifndef __BITSTREAM_NATIVE_END
   1.989 +		if (DoEndianSwap())
   1.990 +		{
   1.991 +			unsigned char output[sizeof(templateType)];
   1.992 +			ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType));
   1.993 +			WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true );
   1.994 +		}
   1.995 +		else
   1.996 +#endif
   1.997 +			WriteBits( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true );
   1.998 +	}
   1.999 +}
  1.1000 +
  1.1001 +template <class templateType>
  1.1002 +inline void BitStream::WritePtr(templateType *inTemplateVar)
  1.1003 +{
  1.1004 +#ifdef OVR_CC_MSVC
  1.1005 +#pragma warning(disable:4127)   // conditional expression is constant
  1.1006 +#endif
  1.1007 +	if (sizeof(templateType)==1)
  1.1008 +		WriteBits( ( unsigned char* ) inTemplateVar, sizeof( templateType ) * 8, true );
  1.1009 +	else
  1.1010 +	{
  1.1011 +#ifndef __BITSTREAM_NATIVE_END
  1.1012 +		if (DoEndianSwap())
  1.1013 +		{
  1.1014 +			unsigned char output[sizeof(templateType)];
  1.1015 +			ReverseBytes((unsigned char*) inTemplateVar, output, sizeof(templateType));
  1.1016 +			WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true );
  1.1017 +		}
  1.1018 +		else
  1.1019 +#endif
  1.1020 +			WriteBits( ( unsigned char* ) inTemplateVar, sizeof(templateType) * 8, true );
  1.1021 +	}
  1.1022 +}
  1.1023 +
  1.1024 +/// \brief Write a bool to a bitstream.
  1.1025 +/// \param[in] inTemplateVar The value to write
  1.1026 +template <>
  1.1027 +inline void BitStream::Write(const bool &inTemplateVar)
  1.1028 +{
  1.1029 +	if ( inTemplateVar )
  1.1030 +		Write1();
  1.1031 +	else
  1.1032 +		Write0();
  1.1033 +}
  1.1034 +
  1.1035 +
  1.1036 +/// \brief Write a string to a bitstream.
  1.1037 +/// \param[in] var The value to write
  1.1038 +template <>
  1.1039 +inline void BitStream::Write(const OVR::String &inTemplateVar)
  1.1040 +{
  1.1041 +	uint16_t l = (uint16_t) inTemplateVar.GetLength();
  1.1042 +	Write(l);
  1.1043 +	WriteAlignedBytes((const unsigned char*) inTemplateVar.ToCStr(), (const unsigned int) l);
  1.1044 +}
  1.1045 +template <>
  1.1046 +inline void BitStream::Write(const char * const &inStringVar)
  1.1047 +{
  1.1048 +	uint16_t l = (uint16_t) strlen(inStringVar);
  1.1049 +	Write(l);
  1.1050 +	WriteAlignedBytes((const unsigned char*) inStringVar, (const unsigned int) l);
  1.1051 +}
  1.1052 +template <>
  1.1053 +inline void BitStream::Write(const unsigned char * const &inTemplateVar)
  1.1054 +{
  1.1055 +	Write((const char*)inTemplateVar);
  1.1056 +}
  1.1057 +template <>
  1.1058 +inline void BitStream::Write(char * const &inTemplateVar)
  1.1059 +{
  1.1060 +	Write((const char*)inTemplateVar);
  1.1061 +}
  1.1062 +template <>
  1.1063 +inline void BitStream::Write(unsigned char * const &inTemplateVar)
  1.1064 +{
  1.1065 +	Write((const char*)inTemplateVar);
  1.1066 +}
  1.1067 +
  1.1068 +/// \brief Write any integral type to a bitstream.  
  1.1069 +/// \details If the current value is different from the last value
  1.1070 +/// the current value will be written.  Otherwise, a single bit will be written
  1.1071 +/// \param[in] currentValue The current value to write
  1.1072 +/// \param[in] lastValue The last value to compare against
  1.1073 +template <class templateType>
  1.1074 +inline void BitStream::WriteDelta(const templateType &currentValue, const templateType &lastValue)
  1.1075 +{
  1.1076 +	if (currentValue==lastValue)
  1.1077 +	{
  1.1078 +		Write(false);
  1.1079 +	}
  1.1080 +	else
  1.1081 +	{
  1.1082 +		Write(true);
  1.1083 +		Write(currentValue);
  1.1084 +	}
  1.1085 +}
  1.1086 +
  1.1087 +/// \brief Write a bool delta. Same thing as just calling Write
  1.1088 +/// \param[in] currentValue The current value to write
  1.1089 +/// \param[in] lastValue The last value to compare against
  1.1090 +template <>
  1.1091 +inline void BitStream::WriteDelta(const bool &currentValue, const bool &lastValue)
  1.1092 +{
  1.1093 +	(void) lastValue;
  1.1094 +
  1.1095 +	Write(currentValue);
  1.1096 +}
  1.1097 +
  1.1098 +/// \brief WriteDelta when you don't know what the last value is, or there is no last value.
  1.1099 +/// \param[in] currentValue The current value to write
  1.1100 +template <class templateType>
  1.1101 +inline void BitStream::WriteDelta(const templateType &currentValue)
  1.1102 +{
  1.1103 +	Write(true);
  1.1104 +	Write(currentValue);
  1.1105 +}
  1.1106 +
  1.1107 +/// \brief Write any integral type to a bitstream.  
  1.1108 +/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
  1.1109 +/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
  1.1110 +/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
  1.1111 +/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
  1.1112 +/// \param[in] inTemplateVar The value to write
  1.1113 +template <class templateType>
  1.1114 +inline void BitStream::WriteCompressed(const templateType &inTemplateVar)
  1.1115 +{
  1.1116 +#ifdef OVR_CC_MSVC
  1.1117 +#pragma warning(disable:4127)   // conditional expression is constant
  1.1118 +#endif
  1.1119 +	if (sizeof(inTemplateVar)==1)
  1.1120 +		WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true );
  1.1121 +	else
  1.1122 +	{
  1.1123 +#ifndef __BITSTREAM_NATIVE_END
  1.1124 +#ifdef OVR_CC_MSVC
  1.1125 +#pragma warning(disable:4244)   // '=' : conversion from 'unsigned long' to 'uint16_t', possible loss of data
  1.1126 +#endif
  1.1127 +
  1.1128 +		if (DoEndianSwap())
  1.1129 +		{
  1.1130 +			unsigned char output[sizeof(templateType)];
  1.1131 +			ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType));
  1.1132 +			WriteCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true );
  1.1133 +		}
  1.1134 +		else
  1.1135 +#endif
  1.1136 +			WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true );
  1.1137 +	}
  1.1138 +}
  1.1139 +
  1.1140 +template <>
  1.1141 +inline void BitStream::WriteCompressed(const bool &inTemplateVar)
  1.1142 +{
  1.1143 +	Write(inTemplateVar);
  1.1144 +}
  1.1145 +
  1.1146 +/// For values between -1 and 1
  1.1147 +template <>
  1.1148 +inline void BitStream::WriteCompressed(const float &inTemplateVar)
  1.1149 +{
  1.1150 +	OVR_ASSERT(inTemplateVar > -1.01f && inTemplateVar < 1.01f);
  1.1151 +	float varCopy=inTemplateVar;
  1.1152 +	if (varCopy < -1.0f)
  1.1153 +		varCopy=-1.0f;
  1.1154 +	if (varCopy > 1.0f)
  1.1155 +		varCopy=1.0f;
  1.1156 +	Write((uint16_t)((varCopy+1.0f)*32767.5f));
  1.1157 +}
  1.1158 +
  1.1159 +/// For values between -1 and 1
  1.1160 +template <>
  1.1161 +inline void BitStream::WriteCompressed(const double &inTemplateVar)
  1.1162 +{
  1.1163 +	OVR_ASSERT(inTemplateVar > -1.01 && inTemplateVar < 1.01);
  1.1164 +	double varCopy=inTemplateVar;
  1.1165 +	if (varCopy < -1.0f)
  1.1166 +		varCopy=-1.0f;
  1.1167 +	if (varCopy > 1.0f)
  1.1168 +		varCopy=1.0f;
  1.1169 +	Write((uint32_t)((varCopy+1.0)*2147483648.0));
  1.1170 +}
  1.1171 +
  1.1172 +/// \brief Write any integral type to a bitstream.  
  1.1173 +/// \details If the current value is different from the last value
  1.1174 +/// the current value will be written.  Otherwise, a single bit will be written
  1.1175 +/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
  1.1176 +/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
  1.1177 +/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
  1.1178 +/// \param[in] currentValue The current value to write
  1.1179 +/// \param[in] lastValue The last value to compare against
  1.1180 +template <class templateType>
  1.1181 +inline void BitStream::WriteCompressedDelta(const templateType &currentValue, const templateType &lastValue)
  1.1182 +{
  1.1183 +	if (currentValue==lastValue)
  1.1184 +	{
  1.1185 +		Write(false);
  1.1186 +	}
  1.1187 +	else
  1.1188 +	{
  1.1189 +		Write(true);
  1.1190 +		WriteCompressed(currentValue);
  1.1191 +	}
  1.1192 +}
  1.1193 +
  1.1194 +/// \brief Write a bool delta.  Same thing as just calling Write
  1.1195 +/// \param[in] currentValue The current value to write
  1.1196 +/// \param[in] lastValue The last value to compare against
  1.1197 +template <>
  1.1198 +inline void BitStream::WriteCompressedDelta(const bool &currentValue, const bool &lastValue)
  1.1199 +{
  1.1200 +	(void) lastValue;
  1.1201 +
  1.1202 +	Write(currentValue);
  1.1203 +}
  1.1204 +
  1.1205 +/// \brief Save as WriteCompressedDelta(const templateType &currentValue, const templateType &lastValue) 
  1.1206 +/// when we have an unknown second parameter
  1.1207 +template <class templateType>
  1.1208 +inline void BitStream::WriteCompressedDelta(const templateType &currentValue)
  1.1209 +{
  1.1210 +	Write(true);
  1.1211 +	WriteCompressed(currentValue);
  1.1212 +}
  1.1213 +
  1.1214 +/// \brief Save as WriteCompressedDelta(bool currentValue, const templateType &lastValue) 
  1.1215 +/// when we have an unknown second bool
  1.1216 +template <>
  1.1217 +inline void BitStream::WriteCompressedDelta(const bool &currentValue)
  1.1218 +{
  1.1219 +	Write(currentValue);
  1.1220 +}
  1.1221 +
  1.1222 +/// \brief Read any integral type from a bitstream.  Define __BITSTREAM_NATIVE_END if you need endian swapping.
  1.1223 +/// \param[in] outTemplateVar The value to read
  1.1224 +template <class templateType>
  1.1225 +inline bool BitStream::Read(templateType &outTemplateVar)
  1.1226 +{
  1.1227 +#ifdef OVR_CC_MSVC
  1.1228 +#pragma warning(disable:4127)   // conditional expression is constant
  1.1229 +#endif
  1.1230 +	if (sizeof(outTemplateVar)==1)
  1.1231 +		return ReadBits( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true );
  1.1232 +	else
  1.1233 +	{
  1.1234 +#ifndef __BITSTREAM_NATIVE_END
  1.1235 +#ifdef OVR_CC_MSVC
  1.1236 +#pragma warning(disable:4244)   // '=' : conversion from 'unsigned long' to 'uint16_t', possible loss of data
  1.1237 +#endif
  1.1238 +		if (DoEndianSwap())
  1.1239 +		{
  1.1240 +			unsigned char output[sizeof(templateType)];
  1.1241 +			if (ReadBits( ( unsigned char* ) output, sizeof(templateType) * 8, true ))
  1.1242 +			{
  1.1243 +				ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType));
  1.1244 +				return true;
  1.1245 +			}
  1.1246 +			return false;
  1.1247 +		}
  1.1248 +		else
  1.1249 +#endif
  1.1250 +			return ReadBits( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true );
  1.1251 +	}
  1.1252 +}
  1.1253 +
  1.1254 +/// \brief Read a bool from a bitstream.
  1.1255 +/// \param[in] outTemplateVar The value to read
  1.1256 +template <>
  1.1257 +inline bool BitStream::Read(bool &outTemplateVar)
  1.1258 +{
  1.1259 +	if ( readOffset + 1 > numberOfBitsUsed )
  1.1260 +		return false;
  1.1261 +
  1.1262 +	if ( data[ readOffset >> 3 ] & ( 0x80 >> ( readOffset & 7 ) ) )   // Is it faster to just write it out here?
  1.1263 +		outTemplateVar = true;
  1.1264 +	else
  1.1265 +		outTemplateVar = false;
  1.1266 +
  1.1267 +	// Has to be on a different line for Mac
  1.1268 +	readOffset++;
  1.1269 +
  1.1270 +	return true;
  1.1271 +}
  1.1272 +
  1.1273 +template <>
  1.1274 +inline bool BitStream::Read(OVR::String &outTemplateVar)
  1.1275 +{
  1.1276 +	bool b;
  1.1277 +	uint16_t l;
  1.1278 +	b=Read(l);
  1.1279 +	if (b && l>0)
  1.1280 +	{
  1.1281 +		AlignReadToByteBoundary();
  1.1282 +		outTemplateVar.AssignString((const char*) (data + ( readOffset >> 3 )), (size_t) l);
  1.1283 +		IgnoreBytes(l);
  1.1284 +	}
  1.1285 +	else
  1.1286 +	{
  1.1287 +		AlignReadToByteBoundary();
  1.1288 +	}
  1.1289 +	return b;
  1.1290 +}
  1.1291 +template <>
  1.1292 +inline bool BitStream::Read(char *&varString)
  1.1293 +{
  1.1294 +	bool b;
  1.1295 +	uint16_t l;
  1.1296 +	b=Read(l);
  1.1297 +	if (b && l>0)
  1.1298 +	{
  1.1299 +		memcpy(varString, data + ( readOffset >> 3 ), l);
  1.1300 +		IgnoreBytes(l);
  1.1301 +	}
  1.1302 +	else
  1.1303 +	{
  1.1304 +		AlignReadToByteBoundary();
  1.1305 +	}
  1.1306 +	return b;
  1.1307 +}
  1.1308 +template <>
  1.1309 +inline bool BitStream::Read(unsigned char *&varString)
  1.1310 +{
  1.1311 +	bool b;
  1.1312 +	uint16_t l;
  1.1313 +	b=Read(l);
  1.1314 +	if (b && l>0)
  1.1315 +	{
  1.1316 +		memcpy(varString, data + ( readOffset >> 3 ), l);
  1.1317 +		IgnoreBytes(l);
  1.1318 +	}
  1.1319 +	else
  1.1320 +	{
  1.1321 +		AlignReadToByteBoundary();
  1.1322 +	}
  1.1323 +	return b;
  1.1324 +}
  1.1325 +
  1.1326 +/// \brief Read any integral type from a bitstream.  
  1.1327 +/// \details If the written value differed from the value compared against in the write function,
  1.1328 +/// var will be updated.  Otherwise it will retain the current value.
  1.1329 +/// ReadDelta is only valid from a previous call to WriteDelta
  1.1330 +/// \param[in] outTemplateVar The value to read
  1.1331 +template <class templateType>
  1.1332 +inline bool BitStream::ReadDelta(templateType &outTemplateVar)
  1.1333 +{
  1.1334 +	bool dataWritten;
  1.1335 +	bool success;
  1.1336 +	success=Read(dataWritten);
  1.1337 +	if (dataWritten)
  1.1338 +		success=Read(outTemplateVar);
  1.1339 +	return success;
  1.1340 +}
  1.1341 +
  1.1342 +/// \brief Read a bool from a bitstream.
  1.1343 +/// \param[in] outTemplateVar The value to read
  1.1344 +template <>
  1.1345 +inline bool BitStream::ReadDelta(bool &outTemplateVar)
  1.1346 +{
  1.1347 +	return Read(outTemplateVar);
  1.1348 +}
  1.1349 +
  1.1350 +/// \brief Read any integral type from a bitstream.  
  1.1351 +/// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
  1.1352 +/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
  1.1353 +/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
  1.1354 +/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
  1.1355 +/// \param[in] outTemplateVar The value to read
  1.1356 +template <class templateType>
  1.1357 +inline bool BitStream::ReadCompressed(templateType &outTemplateVar)
  1.1358 +{
  1.1359 +#ifdef OVR_CC_MSVC
  1.1360 +#pragma warning(disable:4127)   // conditional expression is constant
  1.1361 +#endif
  1.1362 +	if (sizeof(outTemplateVar)==1)
  1.1363 +		return ReadCompressed( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true );
  1.1364 +	else
  1.1365 +	{
  1.1366 +#ifndef __BITSTREAM_NATIVE_END
  1.1367 +		if (DoEndianSwap())
  1.1368 +		{
  1.1369 +			unsigned char output[sizeof(templateType)];
  1.1370 +			if (ReadCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true ))
  1.1371 +			{
  1.1372 +				ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType));
  1.1373 +				return true;
  1.1374 +			}
  1.1375 +			return false;
  1.1376 +		}
  1.1377 +		else
  1.1378 +#endif
  1.1379 +			return ReadCompressed( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true );
  1.1380 +	}
  1.1381 +}
  1.1382 +
  1.1383 +template <>
  1.1384 +inline bool BitStream::ReadCompressed(bool &outTemplateVar)
  1.1385 +{
  1.1386 +	return Read(outTemplateVar);
  1.1387 +}
  1.1388 +
  1.1389 +/// For values between -1 and 1
  1.1390 +template <>
  1.1391 +inline bool BitStream::ReadCompressed(float &outTemplateVar)
  1.1392 +{
  1.1393 +	uint16_t compressedFloat;
  1.1394 +	if (Read(compressedFloat))
  1.1395 +	{
  1.1396 +		outTemplateVar = ((float)compressedFloat / 32767.5f - 1.0f);
  1.1397 +		return true;
  1.1398 +	}
  1.1399 +	return false;
  1.1400 +}
  1.1401 +
  1.1402 +/// For values between -1 and 1
  1.1403 +template <>
  1.1404 +inline bool BitStream::ReadCompressed(double &outTemplateVar)
  1.1405 +{
  1.1406 +	uint32_t compressedFloat;
  1.1407 +	if (Read(compressedFloat))
  1.1408 +	{
  1.1409 +		outTemplateVar = ((double)compressedFloat / 2147483648.0 - 1.0);
  1.1410 +		return true;
  1.1411 +	}
  1.1412 +	return false;
  1.1413 +}
  1.1414 +
  1.1415 +/// \brief Read any integral type from a bitstream.  
  1.1416 +/// \details If the written value differed from the value compared against in the write function,
  1.1417 +/// var will be updated.  Otherwise it will retain the current value.
  1.1418 +/// the current value will be updated.
  1.1419 +/// For floating point, this is lossy, using 2 bytes for a float and 4 for a double.  The range must be between -1 and +1.
  1.1420 +/// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type
  1.1421 +/// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
  1.1422 +/// ReadCompressedDelta is only valid from a previous call to WriteDelta
  1.1423 +/// \param[in] outTemplateVar The value to read
  1.1424 +template <class templateType>
  1.1425 +inline bool BitStream::ReadCompressedDelta(templateType &outTemplateVar)
  1.1426 +{
  1.1427 +	bool dataWritten;
  1.1428 +	bool success;
  1.1429 +	success=Read(dataWritten);
  1.1430 +	if (dataWritten)
  1.1431 +		success=ReadCompressed(outTemplateVar);
  1.1432 +	return success;
  1.1433 +}
  1.1434 +
  1.1435 +/// \brief Read a bool from a bitstream.
  1.1436 +/// \param[in] outTemplateVar The value to read
  1.1437 +template <>
  1.1438 +inline bool BitStream::ReadCompressedDelta(bool &outTemplateVar)
  1.1439 +{
  1.1440 +	return Read(outTemplateVar);
  1.1441 +}
  1.1442 +
  1.1443 +template <class destinationType, class sourceType >
  1.1444 +void BitStream::WriteCasted( const sourceType &value )
  1.1445 +{
  1.1446 +	destinationType val = (destinationType) value;
  1.1447 +	Write(val);
  1.1448 +}
  1.1449 +
  1.1450 +template <class templateType>
  1.1451 +void BitStream::WriteBitsFromIntegerRange( const templateType value, const templateType minimum,const templateType maximum, bool allowOutsideRange )
  1.1452 +{
  1.1453 +	int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum));
  1.1454 +	WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
  1.1455 +}
  1.1456 +template <class templateType>
  1.1457 +void BitStream::WriteBitsFromIntegerRange( const templateType value, const templateType minimum,const templateType maximum, const int requiredBits, bool allowOutsideRange )
  1.1458 +{
  1.1459 +	OVR_ASSERT(maximum>=minimum);
  1.1460 +	OVR_ASSERT(allowOutsideRange==true || (value>=minimum && value<=maximum));
  1.1461 +	if (allowOutsideRange)
  1.1462 +	{
  1.1463 +		if (value<minimum || value>maximum)
  1.1464 +		{
  1.1465 +			Write(true);
  1.1466 +			Write(value);
  1.1467 +			return;
  1.1468 +		}
  1.1469 +		Write(false);
  1.1470 +	}
  1.1471 +	templateType valueOffMin=value-minimum;
  1.1472 +	if (IsBigEndian()==true)
  1.1473 +	{
  1.1474 +		unsigned char output[sizeof(templateType)];
  1.1475 +		ReverseBytes((unsigned char*)&valueOffMin, output, sizeof(templateType));
  1.1476 +		WriteBits(output,requiredBits);
  1.1477 +	}
  1.1478 +	else
  1.1479 +	{
  1.1480 +		WriteBits((unsigned char*) &valueOffMin,requiredBits);
  1.1481 +	}
  1.1482 +}
  1.1483 +
  1.1484 +template <class templateType> // templateType for this function must be a float or double
  1.1485 +void BitStream::WriteNormVector( templateType x, templateType y, templateType z )
  1.1486 +{
  1.1487 +#ifdef _DEBUG
  1.1488 +	OVR_ASSERT(x <= 1.01 && y <= 1.01 && z <= 1.01 && x >= -1.01 && y >= -1.01 && z >= -1.01);
  1.1489 +#endif
  1.1490 +
  1.1491 +	WriteFloat16((float)x,-1.0f,1.0f);
  1.1492 +	WriteFloat16((float)y,-1.0f,1.0f);
  1.1493 +	WriteFloat16((float)z,-1.0f,1.0f);
  1.1494 +}
  1.1495 +
  1.1496 +template <class templateType> // templateType for this function must be a float or double
  1.1497 +void BitStream::WriteVector( templateType x, templateType y, templateType z )
  1.1498 +{
  1.1499 +	templateType magnitude = sqrt(x * x + y * y + z * z);
  1.1500 +	Write((float)magnitude);
  1.1501 +	if (magnitude > 0.00001f)
  1.1502 +	{
  1.1503 +		WriteCompressed((float)(x/magnitude));
  1.1504 +		WriteCompressed((float)(y/magnitude));
  1.1505 +		WriteCompressed((float)(z/magnitude));
  1.1506 +		//	Write((uint16_t)((x/magnitude+1.0f)*32767.5f));
  1.1507 +		//	Write((uint16_t)((y/magnitude+1.0f)*32767.5f));
  1.1508 +		//	Write((uint16_t)((z/magnitude+1.0f)*32767.5f));
  1.1509 +	}
  1.1510 +}
  1.1511 +
  1.1512 +template <class templateType> // templateType for this function must be a float or double
  1.1513 +void BitStream::WriteNormQuat( templateType w, templateType x, templateType y, templateType z)
  1.1514 +{
  1.1515 +	Write((bool)(w<0.0));
  1.1516 +	Write((bool)(x<0.0));
  1.1517 +	Write((bool)(y<0.0));
  1.1518 +	Write((bool)(z<0.0));
  1.1519 +	Write((uint16_t)(fabs(x)*65535.0));
  1.1520 +	Write((uint16_t)(fabs(y)*65535.0));
  1.1521 +	Write((uint16_t)(fabs(z)*65535.0));
  1.1522 +	// Leave out w and calculate it on the target
  1.1523 +}
  1.1524 +
  1.1525 +template <class templateType> // templateType for this function must be a float or double
  1.1526 +void BitStream::WriteOrthMatrix(
  1.1527 +	templateType m00, templateType m01, templateType m02,
  1.1528 +	templateType m10, templateType m11, templateType m12,
  1.1529 +	templateType m20, templateType m21, templateType m22 )
  1.1530 +{
  1.1531 +
  1.1532 +	double qw;
  1.1533 +	double qx;
  1.1534 +	double qy;
  1.1535 +	double qz;
  1.1536 +
  1.1537 +	// Convert matrix to quat
  1.1538 +	// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
  1.1539 +	float sum;
  1.1540 +	sum = 1 + m00 + m11 + m22;
  1.1541 +	if (sum < 0.0f) sum=0.0f;
  1.1542 +	qw = sqrt( sum  ) / 2;
  1.1543 +	sum = 1 + m00 - m11 - m22;
  1.1544 +	if (sum < 0.0f) sum=0.0f;
  1.1545 +	qx = sqrt( sum  ) / 2;
  1.1546 +	sum = 1 - m00 + m11 - m22;
  1.1547 +	if (sum < 0.0f) sum=0.0f;
  1.1548 +	qy = sqrt( sum  ) / 2;
  1.1549 +	sum = 1 - m00 - m11 + m22;
  1.1550 +	if (sum < 0.0f) sum=0.0f;
  1.1551 +	qz = sqrt( sum  ) / 2;
  1.1552 +	if (qw < 0.0) qw=0.0;
  1.1553 +	if (qx < 0.0) qx=0.0;
  1.1554 +	if (qy < 0.0) qy=0.0;
  1.1555 +	if (qz < 0.0) qz=0.0;
  1.1556 +#ifdef OVR_OS_WIN32
  1.1557 +	qx = _copysign( (double) qx, (double) (m21 - m12) );
  1.1558 +	qy = _copysign( (double) qy, (double) (m02 - m20) );
  1.1559 +	qz = _copysign( (double) qz, (double) (m10 - m01) );
  1.1560 +#else
  1.1561 +	qx = copysign( (double) qx, (double) (m21 - m12) );
  1.1562 +	qy = copysign( (double) qy, (double) (m02 - m20) );
  1.1563 +	qz = copysign( (double) qz, (double) (m10 - m01) );
  1.1564 +#endif
  1.1565 +
  1.1566 +	WriteNormQuat(qw,qx,qy,qz);
  1.1567 +}
  1.1568 +
  1.1569 +template <class serializationType, class sourceType >
  1.1570 +bool BitStream::ReadCasted( sourceType &value )
  1.1571 +{
  1.1572 +	serializationType val;
  1.1573 +	bool success = Read(val);
  1.1574 +	value=(sourceType) val;
  1.1575 +	return success;
  1.1576 +}
  1.1577 +
  1.1578 +template <class templateType>
  1.1579 +bool BitStream::ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange )
  1.1580 +{
  1.1581 +	int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum));
  1.1582 +	return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange);
  1.1583 +}
  1.1584 +template <class templateType>
  1.1585 +bool BitStream::ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange )
  1.1586 +{
  1.1587 +	OVR_ASSERT_AND_UNUSED(maximum>=minimum, maximum);
  1.1588 +	if (allowOutsideRange)
  1.1589 +	{
  1.1590 +		bool isOutsideRange;
  1.1591 +		Read(isOutsideRange);
  1.1592 +		if (isOutsideRange)
  1.1593 +			return Read(value);
  1.1594 +	}
  1.1595 +	unsigned char output[sizeof(templateType)];
  1.1596 +	memset(output,0,sizeof(output));
  1.1597 +	bool success = ReadBits(output,requiredBits);
  1.1598 +	if (success)
  1.1599 +	{
  1.1600 +		if (IsBigEndian()==true)
  1.1601 +			ReverseBytesInPlace(output,sizeof(output));
  1.1602 +		memcpy(&value,output,sizeof(output));
  1.1603 +
  1.1604 +		value+=minimum;
  1.1605 +	}
  1.1606 +
  1.1607 +	return success;
  1.1608 +}
  1.1609 +
  1.1610 +template <class templateType> // templateType for this function must be a float or double
  1.1611 +bool BitStream::ReadNormVector( templateType &x, templateType &y, templateType &z )
  1.1612 +{
  1.1613 +	float xIn,yIn,zIn;
  1.1614 +	ReadFloat16(xIn,-1.0f,1.0f);
  1.1615 +	ReadFloat16(yIn,-1.0f,1.0f);
  1.1616 +	ReadFloat16(zIn,-1.0f,1.0f);
  1.1617 +	x=xIn;
  1.1618 +	y=yIn;
  1.1619 +	z=zIn;
  1.1620 +	return true;
  1.1621 +}
  1.1622 +
  1.1623 +template <class templateType> // templateType for this function must be a float or double
  1.1624 +bool BitStream::ReadVector( templateType &x, templateType &y, templateType &z )
  1.1625 +{
  1.1626 +	float magnitude;
  1.1627 +	//uint16_t sx,sy,sz;
  1.1628 +	if (!Read(magnitude))
  1.1629 +		return false;
  1.1630 +	if (magnitude>0.00001f)
  1.1631 +	{
  1.1632 +		//	Read(sx);
  1.1633 +		//	Read(sy);
  1.1634 +		//	if (!Read(sz))
  1.1635 +		//		return false;
  1.1636 +		//	x=((float)sx / 32767.5f - 1.0f) * magnitude;
  1.1637 +		//	y=((float)sy / 32767.5f - 1.0f) * magnitude;
  1.1638 +		//	z=((float)sz / 32767.5f - 1.0f) * magnitude;
  1.1639 +		float cx=0.0f,cy=0.0f,cz=0.0f;
  1.1640 +		ReadCompressed(cx);
  1.1641 +		ReadCompressed(cy);
  1.1642 +		if (!ReadCompressed(cz))
  1.1643 +			return false;
  1.1644 +		x=cx;
  1.1645 +		y=cy;
  1.1646 +		z=cz;
  1.1647 +		x*=magnitude;
  1.1648 +		y*=magnitude;
  1.1649 +		z*=magnitude;
  1.1650 +	}
  1.1651 +	else
  1.1652 +	{
  1.1653 +		x=0.0;
  1.1654 +		y=0.0;
  1.1655 +		z=0.0;
  1.1656 +	}
  1.1657 +	return true;
  1.1658 +}
  1.1659 +
  1.1660 +template <class templateType> // templateType for this function must be a float or double
  1.1661 +bool BitStream::ReadNormQuat( templateType &w, templateType &x, templateType &y, templateType &z)
  1.1662 +{
  1.1663 +	bool cwNeg=false, cxNeg=false, cyNeg=false, czNeg=false;
  1.1664 +	uint16_t cx,cy,cz;
  1.1665 +	Read(cwNeg);
  1.1666 +	Read(cxNeg);
  1.1667 +	Read(cyNeg);
  1.1668 +	Read(czNeg);
  1.1669 +	Read(cx);
  1.1670 +	Read(cy);
  1.1671 +	if (!Read(cz))
  1.1672 +		return false;
  1.1673 +
  1.1674 +	// Calculate w from x,y,z
  1.1675 +	x=(templateType)(cx/65535.0);
  1.1676 +	y=(templateType)(cy/65535.0);
  1.1677 +	z=(templateType)(cz/65535.0);
  1.1678 +	if (cxNeg) x=-x;
  1.1679 +	if (cyNeg) y=-y;
  1.1680 +	if (czNeg) z=-z;
  1.1681 +	float difference = 1.0f - x*x - y*y - z*z;
  1.1682 +	if (difference < 0.0f)
  1.1683 +		difference=0.0f;
  1.1684 +	w = (templateType)(sqrt(difference));
  1.1685 +	if (cwNeg)
  1.1686 +		w=-w;
  1.1687 +
  1.1688 +	return true;
  1.1689 +}
  1.1690 +
  1.1691 +template <class templateType> // templateType for this function must be a float or double
  1.1692 +bool BitStream::ReadOrthMatrix(
  1.1693 +	templateType &m00, templateType &m01, templateType &m02,
  1.1694 +	templateType &m10, templateType &m11, templateType &m12,
  1.1695 +	templateType &m20, templateType &m21, templateType &m22 )
  1.1696 +{
  1.1697 +	float qw,qx,qy,qz;
  1.1698 +	if (!ReadNormQuat(qw,qx,qy,qz))
  1.1699 +		return false;
  1.1700 +
  1.1701 +	// Quat to orthogonal rotation matrix
  1.1702 +	// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm
  1.1703 +	double sqw = (double)qw*(double)qw;
  1.1704 +	double sqx = (double)qx*(double)qx;
  1.1705 +	double sqy = (double)qy*(double)qy;
  1.1706 +	double sqz = (double)qz*(double)qz;
  1.1707 +	m00 =  (templateType)(sqx - sqy - sqz + sqw); // since sqw + sqx + sqy + sqz =1
  1.1708 +	m11 = (templateType)(-sqx + sqy - sqz + sqw);
  1.1709 +	m22 = (templateType)(-sqx - sqy + sqz + sqw);
  1.1710 +
  1.1711 +	double tmp1 = (double)qx*(double)qy;
  1.1712 +	double tmp2 = (double)qz*(double)qw;
  1.1713 +	m10 = (templateType)(2.0 * (tmp1 + tmp2));
  1.1714 +	m01 = (templateType)(2.0 * (tmp1 - tmp2));
  1.1715 +
  1.1716 +	tmp1 = (double)qx*(double)qz;
  1.1717 +	tmp2 = (double)qy*(double)qw;
  1.1718 +	m20 =(templateType)(2.0 * (tmp1 - tmp2));
  1.1719 +	m02 = (templateType)(2.0 * (tmp1 + tmp2));
  1.1720 +	tmp1 = (double)qy*(double)qz;
  1.1721 +	tmp2 = (double)qx*(double)qw;
  1.1722 +	m21 = (templateType)(2.0 * (tmp1 + tmp2));
  1.1723 +	m12 = (templateType)(2.0 * (tmp1 - tmp2));
  1.1724 +
  1.1725 +	return true;
  1.1726 +}
  1.1727 +
  1.1728 +template <class templateType>
  1.1729 +BitStream& operator<<(BitStream& out, templateType& c)
  1.1730 +{
  1.1731 +	out.Write(c);
  1.1732 +	return out;
  1.1733 +}
  1.1734 +template <class templateType>
  1.1735 +BitStream& operator>>(BitStream& in, templateType& c)
  1.1736 +{
  1.1737 +	bool success = in.Read(c);
  1.1738 +	(void)success;
  1.1739 +
  1.1740 +	OVR_ASSERT(success);
  1.1741 +	return in;
  1.1742 +}
  1.1743 +
  1.1744 +
  1.1745 +}} // OVR::Net
  1.1746 +
  1.1747 +#endif