itom 2.0.0
|
00001 /* ******************************************************************** 00002 itom software 00003 URL: http://www.uni-stuttgart.de/ito 00004 Copyright (C) 2013, Institut für Technische Optik (ITO), 00005 Universität Stuttgart, Germany 00006 00007 This file is part of itom and its software development toolkit (SDK). 00008 00009 itom is free software; you can redistribute it and/or modify it 00010 under the terms of the GNU Library General Public Licence as published by 00011 the Free Software Foundation; either version 2 of the Licence, or (at 00012 your option) any later version. 00013 00014 In addition, as a special exception, the Institut für Technische 00015 Optik (ITO) gives you certain additional rights. 00016 These rights are described in the ITO LGPL Exception version 1.0, 00017 which can be found in the file LGPL_EXCEPTION.txt in this package. 00018 00019 itom is distributed in the hope that it will be useful, but 00020 WITHOUT ANY WARRANTY; without even the implied warranty of 00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library 00022 General Public Licence for more details. 00023 00024 You should have received a copy of the GNU Library General Public License 00025 along with itom. If not, see <http://www.gnu.org/licenses/>. 00026 *********************************************************************** */ 00027 00028 #ifndef BYTEARRAY_H 00029 #define BYTEARRAY_H 00030 00031 #ifdef __APPLE__ 00032 extern "C++" { 00033 #endif 00034 00035 /* includes */ 00036 #include "commonGlobal.h" 00037 #include "typeDefs.h" 00038 00039 #include <stdarg.h> 00040 #include <stdio.h> 00041 #include <assert.h> /* assert */ 00042 00043 #include <cstdlib> 00044 00045 namespace ito 00046 { 00047 00048 #if 1 00049 /*non thread-safe but faster*/ 00050 #define BYTEARRAY_DECREF(d) d->m_ref-- 00051 #define BYTEARRAY_INCCREF(d) d->m_ref++ 00052 #else 00053 /*thread safe*/ 00054 #define BYTEARRAY_DECREF(d) ITOM_DECREF(&(d->m_ref)) 00055 #define BYTEARRAY_INCCREF(d) ITOM_INCREF(&(d->m_ref)) 00056 #endif 00057 00058 //---------------------------------------------------------------------------------------------------------------------------------- 00059 // @class ByteArray 00060 class ITOMCOMMON_EXPORT ByteArray 00061 { 00062 private: 00063 struct Data 00064 { 00065 int m_ref; 00066 char *m_pData; 00067 char m_buffer[1]; 00068 //do not append further members add, only prepend!!! 00069 }; 00070 00071 static char emptyChar; 00072 00073 public: 00074 inline ByteArray() : d(NULL) {} 00075 00076 ByteArray(const char *str); 00077 00078 inline ByteArray(const ByteArray& copyConstr) : d(copyConstr.d) { if (d) {BYTEARRAY_INCCREF(d);} } 00079 00080 inline ~ByteArray() { decAndFree(d); } 00081 00082 ByteArray &operator=(const ByteArray &rhs); 00083 00084 ByteArray &operator=(const char *str); 00085 00086 ByteArray &append(const char *str); 00087 00088 ByteArray &append(const ByteArray &str); 00089 00090 int length() const { if(d){ return (int)strlen(d->m_pData); } return 0; } 00091 00092 int size() const { if(d){ return (int)strlen(d->m_pData); } return 0; } 00093 00094 bool empty() const { if(d) { return strlen(d->m_pData) == 0; } return true; } 00095 00096 const char *data() const { return d ? d->m_pData : &emptyChar; }; 00097 00098 inline char &operator[](unsigned int i) const 00099 { 00100 assert(i >= 0 && i < (unsigned int)(size())); 00101 if (d) 00102 { 00103 return d->m_pData[i]; 00104 } 00105 return emptyChar; //will never occur 00106 } 00107 00108 inline char &operator[](int i) const 00109 { 00110 assert(i >= 0 && i < size()); 00111 if (d) 00112 { 00113 return d->m_pData[i]; 00114 } 00115 return emptyChar; //will never occur 00116 } 00117 00118 bool operator==(const ByteArray &a) const; 00119 inline bool operator!=(const ByteArray &a) const { return !(operator==(a)); } 00120 00121 00122 private: 00123 Data *d; 00124 00125 inline void decAndFree(Data *x) 00126 { 00127 if (x && !(BYTEARRAY_DECREF(x))) 00128 { 00129 free(x); 00130 } 00131 } 00132 00133 00134 }; 00135 00136 inline bool operator==(const ByteArray &a1, const char *a2) 00137 { 00138 return a2 ? strcmp(a1.data(),a2) == 0 : (a1.size() == 0); 00139 } 00140 00141 inline bool operator==(const char *a1, const ByteArray &a2) 00142 { 00143 return a1 ? strcmp(a1,a2.data()) == 0 : (a2.size() == 0); 00144 } 00145 00146 inline bool operator!=(const ByteArray &a1, const char *a2) 00147 { 00148 return a2 ? strcmp(a1.data(),a2) != 0 : (a1.size() > 0); 00149 } 00150 inline bool operator!=(const char *a1, const ByteArray &a2) 00151 { 00152 return a1 ? strcmp(a1,a2.data()) != 0 : (a2.size() > 0); 00153 } 00154 00155 00156 } //end namespace ito 00157 00158 #ifdef __APPLE__ 00159 } 00160 #endif 00161 00162 #endif