NebulaGraph CPP Client  release-3.8
HostAddr.h
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 #pragma once
7 
8 #include <sstream>
9 
10 #include "common/thrift/ThriftTypes.h"
11 
12 namespace nebula {
13 
14 // Host address type and utility functions
15 struct HostAddr {
16  std::string host;
17  Port port;
18 
19  HostAddr() : host(), port(0) {}
20  /*
21  * some one may ctor HostAddr this way : HostAddr host(0, 0)
22  * C++ will compile this successfully even we don't support an (int, int) ctor
23  * so, add an explicit delete ctor
24  * */
25  HostAddr(int h, int p) = delete;
26  HostAddr(std::string h, Port p) : host(std::move(h)), port(p) {}
27 
28  void clear() {
29  host.clear();
30  port = 0;
31  }
32 
33  void __clear() {
34  clear();
35  }
36 
37  std::string toString() const {
38  std::stringstream os;
39  os << "\"" << host << "\""
40  << ":" << port;
41  return os.str();
42  }
43 
44  bool operator==(const HostAddr& rhs) const;
45 
46  bool operator!=(const HostAddr& rhs) const;
47 
48  bool operator<(const HostAddr& rhs) const;
49 };
50 
51 inline std::ostream& operator<<(std::ostream& os, const HostAddr& addr) {
52  return os << addr.toString();
53 }
54 
55 } // namespace nebula
56 
57 namespace std {
58 
59 // Inject a customized hash function
60 template <>
61 struct hash<nebula::HostAddr> {
62  std::size_t operator()(const nebula::HostAddr& h) const noexcept;
63 };
64 
65 } // namespace std