endpoint.hpp 3.16 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
	endpoint(engine& e, hg_addr_t addr, bool take_ownership=true);
Matthieu Dorier's avatar
Matthieu Dorier committed
53

Matthieu Dorier's avatar
Matthieu Dorier committed
54 55 56 57
    /**
     * @brief Default constructor defined so that endpoints can
     * be member of other objects and assigned later.
     */
58 59 60
    endpoint()
    : m_engine(nullptr), m_addr(HG_ADDR_NULL) {}

Matthieu Dorier's avatar
Matthieu Dorier committed
61 62 63
    /**
     * @brief Copy constructor.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
64 65
	endpoint(const endpoint& other);

Matthieu Dorier's avatar
Matthieu Dorier committed
66 67 68
    /**
     * @brief Move constructor.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
69
	endpoint(endpoint&& other)
70
	: m_engine(other.m_engine), m_addr(other.m_addr) {
Matthieu Dorier's avatar
Matthieu Dorier committed
71 72 73
		other.m_addr = HG_ADDR_NULL;
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
74 75 76
    /**
     * @brief Copy-assignment operator.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
77 78
	endpoint& operator=(const endpoint& other);

Matthieu Dorier's avatar
Matthieu Dorier committed
79 80 81
    /**
     * @brief Move-assignment operator.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
82 83
	endpoint& operator=(endpoint&& other);
	
Matthieu Dorier's avatar
Matthieu Dorier committed
84 85 86
    /**
     * @brief Destructor.
     */
Rob Latham's avatar
Rob Latham committed
87
	~endpoint();
Matthieu Dorier's avatar
Matthieu Dorier committed
88

Matthieu Dorier's avatar
Matthieu Dorier committed
89 90 91 92 93
    /**
     * @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
94 95
	operator std::string() const;

Matthieu Dorier's avatar
Matthieu Dorier committed
96 97 98 99 100
    /**
     * @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
101 102 103
    bool is_null() const {
        return m_addr == HG_ADDR_NULL;
    }
104

Matthieu Dorier's avatar
Matthieu Dorier committed
105 106 107 108 109 110 111 112 113 114 115 116
    /**
     * @brief Returns the underlying Mercury address.
     *
     * @param copy If set to true, a copy of the address will be made.
     * (the user will be responsible for calling margo_addr_free on this
     * address) Otherwise, the hg_addr_t returned is the one managed by
     * the endpoint instance and will be deleted when this enpoint is destroyed.
     *
     * @return The underlying hg_addr_t.
     */
    hg_addr_t get_addr(bool copy=false) const;

117 118
    template<typename S>
    friend S& ::operator<<(S& s, const endpoint& e);
Matthieu Dorier's avatar
Matthieu Dorier committed
119 120 121 122
};

}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/**
 * @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
142
#endif