Imported more code from the old engine.
[peakengine.git] / engine / include / core / Buffer.h
blob83b5b120d8d5fb87568b8af4e0556b2cdebc6b72
1 /*
2 Copyright (C) 2008 Mathias Gottschlag, Lukas Kropatschek
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef _BUFFER_H_
23 #define _BUFFER_H_
25 #include <string>
27 #include "core/Vector3D.h"
28 #include "core/Vector2D.h"
29 #include "core/Quaternion.h"
31 //tolua_begin
32 namespace peak
34 /**
35 * \brief Dynamically sized buffer for network data etc.
37 class Buffer
39 public:
40 Buffer();
41 Buffer(unsigned int size);
42 Buffer(void *data, unsigned int size);
43 Buffer(const Buffer &buf);
44 ~Buffer();
46 /**
47 * \brief Sets buffer size.
49 * If there is more data than the new size, the data is truncated.
50 * \param size New size
52 void setSize(unsigned int size);
53 /**
54 * \brief Returns the buffer size.
55 * \return Buffer size
57 unsigned int getSize(void);
58 /**
59 * \brief Returns the size of the data stored in the buffer.
60 * \return Data size
62 unsigned int getDataSize(void);
64 /**
65 * \brief Returns the current write/read position.
66 * \return Position
68 unsigned int getPosition(void);
69 /**
70 * \brief Sets the current write/read position.
71 * \param position New position
72 * \param relative If set, add the current position to the new position
74 void setPosition(unsigned int position, bool relative = false);
76 /**
77 * \brief Reads raw data from the current position.
79 * Might read less than size bytes if there is not enough data.
80 * \param dest Memory to hold the data. Must be at least size bytes long.
81 * \param size Size to be read
82 * \return Number of bytes read
84 int readData(void *dest, unsigned int size);
85 /**
86 * \brief Write data to the buffer.
88 * Might write less then size bytes if resize is not set.
89 * \param source Pointer to the data.
90 * \param size Size to be written
91 * \param resize If set, resize the buffer if it is not big enougth to hold the data
92 * \return Number of bytes written
94 int writeData(const void *source, unsigned int size,
95 bool resize = true);
97 /**
98 * \brief Write integer to the buffer.
99 * \param value Number to be written
101 void writeInt(int value);
103 * \brief Read integer from the buffer.
104 * \return Value of the integer
106 int readInt(void);
108 * \brief Write float to the buffer.
109 * \param value Number to be written
111 void writeFloat(float value);
113 * \brief Read float from the buffer.
114 * \return Value of the float
116 float readFloat(void);
118 * \brief Write string to the buffer.
119 * \param str String to be written
121 void writeString(std::string str);
123 * \brief Read string from the buffer.
124 * \return String which was read
126 std::string readString(void);
128 * \brief Write single byte to the buffer.
129 * \param value Byte to be written
131 void writeByte(unsigned char value);
133 * \brief Read single byte from the buffer.
134 * \return Value of the byte
136 unsigned char readByte(void);
138 * \brief Write word (2 bytes) to the buffer.
139 * \param value Word to be written
141 void writeWord(short value);
143 * \brief Read word (2 bytes) from the buffer.
144 * \return Value of the word
146 short readWord(void);
148 * \brief Write a bool variable to the buffer.
149 * \param flag bool variable to be written.
151 void writeBool(bool flag);
153 * \brief Read a bool variable from the buffer.
154 * \return Value of the bool variable.
156 bool readBool(void);
158 * \brief Write 2-dimensional vector to the buffer.
159 * \param v Vector to be written
161 void writeVector2D(Vector2D v);
163 * \brief Read 2-dimensional vector from the buffer.
164 * \return Value of the vector.
166 Vector2D readVector2D(void);
168 * \brief Write vector to the buffer.
169 * \param v Vector to be written
171 void writeVector3D(Vector3D v);
173 * \brief Read vector from the buffer.
174 * \return Value of the vector
176 Vector3D readVector3D(void);
178 * \brief Write quaternion to the buffer.
179 * \param q Quaternion to be written
181 void writeQuaternion(Quaternion q);
183 * \brief Read quaternion from the buffer.
184 * \return Value of the quaternion
186 Quaternion readQuaternion(void);
189 * \brief Returns pointer to the raw buffer data.
190 * \return Pointer to buffer data
192 char *getData(void);
195 * \brief Clear the buffer and free the data.
197 void clear(void);
199 //tolua_end
200 Buffer &operator=(const Buffer &buf);
201 Buffer &operator+=(const Buffer &buf);
202 Buffer &operator<<(const Buffer &buf);
203 Buffer &operator<<(int &data);
204 Buffer &operator<<(float &data);
205 private:
206 char *data;
207 unsigned int size;
208 unsigned int datasize;
209 unsigned int position;
210 //tolua_begin
213 //tolua_end
215 #endif