Debian Codegraph @

3rdparty/xspublic/xstypes/xsbytearray.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSBYTEARRAY_H
34 #define XSBYTEARRAY_H
35
36 #include "xsarray.h"
37 #include "pstdint.h"
38
39 #ifdef __cplusplus
40 #include "xsstring.h"
41 extern "C" {
42 #endif
43
44 extern XsArrayDescriptor const XSTYPES_DLL_API g_xsByteArrayDescriptor;
45
46 #ifndef __cplusplus
47 #define XSBYTEARRAY_INITIALIZER XSARRAY_INITIALIZER(&g_xsByteArrayDescriptor)
48 XSARRAY_STRUCT(XsByteArray, uint8_t);
49 typedef struct XsByteArray XsByteArray;
50 XSTYPES_DLL_API void XsByteArray_construct(XsByteArray* thisPtr, XsSize count, uint8_t const* src);
51
52 // obsolete:
53 #define XsByteArray_ref(thisPtr, sz, src, flags) XsArray_ref(thisPtr, sz, src, flags)
54 #define XsByteArray_assign(thisPtr, sz, src) XsArray_assign(thisPtr, sz, src)
55 #define XsByteArray_destruct(thisPtr) XsArray_destruct(thisPtr)
56 #define XsByteArray_copy(thisPtr, copy) XsArray_copy(copy, thisPtr)
57 #define XsByteArray_append(thisPtr, other) XsArray_append(thisPtr, other)
58 #define XsByteArray_popFront(thisPtr, count) XsArray_erase(thisPtr, 0, count)
59 #define XsByteArray_popBack(thisPtr, count) XsArray_erase(thisPtr, (XsSize)-1, count)
60 #define XsByteArray_fromString(str, copy) XsArray_assign(copy, str->m_size?str->m_size:1, str->m_size?str->m_data:"\0")
61 #define XsByteArray_swap(a, b) XsArray_swap(a, b)
62 #define XsByteArray_erase(thisPtr, index, count) XsArray_erase(thisPtr, index, count)
63 #endif
64
65 #ifdef __cplusplus
66 } // extern "C"
67
68 struct XsByteArray : public XsArrayImpl<uint8_t, g_xsByteArrayDescriptor, XsByteArray> {
69 //! \brief Constructs an XsByteArray
70 inline explicit XsByteArray(XsSize sz = 0, uint8_t const* src = 0)
71 : ArrayImpl(sz, src)
72 {
73 }
74
75 //! \brief Constructs an XsByteArray as a copy of \a other
76 inline XsByteArray(XsByteArray const& other)
77 : ArrayImpl(other)
78 {
79 }
80
81 //! \brief Constructs an XsByteArray that references the data supplied in \a ref
82 inline explicit XsByteArray(uint8_t* ref, XsSize sz, XsDataFlags flags /* = XSDF_None */)
83 : ArrayImpl(ref, sz, flags)
84 {
85 }
86 #ifndef XSENS_NOITERATOR
87 //! \brief Constructs an XsByteArray with the array bound by the supplied iterators \a beginIt and \a endIt
88 template <typename Iterator>
89 inline XsByteArray(Iterator beginIt, Iterator endIt)
90 : ArrayImpl(beginIt, endIt)
91 {
92 }
93 #endif
94 //! \brief Constructs an XsByteArray as a copy of the supplied XsString, including the terminating 0
95 inline XsByteArray(XsString const& src)
96 : ArrayImpl()
97 {
98 assign(src.size()+1, reinterpret_cast<uint8_t const*>(src.c_str()));
99 }
100
101 //! \brief Return a pointer to the internal data buffer
102 inline uint8_t* data() { return begin().operator ->(); }
103
104 //! \brief Return a pointer to the internal data buffer
105 inline uint8_t const* data() const { return begin().operator ->(); }
106
107 /*! \brief Return the data at position \a offset converted into a T
108 \details This function will translate a part of the contained data into a type T. T needs to be a real
109 POD type or structure with a trivial default constructor, since its contents will be completely
110 overwritten by a memcpy
111 \param offset The offset of the first byte to convert (in byte units, not in T units)
112 \returns The converted value
113 */
114 template <typename T>
115 inline T getValue(XsSize offset) const
116 {
117 assert(offset+sizeof(T) <= size());
118 T tmp;
119 memcpy(&tmp, data()+offset, sizeof(T));
120 return tmp;
121 }
122
123 /*! \brief Append \a value T to the byte array
124 \param value The value to append to the array. The type T needs to be a POD structure or a primitive type
125 since it gets copied into the array using memcpy.
126 */
127 template <typename T>
128 inline void appendValue(T const& value)
129 {
130 XsSize offset = size();
131 resize(offset + sizeof(T));
132 memcpy(data()+offset, &value, sizeof(T));
133 }
134
135 /*! \brief Set \a value T in the byte array at byte position \a offset
136 \param value The value to append to the array. The type T needs to be a POD structure or a primitive type
137 since it gets copied into the array using memcpy.
138 \param offset The position of the first byte to write. The array will be enlarged if necessary
139 */
140 template <typename T>
141 inline void setValue(T const& value, XsSize offset)
142 {
143 if (size() < offset + sizeof(T))
144 resize(offset + sizeof(T));
145 memcpy(data()+offset, &value, sizeof(T));
146 }
147 };
148 #endif
149 #endif