File: ipv4_endpoint.hpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (97 lines) | stat: -rw-r--r-- 3,307 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2019 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#pragma once

#include <cstdint>
#include <functional>

#include "caf/detail/comparable.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/meta/type_name.hpp"

namespace caf {

/// An IP endpoint that contains an ::ipv4_address and a port.
class ipv4_endpoint : detail::comparable<ipv4_endpoint> {
public:
  // -- constructors -----------------------------------------------------------

  ipv4_endpoint(ipv4_address address, uint16_t port);

  ipv4_endpoint() = default;

  ipv4_endpoint(const ipv4_endpoint&) = default;

  ipv4_endpoint& operator=(const ipv4_endpoint&) = default;

  // -- properties -------------------------------------------------------------

  /// Returns the IPv4 address.
  ipv4_address address() const noexcept {
    return address_;
  }

  /// Sets the address of this endpoint.
  void address(ipv4_address x) noexcept {
    address_ = x;
  }

  /// Returns the port of this endpoint.
  uint16_t port() const noexcept {
    return port_;
  }

  /// Sets the port of this endpoint.
  void port(uint16_t x) noexcept {
    port_ = x;
  }

  /// Returns a hash for this object
  size_t hash_code() const noexcept;

  /// Compares this endpoint to `x`.
  /// @returns 0 if `*this == x`, a positive value if `*this > x` and a negative
  /// value otherwise.
  long compare(ipv4_endpoint x) const noexcept;

  template <class Inspector>
  friend typename Inspector::result_type inspect(Inspector& f,
                                                 ipv4_endpoint& x) {
    return f(meta::type_name("ipv4_endpoint"), x.address_, x.port_);
  }

private:
  ipv4_address address_; /// The address of this endpoint.
  uint16_t port_;        /// The port of this endpoint.
};

std::string to_string(const ipv4_endpoint& ep);

} // namespace caf

namespace std {

template <>
struct hash<caf::ipv4_endpoint> {
  size_t operator()(const caf::ipv4_endpoint& ep) const noexcept {
    return ep.hash_code();
  }
};

} // namespace std