itom 2.2.1
K:/git-itom/sources/itom/common/byteArray.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2016, Institut fuer Technische Optik (ITO),
00005     Universitaet 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 fuer 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 
00069 class ITOMCOMMON_EXPORT ByteArray
00070 {       
00071     private:
00083         struct Data
00084         {
00085             int m_ref;               
00086             char *m_pData;           
00087             char m_buffer[1];        
00088             //do not append further members add, only prepend!!!
00089         };
00090 
00091         static char emptyChar; 
00093     public:
00095         inline ByteArray() : d(NULL) {}
00096         
00098         ByteArray(const char *str);
00099         
00101         inline ByteArray(const ByteArray& copyConstr) : d(copyConstr.d) { if (d) {BYTEARRAY_INCCREF(d);} }
00102         
00104         inline ~ByteArray() { decAndFree(d); }
00105 
00107         ByteArray &operator=(const ByteArray &rhs);
00108 
00110         ByteArray &operator=(const char *str);
00111 
00115         ByteArray &append(const char *str);
00116 
00120         ByteArray &append(const ByteArray &str);
00121 
00125         int length() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
00126 
00130         int size() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
00131 
00133         bool empty() const { if(d) { return strlen(d->m_pData) == 0; } return true; }
00134 
00136         const char *data() const { return d ? d->m_pData : &emptyChar; };
00137 
00139 
00143         inline char &operator[](unsigned int i) const
00144         {
00145             assert(i >= 0 && i < (unsigned int)(size()));
00146             if (d)
00147             {
00148                 return d->m_pData[i];
00149             }
00150             return emptyChar; //will never occur
00151         }
00152 
00154 
00158         inline char &operator[](int i) const
00159         {
00160             assert(i >= 0 && i < size());
00161             if (d)
00162             {
00163                 return d->m_pData[i];
00164             }
00165             return emptyChar; //will never occur
00166         }
00167 
00169         bool operator==(const ByteArray &a) const;
00170 
00172         inline bool operator!=(const ByteArray &a) const { return !(operator==(a)); }       
00173         
00174 
00175     private:
00176         Data *d;  
00178         inline void decAndFree(Data *x) 
00179         { 
00180             if (x && !(BYTEARRAY_DECREF(x))) 
00181             { 
00182                 free(x);
00183             }
00184         }
00185 
00186 
00187 };
00188 
00190 inline bool operator==(const ByteArray &a1, const char *a2)
00191 { 
00192     return a2 ? strcmp(a1.data(),a2) == 0 : (a1.size() == 0);  
00193 }
00194 
00196 inline bool operator==(const char *a1, const ByteArray &a2)
00197 { 
00198     return a1 ? strcmp(a1,a2.data()) == 0 : (a2.size() == 0);
00199 }
00200 
00202 inline bool operator!=(const ByteArray &a1, const char *a2)
00203 { 
00204     return a2 ? strcmp(a1.data(),a2) != 0 : (a1.size() > 0); 
00205 }
00206 
00208 inline bool operator!=(const char *a1, const ByteArray &a2)
00209 { 
00210     return a1 ? strcmp(a1,a2.data()) != 0 : (a2.size() > 0); 
00211 }
00212 
00213 
00214 } //end namespace ito
00215 
00216 #ifdef __APPLE__
00217 }
00218 #endif
00219 
00220 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Friends