MongoDB C++ Driver legacy-1.1.2
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 {
25class Status;
26class StringData;
27template <typename T>
28class StatusWith;
29
37struct MONGO_CLIENT_API HostAndPort {
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
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
108private:
109 std::string _host;
110 int _port; // -1 indicates unspecified
111};
112
113MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC
114operator<<(std::ostream& os, const HostAndPort& hp);
115
116} // namespace mongo
117
118MONGO_HASH_NAMESPACE_START
119
120template <>
121struct hash<mongo::HostAndPort> {
122 size_t operator()(const mongo::HostAndPort& host) const;
123};
124
125MONGO_HASH_NAMESPACE_END
Definition status_with.h:43
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:325
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
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:37
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.