argument.hpp 1.15 KB
Newer Older
Paul's avatar
Paul committed
1
#ifndef RTG_GUARD_RTGLIB_ARGUMENT_HPP
Paul's avatar
Paul committed
2
#define RTG_GUARD_RTGLIB_ARGUMENT_HPP
Paul's avatar
Paul committed
3
4

#include <rtg/shape.hpp>
Paul's avatar
Paul committed
5
#include <rtg/raw_data.hpp>
Paul's avatar
Paul committed
6
7
8
9
#include <functional>

namespace rtg {

Paul's avatar
Paul committed
10
11
/**
 * @brief Arguments passed to instructions
Paul's avatar
Paul committed
12
13
14
15
 *
 * An `argument` can represent a raw buffer of data that either be referenced from another element
 * or it can be owned by the argument.
 *
Paul's avatar
Paul committed
16
 */
Paul's avatar
Paul committed
17
struct argument : raw_data<argument>
Paul's avatar
Paul committed
18
{
Paul's avatar
Paul committed
19
    argument() {}
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
    argument(shape s) : m_shape(s)
Paul's avatar
Paul committed
22
23
24
25
26
    {
        std::vector<char> buffer(s.bytes());
        // TODO: Move vector
        data = [=]() mutable { return buffer.data(); };
    }
Paul's avatar
Paul committed
27

Paul's avatar
Paul committed
28
    argument(shape s, std::function<char*()> d) : data(d), m_shape(s) {}
Paul's avatar
Paul committed
29
30
    template<class T>
    argument(shape s, T* d) : data([d] { return reinterpret_cast<char*>(d); }), m_shape(s) {}
Paul's avatar
Paul committed
31

Paul's avatar
Paul committed
32
    /// Provides a raw pointer to the data
Paul's avatar
Paul committed
33
    std::function<char*()> data;
Paul's avatar
Paul committed
34

Paul's avatar
Paul committed
35
    /// Whether data is available
Paul's avatar
Paul committed
36
    bool empty() const { return not data; }
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
    const shape& get_shape() const { return this->m_shape; }
Paul's avatar
Paul committed
39

Paul's avatar
Paul committed
40
    template <class T>
Paul's avatar
Paul committed
41
42
43
44
45
    T* cast() const
    {
        return reinterpret_cast<T*>(this->data());
    }

Paul's avatar
Paul committed
46
    private:
Paul's avatar
Paul committed
47
    shape m_shape;
Paul's avatar
Paul committed
48
49
};

Paul's avatar
Paul committed
50
} // namespace rtg
Paul's avatar
Paul committed
51
52

#endif