itom
Loading...
Searching...
No Matches
byteArray.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität 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 für 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__
32extern "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
45namespace 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
64class 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)
97 {
98 if (d)
99 {
100 BYTEARRAY_INCCREF(d);
101 }
102 }
103
104 inline ByteArray(ByteArray&& other) : d(other.d)
105 {
106 other.d = nullptr;
107 }
108
110 inline ~ByteArray() { decAndFree(d); }
111
113 ByteArray &operator=(const ByteArray &rhs);
114
115 ByteArray &operator=(ByteArray &&rhs);
116
118 ByteArray &operator=(const char *str);
119
123 ByteArray &append(const char *str);
124
128 ByteArray &append(const ByteArray &str);
129
133 int length() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
134
138 int size() const { if(d){ return (int)strlen(d->m_pData); } return 0; }
139
141 bool empty() const { if(d) { return strlen(d->m_pData) == 0; } return true; }
142
144 const char *data() const { return d ? d->m_pData : &emptyChar; };
145
147
151 inline char &operator[](unsigned int i) const
152 {
153 assert(i >= 0 && i < (unsigned int)(size()));
154 if (d)
155 {
156 return d->m_pData[i];
157 }
158 return emptyChar; //will never occur
159 }
160
162
166 inline char &operator[](int i) const
167 {
168 assert(i >= 0 && i < size());
169 if (d)
170 {
171 return d->m_pData[i];
172 }
173 return emptyChar; //will never occur
174 }
175
177 bool operator==(const ByteArray &a) const;
178
180 inline bool operator!=(const ByteArray &a) const { return !(operator==(a)); }
181
182
183 private:
186 inline void decAndFree(Data *x)
187 {
188 if (x && !(BYTEARRAY_DECREF(x)))
189 {
190 free(x);
191 }
192 }
193
194
195};
196
198inline bool operator==(const ByteArray &a1, const char *a2)
199{
200 return a2 ? strcmp(a1.data(),a2) == 0 : (a1.size() == 0);
201}
202
204inline bool operator==(const char *a1, const ByteArray &a2)
205{
206 return a1 ? strcmp(a1,a2.data()) == 0 : (a2.size() == 0);
207}
208
210inline bool operator!=(const ByteArray &a1, const char *a2)
211{
212 return a2 ? strcmp(a1.data(),a2) != 0 : (a1.size() > 0);
213}
214
216inline bool operator!=(const char *a1, const ByteArray &a2)
217{
218 return a1 ? strcmp(a1,a2.data()) != 0 : (a2.size() > 0);
219}
220
221
222} //end namespace ito
223
224#ifdef __APPLE__
225}
226#endif
227
228#endif
This is a Qt-free class for byte arrays (strings) without specific encoding information.
Definition byteArray.h:65
bool operator!=(const ByteArray &a) const
return false, if the content of this ByteArray is equal to the given ByteArray a.
Definition byteArray.h:180
~ByteArray()
destructor: the internal data is deleted if no other instance needs it.
Definition byteArray.h:110
int size() const
Definition byteArray.h:138
ByteArray(const ByteArray &copyConstr)
copy constructor: the given byte array is implicitly shared between both instances until its content ...
Definition byteArray.h:96
const char * data() const
return the pointer to the internal character array. If it is empty, the returned pointer still points...
Definition byteArray.h:144
bool empty() const
return true if the ByteArray is empty hence its length is 0
Definition byteArray.h:141
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:151
int length() const
Definition byteArray.h:133
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:166
static char emptyChar
Definition byteArray.h:86
ByteArray()
default constructor. The ByteArray is empty.
Definition byteArray.h:90
Data * d
Definition byteArray.h:184
Definition apiFunctionsGraph.cpp:40
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:210
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:198
basic data container for class ByteArray that is implicitly shared over multiple instances of ByteArr...
Definition byteArray.h:79
char * m_pData
Definition byteArray.h:81
int m_ref
Definition byteArray.h:80