Bifrost SDK
Bifrost SDK documentation
Array.h
Go to the documentation of this file.
1//-
2// =============================================================================
3// Copyright 2024 Autodesk, Inc. All rights reserved.
4//
5// Use of this software is subject to the terms of the Autodesk license
6// agreement provided at the time of installation or download, or which
7// otherwise accompanies this software in either electronic or hard copy form.
8// =============================================================================
9//+
10
11#ifndef AMINO_ARRAY_H
12#define AMINO_ARRAY_H
13
19
20//==============================================================================
21// EXTERNAL DECLARATIONS
22//==============================================================================
23
24#include "ArrayFwd.h"
25
26#include "internal/ArrayImpl.h"
27#include "internal/ConfigMacros.h"
28
29#include <type_traits>
30
31namespace Amino {
32
33//==============================================================================
34// FORWARD DECLARATIONS
35//==============================================================================
36
37template <typename T>
38class Array;
39
40//==============================================================================
41// ALIAS ArrayD (Array with given dimension)
42//==============================================================================
43
65template <unsigned Dimension, typename T>
66using ArrayD_t = Internal::MultiDimensionalArray_t<Dimension, T>;
67
68//==============================================================================
69// FUNCTION warn_if_unsupported_element
70//==============================================================================
71
75template <typename T>
76inline constexpr void warn_if_unsupported_element();
78
79//==============================================================================
80// CLASS Array
81//==============================================================================
82
104template <typename T>
105class Array : private Internal::ArrayImpl<T> {
106private:
107 static_assert(
108 std::is_same<std::remove_cv_t<T>, T>::value,
109 "Amino::Array must have a non-const, non-volatile value_type");
110
112 using BaseClass = Internal::ArrayImpl<T>;
113
114public:
115 /*----- types -----*/
116
119 using value_type = typename BaseClass::value_type;
120
123 using pointer = typename BaseClass::pointer;
124
127 using const_pointer = typename BaseClass::const_pointer;
128
131 using reference = typename BaseClass::reference;
132
135 using const_reference = typename BaseClass::const_reference;
136
138 using difference_type = typename BaseClass::difference_type;
139
142 using size_type = typename BaseClass::size_type;
143
145 using iterator = typename BaseClass::iterator;
146
149 using const_iterator = typename BaseClass::const_iterator;
150
151private:
153 template <typename InputIterator>
154 using enable_if_input_iterator_t = std::enable_if_t<std::is_convertible<
155 typename std::iterator_traits<InputIterator>::iterator_category,
156 std::input_iterator_tag>::value>;
157
160 template <typename... Args>
161 using enable_if_constructible_t =
162 std::enable_if_t<std::is_constructible<T, Args...>::value>;
163
164public:
165 /*----- constructors and assignment functions -----*/
166
168 explicit Array() : BaseClass() { warn_if_unsupported_element<T>(); }
169
173 explicit Array(size_type n) : BaseClass(n) {
174 warn_if_unsupported_element<T>();
175 }
176
181 Array(size_type n, const value_type& value) : BaseClass(n, value) {
182 warn_if_unsupported_element<T>();
183 }
184
196 template <
197 class InputIterator,
198 typename = enable_if_input_iterator_t<InputIterator>>
199 Array(InputIterator first, InputIterator last) : BaseClass(first, last) {
200 warn_if_unsupported_element<T>();
201 }
202
206 Array(std::initializer_list<value_type> init) : BaseClass(init) {
207 warn_if_unsupported_element<T>();
208 }
209
216 Array(const Array<T>& other) = default;
217
223 Array(Array<T>&& other) noexcept = default;
224
227 ~Array() = default;
228
235 Array<T>& operator=(const Array<T>& other) = default;
236
243 Array<T>& operator=(Array<T>&& other) noexcept = default;
244
250 Array<T>& operator=(std::initializer_list<value_type> init) {
251 BaseClass::assign(init.begin(), init.end());
252 return *this;
253 }
254
266 void assign(size_type count, const value_type& value) {
267 BaseClass::assign(count, value);
268 }
269
283 template <
284 class InputIterator,
285 typename = enable_if_input_iterator_t<InputIterator>>
286 void assign(InputIterator first, InputIterator last) {
287 BaseClass::assign(first, last);
288 }
289
305 void assign(std::initializer_list<T> init) {
306 BaseClass::assign(init.begin(), init.end());
307 }
308
309 /*----- element accessor functions -----*/
310
323 reference at(size_type n) { return BaseClass::at(n); }
324 const_reference at(size_type n) const { return BaseClass::at(n); }
326
335 reference operator[](size_type n) { return BaseClass::operator[](n); }
337 return BaseClass::operator[](n);
338 }
340
350 reference front() { return BaseClass::front(); }
351 const_reference front() const { return BaseClass::front(); }
353
363 reference back() { return BaseClass::back(); }
364 const_reference back() const { return BaseClass::back(); }
366
371 pointer data() noexcept { return BaseClass::data(); }
372 const_pointer data() const noexcept { return BaseClass::data(); }
374
375 /*----- iterators functions -----*/
376
386 iterator begin() noexcept { return BaseClass::begin(); }
387 const_iterator begin() const noexcept { return BaseClass::begin(); }
388 const_iterator cbegin() const noexcept { return BaseClass::cbegin(); }
390
402 iterator end() noexcept { return BaseClass::end(); }
403 const_iterator end() const noexcept { return BaseClass::end(); }
404 const_iterator cend() const noexcept { return BaseClass::cend(); }
406
407 /*----- capacity related functions -----*/
408
412 bool empty() const noexcept { return BaseClass::empty(); }
413
417 size_type size() const noexcept { return BaseClass::size(); }
418
429 size_type max_size() const { return BaseClass::max_size(); }
430
439 void reserve(size_type new_capacity) { BaseClass::reserve(new_capacity); }
440
446 size_type capacity() const noexcept { return BaseClass::capacity(); }
447
449 void shrink_to_fit() noexcept { BaseClass::shrink_to_fit(); }
450
451 /*----- modifier functions -----*/
452
455 void clear() noexcept { BaseClass::clear(); }
456
466 return BaseClass::insert(pos, value);
467 }
469 return BaseClass::insert(pos, std::move(value));
470 }
472
483 const_iterator pos, size_type count, const value_type& value) {
484 return BaseClass::insert(pos, count, value);
485 }
486
503 template <
504 class InputIterator,
505 typename = enable_if_input_iterator_t<InputIterator>>
507 const_iterator pos, InputIterator first, InputIterator last) {
508 return BaseClass::insert(pos, first, last);
509 }
510
521 const_iterator pos, std::initializer_list<value_type> init) {
522 return BaseClass::insert(pos, init.begin(), init.end());
523 }
524
532 iterator erase(const_iterator pos) { return BaseClass::erase(pos); }
533
544 return BaseClass::erase(first, last);
545 }
546
549 void push_back(const value_type& value) { BaseClass::push_back(value); }
550 void push_back(value_type&& value) {
551 BaseClass::push_back(std::move(value));
552 }
554
560 template <typename... Args, typename = enable_if_constructible_t<Args...>>
561 void emplace_back(Args&&... args) {
562 BaseClass::emplace_back(std::forward<Args>(args)...);
563 }
564
570 void pop_back() { BaseClass::pop_back(); }
571
581 void resize(size_type count) { BaseClass::resize(count); }
582
593 void resize(size_type count, const value_type& value) {
594 BaseClass::resize(count, value);
595 }
596
608 void swap(Array& other) noexcept { BaseClass::swap(other); }
609
610private:
611 // WARNING - this class MUST NOT have any data members or virtual functions
612 // (It must be perfectly ABI compatible with the UntypedArray).
613};
614
615//------------------------------------------------------------------------------
616//
618template <Internal::TypeCategory>
619struct MaybeWarn {
620 static inline constexpr void amino_array_of_unsupported_element() {}
621};
622template <>
623struct MaybeWarn<Internal::TypeCategory::eUninstantiable> {
625 "The element type of this array is not supported in Amino.\n "
626 "All types must be default constructible, copy/move constructible,"
627 "copy/move assignable, and destructible.\n "
628 "Class types wrapped in Amino::Ptr must provide a default getter "
629 "(see Amino/Cpp/ClassDeclare.h and Amino/Cpp/ClassDefine.h).\n "
630 "It won't be type-erased, nor support virtualized operations.")
631 static inline constexpr void amino_array_of_unsupported_element() {}
632};
634
635template <typename T>
636inline constexpr void warn_if_unsupported_element() {
637 MaybeWarn<Internal::GetTypeCategory<T>::value>::
638 amino_array_of_unsupported_element();
639}
640
641} // namespace Amino
642
643#endif
Forward declaration of Amino Array.
Definition: HostData.h:33
AMINO_INTERNAL_DEPRECATED("Amino::isJobCancelled is deprecated and now always returns false. " "Use a 'StopToken const& jobport instead. ") inline bool isJobCancelled()
Definition: Cancellation.h:30
constexpr void warn_if_unsupported_element()
Helpers to produce a warning on unsupported element types when constructing Amino::Array of such type...
Definition: Array.h:636
Internal::MultiDimensionalArray_t< Dimension, T > ArrayD_t
Helper alias for multidimensional array types.
Definition: Array.h:66
void swap(Any &lhs, Any &rhs) noexcept
Swap the payloads of two instances of Any.
Definition: Any.h:358
Define a Amino array of elements of type T.
Definition: Array.h:105
iterator insert(const_iterator pos, const value_type &value)
Inserts value before pos.
Definition: Array.h:465
reference operator[](size_type n)
Get a reference to the element at position n in the array.
Definition: Array.h:335
const_iterator begin() const noexcept
Get a iterator pointing to the first element in the array.
Definition: Array.h:387
const_reference operator[](size_type n) const
Get a reference to the element at position n in the array.
Definition: Array.h:336
Array< T > & operator=(const Array< T > &other)=default
Copy assignment operator.
reference back()
Get a reference to the last element in the array.
Definition: Array.h:363
Array()
Construct an empty array.
Definition: Array.h:168
reference front()
Get a reference to the first element in the array.
Definition: Array.h:350
typename BaseClass::reference reference
Type to represent a mutable reference to an array element value. Equivalent to value_type&.
Definition: Array.h:131
void push_back(value_type &&value)
Appends the given element value to the end of the container.
Definition: Array.h:550
Array(size_type n, const value_type &value)
Construct an array with n elements of value value.
Definition: Array.h:181
iterator insert(const_iterator pos, value_type &&value)
Inserts value before pos.
Definition: Array.h:468
void resize(size_type count, const value_type &value)
Resizes the container to contain count elements.
Definition: Array.h:593
size_type size() const noexcept
Get the number of elements in the array.
Definition: Array.h:417
~Array()=default
Destroys the array. This calls the destructor for each element, and releases all the storage allocate...
Array(size_type n)
Construct an array with n default initialized elements.
Definition: Array.h:173
void push_back(const value_type &value)
Appends the given element value to the end of the container.
Definition: Array.h:549
const_reference back() const
Get a reference to the last element in the array.
Definition: Array.h:364
Array(std::initializer_list< value_type > init)
Construct an array with the contents of the initializer_list.
Definition: Array.h:206
const_reference at(size_type n) const
Get a reference to the element at position n in the array.
Definition: Array.h:324
typename BaseClass::difference_type difference_type
A signed integral type. Usually the same as ptrdiff_t.
Definition: Array.h:138
const_iterator end() const noexcept
Get a iterator pointing to the element just past the last element in the array (past-the-end element)...
Definition: Array.h:403
iterator erase(const_iterator first, const_iterator last)
Removes the elements in the range [first,last) from the container.
Definition: Array.h:543
size_type capacity() const noexcept
Get the number of elements that the container has currently allocated space for.
Definition: Array.h:446
void reserve(size_type new_capacity)
Increase the capacity of the array to a value that's greater or equal to new_capacity.
Definition: Array.h:439
typename BaseClass::pointer pointer
Type to represent a mutable pointer to an array element value. Equivalent to value_type*.
Definition: Array.h:123
typename BaseClass::const_pointer const_pointer
Type to represent a constant pointer to an array element value. Equivalent to value_type const*.
Definition: Array.h:127
const_iterator cbegin() const noexcept
Get a iterator pointing to the first element in the array.
Definition: Array.h:388
void emplace_back(Args &&... args)
Appends an element constructed from the given arguments to the end of the container.
Definition: Array.h:561
Array(Array< T > &&other) noexcept=default
Move constructor.
iterator insert(const_iterator pos, InputIterator first, InputIterator last)
Inserts elements from range [first,last) before pos.
Definition: Array.h:506
iterator insert(const_iterator pos, std::initializer_list< value_type > init)
inserts elements from initializer list init before pos.
Definition: Array.h:520
Array(const Array< T > &other)=default
Copy constructor.
void swap(Array &other) noexcept
Exchange the content of the array by the content of other, which is another array object containing e...
Definition: Array.h:608
size_type max_size() const
Return the maximum number of elements the array is able to hold due to system or library implementati...
Definition: Array.h:429
typename BaseClass::size_type size_type
An unsigned integral that can represent any non-negative value of difference_type....
Definition: Array.h:142
typename BaseClass::value_type value_type
The type of the value being stored in the array. Equivalent to T.
Definition: Array.h:119
Array< T > & operator=(Array< T > &&other) noexcept=default
Move assignment operator.
const_pointer data() const noexcept
Get a pointer to the underlying storage of the array.
Definition: Array.h:372
void clear() noexcept
Removes all elements from the container.
Definition: Array.h:455
pointer data() noexcept
Get a pointer to the underlying storage of the array.
Definition: Array.h:371
Array< T > & operator=(std::initializer_list< value_type > init)
Copy all the elements from init into the current array.
Definition: Array.h:250
iterator end() noexcept
Get a iterator pointing to the element just past the last element in the array (past-the-end element)...
Definition: Array.h:402
void shrink_to_fit() noexcept
Requests the removal of unused capacity.
Definition: Array.h:449
Array(InputIterator first, InputIterator last)
Construct an array with the values in the range [first,last), in the same order.
Definition: Array.h:199
bool empty() const noexcept
Check whether the array is empty (size() == 0).
Definition: Array.h:412
void assign(size_type count, const value_type &value)
Replaces the contents of the container with count copies of value value.
Definition: Array.h:266
typename BaseClass::const_iterator const_iterator
Type representing a random-access iterator to a const value_type.
Definition: Array.h:149
void pop_back()
Removes the last element of the container.
Definition: Array.h:570
void assign(std::initializer_list< T > init)
Replaces the contents of the container with the elements from the initializer list init.
Definition: Array.h:305
const_reference front() const
Get a reference to the first element in the array.
Definition: Array.h:351
iterator begin() noexcept
Get a iterator pointing to the first element in the array.
Definition: Array.h:386
void resize(size_type count)
Resizes the container to contain count elements.
Definition: Array.h:581
reference at(size_type n)
Get a reference to the element at position n in the array.
Definition: Array.h:323
typename BaseClass::iterator iterator
Type representing a random-access iterator to a value_type.
Definition: Array.h:145
void assign(InputIterator first, InputIterator last)
Replaces the contents of the container with copies of those in the range [first,last).
Definition: Array.h:286
const_iterator cend() const noexcept
Get a iterator pointing to the element just past the last element in the array (past-the-end element)...
Definition: Array.h:404
iterator erase(const_iterator pos)
Removes the element at pos from the container.
Definition: Array.h:532
iterator insert(const_iterator pos, size_type count, const value_type &value)
Inserts count copies of the value before pos.
Definition: Array.h:482
typename BaseClass::const_reference const_reference
Type to represent a constant reference to an array element value. Equivalent to value_type const&.
Definition: Array.h:135