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

#include <margo.h>
5 6
#include <thallium/serialization/serialize.hpp>
#include <thallium/serialization/buffer_output_archive.hpp>
Matthieu Dorier's avatar
Matthieu Dorier committed
7 8 9

namespace thallium {

10
class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
11 12 13

class request {

14
	friend class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
15 16 17 18

private:

	hg_handle_t m_handle;
19
    bool        m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
20

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

public:

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

	request(request&& other) 
32
	: m_handle(other.m_handle), m_disable_response(other.m_disable_response) {
Matthieu Dorier's avatar
Matthieu Dorier committed
33 34 35 36 37 38 39
		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;
40
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
41 42 43 44 45 46 47 48
		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;
49
        m_disable_response = other.m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
50 51 52 53 54 55 56 57
		other.m_handle = HG_HANDLE_NULL;
		return *this;
	}

	~request() {
		margo_destroy(m_handle);
	}

58 59
	template<typename ... T>
	void respond(T&&... t) const {
60
        if(m_disable_response) return; // XXX throwing an exception?
Matthieu Dorier's avatar
Matthieu Dorier committed
61
		if(m_handle != HG_HANDLE_NULL) {
62 63 64 65
            buffer b;
            buffer_output_archive arch(b);
            serialize_many(arch, std::forward<T>(t)...);
			margo_respond(m_handle, &b);
Matthieu Dorier's avatar
Matthieu Dorier committed
66 67
		}
	}
68
/*
69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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);
    }
83
*/
Matthieu Dorier's avatar
Matthieu Dorier committed
84 85 86 87 88
};

}

#endif