endpoint.hpp 2.88 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 10 11
#ifndef __THALLIUM_ENDPOINT_HPP
#define __THALLIUM_ENDPOINT_HPP

#include <cstdint>
#include <string>
#include <margo.h>
12
#include <thallium/margo_exception.hpp>
Matthieu Dorier's avatar
Matthieu Dorier committed
13

14 15 16 17 18 19 20
namespace thallium {
    class endpoint;
}

template<typename S>
S& operator<<(S& s, const thallium::endpoint& e);

Matthieu Dorier's avatar
Matthieu Dorier committed
21 22
namespace thallium {

23
class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
24
class request;
Matthieu Dorier's avatar
Matthieu Dorier committed
25
class remote_bulk;
Matthieu Dorier's avatar
Matthieu Dorier committed
26

Matthieu Dorier's avatar
Matthieu Dorier committed
27 28 29 30
/**
 * @brief endpoint objects represent an address to which
 * RPC can be sent. They are created using engine::lookup().
 */
Matthieu Dorier's avatar
Matthieu Dorier committed
31 32
class endpoint {

33
	friend class engine;
Matthieu Dorier's avatar
Matthieu Dorier committed
34
    friend class request;
Matthieu Dorier's avatar
Matthieu Dorier committed
35
	friend class callable_remote_procedure;
Matthieu Dorier's avatar
Matthieu Dorier committed
36
    friend class remote_bulk;
Matthieu Dorier's avatar
Matthieu Dorier committed
37 38 39

private:

40
	engine*   m_engine;
41
	hg_addr_t m_addr;
Matthieu Dorier's avatar
Matthieu Dorier committed
42

43 44
public:

Matthieu Dorier's avatar
Matthieu Dorier committed
45 46 47 48 49 50 51
    /**
     * @brief Constructor. Made private since endpoint instances
     * can only be created using engine::lookup.
     *
     * @param e Engine that created the endpoint.
     * @param addr Mercury address.
     */
52 53 54 55 56 57 58 59
	endpoint(engine& e, hg_addr_t addr, bool take_ownership=true)
	: m_engine(&e), m_addr(HG_ADDR_NULL) {
        if(take_ownership) {
            m_addr = addr;
        } else {
            margo_addr_dup(m_engine->m_mid, addr, &m_addr);
        }
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
60

Matthieu Dorier's avatar
Matthieu Dorier committed
61 62 63 64
    /**
     * @brief Default constructor defined so that endpoints can
     * be member of other objects and assigned later.
     */
65 66 67
    endpoint()
    : m_engine(nullptr), m_addr(HG_ADDR_NULL) {}

Matthieu Dorier's avatar
Matthieu Dorier committed
68 69 70
    /**
     * @brief Copy constructor.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
71 72
	endpoint(const endpoint& other);

Matthieu Dorier's avatar
Matthieu Dorier committed
73 74 75
    /**
     * @brief Move constructor.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
76
	endpoint(endpoint&& other)
77
	: m_engine(other.m_engine), m_addr(other.m_addr) {
Matthieu Dorier's avatar
Matthieu Dorier committed
78 79 80
		other.m_addr = HG_ADDR_NULL;
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
81 82 83
    /**
     * @brief Copy-assignment operator.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
84 85
	endpoint& operator=(const endpoint& other);

Matthieu Dorier's avatar
Matthieu Dorier committed
86 87 88
    /**
     * @brief Move-assignment operator.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
89 90
	endpoint& operator=(endpoint&& other);
	
Matthieu Dorier's avatar
Matthieu Dorier committed
91 92 93
    /**
     * @brief Destructor.
     */
94
	~endpoint();
Matthieu Dorier's avatar
Matthieu Dorier committed
95

Matthieu Dorier's avatar
Matthieu Dorier committed
96 97 98 99 100
    /**
     * @brief Creates a string representation of the endpoint's address.
     *
     * @return A string representation of the endpoint's address.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
101 102
	operator std::string() const;

Matthieu Dorier's avatar
Matthieu Dorier committed
103 104 105 106 107
    /**
     * @brief Indicates whether the endpoint is null or not.
     *
     * @return true if the endpoint is a null address.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
108 109 110
    bool is_null() const {
        return m_addr == HG_ADDR_NULL;
    }
111 112 113

    template<typename S>
    friend S& ::operator<<(S& s, const endpoint& e);
Matthieu Dorier's avatar
Matthieu Dorier committed
114 115 116 117
};

}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/**
 * @brief Streaming operator for endpoint, converts the endpoint
 * into a string before feeding it to the string operator. This
 * enables, for instance, streaming the endpoint into std::cout
 * for logging without having to explicitely convert it into a
 * string.
 *
 * @tparam S Type of stream.
 * @param s Stream.
 * @param e Endpoint.
 *
 * @return Reference to the provided stream.
 */
template<typename S>
S& operator<<(S& s, const thallium::endpoint& e) {
    s << (std::string)e;
    return s;
}
    
Matthieu Dorier's avatar
Matthieu Dorier committed
137
#endif