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
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#pragma once
#include "buffer.hpp"
#include <cassert>
namespace VideoStitch {
namespace GPU {
/**
* Base class for SharedPtrs
*/
template <typename T>
class SharedBuffer {
public:
/**
* Creates a NULL pointer.
*/
SharedBuffer()
: buf(new Buffer<T>(), [](Buffer<T>* ptr) {
if (ptr) {
ptr->release();
delete ptr;
}
}) {}
SharedBuffer(const SharedBuffer& other) : buf(other.buf) {}
SharedBuffer(SharedBuffer&& other) : buf(other.buf) {}
/**
* Check whether the buffer is valid
*/
operator bool() const { return (buf.get() && (buf->byteSize() > 0)); }
/**
* Returns the const pointer but keeps ownership.
*/
Buffer<const T> borrow_const() const { return *buf.get(); }
/**
* Returns the pointer but keeps ownership.
*/
Buffer<T> borrow() const { return *buf.get(); }
/**
* Allocates a pointer.
* @param size Size (in elements of type T).
* @param name pool name.
*/
Status alloc(size_t numElements, const char* name) {
assert(!buf.get()->wasAllocated());
// this seems like a dangerous mis-use of the API
// enabling accidental reallocations
// introduce a realloc() or something if this is really needed
PotentialValue<Buffer<T>> potBuf = Buffer<T>::allocate(numElements, name);
if (potBuf.ok()) {
*(buf.get()) = potBuf.value();
}
return potBuf.status();
}
private:
std::shared_ptr<Buffer<T>> buf;
};
} // namespace GPU
} // namespace VideoStitch