Skip to content
Jay edited this page Oct 3, 2026 · 2 revisions

TArray<...>

#include <certpp/io/array.hpp> --- certpp

Template: template <typename T>

Represents an array of elements of type T.

Methods

TArray(EArrayType type = EARRAY_NONE)

Constructs an empty array of the specified type.

  • type --- The type of the array.

inline TArray(const SelfType& other)

Copy constructor for the array.

  • other --- The array to copy from.

inline TArray(const TSpan<T>& span)

Constructs an array by copying elements from a span.

  • span --- The span containing elements to copy.

inline TArray(const TReadOnlySpan<T>& span)

Constructs an array by copying elements from a read-only span.

  • span --- The read-only span containing elements to copy.

inline TArray(SelfType&& other) noexcept

Move constructor for the array.

  • other --- The array to move from.

~TArray()

Destructor for the array. Clears the array and trims excess capacity.

inline SelfType& operator=(const SelfType& other)

Copy assignment operator for the array.

  • other --- The array to copy from.

Returns: A reference to the current array.

inline SelfType& operator=(SelfType&& other) noexcept

Move assignment operator for the array.

  • other --- The array to move from.

Returns: A reference to the current array.

inline SelfType& operator=(const TSpan<T>& span)

Assigns elements from a span to the array.

  • span --- The span containing elements to assign.

Returns: A reference to the current array.

static void wrap(SelfType& array, T* p, size_t size, bool fixed = false)

Wraps the array around an existing static buffer.

  • array --- The array to modify.
  • p --- The pointer to the static buffer.
  • size --- The size of the static buffer.
  • fixed --- Whether the wrapped array should also be marked fixed (rejecting reserve()/trimExcess()).

inline void markFixed()

Marks the array as fixed.

inline operator bool() const

Checks if the array contains any elements.

inline bool operator!() const

Checks if the array is empty.

inline bool empty() const

Checks if the array is empty.

inline T& operator[](size_t index)

Provides access to the element at the specified index.

  • index --- The index of the element to access.

Returns: A reference to the element at the specified index.

inline const T& operator[](size_t index) const

Provides access to the element at the specified index (const version).

  • index --- The index of the element to access.

Returns: A const reference to the element at the specified index.

inline T* begin() const

Returns an iterator to the beginning of the array.

inline T* end() const

Returns an iterator to the end of the array.

inline size_t capacity() const

Returns the capacity of the array.

inline EArrayType type() const

Returns the type of the array.

inline size_t size() const

Returns the number of elements in the array.

inline void clear()

Clears the array by destroying all elements.

inline bool reserve(size_t cap)

Reserves memory for at least the specified capacity.

  • cap --- The desired capacity.

Returns: True if the reservation was successful, false otherwise.

inline bool resize(size_t size)

Resizes the array to the specified size.

  • size --- The desired size.

Returns: True if the resize was successful, false otherwise.

inline bool trimExcess()

Trims the excess capacity of the array, reducing it to match the current size.

Returns: True if the trim was successful, false otherwise.

inline bool add(const T& value)

Adds a new element to the end of the array.

  • value --- The value to be added to the array.

Returns: True if the addition was successful, false otherwise.

inline bool add(T&& value)

Adds a new element to the end of the array using move semantics.

  • value --- The value to be added to the array.

Returns: True if the addition was successful, false otherwise.

inline bool remove(size_t index)

Removes the element at the specified index from the array.

  • index --- The index of the element to be removed.

Returns: True if the removal was successful, false otherwise.

inline size_t remove(size_t index, size_t count)

Removes a range of elements from the array starting at the specified index.

  • index --- The starting index of the elements to be removed.
  • count --- The number of elements to be removed.

Returns: The number of elements actually removed.

inline bool insert(size_t index, const T& value)

Inserts a new element at the specified index using copy semantics.

  • index --- The index at which the element should be inserted.
  • value --- The value to be inserted.

Returns: True if the insertion was successful, false otherwise.

inline bool insert(size_t index, T&& value)

Inserts a new element at the specified index using move semantics.

  • index --- The index at which the element should be inserted.
  • value --- The value to be inserted.

Returns: True if the insertion was successful, false otherwise.

inline bool pop(T& value)

Removes the last element from the array and stores it in the provided reference.

  • value --- A reference to store the removed element.

Returns: True if an element was removed, false if the array was empty.

inline bool pop()

Removes the last element from the array without storing it.

Returns: True if an element was removed, false if the array was empty.

Example

Collects bytes into a growable TArray<uint8_t>, the array type the library's own byte-producing calls fill. Every growing call reports failure with a bool rather than throwing, and reserve() up front turns one reallocation per add() into one in total.

// given: SReadOnlyByteSpan source
TArray<uint8_t> bytes;
if (!bytes.reserve(source.size)) {
    return;
}

for (size_t i = 0; i < source.size; ++i) {
    if (!bytes.add(source.data[i])) {
        return;
    }
}

// A leading zero byte is not part of an unsigned magnitude; remove() shifts the tail down
// and reports false only for an out-of-range index.
while (bytes.size() > 1 && bytes[0] == 0x00) {
    if (!bytes.remove(0)) {
        return;
    }
}

Reference generated from certpp/io/array.hpp at commit 0834b29 (v1.0.1). The header is authoritative --- if this page disagrees with it, the header is right and this page needs regenerating.

Clone this wiki locally