request.hpp 1.79 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1
2
3
4
5
6
7
#ifndef __THALLIUM_REQUEST_HPP
#define __THALLIUM_REQUEST_HPP

#include <margo.h>

namespace thallium {

8
class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
9
10
11

class request {

12
	friend class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
13
14
15
16

private:

	hg_handle_t m_handle;
17
    bool        m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
18

19
20
	request(hg_handle_t h, bool disable_resp)
	: m_handle(h), m_disable_response(disable_resp) {}
Matthieu Dorier's avatar
Matthieu Dorier committed
21
22
23
24

public:

	request(const request& other)
25
	: m_handle(other.m_handle), m_disable_response(other.m_disable_response) {
Matthieu Dorier's avatar
Matthieu Dorier committed
26
27
28
29
		margo_ref_incr(m_handle);
	}

	request(request&& other) 
30
	: m_handle(other.m_handle), m_disable_response(other.m_disable_response) {
Matthieu Dorier's avatar
Matthieu Dorier committed
31
32
33
34
35
36
37
		other.m_handle = HG_HANDLE_NULL;
	}

	request& operator=(const request& other) {
		if(m_handle == other.m_handle) return *this;
		margo_destroy(m_handle);
		m_handle = other.m_handle;
38
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
39
40
41
42
43
44
45
46
		margo_ref_incr(m_handle);
		return *this;
	}

	request& operator=(request&& other) {
		if(m_handle == other.m_handle) return *this;
		margo_destroy(m_handle);
		m_handle = other.m_handle;
47
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
48
49
50
51
52
53
54
55
56
		other.m_handle = HG_HANDLE_NULL;
		return *this;
	}

	~request() {
		margo_destroy(m_handle);
	}

	template<typename T>
57
58
	void respond(T&& t) const {
        if(m_disable_response) return; // XXX throwing an exception?
Matthieu Dorier's avatar
Matthieu Dorier committed
59
60
61
62
63
		// TODO serialize
		if(m_handle != HG_HANDLE_NULL) {
			margo_respond(m_handle, nullptr);
		}
	}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

    void respond(const buffer& output) const {
        if(m_disable_response) return; // XXX throwing an exception?
        if(m_handle != HG_HANDLE_NULL) {
            margo_respond(m_handle, const_cast<void*>(static_cast<const void*>(&output)));
        }
    }

    void respond(buffer& output) const {
        respond((const buffer&)output);
    }

    void respond(buffer&& output) const {
        respond((const buffer&)output);
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
79
80
81
82
83
};

}

#endif