Added an implicit conversion from strings to network_address objects.

This commit is contained in:
Davis King 2012-10-21 15:34:35 -04:00
parent 81c484c831
commit ccc6a8bbfc
3 changed files with 72 additions and 26 deletions

View File

@ -17,6 +17,19 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
network_address::
network_address(
const std::string& full_address
)
{
std::istringstream sin(full_address);
sin >> *this;
if (!sin)
throw invalid_network_address(full_address);
}
// ----------------------------------------------------------------------------------------
void serialize(
@ -242,16 +255,6 @@ namespace dlib
return data->con;
}
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr,
unsigned long timeout
)
{
return connect(addr.host_address, addr.port, timeout);
}
// ----------------------------------------------------------------------------------------
bool is_ip_address (

View File

@ -12,12 +12,31 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
class invalid_network_address : public dlib::error
{
public:
invalid_network_address(const std::string& msg) : dlib::error(msg) {};
};
// ----------------------------------------------------------------------------------------
struct network_address
{
network_address() : port(0){}
network_address(
const std::string& full_address
);
network_address (
const char* full_address
)
{
*this = network_address(std::string(full_address));
}
network_address(
const std::string& host_address_,
const unsigned short port_
@ -68,13 +87,6 @@ namespace dlib
unsigned long timeout
);
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr,
unsigned long timeout
);
// ----------------------------------------------------------------------------------------
bool is_ip_address (

View File

@ -11,6 +11,17 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
class invalid_network_address : public dlib::error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception thrown by network_address's constructor if the
input is invalid.
!*/
};
// ----------------------------------------------------------------------------------------
struct network_address
@ -33,6 +44,33 @@ namespace dlib
- #port == 0
!*/
network_address(
const std::string& full_address
);
/*!
ensures
- interprets full_address as a network address of the form:
host_address:port
and assigns each part into #host_address and #port. For example,
network_address("localhost:80") would result in a network_address
object where host_address was "localhost" and port was 80.
throws
- invalid_network_address
This exception is thrown if the full_address string can't be
interpreted as a valid network address.
!*/
network_address (
const char* full_address
);
/*!
requires
- full_address == a valid pointer to a null terminated string
ensures
- Invoking this constructor is equivalent to performing
network_address(std::string(full_address))
!*/
network_address(
const std::string& host_address_,
const unsigned short port_
@ -110,6 +148,8 @@ namespace dlib
- std::bad_alloc
!*/
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr
);
@ -138,15 +178,6 @@ namespace dlib
- std::bad_alloc
!*/
connection* connect (
const network_address& addr,
unsigned long timeout
);
/*!
ensures
- returns connect(addr.host_address, addr_port, timeout);
!*/
// ----------------------------------------------------------------------------------------