itom  4.1.0
byteArray.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, 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 
49 /*thread safe*/
50 #define BYTEARRAY_DECREF(d) ITOM_DECREF(&(d->m_ref))
51 #define BYTEARRAY_INCCREF(d) ITOM_INCREF(&(d->m_ref))
52 
53 //----------------------------------------------------------------------------------------------------------------------------------
54 
64 class ITOMCOMMON_EXPORT ByteArray
65 {
66  private:
78  struct Data
79  {
80  int m_ref;
81  char *m_pData;
82  char m_buffer[1];
83  //do not append further members add, only prepend!!!
84  };
85 
86  static char emptyChar;
88  public:
90  inline ByteArray() : d(NULL) {}
91 
93  ByteArray(const char *str);
94 
96  inline ByteArray(const ByteArray& copyConstr) : d(copyConstr.d) { if (d) {BYTEARRAY_INCCREF(d);} }
97 
99  inline ~ByteArray() { decAndFree(d); }
100 
102  ByteArray &operator=(const ByteArray &rhs);
103 
105  ByteArray &operator=(const char *str);
106 
110  ByteArray &append(const char *str);
111 
115  ByteArray &append(const ByteArray &str);
116 
120  int length() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
121 
125  int size() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
126 
128  bool empty() const { if(d) { return strlen(d->m_pData) == 0; } return true; }
129 
131  const char *data() const { return d ? d->m_pData : &emptyChar; };
132 
134 
138  inline char &operator[](unsigned int i) const
139  {
140  assert(i >= 0 && i < (unsigned int)(size()));
141  if (d)
142  {
143  return d->m_pData[i];
144  }
145  return emptyChar; //will never occur
146  }
147 
149 
153  inline char &operator[](int i) const
154  {
155  assert(i >= 0 && i < size());
156  if (d)
157  {
158  return d->m_pData[i];
159  }
160  return emptyChar; //will never occur
161  }
162 
164  bool operator==(const ByteArray &a) const;
165 
167  inline bool operator!=(const ByteArray &a) const { return !(operator==(a)); }
168 
169 
170  private:
171  Data *d;
173  inline void decAndFree(Data *x)
174  {
175  if (x && !(BYTEARRAY_DECREF(x)))
176  {
177  free(x);
178  }
179  }
180 
181 
182 };
183 
185 inline bool operator==(const ByteArray &a1, const char *a2)
186 {
187  return a2 ? strcmp(a1.data(),a2) == 0 : (a1.size() == 0);
188 }
189 
191 inline bool operator==(const char *a1, const ByteArray &a2)
192 {
193  return a1 ? strcmp(a1,a2.data()) == 0 : (a2.size() == 0);
194 }
195 
197 inline bool operator!=(const ByteArray &a1, const char *a2)
198 {
199  return a2 ? strcmp(a1.data(),a2) != 0 : (a1.size() > 0);
200 }
201 
203 inline bool operator!=(const char *a1, const ByteArray &a2)
204 {
205  return a1 ? strcmp(a1,a2.data()) != 0 : (a2.size() > 0);
206 }
207 
208 
209 } //end namespace ito
210 
211 #ifdef __APPLE__
212 }
213 #endif
214 
215 #endif
ByteArray(const ByteArray &copyConstr)
copy constructor: the given byte array is implicitely shared between both instances until its content...
Definition: byteArray.h:96
int m_ref
Definition: byteArray.h:80
basic data container for class ByteArray that is implicitely shared over multiple instances of ByteAr...
Definition: byteArray.h:78
char * m_pData
Definition: byteArray.h:81
int size() const
Definition: byteArray.h:125
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:153
static char emptyChar
Definition: byteArray.h:86
This is a Qt-free class for byte arrays (strings) without specific encoding information.
Definition: byteArray.h:64
Definition: apiFunctionsGraph.cpp:39
bool empty() const
return true if the ByteArray is empty hence its length is 0
Definition: byteArray.h:128
~ByteArray()
destructor: the internal data is deleted if no other instance needs it.
Definition: byteArray.h:99
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:138
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:185
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:197
bool operator!=(const ByteArray &a) const
return false, if the content of this ByteArray is equal to the given ByteArray a. ...
Definition: byteArray.h:167
Data * d
Definition: byteArray.h:171
ByteArray()
default constructor. The ByteArray is empty.
Definition: byteArray.h:90
const char * data() const
return the pointer to the internal character array. If it is empty, the returned pointer still points...
Definition: byteArray.h:131
int length() const
Definition: byteArray.h:120