Skip to content

Commit 6f71736

Browse files
committed
Keyframe: mark all non-modifying member functions const
Member functions that do not (need to) modify any members or internal state of the Keyframe class should be declared as const.
1 parent edf85dd commit 6f71736

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

include/KeyFrame.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,57 +83,57 @@ namespace openshot {
8383
void AddPoint(double x, double y, InterpolationType interpolate);
8484

8585
/// Does this keyframe contain a specific point
86-
bool Contains(Point p);
86+
bool Contains(Point p) const;
8787

8888
/// Flip all the points in this openshot::Keyframe (useful for reversing an effect or transition, etc...)
8989
void FlipPoints();
9090

9191
/// Get the index of a point by matching a coordinate
92-
int64_t FindIndex(Point p);
92+
int64_t FindIndex(Point p) const;
9393

9494
/// Get the value at a specific index
95-
double GetValue(int64_t index);
95+
double GetValue(int64_t index) const;
9696

9797
/// Get the rounded INT value at a specific index
98-
int GetInt(int64_t index);
98+
int GetInt(int64_t index) const;
9999

100100
/// Get the rounded LONG value at a specific index
101-
int64_t GetLong(int64_t index);
101+
int64_t GetLong(int64_t index) const;
102102

103103
/// Get the fraction that represents how many times this value is repeated in the curve
104-
Fraction GetRepeatFraction(int64_t index);
104+
Fraction GetRepeatFraction(int64_t index) const;
105105

106106
/// Get the change in Y value (from the previous Y value)
107-
double GetDelta(int64_t index);
107+
double GetDelta(int64_t index) const;
108108

109109
/// Get a point at a specific index
110-
Point const & GetPoint(int64_t index);
110+
Point const & GetPoint(int64_t index) const;
111111

112112
/// Get current point (or closest point to the right) from the X coordinate (i.e. the frame number)
113-
Point GetClosestPoint(Point p);
113+
Point GetClosestPoint(Point p) const;
114114

115115
/// Get current point (or closest point) from the X coordinate (i.e. the frame number)
116116
/// Either use the closest left point, or right point
117-
Point GetClosestPoint(Point p, bool useLeft);
117+
Point GetClosestPoint(Point p, bool useLeft) const;
118118

119119
/// Get previous point (
120-
Point GetPreviousPoint(Point p);
120+
Point GetPreviousPoint(Point p) const;
121121

122122
/// Get max point (by Y coordinate)
123-
Point GetMaxPoint();
123+
Point GetMaxPoint() const;
124124

125125
// Get the number of values (i.e. coordinates on the X axis)
126-
int64_t GetLength();
126+
int64_t GetLength() const;
127127

128128
/// Get the number of points (i.e. # of points)
129-
int64_t GetCount();
129+
int64_t GetCount() const;
130130

131131
/// Get the direction of the curve at a specific index (increasing or decreasing)
132-
bool IsIncreasing(int index);
132+
bool IsIncreasing(int index) const;
133133

134134
/// Get and Set JSON methods
135-
std::string Json(); ///< Generate JSON string of this object
136-
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
135+
std::string Json() const; ///< Generate JSON string of this object
136+
Json::Value JsonValue() const; ///< Generate Json::JsonValue for this object
137137
void SetJson(std::string value); ///< Load JSON string into this object
138138
void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
139139

@@ -151,10 +151,10 @@ namespace openshot {
151151
void UpdatePoint(int64_t index, Point p);
152152

153153
/// Print a list of points
154-
void PrintPoints();
154+
void PrintPoints() const;
155155

156156
/// Print just the Y value of the point's primary coordinate
157-
void PrintValues();
157+
void PrintValues() const;
158158

159159
};
160160

src/KeyFrame.cpp

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void Keyframe::AddPoint(double x, double y, InterpolationType interpolate)
9191
}
9292

9393
// Get the index of a point by matching a coordinate
94-
int64_t Keyframe::FindIndex(Point p) {
94+
int64_t Keyframe::FindIndex(Point p) const {
9595
// loop through points, and find a matching coordinate
9696
for (int64_t x = 0; x < Points.size(); x++) {
9797
// Get each point
@@ -109,7 +109,7 @@ int64_t Keyframe::FindIndex(Point p) {
109109
}
110110

111111
// Determine if point already exists
112-
bool Keyframe::Contains(Point p) {
112+
bool Keyframe::Contains(Point p) const {
113113
// loop through points, and find a matching coordinate
114114
for (int64_t x = 0; x < Points.size(); x++) {
115115
// Get each point
@@ -127,7 +127,7 @@ bool Keyframe::Contains(Point p) {
127127
}
128128

129129
// Get current point (or closest point) from the X coordinate (i.e. the frame number)
130-
Point Keyframe::GetClosestPoint(Point p, bool useLeft) {
130+
Point Keyframe::GetClosestPoint(Point p, bool useLeft) const {
131131
Point closest(-1, -1);
132132

133133
// loop through points, and find a matching coordinate
@@ -164,12 +164,12 @@ Point Keyframe::GetClosestPoint(Point p, bool useLeft) {
164164
}
165165

166166
// Get current point (or closest point to the right) from the X coordinate (i.e. the frame number)
167-
Point Keyframe::GetClosestPoint(Point p) {
167+
Point Keyframe::GetClosestPoint(Point p) const {
168168
return GetClosestPoint(p, false);
169169
}
170170

171171
// Get previous point (if any)
172-
Point Keyframe::GetPreviousPoint(Point p) {
172+
Point Keyframe::GetPreviousPoint(Point p) const {
173173

174174
// Lookup the index of this point
175175
try {
@@ -188,7 +188,7 @@ Point Keyframe::GetPreviousPoint(Point p) {
188188
}
189189

190190
// Get max point (by Y coordinate)
191-
Point Keyframe::GetMaxPoint() {
191+
Point Keyframe::GetMaxPoint() const {
192192
Point maxPoint(-1, -1);
193193

194194
// loop through points, and find the largest Y value
@@ -207,12 +207,11 @@ Point Keyframe::GetMaxPoint() {
207207
}
208208

209209
// Get the value at a specific index
210-
double Keyframe::GetValue(int64_t index)
211-
{
210+
double Keyframe::GetValue(int64_t index) const {
212211
if (Points.empty()) {
213212
return 0;
214213
}
215-
std::vector<Point>::iterator candidate =
214+
std::vector<Point>::const_iterator candidate =
216215
std::lower_bound(begin(Points), end(Points), Point(index, -1), [](Point const & l, Point const & r) {
217216
return l.co.X < r.co.X;
218217
});
@@ -229,7 +228,7 @@ double Keyframe::GetValue(int64_t index)
229228
// index is directly on a point
230229
return candidate->co.Y;
231230
}
232-
std::vector<Point>::iterator predecessor = candidate - 1;
231+
std::vector<Point>::const_iterator predecessor = candidate - 1;
233232
assert(predecessor->co.X < index);
234233
assert(index < candidate->co.X);
235234

@@ -284,19 +283,17 @@ double Keyframe::GetValue(int64_t index)
284283
}
285284

286285
// Get the rounded INT value at a specific index
287-
int Keyframe::GetInt(int64_t index)
288-
{
286+
int Keyframe::GetInt(int64_t index) const {
289287
return int(round(GetValue(index)));
290288
}
291289

292290
// Get the rounded INT value at a specific index
293-
int64_t Keyframe::GetLong(int64_t index)
294-
{
291+
int64_t Keyframe::GetLong(int64_t index) const {
295292
return long(round(GetValue(index)));
296293
}
297294

298295
// Get the direction of the curve at a specific index (increasing or decreasing)
299-
bool Keyframe::IsIncreasing(int index)
296+
bool Keyframe::IsIncreasing(int index) const
300297
{
301298
if (index < 1 || (index + 1) >= GetLength()) {
302299
return true;
@@ -312,14 +309,14 @@ bool Keyframe::IsIncreasing(int index)
312309
}
313310

314311
// Generate JSON string of this object
315-
std::string Keyframe::Json() {
312+
std::string Keyframe::Json() const {
316313

317314
// Return formatted string
318315
return JsonValue().toStyledString();
319316
}
320317

321318
// Generate Json::JsonValue for this object
322-
Json::Value Keyframe::JsonValue() {
319+
Json::Value Keyframe::JsonValue() const {
323320

324321
// Create root json object
325322
Json::Value root;
@@ -389,8 +386,7 @@ void Keyframe::SetJsonValue(Json::Value root) {
389386

390387
// Get the fraction that represents how many times this value is repeated in the curve
391388
// This is depreciated and will be removed soon.
392-
Fraction Keyframe::GetRepeatFraction(int64_t index)
393-
{
389+
Fraction Keyframe::GetRepeatFraction(int64_t index) const {
394390
// Is index a valid point?
395391
if (index >= 1 && (index + 1) < GetLength()) {
396392
int64_t current_value = GetLong(index);
@@ -428,16 +424,15 @@ Fraction Keyframe::GetRepeatFraction(int64_t index)
428424
}
429425

430426
// Get the change in Y value (from the previous Y value)
431-
double Keyframe::GetDelta(int64_t index)
432-
{
427+
double Keyframe::GetDelta(int64_t index) const {
433428
if (index < 1) return 0;
434429
if (index == 1 && ! Points.empty()) return Points[0].co.Y;
435430
if (index >= GetLength()) return 0;
436431
return GetLong(index) - GetLong(index - 1);
437432
}
438433

439434
// Get a point at a specific index
440-
Point const & Keyframe::GetPoint(int64_t index) {
435+
Point const & Keyframe::GetPoint(int64_t index) const {
441436
// Is index a valid point?
442437
if (index >= 0 && index < Points.size())
443438
return Points[index];
@@ -447,14 +442,14 @@ Point const & Keyframe::GetPoint(int64_t index) {
447442
}
448443

449444
// Get the number of values (i.e. coordinates on the X axis)
450-
int64_t Keyframe::GetLength() {
445+
int64_t Keyframe::GetLength() const {
451446
if (Points.empty()) return 0;
452447
if (Points.size() == 1) return 1;
453448
return round(Points.back().co.X) + 1;
454449
}
455450

456451
// Get the number of points (i.e. # of points)
457-
int64_t Keyframe::GetCount() {
452+
int64_t Keyframe::GetCount() const {
458453

459454
return Points.size();
460455
}
@@ -499,15 +494,15 @@ void Keyframe::UpdatePoint(int64_t index, Point p) {
499494
AddPoint(p);
500495
}
501496

502-
void Keyframe::PrintPoints() {
497+
void Keyframe::PrintPoints() const {
503498
cout << fixed << setprecision(4);
504-
for (std::vector<Point>::iterator it = Points.begin(); it != Points.end(); it++) {
499+
for (std::vector<Point>::const_iterator it = Points.begin(); it != Points.end(); it++) {
505500
Point p = *it;
506501
cout << p.co.X << "\t" << p.co.Y << endl;
507502
}
508503
}
509504

510-
void Keyframe::PrintValues() {
505+
void Keyframe::PrintValues() const {
511506
cout << fixed << setprecision(4);
512507
cout << "Frame Number (X)\tValue (Y)\tIs Increasing\tRepeat Numerator\tRepeat Denominator\tDelta (Y Difference)\n";
513508

@@ -534,8 +529,7 @@ void Keyframe::ScalePoints(double scale)
534529
}
535530

536531
// Flip all the points in this openshot::Keyframe (useful for reversing an effect or transition, etc...)
537-
void Keyframe::FlipPoints()
538-
{
532+
void Keyframe::FlipPoints() {
539533
for (int64_t point_index = 0, reverse_index = Points.size() - 1; point_index < reverse_index; point_index++, reverse_index--) {
540534
// Flip the points
541535
using std::swap;

0 commit comments

Comments
 (0)