Skip to content

Commit 4b51903

Browse files
committed
Curve: default constructor args and pass values by ref
1 parent 9b3a6ac commit 4b51903

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

libopenage/curve/container/array.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#pragma once
44

5+
#include <algorithm>
56
#include <array>
67

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

109+
110+
void set_insert_range(const time::time_t &t, auto begin_it, auto end_it) {
111+
ENSURE(std::distance(begin_it, end_it) <= Size,
112+
"trying to insert more values than there are postions: max allowed = " << Size);
113+
index_t i = 0;
114+
std::for_each(begin_it, end_it, [&](const T &val) { this->set_insert(t, i++, val); });
115+
}
116+
108117
/**
109118
* Insert a new keyframe value at time t.
110119
*
@@ -114,7 +123,7 @@ class Array : event::EventEntity {
114123
* @param index Index of the array element.
115124
* @param value Keyframe value.
116125
*/
117-
void set_insert(const time::time_t &t, const index_t index, T value);
126+
void set_insert(const time::time_t &t, const index_t index, const T &value);
118127

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

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

137146
/**
138147
* Copy keyframes from another container to this container.
@@ -321,7 +330,7 @@ consteval size_t Array<T, Size>::size() const {
321330
template <typename T, size_t Size>
322331
void Array<T, Size>::set_insert(const time::time_t &t,
323332
const index_t index,
324-
T value) {
333+
const T &value) {
325334
// find elem_ptr in container to get the last keyframe with time <= t
326335
auto hint = this->last_elements[index];
327336
auto e = this->containers.at(index).insert_after(Keyframe{t, value}, hint);
@@ -335,7 +344,7 @@ void Array<T, Size>::set_insert(const time::time_t &t,
335344
template <typename T, size_t Size>
336345
void Array<T, Size>::set_last(const time::time_t &t,
337346
const index_t index,
338-
T value) {
347+
const T &value) {
339348
// find elem_ptr in container to get the last keyframe with time <= t
340349
auto hint = this->last_elements[index];
341350
auto e = this->containers.at(index).last(t, hint);
@@ -360,7 +369,7 @@ void Array<T, Size>::set_last(const time::time_t &t,
360369
template <typename T, size_t Size>
361370
void Array<T, Size>::set_replace(const time::time_t &t,
362371
const index_t index,
363-
T value) {
372+
const T &value) {
364373
// find elem_ptr in container to get the last keyframe with time <= t
365374
auto hint = this->last_elements[index];
366375
auto e = this->containers.at(index).insert_overwrite(Keyframe{t, value}, hint);

libopenage/curve/tests/container.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,7 @@ void test_queue() {
244244
}
245245

246246
void test_array() {
247-
auto loop = std::make_shared<event::EventLoop>();
248-
249-
Array<int, 4> a(loop, 0);
247+
Array<int, 4> a;
250248
a.set_insert(1, 0, 0);
251249
a.set_insert(1, 1, 1);
252250
a.set_insert(1, 2, 2);
@@ -273,7 +271,7 @@ void test_array() {
273271
TESTEQUALS(res.at(2), 0);
274272
TESTEQUALS(res.at(3), 0);
275273

276-
Array<int, 4> other(loop, 0);
274+
Array<int, 4> other;
277275
other.set_last(0, 0, 999);
278276
other.set_last(0, 1, 999);
279277
other.set_last(0, 2, 999);

0 commit comments

Comments
 (0)