-
Notifications
You must be signed in to change notification settings - Fork 0
/
mattress.h
57 lines (47 loc) · 1.34 KB
/
mattress.h
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef RTATAMI_H
#define RTATAMI_H
#include "pybind11/pybind11.h"
#include "tatami/tatami.hpp"
#include <memory>
namespace mattress {
/**
* Type of the matrix value.
*/
typedef double MatrixValue;
/**
* Type of the matrix index.
*/
typedef uint32_t MatrixIndex;
/**
* @brief Pointer to a **tatami** matrix.
*
* The `tatami::Matrix` is allowed to hold views on Python-owned data, to avoid copies when moving from Python to C++.
* However, if garbage collection occurs on the Python-owned data, the use of that data in C++ becomes invalid.
* To avoid this, we hold the original Python objects to protect them from the garbage collector until this object is also destroyed.
*/
struct BoundMatrix {
/**
* Pointer to a `tatami::Matrix`.
*/
std::shared_ptr<tatami::Matrix<MatrixValue, MatrixIndex> > ptr;
/**
* Python object containing the data referenced by `ptr`.
*/
pybind11::object original;
};
/**
* @param ptr A stored pointer.
* @return Pointer to a `BoundMatrix`.
*/
inline BoundMatrix* cast(uintptr_t ptr) {
return static_cast<BoundMatrix*>(reinterpret_cast<void*>(ptr));
}
/**
* @param ptr Pointer to a `BoundMatrix`.
* @return A stored pointer.
*/
inline uintptr_t cast(const BoundMatrix* ptr) {
return reinterpret_cast<uintptr_t>(static_cast<void*>(const_cast<BoundMatrix*>(ptr)));
}
}
#endif