MongoDB C++ Driver  legacy-1.1.2
hostandport.h
1 /* Copyright 2009 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <iosfwd>
19 #include <string>
20 
21 #include "mongo/bson/util/builder.h"
22 #include "mongo/platform/hash_namespace.h"
23 
24 namespace mongo {
25 class Status;
26 class StringData;
27 template <typename T>
28 class StatusWith;
29 
37 struct MONGO_CLIENT_API HostAndPort {
42  static StatusWith<HostAndPort> MONGO_CLIENT_FUNC parse(const StringData& text);
43 
47  HostAndPort();
48 
53  explicit HostAndPort(const StringData& text);
54 
60  HostAndPort(const std::string& h, int p);
61 
68  Status initialize(const StringData& s);
69 
70  bool operator<(const HostAndPort& r) const;
71  bool operator==(const HostAndPort& r) const;
72  bool operator!=(const HostAndPort& r) const {
73  return !(*this == r);
74  }
75 
82  bool isLocalHost() const;
83 
87  std::string toString() const;
88 
92  void append(StringBuilder& ss) const;
93 
97  bool empty() const;
98 
99  const std::string& host() const {
100  return _host;
101  }
102  int port() const;
103 
104  bool hasPort() const {
105  return _port >= 0;
106  }
107 
108 private:
109  std::string _host;
110  int _port; // -1 indicates unspecified
111 };
112 
113 MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC
114 operator<<(std::ostream& os, const HostAndPort& hp);
115 
116 } // namespace mongo
117 
118 MONGO_HASH_NAMESPACE_START
119 
120 template <>
121 struct hash<mongo::HostAndPort> {
122  size_t operator()(const mongo::HostAndPort& host) const;
123 };
124 
125 MONGO_HASH_NAMESPACE_END
Status represents an error state or the absence thereof.
Definition: status.h:50
std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:53
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Definition: status_with.h:43
Name of a process on the network.
Definition: hostandport.h:37
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.