| 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 XSARRAY_H |
| 34 |
#define XSARRAY_H |
| 35 |
|
| 36 |
#include "xstypesconfig.h" |
| 37 |
|
| 38 |
#define XSARRAY_DECL(T)\ |
| 39 |
T* const m_data; /*!< Pointer to contained data buffer */\ |
| 40 |
const XsSize m_size; /*!< Size of used data buffer in number of items */\ |
| 41 |
const XsSize m_reserved; /*!< Size of allocated data buffer in number of items */\ |
| 42 |
const XsSize m_flags; /*!< Flags for data management */\ |
| 43 |
XsArrayDescriptor const* const m_descriptor; /*!< Describes how to handle the items in the array */ |
| 44 |
|
| 45 |
#define XSARRAY_STRUCT(S,T) struct S { XSARRAY_DECL(T) } |
| 46 |
#define XSARRAY_INITIALIZER(D) { 0, 0, 0, XSDF_Managed, D } |
| 47 |
|
| 48 |
/*! \cond XS_INTERNAL */ |
| 49 |
typedef void (*XsArrayItemSwapFunc)(void*, void*); |
| 50 |
typedef void (*XsArrayItemStructFunc)(void*); |
| 51 |
typedef void (*XsArrayItemCopyFunc)(void*, void const*); |
| 52 |
typedef int (*XsArrayItemCompareFunc)(void const*, void const*); //!< \brief Custom item compare function \return 0 means the items are equal, negative and positive values may be returned for sorting |
| 53 |
typedef void (*XsArrayRawCopy)(void*, void const*, XsSize, XsSize); |
| 54 |
|
| 55 |
#define XSEXPCASTITEMSWAP (XsArrayItemSwapFunc) |
| 56 |
#define XSEXPCASTITEMMAKE (XsArrayItemStructFunc) |
| 57 |
#define XSEXPCASTITEMCOPY (XsArrayItemCopyFunc) |
| 58 |
#define XSEXPCASTITEMCOMP (XsArrayItemCompareFunc) |
| 59 |
#define XSEXPCASTRAWCOPY (XsArrayRawCopy) |
| 60 |
/*! \endcond */ |
| 61 |
|
| 62 |
/*! \brief This object describes how to treat the data in an array. |
| 63 |
\details Ususally there is one static instance per type of array that will be used by all |
| 64 |
XsArrays of that type. |
| 65 |
*/ |
| 66 |
struct XsArrayDescriptor { |
| 67 |
#ifndef __cplusplus |
| 68 |
const |
| 69 |
#else |
| 70 |
protected: |
| 71 |
template <typename T, XsArrayDescriptor const& D, typename I> friend struct XsArrayImpl; |
| 72 |
#endif |
| 73 |
XsSize itemSize; //!< The size of an array item in bytes |
| 74 |
void (*itemSwap)(void* a, void* b); //!< The function to use for swapping the data of two array items. \param a Pointer to first item to swap. \param b Pointer to second item to swap. |
| 75 |
void (*itemConstruct)(void* e); //!< The function to use for constructing a new array item. May be 0 for simple types. \param e Pointer to item to construct. |
| 76 |
void (*itemCopyConstruct)(void* e, void const* s); //!< The function to use for constructing a new array item with a source initializer. This may not be 0. \param e Pointer to item to construct. \param s Pointer to source item to copy from. |
| 77 |
void (*itemDestruct)(void* e); //!< The function to use for destructing a array item. May be 0 for simple types. \param e Pointer to item to destruct. |
| 78 |
void (*itemCopy)(void* to, void const* from); //!< The function to use for copying the data of \a from to \a to. \param to Pointer to item to copy to. \param from Pointer to item to copy from. |
| 79 |
int (*itemCompare)(void const* a, void const* b); //!< The function to use for comparing two items. \param a Left hand side of comparison. \param b Right hand side of comparison. \returns The function will return 0 when the items are equal. When greater/less comparison is possible, the function should return < 0 if a < b and > 0 if a > b. |
| 80 |
void (*rawCopy)(void* to, void const* from, XsSize count, XsSize iSize); //!< The function to use for copying the data of an array of \a from to \a to. \param to Pointer to array to copy to. \param from Pointer to array to copy from. \param count The number of items to copy. \param iSize The size of an individual item, should match the \a itemSize descriptor field. \note If this function pointer is 0, itemCopy will be used instead. \note XsArray_rawCopy can be used here for all types that can be safely memcpy'd. This typically means all means non-pointer types and structures containing only types matching these criteria. |
| 81 |
}; |
| 82 |
typedef struct XsArrayDescriptor XsArrayDescriptor; |
| 83 |
|
| 84 |
#ifdef __cplusplus |
| 85 |
#include <iterator> |
| 86 |
#include <stdexcept> |
| 87 |
extern "C" { |
| 88 |
#endif |
| 89 |
|
| 90 |
XSTYPES_DLL_API void XsArray_construct(void* thisPtr, XsArrayDescriptor const* const descriptor, XsSize count, void const* src); |
| 91 |
XSTYPES_DLL_API void XsArray_copyConstruct(void* thisPtr, void const* src); |
| 92 |
XSTYPES_DLL_API void XsArray_destruct(void* thisPtr); |
| 93 |
XSTYPES_DLL_API void XsArray_assign(void* thisPtr, XsSize count, void const* src); |
| 94 |
XSTYPES_DLL_API void XsArray_resize(void* thisPtr, XsSize count); |
| 95 |
XSTYPES_DLL_API void XsArray_reserve(void* thisPtr, XsSize count); |
| 96 |
XSTYPES_DLL_API void XsArray_copy(void* thisPtr, void const* src); |
| 97 |
XSTYPES_DLL_API void XsArray_append(void* thisPtr, void const* other); |
| 98 |
XSTYPES_DLL_API void XsArray_insert(void* thisPtr, XsSize index, XsSize count, void const* src); |
| 99 |
XSTYPES_DLL_API void XsArray_erase(void* thisPtr, XsSize index, XsSize count); |
| 100 |
XSTYPES_DLL_API void XsArray_swap(void* a, void* b); |
| 101 |
XSTYPES_DLL_API int XsArray_compare(void const* a, void const* b); |
| 102 |
XSTYPES_DLL_API int XsArray_compareSet(void const* a, void const* b); |
| 103 |
XSTYPES_DLL_API int XsArray_comparePredicate(void const* a, void const* b, XsArrayItemCompareFunc predicate); |
| 104 |
XSTYPES_DLL_API int XsArray_find(void const* thisPtr, void const* needle); |
| 105 |
XSTYPES_DLL_API int XsArray_findPredicate(void const* thisPtr, void const* needle, XsArrayItemCompareFunc predicate); |
| 106 |
XSTYPES_DLL_API int XsArray_empty(void const* thisPtr); |
| 107 |
XSTYPES_DLL_API void const* XsArray_at(void const* thisPtr, XsSize index); |
| 108 |
XSTYPES_DLL_API void* XsArray_atIndex(void* thisPtr, XsSize index); |
| 109 |
XSTYPES_DLL_API void XsArray_removeDuplicates(void* thisPtr); |
| 110 |
XSTYPES_DLL_API void XsArray_removeDuplicatesPredicate(void* thisPtr, XsArrayItemCompareFunc predicate); |
| 111 |
XSTYPES_DLL_API void XsArray_rawCopy(void* to, void const* from, XsSize count, XsSize iSize); |
| 112 |
XSTYPES_DLL_API void XsArray_sort(void* thisPtr); |
| 113 |
XSTYPES_DLL_API void XsArray_reverse(void* thisPtr); |
| 114 |
|
| 115 |
struct XsArray { |
| 116 |
XSARRAY_DECL(void) |
| 117 |
#ifdef __cplusplus |
| 118 |
//! \copydoc XsArray_construct |
| 119 |
inline XsArray(XsArrayDescriptor const* descriptor, XsSize count = 0, void const* src = 0) |
| 120 |
: m_data(0) |
| 121 |
, m_size(0) |
| 122 |
, m_reserved(0) |
| 123 |
, m_flags(0) |
| 124 |
, m_descriptor(0) |
| 125 |
{ |
| 126 |
XsArray_construct(this, descriptor, count, src); |
| 127 |
} |
| 128 |
|
| 129 |
//! \copydoc XsArray_copyConstruct |
| 130 |
inline XsArray(const XsArray& src) |
| 131 |
: m_data(0) |
| 132 |
, m_size(0) |
| 133 |
, m_reserved(0) |
| 134 |
, m_flags(0) |
| 135 |
, m_descriptor(0) |
| 136 |
{ |
| 137 |
XsArray_copyConstruct(this, &src); |
| 138 |
} |
| 139 |
|
| 140 |
//! \brief Creates a array that references the data supplied in \a ref without allocating the data itself |
| 141 |
inline explicit XsArray(XsArrayDescriptor const* descriptor, void* ref, XsSize count, XsDataFlags flags) |
| 142 |
: m_data(ref) |
| 143 |
, m_size(count) |
| 144 |
, m_reserved(count) |
| 145 |
, m_flags((XsSize)flags) |
| 146 |
, m_descriptor(descriptor) |
| 147 |
{ |
| 148 |
} |
| 149 |
|
| 150 |
//! \brief Destructor |
| 151 |
~XsArray() |
| 152 |
{ |
| 153 |
XsArray_destruct(this); |
| 154 |
} |
| 155 |
|
| 156 |
/*! \brief Assignment operator |
| 157 |
\details Copies the values in \a src into \a this |
| 158 |
\param src The array to copy from |
| 159 |
\return A reference to this |
| 160 |
*/ |
| 161 |
inline XsArray const& operator=(const XsArray& src) |
| 162 |
{ |
| 163 |
if (this != &src) |
| 164 |
XsArray_copy(this, &src); |
| 165 |
return *this; |
| 166 |
} |
| 167 |
#endif |
| 168 |
}; |
| 169 |
|
| 170 |
typedef struct XsArray XsArray; |
| 171 |
|
| 172 |
#ifdef __cplusplus |
| 173 |
} // extern "C" |
| 174 |
|
| 175 |
/*! \brief A C++ wrapper for XsArray, this is a template class for the actual specialized classes |
| 176 |
\tparam T The type of the contained values |
| 177 |
\tparam D The descriptor to use for this specific array implementation. This must be statically allocated and its lifetime must encompass the lifetime of objects that use it. |
| 178 |
\tparam I The class that inherits from the XsArrayImpl. Some functions (such as the streaming operator) require the inheriting type to be returned for proper functionality. |
| 179 |
*/ |
| 180 |
template <typename T, XsArrayDescriptor const& D, typename I> |
| 181 |
struct XsArrayImpl : private XsArray { |
| 182 |
//! \brief The contained type |
| 183 |
typedef T value_type; |
| 184 |
|
| 185 |
//! \brief The type of size fields |
| 186 |
typedef XsSize size_type; |
| 187 |
|
| 188 |
//! \brief A shorthand for the type of this specific implementation |
| 189 |
typedef XsArrayImpl<T, D, I> ArrayImpl; |
| 190 |
|
| 191 |
/*! \brief Construct an XsArray |
| 192 |
\param count the number of items in src |
| 193 |
\param src pointer to an array of output configurations |
| 194 |
\sa XsArray_construct |
| 195 |
*/ |
| 196 |
inline explicit XsArrayImpl<T, D, I>(XsSize count = 0, T const* src = 0) |
| 197 |
: XsArray(&D, count, src) |
| 198 |
{ |
| 199 |
} |
| 200 |
|
| 201 |
//! \brief Constructs the XsArray as a copy of \a other |
| 202 |
inline XsArrayImpl<T, D, I>(ArrayImpl const& other) |
| 203 |
: XsArray(other) |
| 204 |
{ |
| 205 |
} |
| 206 |
|
| 207 |
#ifndef XSENS_NOITERATOR |
| 208 |
//! \brief Constructs the XsArray with a copy of the array bound by the supplied iterators \a beginIt and \a endIt |
| 209 |
template <typename Iterator> |
| 210 |
inline explicit XsArrayImpl<T, D, I>(Iterator const& beginIt, Iterator const& endIt) |
| 211 |
: XsArray(&D, 0, 0) |
| 212 |
{ |
| 213 |
ptrdiff_t diff = endIt-beginIt; |
| 214 |
if (diff > 0) |
| 215 |
{ |
| 216 |
reserve((XsSize) diff); |
| 217 |
for (Iterator it = beginIt; it != endIt; ++it) |
| 218 |
push_back(*it); |
| 219 |
} |
| 220 |
} |
| 221 |
#endif |
| 222 |
//! \brief Creates the XsArray as a reference to the data supplied in \a ref |
| 223 |
inline explicit XsArrayImpl<T, D, I>(T* ref, XsSize sz, XsDataFlags flags /* = XSDF_None */) |
| 224 |
: XsArray(&D, ref, sz, flags) |
| 225 |
{ |
| 226 |
} |
| 227 |
|
| 228 |
//! \copydoc XsArray_destruct |
| 229 |
inline ~XsArrayImpl() |
| 230 |
{ |
| 231 |
XsArray_destruct(this); |
| 232 |
} |
| 233 |
|
| 234 |
//! \brief Clears the array \sa XsArray_destruct() |
| 235 |
inline void clear() |
| 236 |
{ |
| 237 |
XsArray_destruct(this); |
| 238 |
} |
| 239 |
|
| 240 |
#ifndef SWIG |
| 241 |
/*! \brief Tests \a other for equality according to \a predicate |
| 242 |
\param other the array to compare against |
| 243 |
\param predicate the custom item comparison predicate to apply |
| 244 |
\returns 0 if the two arrays are considered equal |
| 245 |
\sa XsArray_comparePredicate |
| 246 |
*/ |
| 247 |
inline int comparePredicate(ArrayImpl const& other, XsArrayItemCompareFunc predicate) const |
| 248 |
{ |
| 249 |
return XsArray_comparePredicate(this, &other, predicate); |
| 250 |
} |
| 251 |
|
| 252 |
/*! \brief Tests \a other for equality according to \a predicate |
| 253 |
\param other the array to compare against |
| 254 |
\param predicate the custom item comparison predicate to apply |
| 255 |
\returns true if the two arrays are considered equal |
| 256 |
\sa XsArray_comparePredicate |
| 257 |
*/ |
| 258 |
inline bool isEqualPredicate(ArrayImpl const& other, XsArrayItemCompareFunc predicate) const |
| 259 |
{ |
| 260 |
return !XsArray_comparePredicate(this, &other, predicate); |
| 261 |
} |
| 262 |
#endif |
| 263 |
|
| 264 |
/*! \brief Tests \a other for equality |
| 265 |
\param other the array to compare against |
| 266 |
\returns true if the two arrays are equal |
| 267 |
\sa XsArray_compareArray |
| 268 |
*/ |
| 269 |
inline bool operator == (ArrayImpl const& other) const |
| 270 |
{ |
| 271 |
return !XsArray_compare(this, &other); |
| 272 |
} |
| 273 |
|
| 274 |
/*! \brief Tests \a other for inequality |
| 275 |
\param other the array to compare against |
| 276 |
\returns true if the two arrays are not equal |
| 277 |
\sa XsArray_compareArray |
| 278 |
*/ |
| 279 |
inline bool operator != (ArrayImpl const& other) const |
| 280 |
{ |
| 281 |
return !!XsArray_compare(this, &other); |
| 282 |
} |
| 283 |
|
| 284 |
//! \copydoc XsArray_reserve |
| 285 |
inline void reserve(XsSize count) |
| 286 |
{ |
| 287 |
XsArray_reserve(this, count); |
| 288 |
} |
| 289 |
|
| 290 |
//! \brief Returns the reserved space in number of items |
| 291 |
inline XsSize reserved() const |
| 292 |
{ |
| 293 |
return m_reserved; |
| 294 |
} |
| 295 |
|
| 296 |
//! \brief Returns the XsArrayDescriptor describing the array |
| 297 |
inline XsArrayDescriptor const& descriptor() const |
| 298 |
{ |
| 299 |
return *m_descriptor; |
| 300 |
} |
| 301 |
|
| 302 |
protected: |
| 303 |
#ifndef XSENS_NOITERATOR |
| 304 |
/*! \brief STL-style iterator */ |
| 305 |
template <ptrdiff_t F, typename R, typename Derived> |
| 306 |
struct IteratorImplBase { |
| 307 |
public: |
| 308 |
//! \brief Difference between two items |
| 309 |
typedef ptrdiff_t difference_type; |
| 310 |
//! \brief The contained type |
| 311 |
typedef T value_type; |
| 312 |
//! \brief Type of pointer |
| 313 |
typedef T* pointer; |
| 314 |
//! \brief Type of reference |
| 315 |
typedef T& reference; |
| 316 |
#ifndef XSENS_NO_STL |
| 317 |
//! \brief The category of this type of iterator (random access) |
| 318 |
typedef std::random_access_iterator_tag iterator_category; |
| 319 |
#endif |
| 320 |
//! \brief The type of the inherited class that is actually this class |
| 321 |
typedef Derived this_type; |
| 322 |
//! \brief The direction of the iterator, +1 = forward, -1 = reverse |
| 323 |
static const ptrdiff_t direction = F; |
| 324 |
protected: |
| 325 |
//! \brief Basic constructor |
| 326 |
inline explicit IteratorImplBase(void* p = 0) : m_ptr((T*) p) {} |
| 327 |
//! \brief Basic constructor |
| 328 |
inline explicit IteratorImplBase(T* p) : m_ptr(p) {} |
| 329 |
//! \brief Copy constructor |
| 330 |
inline IteratorImplBase(this_type const& i) : m_ptr(i.m_ptr) {} |
| 331 |
public: |
| 332 |
//! \brief Assignment operator |
| 333 |
inline this_type& operator =(void* p) |
| 334 |
{ |
| 335 |
m_ptr = (T*) p; |
| 336 |
return *(this_type*)this; |
| 337 |
} |
| 338 |
//! \brief Assignment operator |
| 339 |
inline this_type& operator =(T* p) |
| 340 |
{ |
| 341 |
m_ptr = p; |
| 342 |
return *(this_type*)this; |
| 343 |
} |
| 344 |
//! \brief Assignment operator |
| 345 |
inline this_type& operator =(this_type const& i) |
| 346 |
{ |
| 347 |
m_ptr = i.m_ptr; |
| 348 |
return *(this_type*)this; |
| 349 |
} |
| 350 |
//! \brief Prefix increment by one operator |
| 351 |
inline this_type& operator ++() |
| 352 |
{ |
| 353 |
m_ptr = (T*) ptrAt(m_ptr, F); |
| 354 |
return *(this_type*)this; |
| 355 |
} |
| 356 |
//! \brief Postfix increment by one operator |
| 357 |
inline this_type operator ++(int) |
| 358 |
{ |
| 359 |
this_type p(m_ptr); |
| 360 |
m_ptr = (T*) ptrAt(m_ptr, F); |
| 361 |
return p; |
| 362 |
} |
| 363 |
//! \brief Prefix decrement by one operator |
| 364 |
inline this_type& operator --() |
| 365 |
{ |
| 366 |
m_ptr = (T*) ptrAt(m_ptr, -F); |
| 367 |
return *(this_type*)this; |
| 368 |
} |
| 369 |
//! \brief Postfix decrement by one operator |
| 370 |
inline this_type operator --(int) |
| 371 |
{ |
| 372 |
this_type p(m_ptr); |
| 373 |
m_ptr = (T*) ptrAt(m_ptr, -F); |
| 374 |
return p; |
| 375 |
} |
| 376 |
//! \brief Increment by \a count operator |
| 377 |
inline this_type const& operator +=(ptrdiff_t count) |
| 378 |
{ |
| 379 |
m_ptr = ptrAt(m_ptr, F*count); |
| 380 |
return *(this_type*)this; |
| 381 |
} |
| 382 |
//! \brief Decrement by \a count operator |
| 383 |
inline this_type const& operator -=(ptrdiff_t count) |
| 384 |
{ |
| 385 |
m_ptr = ptrAt(m_ptr, -F*count); |
| 386 |
return *(this_type*)this; |
| 387 |
} |
| 388 |
//! \brief Addition by \a count operator |
| 389 |
inline this_type operator +(ptrdiff_t count) const |
| 390 |
{ |
| 391 |
return this_type(ptrAt(m_ptr, F*count)); |
| 392 |
} |
| 393 |
//! \brief Subtraction by \a count operator |
| 394 |
inline this_type operator -(ptrdiff_t count) const |
| 395 |
{ |
| 396 |
return this_type(ptrAt(m_ptr, -F*count)); |
| 397 |
} |
| 398 |
/*! \brief Returns the difference in number of items between the two iterators |
| 399 |
\details The function computes the difference between this iterator and \a other. The |
| 400 |
difference may be negative if \a other is 'ahead' of this. The function takes the direction |
| 401 |
of the iterator into consideration. |
| 402 |
\param other The iterator to subtract from this. |
| 403 |
\returns The difference between the two iterators. |
| 404 |
*/ |
| 405 |
inline difference_type operator - (const this_type& other) const |
| 406 |
{ |
| 407 |
return (F * (reinterpret_cast<char*>(m_ptr) - reinterpret_cast<char*>(other.m_ptr))) / D.itemSize; |
| 408 |
} |
| 409 |
//! \brief Iterator comparison |
| 410 |
inline bool operator == (this_type const& i) const { return m_ptr == i.m_ptr; } |
| 411 |
//! \brief Iterator comparison, taking direction into account |
| 412 |
inline bool operator <= (this_type const& i) const { return (F==1)?(m_ptr <= i.m_ptr):(m_ptr >= i.m_ptr); } |
| 413 |
//! \brief Iterator comparison, taking direction into account |
| 414 |
inline bool operator < (this_type const& i) const { return (F==1)?(m_ptr < i.m_ptr):(m_ptr > i.m_ptr); } |
| 415 |
//! \brief Iterator comparison |
| 416 |
inline bool operator != (this_type const& i) const { return m_ptr != i.m_ptr; } |
| 417 |
//! \brief Iterator comparison, taking direction into account |
| 418 |
inline bool operator >= (this_type const& i) const { return (F==1)?(m_ptr >= i.m_ptr):(m_ptr <= i.m_ptr); } |
| 419 |
//! \brief Iterator comparison, taking direction into account |
| 420 |
inline bool operator > (this_type const& i) const { return (F==1)?(m_ptr > i.m_ptr):(m_ptr < i.m_ptr); } |
| 421 |
//! \brief Dereferencing operator |
| 422 |
inline R& operator *() const { return *(R*) ptr(); } |
| 423 |
//! \brief Pointer operator |
| 424 |
inline R* operator ->() const { return (R*) ptr(); } |
| 425 |
//! \brief Access to internal pointer object, use should be avoided |
| 426 |
inline T* ptr() const { return m_ptr; } |
| 427 |
private: |
| 428 |
//! \brief The internal pointer |
| 429 |
T* m_ptr; |
| 430 |
}; |
| 431 |
#endif |
| 432 |
|
| 433 |
public: |
| 434 |
#ifndef XSENS_NOITERATOR |
| 435 |
/*! \brief A non-const iterator implementation */ |
| 436 |
template <ptrdiff_t F> |
| 437 |
struct IteratorImpl : public IteratorImplBase<F, T, IteratorImpl<F> > |
| 438 |
{ |
| 439 |
private: |
| 440 |
//! \brief A shorthand for the base object |
| 441 |
typedef IteratorImplBase<F, T, IteratorImpl<F> > ParentType; |
| 442 |
public: |
| 443 |
//! \brief Basic constructor |
| 444 |
inline IteratorImpl(void* p = 0) : ParentType(p) {} |
| 445 |
//! \brief Basic constructor |
| 446 |
inline IteratorImpl(T* p) : ParentType(p) {} |
| 447 |
//! \brief Copy constructor |
| 448 |
inline IteratorImpl(const IteratorImpl& i) : ParentType(i) {} |
| 449 |
}; |
| 450 |
|
| 451 |
/*! \brief A const iterator implementation */ |
| 452 |
template <ptrdiff_t F> |
| 453 |
struct IteratorImplConst : public IteratorImplBase<F, T const, IteratorImplConst<F> > |
| 454 |
{ |
| 455 |
private: |
| 456 |
//! \brief A shorthand for the base object |
| 457 |
typedef IteratorImplBase<F, T const, IteratorImplConst<F> > ParentType; |
| 458 |
public: |
| 459 |
//! \brief Basic constructor |
| 460 |
inline IteratorImplConst(void* p = 0) : ParentType(p) {} |
| 461 |
//! \brief Basic constructor |
| 462 |
inline IteratorImplConst(T* p) : ParentType(p) {} |
| 463 |
//! \brief Copy constructor |
| 464 |
inline IteratorImplConst(IteratorImpl<F> const& i) : ParentType(i.ptr()) {} |
| 465 |
//! \brief Copy constructor |
| 466 |
inline IteratorImplConst(IteratorImplConst const& i) : ParentType(i) {} |
| 467 |
}; |
| 468 |
|
| 469 |
//! \brief STL-style mutable forward iterator |
| 470 |
typedef IteratorImpl<1> iterator; |
| 471 |
//! \brief STL-style mutable reverse iterator |
| 472 |
typedef IteratorImpl<-1> reverse_iterator; |
| 473 |
//! \brief STL-style mutable const forward iterator |
| 474 |
typedef IteratorImplConst<1> const_iterator; |
| 475 |
//! \brief STL-style mutable const reverse iterator |
| 476 |
typedef IteratorImplConst<-1> const_reverse_iterator; |
| 477 |
|
| 478 |
/*! \brief STL-style const_iterator to the first data item in the array */ |
| 479 |
inline const_iterator begin() const { return const_iterator(m_data); } |
| 480 |
/*! \brief STL-style const_iterator to the first data item past the end of the array */ |
| 481 |
inline const_iterator end() const { return begin() + (ptrdiff_t) size(); } |
| 482 |
|
| 483 |
/*! \brief STL-style const_reverse_iterator to the first data item in the reversed array */ |
| 484 |
inline const_reverse_iterator rbegin() const { return rend() - (ptrdiff_t) size(); } |
| 485 |
/*! \brief STL-style const_reverse_iterator to the first data item past the end of the reversed array */ |
| 486 |
inline const_reverse_iterator rend() const { return const_reverse_iterator(m_data) + (ptrdiff_t) 1; } |
| 487 |
|
| 488 |
/*! \brief STL-style iterator to the first data item in the array */ |
| 489 |
inline iterator begin() { return iterator(m_data); } |
| 490 |
/*! \brief STL-style iterator to the first data item past the end of the array */ |
| 491 |
inline iterator end() { return begin() + (ptrdiff_t) size(); } |
| 492 |
|
| 493 |
/*! \brief STL-style reverse_iterator to the first data item in the reversed array */ |
| 494 |
inline reverse_iterator rbegin() { return rend() - (ptrdiff_t) size(); } |
| 495 |
/*! \brief STL-style reverse_iterator to the first data item past the end of the reversed array */ |
| 496 |
inline reverse_iterator rend() { return reverse_iterator(m_data) + (ptrdiff_t) 1; } |
| 497 |
#endif |
| 498 |
/*! \brief indexed data access operator */ |
| 499 |
inline T & operator[] (XsSize index) const |
| 500 |
{ |
| 501 |
assert(index < m_size); |
| 502 |
return *ptrAt(m_data, (ptrdiff_t) index); |
| 503 |
} |
| 504 |
/*! \brief indexed data access operator */ |
| 505 |
inline T& operator[] (XsSize index) |
| 506 |
{ |
| 507 |
assert(index < m_size); |
| 508 |
return *ptrAt(m_data, (ptrdiff_t) index); |
| 509 |
} |
| 510 |
/*! \brief indexed data access \sa operator[] \param index Index of item to access. \returns The item at \a index (by value). */ |
| 511 |
inline T value(XsSize index) const |
| 512 |
{ |
| 513 |
#ifdef XSENS_NO_EXCEPTIONS |
| 514 |
assert(index >= m_size); |
| 515 |
#else |
| 516 |
if (index >= m_size) |
| 517 |
throw std::out_of_range("index out of range"); |
| 518 |
#endif |
| 519 |
return *ptrAt(m_data, index); |
| 520 |
} |
| 521 |
/*! \brief Returns the first item in the array (by value). */ |
| 522 |
inline T first() const |
| 523 |
{ |
| 524 |
#ifdef XSENS_NO_EXCEPTIONS |
| 525 |
assert(m_size); |
| 526 |
#else |
| 527 |
if (!m_size) |
| 528 |
throw std::out_of_range("out of range"); |
| 529 |
#endif |
| 530 |
return *ptrAt(m_data, 0); |
| 531 |
} |
| 532 |
/*! \brief Returns the last item in the array (by value). */ |
| 533 |
inline T last() const |
| 534 |
{ |
| 535 |
#ifdef XSENS_NO_EXCEPTIONS |
| 536 |
assert(m_size); |
| 537 |
#else |
| 538 |
if (!m_size) |
| 539 |
throw std::out_of_range("out of range"); |
| 540 |
#endif |
| 541 |
return *ptrAt(m_data, m_size-1); |
| 542 |
} |
| 543 |
/*! \brief indexed data access \sa operator[] \param index Index of item to access. \returns The item at \a index (by reference). */ |
| 544 |
inline T const& at(XsSize index) const |
| 545 |
{ |
| 546 |
#ifdef XSENS_NO_EXCEPTIONS |
| 547 |
assert(index >= m_size); |
| 548 |
#else |
| 549 |
if (index >= m_size) |
| 550 |
throw std::out_of_range("index out of range"); |
| 551 |
#endif |
| 552 |
return *ptrAt(m_data, index); |
| 553 |
} |
| 554 |
/*! \brief indexed data access \sa operator[] \param index Index of item to access. \returns The item at \a index (by reference). */ |
| 555 |
inline T& at(XsSize index) |
| 556 |
{ |
| 557 |
#ifdef XSENS_NO_EXCEPTIONS |
| 558 |
assert(index >= m_size); |
| 559 |
#else |
| 560 |
if (index >= m_size) |
| 561 |
throw std::out_of_range("index out of range"); |
| 562 |
#endif |
| 563 |
return *ptrAt(m_data, index); |
| 564 |
} |
| 565 |
/*! \brief Insert \a item at \a index |
| 566 |
\param item The item to insert |
| 567 |
\param index The index to insert the item. When beyond the end of the array, the item is appended. |
| 568 |
\sa XsArray_insert |
| 569 |
*/ |
| 570 |
inline void insert(T const& item, XsSize index) |
| 571 |
{ |
| 572 |
insert(&item, index, 1); |
| 573 |
} |
| 574 |
/*! \brief Insert \a item at \a index |
| 575 |
\param items The items to insert |
| 576 |
\param index The index to insert the items. When beyond the end of the array, the items are appended. |
| 577 |
\param count The number of items to insert |
| 578 |
\sa XsArray_insert |
| 579 |
*/ |
| 580 |
inline void insert(T const* items, XsSize index, XsSize count) |
| 581 |
{ |
| 582 |
XsArray_insert(this, index, count, items); |
| 583 |
} |
| 584 |
|
| 585 |
#ifndef XSENS_NOITERATOR |
| 586 |
/*! \brief Insert \a item before iterator \a it |
| 587 |
\param item The item to insert |
| 588 |
\param it The iterator before which to insert the item. |
| 589 |
\sa XsArray_insert |
| 590 |
*/ |
| 591 |
inline void insert(T const& item, const_iterator it) |
| 592 |
{ |
| 593 |
insert(&item, indexOf(it), 1); |
| 594 |
} |
| 595 |
/*! \brief Insert \a item before iterator \a it |
| 596 |
\param item The item to insert |
| 597 |
\param it The iterator before which to insert the item. |
| 598 |
\sa XsArray_insert |
| 599 |
*/ |
| 600 |
inline void insert(T const& item, const_reverse_iterator it) |
| 601 |
{ |
| 602 |
insert(&item, indexOf(it), 1); |
| 603 |
} |
| 604 |
|
| 605 |
/*! \brief Insert \a item at \a index |
| 606 |
\param items The items to insert |
| 607 |
\param it The iterator before which to insert the item. |
| 608 |
\param count The number of items to insert |
| 609 |
\sa XsArray_insert |
| 610 |
*/ |
| 611 |
inline void insert(T const* items, const_iterator it, XsSize count) |
| 612 |
{ |
| 613 |
insert(items, indexOf(it), count); |
| 614 |
} |
| 615 |
/*! \brief Insert \a item at \a index |
| 616 |
\param items The items to insert |
| 617 |
\param it The iterator before which to insert the item. |
| 618 |
\param count The number of items to insert |
| 619 |
\sa XsArray_insert |
| 620 |
*/ |
| 621 |
inline void insert(T const* items, const_reverse_iterator it, XsSize count) |
| 622 |
{ |
| 623 |
insert(items, indexOf(it), count); |
| 624 |
} |
| 625 |
#endif |
| 626 |
|
| 627 |
/*! \brief Adds \a item to the end of the array \sa XsArray_insert \param item The item to append to the array. */ |
| 628 |
inline void push_back(T const& item) |
| 629 |
{ |
| 630 |
insert(&item, (XsSize) -1, 1); |
| 631 |
} |
| 632 |
/*! \brief Removes \a count items from the end of the array \sa XsArray_erase \param count The number items to remove */ |
| 633 |
inline void pop_back(XsSize count = 1) |
| 634 |
{ |
| 635 |
if (count >= size()) |
| 636 |
erase(0, (XsSize) -1); |
| 637 |
else |
| 638 |
erase(size()-count, count); |
| 639 |
} |
| 640 |
/*! \brief Adds \a item to the start of the array \sa XsArray_insert \param item The item to insert at the front of the array */ |
| 641 |
inline void push_front(T const& item) |
| 642 |
{ |
| 643 |
insert(&item, 0, 1); |
| 644 |
} |
| 645 |
/*! \brief Removes \a count items from the start of the array \param count The number items to remove \sa XsArray_erase */ |
| 646 |
inline void pop_front(XsSize count = 1) |
| 647 |
{ |
| 648 |
erase(0, count); |
| 649 |
} |
| 650 |
/*! \brief Returns the number of items currently in the array |
| 651 |
\returns The number of items currently in the array |
| 652 |
\sa reserved \sa setSize \sa resize |
| 653 |
*/ |
| 654 |
inline XsSize size() const |
| 655 |
{ |
| 656 |
return m_size; |
| 657 |
} |
| 658 |
/*! \brief Removes \a count items from the array starting at \a index. \param index The index of the first item to remove. \param count The number of items to remove. */ |
| 659 |
inline void erase(XsSize index, XsSize count = 1) |
| 660 |
{ |
| 661 |
XsArray_erase(this, index, count); |
| 662 |
} |
| 663 |
#ifndef XSENS_NOITERATOR |
| 664 |
/*! \brief Removes the item at iterator \a it. \details \param it The item to remove. \returns An iterator pointing to the next item after the erased item. */ |
| 665 |
inline iterator erase(iterator it) |
| 666 |
{ |
| 667 |
XsSize idx = indexOf(it); |
| 668 |
erase(idx, 1); |
| 669 |
return (idx < size()) ? ptrAt(m_data, idx) : end(); |
| 670 |
} |
| 671 |
/*! \brief Removes the item at iterator \a it. \details \param it The item to remove. \returns An iterator pointing to the next item after the erased item. */ |
| 672 |
inline reverse_iterator erase(reverse_iterator it) |
| 673 |
{ |
| 674 |
XsSize idx = indexOf(it); |
| 675 |
erase(idx, 1); |
| 676 |
return (idx < size()) ? ptrAt(m_data, idx) : rend(); |
| 677 |
} |
| 678 |
#endif |
| 679 |
/*! \copydoc XsArray_assign \sa XsArray_assign */ |
| 680 |
inline void assign(XsSize count, T const* src) |
| 681 |
{ |
| 682 |
XsArray_assign(this, count, src); |
| 683 |
} |
| 684 |
/*! \copydoc XsArray_resize \sa XsArray_resize */ |
| 685 |
inline void resize(XsSize count) |
| 686 |
{ |
| 687 |
XsArray_resize(this, count); |
| 688 |
} |
| 689 |
/*! \brief Set the size of the array to \a count. \details The contents of the array after this operation are undefined. \param count The desired new size fo the array. \sa XsArray_assign \sa reserve \sa resize */ |
| 690 |
inline void setSize(XsSize count) |
| 691 |
{ |
| 692 |
if (count != m_size) |
| 693 |
XsArray_assign(this, count, 0); |
| 694 |
} |
| 695 |
/*! \copydoc XsArray_append \sa XsArray_append */ |
| 696 |
inline void append(ArrayImpl const& other) |
| 697 |
{ |
| 698 |
XsArray_append(this, &other); |
| 699 |
} |
| 700 |
/*! \brief Assignment operator, copies \a other into this, overwriting the old contents \param other The array to copy from \returns A reference to this \sa XsArray_copy */ |
| 701 |
inline ArrayImpl& operator=(ArrayImpl const& other) |
| 702 |
{ |
| 703 |
if (this != &other) |
| 704 |
XsArray_copy(this, &other); |
| 705 |
return *this; |
| 706 |
} |
| 707 |
/*! \brief Returns whether the array is empty. \details This differs slightly from a straight check for size() != 0 in that it also works for fixed-size XsArrays. \returns true if the array is empty. */ |
| 708 |
inline bool empty() const |
| 709 |
{ |
| 710 |
return (size() == 0) || (m_data == 0) || (m_flags & XSDF_Empty); |
| 711 |
} |
| 712 |
|
| 713 |
#ifndef XSENS_NOITERATOR |
| 714 |
/*! \brief Return the inheriting object */ |
| 715 |
inline I const& inherited() const { return *static_cast<I const*>(this); } |
| 716 |
|
| 717 |
/*! \brief Return the inheriting object */ |
| 718 |
inline I& inherited() { return *static_cast<I*>(this); } |
| 719 |
|
| 720 |
#endif |
| 721 |
/*! \brief Swap the contents of the array with those of \a other. \param other The array to swap contents with. \sa XsArray_swap*/ |
| 722 |
inline void swap(ArrayImpl& other) |
| 723 |
{ |
| 724 |
XsArray_swap(this, &other); |
| 725 |
} |
| 726 |
|
| 727 |
/*! \brief Append \a item in a stream-like manner. |
| 728 |
\details This allows for constructing XsArrays easily like this: |
| 729 |
XsInt64Array array = XsInt64Array() << 1 << 2 << 3; |
| 730 |
\param item The item to append to the array. |
| 731 |
\returns A reference to the modified array |
| 732 |
\sa push_back |
| 733 |
*/ |
| 734 |
#ifndef XSENS_NOITERATOR |
| 735 |
inline I& operator <<(T const& item) |
| 736 |
{ |
| 737 |
push_back(item); |
| 738 |
return inherited(); |
| 739 |
} |
| 740 |
#endif |
| 741 |
|
| 742 |
/*! \copydoc XsArray_find */ |
| 743 |
inline int find(T const& needle) const |
| 744 |
{ |
| 745 |
return XsArray_find(this, &needle); |
| 746 |
} |
| 747 |
#ifndef SWIG |
| 748 |
/*! \copydoc XsArray_findPredicate */ |
| 749 |
inline int findPredicate(T const& needle, XsArrayItemCompareFunc predicate) const |
| 750 |
{ |
| 751 |
return XsArray_findPredicate(this, &needle, predicate); |
| 752 |
} |
| 753 |
#endif |
| 754 |
#ifndef XSENS_NOITERATOR |
| 755 |
/*! \brief Returns the linear index of \a it in the array |
| 756 |
\param it The iterator to analyze |
| 757 |
\returns The linear forward index of the item pointed to by \a it. If \a it points to before the |
| 758 |
beginning of the array it returns 0. If it points beyond the end, it returns the current size() |
| 759 |
*/ |
| 760 |
template <ptrdiff_t F, typename R, typename Derived> |
| 761 |
XsSize indexOf(IteratorImplBase<F,R,Derived> const& it) const |
| 762 |
{ |
| 763 |
ptrdiff_t d = ((char const*) it.ptr() - (char const*) m_data); |
| 764 |
if (d >= 0) |
| 765 |
{ |
| 766 |
XsSize r = d/D.itemSize; |
| 767 |
if (r <= size()) |
| 768 |
return r; |
| 769 |
return size(); |
| 770 |
} |
| 771 |
return 0; |
| 772 |
} |
| 773 |
#endif |
| 774 |
|
| 775 |
/*! \copydoc XsArray_removeDuplicates */ |
| 776 |
inline void removeDuplicates() |
| 777 |
{ |
| 778 |
XsArray_removeDuplicates(this); |
| 779 |
} |
| 780 |
|
| 781 |
/*! \copydoc XsArray_removeDuplicatesPredicate */ |
| 782 |
inline void removeDuplicatesPredicate(XsArrayItemCompareFunc predicate) |
| 783 |
{ |
| 784 |
XsArray_removeDuplicatesPredicate(this, predicate); |
| 785 |
} |
| 786 |
|
| 787 |
/*! \copydoc XsArray_sort */ |
| 788 |
inline void sort() |
| 789 |
{ |
| 790 |
XsArray_sort(this); |
| 791 |
} |
| 792 |
|
| 793 |
/*! \copydoc XsArray_reverse */ |
| 794 |
inline void reverse() |
| 795 |
{ |
| 796 |
XsArray_reverse(this); |
| 797 |
} |
| 798 |
|
| 799 |
private: |
| 800 |
/*! \internal |
| 801 |
\brief Generic pointer movement function |
| 802 |
\details This function adds \a count items to \a ptr based on the size specified in \a descriptor |
| 803 |
\param ptr The pointer to start from |
| 804 |
\param count The number of items to move up or down. count may be negative |
| 805 |
\returns The requested pointer. |
| 806 |
\note The return value loses its constness, take care when using this function directly. In most cases |
| 807 |
it should not be necessary to use this function in user code. |
| 808 |
*/ |
| 809 |
inline static const T* ptrAt(void const* ptr, ptrdiff_t count) |
| 810 |
{ |
| 811 |
return (const T*)(void const*)(((char const*)ptr)+count*(ptrdiff_t)D.itemSize); |
| 812 |
} |
| 813 |
|
| 814 |
/*! \internal |
| 815 |
\brief Generic pointer movement function |
| 816 |
\details This function adds \a count items to \a ptr based on the size specified in \a descriptor |
| 817 |
\param ptr The pointer to start from |
| 818 |
\param count The number of items to move up or down. count may be negative |
| 819 |
\returns The requested pointer. |
| 820 |
\note The return value loses its constness, take care when using this function directly. In most cases |
| 821 |
it should not be necessary to use this function in user code. |
| 822 |
*/ |
| 823 |
inline static T* ptrAt(void* ptr, ptrdiff_t count) |
| 824 |
{ |
| 825 |
return (T*)(void*)(((char*)ptr)+count*(ptrdiff_t)D.itemSize); |
| 826 |
} |
| 827 |
}; |
| 828 |
#endif |
| 829 |
|
| 830 |
#endif |