posix/interface: move data holding into the container

This commit is contained in:
Danny Robson 2019-02-02 20:39:32 +11:00
parent a64e721cf0
commit 0349d24473

View File

@ -19,13 +19,8 @@ namespace cruft::posix {
interface (void) interface (void)
{ {
struct iterator { struct iterator {
iterator () explicit iterator (ifaddrs *_cursor) noexcept
{ : cursor (_cursor)
error::try_call (getifaddrs, &cursor);
}
iterator (ifaddrs *_cursor):
cursor (_cursor)
{ ; } { ; }
ifaddrs *cursor; ifaddrs *cursor;
@ -44,9 +39,37 @@ namespace cruft::posix {
}; };
struct container { class container {
auto begin (void) { return iterator ( ); } public:
container ()
{
error::try_call (getifaddrs, &m_data);
}
~container ()
{
if (m_data)
freeifaddrs (m_data);
}
container (container &&rhs) noexcept:
m_data (std::exchange (rhs.m_data, nullptr))
{ ; }
container& operator= (container &&rhs) noexcept
{
std::swap (m_data, rhs.m_data);
return *this;
}
container (container const&) = delete;
container& operator= (container const&) = delete;
auto begin (void) { return iterator (m_data); }
auto end (void) { return iterator (nullptr); } auto end (void) { return iterator (nullptr); }
ifaddrs *m_data;
}; };
return container (); return container ();