Skip to content

Commit

Permalink
Proof of concept for C wrapper around C++ API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jminor committed Sep 11, 2018
1 parent 894e9b7 commit 52a10b5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cpp_api/opentime.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class RationalTime
value(0),
rate(1)
{}
RationalTime(const RationalTime& in):
value(in.value),
rate(in.rate)
{}

// data
rt_value_t value;
Expand Down
21 changes: 21 additions & 0 deletions cpp_api/test_wrap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include "wrap.h"

int main(int argc, char**argv){
printf("Hello\n");

RationalTime* t1 = OTIO_RationalTime_new(1,24);
RationalTime* t2 = OTIO_RationalTime_new(10,24);
RationalTime* t3 = OTIO_RationalTime_add(t1, t2);
RationalTime* t4 = OTIO_RationalTime_rescaled_to(t3, 30);

printf("t3 = %g @ %g\n", OTIO_RationalTime_get_value(t3), OTIO_RationalTime_get_rate(t3));
printf("t4 = %g @ %g\n", OTIO_RationalTime_get_value(t4), OTIO_RationalTime_get_rate(t4));

OTIO_RationalTime_delete(t1);
OTIO_RationalTime_delete(t2);
OTIO_RationalTime_delete(t3);
OTIO_RationalTime_delete(t4);

return 0;
}
1 change: 1 addition & 0 deletions cpp_api/test_wrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c++ -std=c++11 test_wrap.c wrap.cpp opentime.cpp && ./a.out
33 changes: 33 additions & 0 deletions cpp_api/wrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#include "wrap.h"

#include "opentime.h"

RationalTime* OTIO_RationalTime_new(double value, double rate) {
return (RationalTime*)new opentime::RationalTime(value, rate);
}

void OTIO_RationalTime_delete(RationalTime* t) {
delete (opentime::RationalTime*)t;
}

RationalTime* OTIO_RationalTime_add(RationalTime* a, RationalTime* b) {
opentime::RationalTime* result = new opentime::RationalTime(*(opentime::RationalTime*)a);
*result += *(opentime::RationalTime*)b;
return (RationalTime*)result;
}

RationalTime* OTIO_RationalTime_rescaled_to(RationalTime* t, double rate) {
opentime::RationalTime* result = new opentime::RationalTime(
((opentime::RationalTime*)t)->rescaled_to(rate)
);
return (RationalTime*)result;
}

double OTIO_RationalTime_get_value(RationalTime* t) {
return ((opentime::RationalTime*)t)->value;
}

double OTIO_RationalTime_get_rate(RationalTime* t) {
return ((opentime::RationalTime*)t)->rate;
}
12 changes: 12 additions & 0 deletions cpp_api/wrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

extern "C"
{
typedef void* RationalTime;

RationalTime* OTIO_RationalTime_new(double value, double rate);
void OTIO_RationalTime_delete(RationalTime* t);
RationalTime* OTIO_RationalTime_add(RationalTime* a, RationalTime* b);
RationalTime* OTIO_RationalTime_rescaled_to(RationalTime* t, double rate);
double OTIO_RationalTime_get_value(RationalTime* t);
double OTIO_RationalTime_get_rate(RationalTime* t);
}

0 comments on commit 52a10b5

Please sign in to comment.