request.hpp 2.09 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
6 7 8 9
#ifndef __THALLIUM_REQUEST_HPP
#define __THALLIUM_REQUEST_HPP

#include <margo.h>
10 11
#include <thallium/serialization/serialize.hpp>
#include <thallium/serialization/buffer_output_archive.hpp>
Matthieu Dorier's avatar
Matthieu Dorier committed
12 13 14

namespace thallium {

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

class request {

19
	friend class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
20 21 22 23

private:

	hg_handle_t m_handle;
24
    bool        m_disable_response;
Matthieu Dorier's avatar
Matthieu Dorier committed
25

26 27
	request(hg_handle_t h, bool disable_resp)
	: m_handle(h), m_disable_response(disable_resp) {}
Matthieu Dorier's avatar
Matthieu Dorier committed
28 29 30 31

public:

	request(const 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
		margo_ref_incr(m_handle);
	}

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

	~request() {
		margo_destroy(m_handle);
	}

63 64
	template<typename ... T>
	void respond(T&&... t) const {
65
        if(m_disable_response) return; // XXX throwing an exception?
Matthieu Dorier's avatar
Matthieu Dorier committed
66
		if(m_handle != HG_HANDLE_NULL) {
67 68 69 70
            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
71 72
		}
	}
73
/*
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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);
    }
88
*/
Matthieu Dorier's avatar
Matthieu Dorier committed
89 90 91 92 93
};

}

#endif