argument.hpp 1.09 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
7
#include <functional>

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

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
31
32
    template <class T>
    argument(shape s, T* d) : data([d] { return reinterpret_cast<char*>(d); }), m_shape(s)
    {
    }
Paul's avatar
Paul committed
33

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

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

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

    private:
Paul's avatar
Paul committed
43
    shape m_shape;
Paul's avatar
Paul committed
44
45
};

Paul's avatar
Paul committed
46
} // namespace migraph
Paul's avatar
Paul committed
47
48

#endif