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

Paul's avatar
Paul committed
4
5
#include <migraph/shape.hpp>
#include <migraph/raw_data.hpp>
Paul's avatar
Paul committed
6
#include <functional>
Paul's avatar
Paul committed
7
#include <utility>
Paul's avatar
Paul committed
8

Paul's avatar
Paul committed
9
namespace migraph {
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
12
/**
 * @brief Arguments passed to instructions
Paul's avatar
Paul committed
13
14
15
16
 *
 * 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
17
 */
Paul's avatar
Paul committed
18
struct argument : raw_data<argument>
Paul's avatar
Paul committed
19
{
Paul's avatar
Paul committed
20
    argument() {}
Paul's avatar
Paul committed
21

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

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

Paul's avatar
Paul committed
36
    /// Provides a raw pointer to the data
Paul's avatar
Paul committed
37
    std::function<char*()> data;
Paul's avatar
Paul committed
38

Paul's avatar
Paul committed
39
    /// Whether data is available
Paul's avatar
Paul committed
40
    bool empty() const { return not data; }
Paul's avatar
Paul committed
41

Paul's avatar
Paul committed
42
    const shape& get_shape() const { return this->m_shape; }
Paul's avatar
Paul committed
43
44

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

Paul's avatar
Paul committed
48
} // namespace migraph
Paul's avatar
Paul committed
49
50

#endif