itom 1.3.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 &append(const char *str); 00085 00086 ByteArray &append(const ByteArray &str); 00087 00088 int length() const { if(d){ return (int)strlen(d->m_pData); } return 0; } 00089 00090 int size() const { if(d){ return (int)strlen(d->m_pData); } return 0; } 00091 00092 bool empty() const { if(d) { return strlen(d->m_pData) == 0; } return true; } 00093 00094 const char *data() const { return d ? d->m_pData : &emptyChar; }; 00095 00096 inline char &operator[](unsigned int i) const 00097 { 00098 assert(i >= 0 && i < (unsigned int)(size())); 00099 if (d) 00100 { 00101 return d->m_pData[i]; 00102 } 00103 return emptyChar; //will never occur 00104 } 00105 00106 inline char &operator[](int i) const 00107 { 00108 assert(i >= 0 && i < size()); 00109 if (d) 00110 { 00111 return d->m_pData[i]; 00112 } 00113 return emptyChar; //will never occur 00114 } 00115 00116 bool operator==(const ByteArray &a) const; 00117 inline bool operator!=(const ByteArray &a) const { return !(operator==(a)); } 00118 00119 00120 private: 00121 Data *d; 00122 00123 inline void decAndFree(Data *x) 00124 { 00125 if (x && !(BYTEARRAY_DECREF(x))) 00126 { 00127 free(x); 00128 } 00129 } 00130 00131 00132 }; 00133 00134 inline bool operator==(const ByteArray &a1, const char *a2) 00135 { 00136 return a2 ? strcmp(a1.data(),a2) == 0 : (a1.size() == 0); 00137 } 00138 00139 inline bool operator==(const char *a1, const ByteArray &a2) 00140 { 00141 return a1 ? strcmp(a1,a2.data()) == 0 : (a2.size() == 0); 00142 } 00143 00144 inline bool operator!=(const ByteArray &a1, const char *a2) 00145 { 00146 return a2 ? strcmp(a1.data(),a2) != 0 : (a1.size() > 0); 00147 } 00148 inline bool operator!=(const char *a1, const ByteArray &a2) 00149 { 00150 return a1 ? strcmp(a1,a2.data()) != 0 : (a2.size() > 0); 00151 } 00152 00153 00154 } //end namespace ito 00155 00156 #ifdef __APPLE__ 00157 } 00158 #endif 00159 00160 #endif