Skip to content

Commit

Permalink
Fraction: New ctors accept STL container args
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Nov 21, 2020
1 parent 1c8a673 commit 762f0e7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions bindings/python/openshot.i
Expand Up @@ -127,6 +127,8 @@

/* Instantiate the required template specializations */
%template() std::map<std::string, int>;
%template() std::pair<int, int>;
%template() std::vector<int>;

/* Make openshot.Fraction more Pythonic */
%extend openshot::Fraction {
Expand Down
18 changes: 14 additions & 4 deletions src/Fraction.cpp
Expand Up @@ -29,13 +29,23 @@
*/

#include "Fraction.h"
#include <cmath>

using namespace openshot;

// Constructor
Fraction::Fraction() :
num(1), den(1) {
}
// Delegating constructors
Fraction::Fraction() : Fraction::Fraction(1, 1) {};

Fraction::Fraction(std::pair<int, int> pair)
: Fraction::Fraction(pair.first, pair.second) {};

Fraction::Fraction(std::map<std::string, int> mapping)
: Fraction::Fraction(mapping["num"], mapping["den"]) {};

Fraction::Fraction(std::vector<int> vector)
: Fraction::Fraction(vector[0], vector[1]) {};

// Full constructor
Fraction::Fraction(int num, int den) :
num(num), den(den) {
}
Expand Down
15 changes: 14 additions & 1 deletion src/Fraction.h
Expand Up @@ -31,7 +31,10 @@
#ifndef OPENSHOT_FRACTION_H
#define OPENSHOT_FRACTION_H

#include <cmath>
#include <string> // for std::string
#include <utility> // for std::pair
#include <map> // for std::map
#include <vector> // for std::vector

namespace openshot {

Expand All @@ -49,9 +52,19 @@ namespace openshot {

/// Default Constructor
Fraction();

/// Constructor with numerator and denominator
Fraction(int num, int den);

/// Constructor that accepts a (num, den) pair
Fraction(std::pair<int, int> pair);

/// Constructor that takes a vector of length 2 (containing {num, den})
Fraction(std::vector<int> vector);

/// Constructor that takes a key-value mapping (keys: 'num'. 'den')
Fraction(std::map<std::string, int> mapping);

/// Calculate the greatest common denominator
int GreatestCommonDenominator();

Expand Down

0 comments on commit 762f0e7

Please sign in to comment.