Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

bytearray.cpp

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2005 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00029 #include "precomp.h"
00030 #include "bytearray.h"
00031 #include <string.h>
00032 
00034 // CL_ByteArray Construction:
00035 
00036 CL_ByteArray::CL_ByteArray() : data(0), size(0), allocated_size(0)
00037 {
00038 }
00039 
00040 CL_ByteArray::CL_ByteArray(const CL_ByteArray &copy) : data(0), size(0), allocated_size(0)
00041 {
00042         set_size(copy.size);
00043         memcpy(data, copy.data, size);
00044 }
00045 
00046 CL_ByteArray::CL_ByteArray(int size) : data(0), size(0), allocated_size(0)
00047 {
00048         set_size(size);
00049 }
00050 
00051 CL_ByteArray::~CL_ByteArray()
00052 {
00053         delete[] data;
00054 }
00055 
00057 // CL_ByteArray Attributes:
00058 
00059 char *CL_ByteArray::get_data()
00060 {
00061         return data;
00062 }
00063 
00064 const char *CL_ByteArray::get_data() const
00065 {
00066         return data;
00067 }
00068 
00069 int CL_ByteArray::get_size() const
00070 {
00071         return size;
00072 }
00073 
00074 int CL_ByteArray::get_capacity() const
00075 {
00076         return allocated_size;
00077 }
00078 
00079 char &CL_ByteArray::operator[](int i)
00080 {
00081         return data[i];
00082 }
00083 
00084 const char &CL_ByteArray::operator[](int i) const
00085 {
00086         return data[i];
00087 }
00088 
00089 char &CL_ByteArray::operator[](unsigned int i)
00090 {
00091         return data[i];
00092 }
00093 
00094 const char &CL_ByteArray::operator[](unsigned int i) const
00095 {
00096         return data[i];
00097 }
00098 
00100 // CL_ByteArray Operations:
00101 
00102 CL_ByteArray &CL_ByteArray::operator =(const CL_ByteArray &copy)
00103 {
00104         if (data != copy.data)
00105         {
00106                 set_size(copy.size);
00107                 memcpy(data, copy.data, size);
00108         }
00109         return *this;
00110 }
00111 
00112 void CL_ByteArray::set_size(int new_size)
00113 {
00114         if (new_size > allocated_size)
00115         {
00116                 char *old_data = data;
00117                 data = new char[new_size];
00118                 memcpy(data, old_data, size);
00119                 delete[] old_data;
00120                 memset(data+size, 0, new_size-size);
00121                 size = new_size;
00122                 allocated_size = new_size;
00123         }
00124         else if (new_size <= allocated_size)
00125         {
00126                 size = new_size;
00127         }
00128 }
00129 
00130 void CL_ByteArray::set_capacity(int new_capacity)
00131 {
00132         if (new_capacity > allocated_size)
00133         {
00134                 char *old_data = data;
00135                 data = new char[new_capacity];
00136                 memcpy(data, old_data, size);
00137                 delete[] old_data;
00138                 memset(data+size, 0, new_capacity-size);
00139                 allocated_size = new_capacity;
00140         }
00141 }
00142 
00144 // CL_ByteArray Implementation:

Generated on Sat Feb 19 22:51:15 2005 for npcore by  doxygen 1.4.1