Permalink
Browse files

Make the non-throwing version of firstAddress() be as lightweight as …

…possible.
  • Loading branch information...
1 parent 6105690 commit 9ab50e526f2d1b2f47e4f2128374b569c8a1163a @pprindeville committed Dec 12, 2012
Showing with 22 additions and 15 deletions.
  1. +22 −15 Net/src/NetworkInterface.cpp
@@ -95,7 +95,7 @@ class NetworkInterfaceImpl: public Poco::RefCountedObject
unsigned index() const;
const std::string& name() const;
const std::string& displayName() const;
- const IPAddress& firstAddress(IPAddress::Family family) const;
+ void firstAddress(IPAddress& address, IPAddress::Family family) const;
void addAddress(const AddressTuple& address);
const IPAddress& address(unsigned index) const;
const NetworkInterface::AddressList& addressList() const;
@@ -240,7 +240,9 @@ NetworkInterfaceImpl::~NetworkInterfaceImpl()
bool NetworkInterfaceImpl::supportsIPv4() const
{
- IPAddress addr = firstAddress(IPAddress::IPv4);
+ IPAddress addr(IPAddress::IPv4);
+
+ firstAddress(addr, IPAddress::IPv4);
return !addr.isWildcard();
}
@@ -249,7 +251,9 @@ bool NetworkInterfaceImpl::supportsIPv4() const
bool NetworkInterfaceImpl::supportsIPv6() const
{
#ifdef POCO_HAVE_IPv6
- IPAddress addr = firstAddress(IPAddress::IPv6);
+ IPAddress addr(IPAddress::IPv6);
+
+ firstAddress(addr, IPAddress::IPv6);
return !addr.isWildcard();
#else
@@ -276,20 +280,25 @@ inline const std::string& NetworkInterfaceImpl::displayName() const
}
-const IPAddress& NetworkInterfaceImpl::firstAddress(IPAddress::Family family) const
+void NetworkInterfaceImpl::firstAddress(IPAddress& address, IPAddress::Family family) const
{
AddressList::const_iterator it = _addressList.begin();
AddressList::const_iterator end = _addressList.end();
for (;it != end; ++it)
{
const IPAddress& addr = it->get<NetworkInterface::IP_ADDRESS>();
- if (addr.family() == family) return addr;
+ if (addr.family() == family)
+ {
+ address = addr;
+ return;
+ }
}
// because testing isWildcard() is a lot faster than catching an
// exception... and having an unconfigured interface is hardly
// an 'exceptional' state.
- return IPAddress(family);
+ address = IPAddress(family);
+ return;
}
@@ -602,20 +611,18 @@ const std::string& NetworkInterface::displayName() const
const IPAddress& NetworkInterface::firstAddress(IPAddress::Family family) const
{
- return _pImpl->firstAddress(family);
+ IPAddress addr(family);
+
+ _pImpl->firstAddress(addr, family);
+ if (addr.isWildcard())
+ throw NotFoundException(format("%s family address not found.", (family == IPAddress::IPv4) ? std::string("IPv4") : std::string("IPv6")));
+ return addr;
}
void NetworkInterface::firstAddress(IPAddress& addr, IPAddress::Family family) const
{
- try
- {
- addr = firstAddress(family);
- }
- catch (NotFoundException&)
- {
- addr = IPAddress(family);
- }
+ _pImpl->firstAddress(addr, family);
}

0 comments on commit 9ab50e5

Please sign in to comment.