diff --git a/libopenage/curve/array.h b/libopenage/curve/array.h index 34109365db..fb5be284cd 100644 --- a/libopenage/curve/array.h +++ b/libopenage/curve/array.h @@ -1,25 +1,34 @@ -// Copyright 2024-2024 the openage authors. See copying.md for legal info. +// Copyright 2024-2025 the openage authors. See copying.md for legal info. #pragma once #include +#include "curve/iterator.h" #include "curve/keyframe_container.h" #include "event/evententity.h" -// remember to update docs +// ASDF: remember to update docs namespace openage { namespace curve { template class Array : event::EventEntity { public: + /** + * The underlaying container type. + */ + using container_t = std::array, Size>; + Array(const std::shared_ptr &loop, size_t id, + const T &default_val = T(), const std::string &idstr = "", const EventEntity::single_change_notifier ¬ifier = nullptr) : - EventEntity(loop, notifier), _id{id}, _idstr{idstr}, loop{loop} {} + EventEntity(loop, notifier), + _id{id}, _idstr{idstr}, loop{loop}, container{KeyframeContainer(default_val)} { + } Array(const Array &) = delete; @@ -37,7 +46,9 @@ class Array : event::EventEntity { */ std::array get(const time::time_t &t) const; - // Get the amount of KeyframeContainers in array curve + /** + * Get the amount of KeyframeContainers in array curve + */ consteval size_t size() const; /** @@ -107,59 +118,80 @@ class Array : event::EventEntity { } - // get a copy to the KeyframeContainer at index - KeyframeContainer operator[](size_t index) const { - return this->container.at(index); - } - - // Array::Iterator is used to iterate over KeyframeContainers contained in a curve at a given time. + /** + * Array::Iterator is used to iterate over KeyframeContainers contained in a curve at a given time. + */ class Iterator { public: Iterator(Array *curve, const time::time_t &time = time::TIME_MAX, size_t offset = 0) : curve(curve), time(time), offset(offset) {}; - // returns a copy of the keyframe at the current offset and time + /** + * returns a copy of the keyframe at the current offset and time + */ const T operator*() { return this->curve->frame(this->time, this->offset).second; } - // increments the Iterator to point at the next KeyframeContainer + /** + * increments the Iterator to point at the next KeyframeContainer + */ void operator++() { this->offset++; } - // Compare two Iterators by their offset + /** + * Compare two Iterators by their offset + */ bool operator!=(const Array::Iterator &rhs) const { return this->offset != rhs.offset; } private: - // used to index the Curve::Array pointed to by this iterator + /** + * used to index the Curve::Array pointed to by this iterator + */ size_t offset; - // curve::Array that this iterator is iterating over + /** + * the curve object that this iterator, iterates over + */ Array *curve; - // time at which this iterator is iterating over + /** + * time at which this iterator is iterating over + */ time::time_t time; }; - // iterator pointing to a keyframe of the first KeyframeContainer in the curve at a given time + /** + * iterator pointing to a keyframe of the first KeyframeContainer in the curve at a given time + */ Iterator begin(const time::time_t &time = time::TIME_MIN); - // iterator pointing after the last KeyframeContainer in the curve at a given time + /** + * iterator pointing after the last KeyframeContainer in the curve at a given time + */ Iterator end(const time::time_t &time = time::TIME_MIN); private: + /** + * get a copy to the KeyframeContainer at index + */ + KeyframeContainer operator[](size_t index) const { + return this->container.at(index); + } + + std::array, Size> container; /** - * hints for KeyframeContainer operations, mutable as hints - * are updated by const read functions. - * This is used to speed up the search for next keyframe to be accessed + * hints for KeyframeContainer operations + * hints is used to speed up the search for next keyframe to be accessed + * mutable as hints are updated by const read-only functions. */ mutable std::array last_element = {}; diff --git a/libopenage/curve/tests/container.cpp b/libopenage/curve/tests/container.cpp index 51a5e4bee2..8aed3c8d05 100644 --- a/libopenage/curve/tests/container.cpp +++ b/libopenage/curve/tests/container.cpp @@ -247,6 +247,7 @@ void test_array() { auto f = std::make_shared(); Array a(f, 0); + const std::array &default_val = std::array(); a.set_insert(1, 0, 0); a.set_insert(1, 1, 1); a.set_insert(1, 2, 2); @@ -308,10 +309,6 @@ void test_array() { TESTEQUALS(next_frame.second, 40); TESTEQUALS(next_frame.first, 5); - // Test operator[] - TESTEQUALS(a[0].get(a[0].last(2)).val(), 25); - TESTEQUALS(a[1].get(a[1].last(2)).val(), 5); - // Test begin and end auto it = a.begin(1); TESTEQUALS(*it, 4);