-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcube.cpp
41 lines (30 loc) · 984 Bytes
/
cube.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "cube.hpp"
#include "cube.h"
#include <new>
// ----------------------------------------------------------------------------
// This is the C++ part of the library
// ----------------------------------------------------------------------------
Cube::Cube(int side) : m_side(side) {}
int Cube::side() const {
return m_side;
}
int Cube::volume() const {
return m_side * m_side * m_side;
}
// ----------------------------------------------------------------------------
// This is the C interface part of the library
// ----------------------------------------------------------------------------
extern "C" {
cube_t Cube_Create(int side) {
return {new(std::nothrow) Cube(side)};
}
int Cube_Side(const cube_t c) {
return static_cast<const Cube*>(c.obj)->side();
}
int Cube_Volume(const cube_t c) {
return static_cast<const Cube*>(c.obj)->volume();
}
void Cube_Destroy(const cube_t c) {
delete static_cast<const Cube*>(c.obj);
}
} // extern "C"