Spacing and instantiation of templates

This commit is contained in:
Danny Robson 2012-07-30 16:33:12 +10:00
parent 1c251699c3
commit d49dd41c65
2 changed files with 56 additions and 33 deletions

View File

@ -23,6 +23,9 @@
#include "../debug.hpp" #include "../debug.hpp"
#include "../endian.hpp" #include "../endian.hpp"
#include "../types.hpp" #include "../types.hpp"
#include "../raii.hpp"
#include <netdb.h>
#ifdef __WIN32 #ifdef __WIN32
#else #else
@ -50,6 +53,37 @@ const char* inet_ntop(int af, const void* src, char* dst, int size){
#endif #endif
//-----------------------------------------------------------------------------
template <domain D>
typename address<D>::port_type
address<D>::port (void) const
{ return m_port; }
template <domain D>
void
address<D>::set_port (const port_type &_port)
{ m_port = _port; }
template <domain D>
typename address<D>::ip_type
address<D>::resolve (const std::string &str) {
addrinfo hint;
memset (&hint, 0, sizeof (hint));
hint.ai_family = static_cast<int> (D);
addrinfo* resolved;
int err = getaddrinfo (str.c_str (), nullptr, nullptr, &resolved);
if (err)
net::error::throw_code ();
auto deletor = [] (addrinfo *a) { freeaddrinfo (a); };
std::unique_ptr<addrinfo, decltype(deletor)> raii(resolved, deletor);
return ip_type (reinterpret_cast<sockaddr_type*> (resolved->ai_addr)->sin_addr.s_addr);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
namespace net { namespace net {
template <> template <>
@ -63,9 +97,9 @@ namespace net {
template <domain D> template <domain D>
address<D>::address (const std::string &_ip, address<D>::address (const std::string &_addr,
port_type _port): port_type _port):
m_ip (_ip), m_ip (resolve (_addr)),
m_mask ( 0), m_mask ( 0),
m_port (_port) m_port (_port)
{ ; } { ; }
@ -115,25 +149,23 @@ net::operator<< (std::ostream &os, const address<net::domain::INET> &addr) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <> template <> const address<domain::INET>
const address<domain::INET>
address<domain::INET>::LOOPBACK ("127.0.0.1", 0); address<domain::INET>::LOOPBACK ("127.0.0.1", 0);
template <> const address<domain::INET>
template <>
const address<domain::INET>
address<domain::INET>::ANY ("0.0.0.0", 0); address<domain::INET>::ANY ("0.0.0.0", 0);
template typename address<domain::INET>::ip_type
template <> address<domain::INET>::resolve (const std::string &);
const address<domain::INET6>
address<domain::INET6>::LOOPBACK ("::1", 0);
template <>
const address<domain::INET6>
address<domain::INET6>::ANY ("::0", 0);
template class address<domain::INET>; template class address<domain::INET>;
template class address<domain::INET6>;
//-----------------------------------------------------------------------------
//template <> const address<domain::INET6>
//address<domain::INET6>::LOOPBACK ("::1", 0);
//
//template <> const address<domain::INET6>
//address<domain::INET6>::ANY ("::0", 0);
//
//template class address<domain::INET6>;

View File

@ -75,26 +75,17 @@ namespace net {
static const address<D> ANY; static const address<D> ANY;
address (const sockaddr_type &); address (const sockaddr_type &);
address (const std::string&, address (const std::string &address, port_type);
port_type);
port_type port_type port (void) const;
port (void) const void set_port (const port_type &);
{ return m_port; }
void ip_type ip (void) const { return m_ip; }
set_port (const port_type &_port)
{ m_port = _port; }
ip_type sockaddr_type to_sockaddr (void) const;
ip (void) const std::string to_string (void) const;
{ return m_ip; }
sockaddr_type static ip_type resolve (const std::string &);
to_sockaddr (void) const;
std::string
to_string (void) const;
bool operator ==(const address<D> &rhs); bool operator ==(const address<D> &rhs);
}; };