MongoDB C++ Driver legacy-1.0.0
Loading...
Searching...
No Matches
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
24namespace mongo {
25 class Status;
26 class StringData;
27 template <typename T> class StatusWith;
28
36 struct MONGO_CLIENT_API HostAndPort {
37
42 static StatusWith<HostAndPort> MONGO_CLIENT_FUNC parse(const StringData& text);
43
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 { return !(*this == r); }
73
80 bool isLocalHost() const;
81
85 std::string toString() const;
86
90 void append( StringBuilder& ss ) const;
91
95 bool empty() const;
96
97 const std::string& host() const {
98 return _host;
99 }
100 int port() const;
101
102 bool hasPort() const {
103 return _port >= 0;
104 }
105
106 private:
107 std::string _host;
108 int _port; // -1 indicates unspecified
109 };
110
111 MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC operator<<(std::ostream& os, const HostAndPort& hp);
112
113} // namespace mongo
114
115MONGO_HASH_NAMESPACE_START
116
117template <>
118struct hash<mongo::HostAndPort> {
119 size_t operator()(const mongo::HostAndPort& host) const;
120};
121
122MONGO_HASH_NAMESPACE_END
Definition sasl_client_conversation.h:27
std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8
Definition builder.h:271
the main MongoDB namespace
Definition bulk_operation_builder.h:24
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.
Name of a process on the network.
Definition hostandport.h:36
std::string toString() const
Returns a string representation of "host:port".
static StatusWith< HostAndPort > MONGO_CLIENT_FUNC parse(const StringData &text)
Parses "text" to produce a HostAndPort.
bool isLocalHost() const
Returns true if the hostname looks localhost-y.
HostAndPort(const StringData &text)
Constructs a HostAndPort by parsing "text" of the form hostname[:portnumber] Throws an AssertionExcep...
void append(StringBuilder &ss) const
Like toString(), above, but writes to "ss", instead.
bool empty() const
Returns true if this object represents no valid HostAndPort.
HostAndPort(const std::string &h, int p)
Constructs a HostAndPort with the hostname "h" and port "p".
Status initialize(const StringData &s)
(Re-)initializes this HostAndPort by parsing "s".
HostAndPort()
Construct an empty/invalid HostAndPort.