itom  3.0.0
byteArray.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2016, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom and its software development toolkit (SDK).
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  In addition, as a special exception, the Institut fuer Technische
15  Optik (ITO) gives you certain additional rights.
16  These rights are described in the ITO LGPL Exception version 1.0,
17  which can be found in the file LGPL_EXCEPTION.txt in this package.
18 
19  itom is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22  General Public Licence for more details.
23 
24  You should have received a copy of the GNU Library General Public License
25  along with itom. If not, see <http://www.gnu.org/licenses/>.
26 *********************************************************************** */
27 
28 #ifndef BYTEARRAY_H
29 #define BYTEARRAY_H
30 
31 #ifdef __APPLE__
32 extern "C++" {
33 #endif
34 
35 /* includes */
36 #include "commonGlobal.h"
37 #include "typeDefs.h"
38 
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <assert.h> /* assert */
42 
43 #include <cstdlib>
44 
45 namespace ito
46 {
47 
48 #if 1
49  /*non thread-safe but faster*/
50  #define BYTEARRAY_DECREF(d) d->m_ref--
51  #define BYTEARRAY_INCCREF(d) d->m_ref++
52 #else
53  /*thread safe*/
54  #define BYTEARRAY_DECREF(d) ITOM_DECREF(&(d->m_ref))
55  #define BYTEARRAY_INCCREF(d) ITOM_INCREF(&(d->m_ref))
56 #endif
57 
58 //----------------------------------------------------------------------------------------------------------------------------------
59 
69 class ITOMCOMMON_EXPORT ByteArray
70 {
71  private:
83  struct Data
84  {
85  int m_ref;
86  char *m_pData;
87  char m_buffer[1];
88  //do not append further members add, only prepend!!!
89  };
90 
91  static char emptyChar;
93  public:
95  inline ByteArray() : d(NULL) {}
96 
98  ByteArray(const char *str);
99 
101  inline ByteArray(const ByteArray& copyConstr) : d(copyConstr.d) { if (d) {BYTEARRAY_INCCREF(d);} }
102 
104  inline ~ByteArray() { decAndFree(d); }
105 
107  ByteArray &operator=(const ByteArray &rhs);
108 
110  ByteArray &operator=(const char *str);
111 
115  ByteArray &append(const char *str);
116 
120  ByteArray &append(const ByteArray &str);
121 
125  int length() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
126 
130  int size() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
131 
133  bool empty() const { if(d) { return strlen(d->m_pData) == 0; } return true; }
134 
136  const char *data() const { return d ? d->m_pData : &emptyChar; };
137 
139 
143  inline char &operator[](unsigned int i) const
144  {
145  assert(i >= 0 && i < (unsigned int)(size()));
146  if (d)
147  {
148  return d->m_pData[i];
149  }
150  return emptyChar; //will never occur
151  }
152 
154 
158  inline char &operator[](int i) const
159  {
160  assert(i >= 0 && i < size());
161  if (d)
162  {
163  return d->m_pData[i];
164  }
165  return emptyChar; //will never occur
166  }
167 
169  bool operator==(const ByteArray &a) const;
170 
172  inline bool operator!=(const ByteArray &a) const { return !(operator==(a)); }
173 
174 
175  private:
176  Data *d;
178  inline void decAndFree(Data *x)
179  {
180  if (x && !(BYTEARRAY_DECREF(x)))
181  {
182  free(x);
183  }
184  }
185 
186 
187 };
188 
190 inline bool operator==(const ByteArray &a1, const char *a2)
191 {
192  return a2 ? strcmp(a1.data(),a2) == 0 : (a1.size() == 0);
193 }
194 
196 inline bool operator==(const char *a1, const ByteArray &a2)
197 {
198  return a1 ? strcmp(a1,a2.data()) == 0 : (a2.size() == 0);
199 }
200 
202 inline bool operator!=(const ByteArray &a1, const char *a2)
203 {
204  return a2 ? strcmp(a1.data(),a2) != 0 : (a1.size() > 0);
205 }
206 
208 inline bool operator!=(const char *a1, const ByteArray &a2)
209 {
210  return a1 ? strcmp(a1,a2.data()) != 0 : (a2.size() > 0);
211 }
212 
213 
214 } //end namespace ito
215 
216 #ifdef __APPLE__
217 }
218 #endif
219 
220 #endif
ByteArray(const ByteArray &copyConstr)
copy constructor: the given byte array is implicitely shared between both instances until its content...
Definition: byteArray.h:101
int m_ref
Definition: byteArray.h:85
basic data container for class ByteArray that is implicitely shared over multiple instances of ByteAr...
Definition: byteArray.h:83
char * m_pData
Definition: byteArray.h:86
int size() const
Definition: byteArray.h:130
char & operator[](int i) const
access the i-th character of the ByteArray. An assertion is raised, if i is out of range ...
Definition: byteArray.h:158
static char emptyChar
Definition: byteArray.h:91
This is a Qt-free class for byte arrays (strings) without specific encoding information.
Definition: byteArray.h:69
Definition: apiFunctionsGraph.cpp:39
bool empty() const
return true if the ByteArray is empty hence its length is 0
Definition: byteArray.h:133
~ByteArray()
destructor: the internal data is deleted if no other instance needs it.
Definition: byteArray.h:104
char & operator[](unsigned int i) const
access the i-th character of the ByteArray. An assertion is raised, if i is out of range ...
Definition: byteArray.h:143
bool operator==(const ByteArray &a1, const char *a2)
comparison operator that returns true if the content of a1 is equal to the given zero-terminated stri...
Definition: byteArray.h:190
bool operator!=(const ByteArray &a1, const char *a2)
comparison operator that returns true if the content of a1 is not equal to the given zero-terminated ...
Definition: byteArray.h:202
bool operator!=(const ByteArray &a) const
return false, if the content of this ByteArray is equal to the given ByteArray a. ...
Definition: byteArray.h:172
Data * d
Definition: byteArray.h:176
ByteArray()
default constructor. The ByteArray is empty.
Definition: byteArray.h:95
const char * data() const
return the pointer to the internal character array. If it is empty, the returned pointer still points...
Definition: byteArray.h:136
int length() const
Definition: byteArray.h:125