Skip to content

Commit

Permalink
address new comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jere8184 committed Jan 8, 2025
1 parent e0eafb3 commit f4ead89
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
62 changes: 46 additions & 16 deletions libopenage/curve/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@

#include <array>

#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 <typename T, size_t Size>
class Array : event::EventEntity {
public:
/**
* The underlaying container type.
*/
using container_t = std::array<KeyframeContainer<T>, Size>;

Array(const std::shared_ptr<event::EventLoop> &loop,
size_t id,
const T &default_val = T(),
const std::string &idstr = "",
const EventEntity::single_change_notifier &notifier = 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;
Array &operator=(const Array &) = delete;
Expand Down Expand Up @@ -108,53 +117,74 @@ class Array : event::EventEntity {
}


// get a copy to the KeyframeContainer at index
KeyframeContainer<T> 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<T, Size> *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<T, Size>::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<T, Size> *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<T> operator[](size_t index) const {
return this->container.at(index);
}


std::array<KeyframeContainer<T>, Size> container;

/**
Expand Down
2 changes: 1 addition & 1 deletion libopenage/curve/tests/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ void test_array() {
auto f = std::make_shared<event::EventLoop>();

Array<int, 4> a(f, 0);
const std::array<int, 4> &default_val = std::array<int, 4>();
a.set_insert(1, 0, 0);
a.set_insert(1, 1, 1);
a.set_insert(1, 2, 2);
Expand Down Expand Up @@ -308,7 +309,6 @@ void test_array() {
TESTEQUALS(next_frame.second, 40);
TESTEQUALS(next_frame.first, 5);


// Test begin and end
auto it = a.begin(1);
TESTEQUALS(*it, 4);
Expand Down

0 comments on commit f4ead89

Please sign in to comment.