Skip to content

Commit

Permalink
Curve: default constructor args and pass values by ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jere8184 committed Jan 26, 2025
1 parent 8316062 commit e123e28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
25 changes: 17 additions & 8 deletions libopenage/curve/container/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <algorithm>
#include <array>

#include "curve/container/iterator.h"
Expand Down Expand Up @@ -38,8 +39,8 @@ class Array : event::EventEntity {
* @param notifier Function to call when this curve changes.
* @param default_vals Default values for the array elements.
*/
Array(const std::shared_ptr<event::EventLoop> &loop,
size_t id,
Array(const std::shared_ptr<event::EventLoop> &loop = nullptr,
size_t id = 0,
const std::string &idstr = "",
const EventEntity::single_change_notifier &notifier = nullptr,
const std::array<T, Size> &default_vals = {}) :
Expand Down Expand Up @@ -105,6 +106,14 @@ class Array : event::EventEntity {
*/
std::pair<time::time_t, T> next_frame(const time::time_t &t, const index_t index) const;


void set_insert_range(const time::time_t &t, auto begin_it, auto end_it) {
ENSURE(std::distance(begin_it, end_it) <= Size,
"trying to insert more values than there are postions: max allowed = " << Size);
index_t i = 0;
std::for_each(begin_it, end_it, [&](const T &val) { this->set_insert(t, i++, val); });
}

/**
* Insert a new keyframe value at time t.
*
Expand All @@ -114,7 +123,7 @@ class Array : event::EventEntity {
* @param index Index of the array element.
* @param value Keyframe value.
*/
void set_insert(const time::time_t &t, const index_t index, T value);
void set_insert(const time::time_t &t, const index_t index, const T &value);

/**
* Insert a new keyframe value at time t. Erase all other keyframes with elem->time > t.
Expand All @@ -123,7 +132,7 @@ class Array : event::EventEntity {
* @param index Index of the array element.
* @param value Keyframe value.
*/
void set_last(const time::time_t &t, const index_t index, T value);
void set_last(const time::time_t &t, const index_t index, const T &value);

/**
* Replace all keyframes at elem->time == t with a new keyframe value.
Expand All @@ -132,7 +141,7 @@ class Array : event::EventEntity {
* @param index Index of the array element.
* @param value Keyframe value.
*/
void set_replace(const time::time_t &t, const index_t index, T value);
void set_replace(const time::time_t &t, const index_t index, const T &value);

/**
* Copy keyframes from another container to this container.
Expand Down Expand Up @@ -321,7 +330,7 @@ consteval size_t Array<T, Size>::size() const {
template <typename T, size_t Size>
void Array<T, Size>::set_insert(const time::time_t &t,
const index_t index,
T value) {
const T &value) {
// find elem_ptr in container to get the last keyframe with time <= t
auto hint = this->last_elements[index];
auto e = this->containers.at(index).insert_after(Keyframe{t, value}, hint);
Expand All @@ -335,7 +344,7 @@ void Array<T, Size>::set_insert(const time::time_t &t,
template <typename T, size_t Size>
void Array<T, Size>::set_last(const time::time_t &t,
const index_t index,
T value) {
const T &value) {
// find elem_ptr in container to get the last keyframe with time <= t
auto hint = this->last_elements[index];
auto e = this->containers.at(index).last(t, hint);
Expand All @@ -360,7 +369,7 @@ void Array<T, Size>::set_last(const time::time_t &t,
template <typename T, size_t Size>
void Array<T, Size>::set_replace(const time::time_t &t,
const index_t index,
T value) {
const T &value) {
// find elem_ptr in container to get the last keyframe with time <= t
auto hint = this->last_elements[index];
auto e = this->containers.at(index).insert_overwrite(Keyframe{t, value}, hint);
Expand Down
6 changes: 2 additions & 4 deletions libopenage/curve/tests/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ void test_queue() {
}

void test_array() {
auto loop = std::make_shared<event::EventLoop>();

Array<int, 4> a(loop, 0);
Array<int, 4> a;
a.set_insert(1, 0, 0);
a.set_insert(1, 1, 1);
a.set_insert(1, 2, 2);
Expand All @@ -273,7 +271,7 @@ void test_array() {
TESTEQUALS(res.at(2), 0);
TESTEQUALS(res.at(3), 0);

Array<int, 4> other(loop, 0);
Array<int, 4> other;
other.set_last(0, 0, 999);
other.set_last(0, 1, 999);
other.set_last(0, 2, 999);
Expand Down

0 comments on commit e123e28

Please sign in to comment.