Modeling an IP AddressDynamic IP-address loggingValidate IP address in JavaArray-like container for uints shorter than 8 bits (Rev 1)Identifying originating country of an IP addressCalculate IP AddressRegex to check IP address in JavaPattern for writing a generic string transformation functionRandom IP Address GeneratorModeling a parking lotValidate IP4 address

Does casting Light, or a similar spell, have any effect when the caster is swallowed by a monster?

Can I use a neutral wire from another outlet to repair a broken neutral?

A reference to a well-known characterization of scattered compact spaces

How do I write bicross product symbols in latex?

Etiquette around loan refinance - decision is going to cost first broker a lot of money

Why is the 'in' operator throwing an error with a string literal instead of logging false?

Doing something right before you need it - expression for this?

Arrow those variables!

Is it unprofessional to ask if a job posting on GlassDoor is real?

Infinite Abelian subgroup of infinite non Abelian group example

Alternative to sending password over mail?

Where does SFDX store details about scratch orgs?

Anagram holiday

Today is the Center

What's the point of deactivating Num Lock on login screens?

What exploit are these user agents trying to use?

What does it mean to describe someone as a butt steak?

Can a virus destroy the BIOS of a modern computer?

Why is Collection not simply treated as Collection<?>

prove that the matrix A is diagonalizable

How do conventional missiles fly?

Reserved de-dupe rules

Why are electrically insulating heatsinks so rare? Is it just cost?

Blender 2.8 I can't see vertices, edges or faces in edit mode



Modeling an IP Address


Dynamic IP-address loggingValidate IP address in JavaArray-like container for uints shorter than 8 bits (Rev 1)Identifying originating country of an IP addressCalculate IP AddressRegex to check IP address in JavaPattern for writing a generic string transformation functionRandom IP Address GeneratorModeling a parking lotValidate IP4 address






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1












$begingroup$


I've recently picked up a book that gives various "modern" C++ challenges/solutions. One of the first ones I did was on modeling an IPv4 address in C++. Below is the full implementation; it's also on Github. Any suggestions? My goal is to make this as modern as possible and I'm not sure if there are some C++17 features I'm not taking advantage of.



ip_address.h



#pragma once

#include <array>
#include <string>
#include <string_view>
#include <stdint.h>

namespace ip

/**
* @brief Thrown when there is an invalid ip address passed via
* string.
*/
class invalid_format_exception : public std::exception

std::string invalid_format_;
public:
invalid_format_exception(const std::string &invalid_format);
char const* what() const override;
;

/**
* Class that models a IPv4 address.
*/
class address

public:

#pragma region Type definitions
using value_type = uint8_t;
using reference = value_type & ;
using pointer = value_type * ;
using iterator = std::array<value_type, 4>::iterator;
using const_iterator = std::array<value_type, 4>::const_iterator;
using reverse_iterator = std::array<value_type, 4>::reverse_iterator;
using const_reverse_iterator = std::array<value_type, 4>::const_reverse_iterator;
using size_type = std::array<value_type, 4>::size_type;
#pragma endregion

/**
* @brief Create an IP address representation from the
* four parts of the address definition.
* @param first the first part of the address
* @param second the second part of the address
* @param third the third part of the address.
* @param fourth the fourth part of the address.
* @details Example:
* @code
* ip::address addr(127, 0, 0, 1);
* @endcode
*/
address(const value_type& first, const value_type &second,
const value_type &third, const value_type& fourth);

/**
* @brief Create an IP address representaiton from an
* array.
* @param data the data array.
* @details Example:
* @code
* ip::address addr = 127, 0, 0, 1;
* @endcode
*/
address(const std::array<unsigned char, 4> &data);

/**
* @brief Create an IP adderss representation from a
* unsigned 32 bit integer.
* @param value the integer representation of an IP address.
*/
explicit address(const uint32_t &value);

/**
* @brief Implicit conversion to an unsigned 32 bit integer.
*/
uint32_t operator()() const;

/**
* @brief Access operator.
* @param index the index to access.
*/
reference operator[](const int &index) noexcept(false);

/**
* @brief Const version of the access operator.
*/
value_type operator[](const int &index) const noexcept(false);

/**
* @brief Prefix increment operator.
*/
void operator++();

/**
* @brief Postfix increment operator.
*/
::ip::address& operator++(int);

/**
* @brief Prefix decrement operator.
*/
void operator--();

/**
* @brief Prefix decrement operator.
*/
::ip::address& operator--(int);

iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
private:
std::array<value_type, 4> data_;
;

bool operator<(const ip::address &first, const ip::address &second);
bool operator==(const ip::address &first, const ip::address &second);
std::ostream& operator<<(std::ostream& output, const ip::address &address);
address from_string(const std::string &view);
std::string to_string(const address& address);



ip_address.cpp



#include <ip_address.h>

#include <iterator>
#include <iostream>
#include <sstream>
#include <regex>
#include <vector>
#include <string>

#pragma region Utilities
template<typename Output>
void split(const std::string &s, char delim, Output result)
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
*(result++) = item;



std::vector<std::string> split(const std::string &s, char delim)
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;


#pragma endregion

ip::invalid_format_exception::invalid_format_exception(const std::string& invalid_format)
: invalid_format_(invalid_format)



char const* ip::invalid_format_exception::what() const

std::ostringstream oss;
oss << "Invalid IP address format: " << invalid_format_;
return oss.str().c_str();


ip::address::address(const value_type & first, const value_type & second, const value_type & third, const value_type & fourth)

data_[0] = first;
data_[1] = second;
data_[2] = third;
data_[3] = fourth;


ip::address::address(const std::array<unsigned char, 4>& data)

data_ = data;


ip::address::address(const uint32_t& value)

data_[0] = value >> 24 & 0xFF;
data_[1] = value >> 16 & 0xFF;
data_[2] = value >> 8 & 0xFF;
data_[3] = value & 0xFF;


uint32_t ip::address::operator()() const
data_[2] << 8

ip::address::reference ip::address::operator[](const int& index)

return data_.at(index);


ip::address::value_type ip::address::operator[](const int& index) const

return data_.at(index);


void ip::address::operator++()

auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

return data < 255;
);

if(location != std::rend(data_))

const auto r_index = std::distance(data_.rbegin(), location);
auto index = 4 - r_index - 1;
data_[index]++;



::ip::address& ip::address::operator++(int)

auto result(*this);
++(*this);
return result;


void ip::address::operator--()

auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

return data < 255;
);

if (location != std::rend(data_))

const auto r_index = std::distance(data_.rbegin(), location);
auto index = 4 - r_index - 1;
data_[index]--;



::ip::address& ip::address::operator--(int)

auto result(*this);
--(*this);
return result;


ip::address::iterator ip::address::begin()

return data_.begin();


ip::address::const_iterator ip::address::end() const

return data_.end();


bool ip::operator<(const ip::address& first, const ip::address& second)

return (uint32_t)first() < (uint32_t)second();


bool ip::operator==(const ip::address& first, const ip::address& second)

return (uint32_t)first() == (uint32_t) second();


ip::address::const_iterator ip::address::begin() const

return data_.begin();


ip::address::iterator ip::address::end()

return data_.end();


std::ostream& ip::operator<<(std::ostream& output, const ip::address& address)

std::copy(address.begin(), address.end()-1,
std::ostream_iterator<short>(output, "."));
output << +address[3];
return output;


ip::address ip::from_string(const std::string &view)

auto parts = split(view, '.');
if (parts.size() != 4)

throw invalid_format_exception(view);


return
(ip::address::value_type)std::stoi(parts[0]),
(ip::address::value_type)std::stoi(parts[1]),
(ip::address::value_type)std::stoi(parts[2]),
(ip::address::value_type)std::stoi(parts[3])
;


std::string ip::to_string(const address& address)

std::ostringstream string_stream;
string_stream << address;
return string_stream.str();









share







New contributor




Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$


















    1












    $begingroup$


    I've recently picked up a book that gives various "modern" C++ challenges/solutions. One of the first ones I did was on modeling an IPv4 address in C++. Below is the full implementation; it's also on Github. Any suggestions? My goal is to make this as modern as possible and I'm not sure if there are some C++17 features I'm not taking advantage of.



    ip_address.h



    #pragma once

    #include <array>
    #include <string>
    #include <string_view>
    #include <stdint.h>

    namespace ip

    /**
    * @brief Thrown when there is an invalid ip address passed via
    * string.
    */
    class invalid_format_exception : public std::exception

    std::string invalid_format_;
    public:
    invalid_format_exception(const std::string &invalid_format);
    char const* what() const override;
    ;

    /**
    * Class that models a IPv4 address.
    */
    class address

    public:

    #pragma region Type definitions
    using value_type = uint8_t;
    using reference = value_type & ;
    using pointer = value_type * ;
    using iterator = std::array<value_type, 4>::iterator;
    using const_iterator = std::array<value_type, 4>::const_iterator;
    using reverse_iterator = std::array<value_type, 4>::reverse_iterator;
    using const_reverse_iterator = std::array<value_type, 4>::const_reverse_iterator;
    using size_type = std::array<value_type, 4>::size_type;
    #pragma endregion

    /**
    * @brief Create an IP address representation from the
    * four parts of the address definition.
    * @param first the first part of the address
    * @param second the second part of the address
    * @param third the third part of the address.
    * @param fourth the fourth part of the address.
    * @details Example:
    * @code
    * ip::address addr(127, 0, 0, 1);
    * @endcode
    */
    address(const value_type& first, const value_type &second,
    const value_type &third, const value_type& fourth);

    /**
    * @brief Create an IP address representaiton from an
    * array.
    * @param data the data array.
    * @details Example:
    * @code
    * ip::address addr = 127, 0, 0, 1;
    * @endcode
    */
    address(const std::array<unsigned char, 4> &data);

    /**
    * @brief Create an IP adderss representation from a
    * unsigned 32 bit integer.
    * @param value the integer representation of an IP address.
    */
    explicit address(const uint32_t &value);

    /**
    * @brief Implicit conversion to an unsigned 32 bit integer.
    */
    uint32_t operator()() const;

    /**
    * @brief Access operator.
    * @param index the index to access.
    */
    reference operator[](const int &index) noexcept(false);

    /**
    * @brief Const version of the access operator.
    */
    value_type operator[](const int &index) const noexcept(false);

    /**
    * @brief Prefix increment operator.
    */
    void operator++();

    /**
    * @brief Postfix increment operator.
    */
    ::ip::address& operator++(int);

    /**
    * @brief Prefix decrement operator.
    */
    void operator--();

    /**
    * @brief Prefix decrement operator.
    */
    ::ip::address& operator--(int);

    iterator begin();
    iterator end();
    const_iterator begin() const;
    const_iterator end() const;
    private:
    std::array<value_type, 4> data_;
    ;

    bool operator<(const ip::address &first, const ip::address &second);
    bool operator==(const ip::address &first, const ip::address &second);
    std::ostream& operator<<(std::ostream& output, const ip::address &address);
    address from_string(const std::string &view);
    std::string to_string(const address& address);



    ip_address.cpp



    #include <ip_address.h>

    #include <iterator>
    #include <iostream>
    #include <sstream>
    #include <regex>
    #include <vector>
    #include <string>

    #pragma region Utilities
    template<typename Output>
    void split(const std::string &s, char delim, Output result)
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim))
    *(result++) = item;



    std::vector<std::string> split(const std::string &s, char delim)
    std::vector<std::string> elems;
    split(s, delim, std::back_inserter(elems));
    return elems;


    #pragma endregion

    ip::invalid_format_exception::invalid_format_exception(const std::string& invalid_format)
    : invalid_format_(invalid_format)



    char const* ip::invalid_format_exception::what() const

    std::ostringstream oss;
    oss << "Invalid IP address format: " << invalid_format_;
    return oss.str().c_str();


    ip::address::address(const value_type & first, const value_type & second, const value_type & third, const value_type & fourth)

    data_[0] = first;
    data_[1] = second;
    data_[2] = third;
    data_[3] = fourth;


    ip::address::address(const std::array<unsigned char, 4>& data)

    data_ = data;


    ip::address::address(const uint32_t& value)

    data_[0] = value >> 24 & 0xFF;
    data_[1] = value >> 16 & 0xFF;
    data_[2] = value >> 8 & 0xFF;
    data_[3] = value & 0xFF;


    uint32_t ip::address::operator()() const
    data_[2] << 8

    ip::address::reference ip::address::operator[](const int& index)

    return data_.at(index);


    ip::address::value_type ip::address::operator[](const int& index) const

    return data_.at(index);


    void ip::address::operator++()

    auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

    return data < 255;
    );

    if(location != std::rend(data_))

    const auto r_index = std::distance(data_.rbegin(), location);
    auto index = 4 - r_index - 1;
    data_[index]++;



    ::ip::address& ip::address::operator++(int)

    auto result(*this);
    ++(*this);
    return result;


    void ip::address::operator--()

    auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

    return data < 255;
    );

    if (location != std::rend(data_))

    const auto r_index = std::distance(data_.rbegin(), location);
    auto index = 4 - r_index - 1;
    data_[index]--;



    ::ip::address& ip::address::operator--(int)

    auto result(*this);
    --(*this);
    return result;


    ip::address::iterator ip::address::begin()

    return data_.begin();


    ip::address::const_iterator ip::address::end() const

    return data_.end();


    bool ip::operator<(const ip::address& first, const ip::address& second)

    return (uint32_t)first() < (uint32_t)second();


    bool ip::operator==(const ip::address& first, const ip::address& second)

    return (uint32_t)first() == (uint32_t) second();


    ip::address::const_iterator ip::address::begin() const

    return data_.begin();


    ip::address::iterator ip::address::end()

    return data_.end();


    std::ostream& ip::operator<<(std::ostream& output, const ip::address& address)

    std::copy(address.begin(), address.end()-1,
    std::ostream_iterator<short>(output, "."));
    output << +address[3];
    return output;


    ip::address ip::from_string(const std::string &view)

    auto parts = split(view, '.');
    if (parts.size() != 4)

    throw invalid_format_exception(view);


    return
    (ip::address::value_type)std::stoi(parts[0]),
    (ip::address::value_type)std::stoi(parts[1]),
    (ip::address::value_type)std::stoi(parts[2]),
    (ip::address::value_type)std::stoi(parts[3])
    ;


    std::string ip::to_string(const address& address)

    std::ostringstream string_stream;
    string_stream << address;
    return string_stream.str();









    share







    New contributor




    Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      1












      1








      1





      $begingroup$


      I've recently picked up a book that gives various "modern" C++ challenges/solutions. One of the first ones I did was on modeling an IPv4 address in C++. Below is the full implementation; it's also on Github. Any suggestions? My goal is to make this as modern as possible and I'm not sure if there are some C++17 features I'm not taking advantage of.



      ip_address.h



      #pragma once

      #include <array>
      #include <string>
      #include <string_view>
      #include <stdint.h>

      namespace ip

      /**
      * @brief Thrown when there is an invalid ip address passed via
      * string.
      */
      class invalid_format_exception : public std::exception

      std::string invalid_format_;
      public:
      invalid_format_exception(const std::string &invalid_format);
      char const* what() const override;
      ;

      /**
      * Class that models a IPv4 address.
      */
      class address

      public:

      #pragma region Type definitions
      using value_type = uint8_t;
      using reference = value_type & ;
      using pointer = value_type * ;
      using iterator = std::array<value_type, 4>::iterator;
      using const_iterator = std::array<value_type, 4>::const_iterator;
      using reverse_iterator = std::array<value_type, 4>::reverse_iterator;
      using const_reverse_iterator = std::array<value_type, 4>::const_reverse_iterator;
      using size_type = std::array<value_type, 4>::size_type;
      #pragma endregion

      /**
      * @brief Create an IP address representation from the
      * four parts of the address definition.
      * @param first the first part of the address
      * @param second the second part of the address
      * @param third the third part of the address.
      * @param fourth the fourth part of the address.
      * @details Example:
      * @code
      * ip::address addr(127, 0, 0, 1);
      * @endcode
      */
      address(const value_type& first, const value_type &second,
      const value_type &third, const value_type& fourth);

      /**
      * @brief Create an IP address representaiton from an
      * array.
      * @param data the data array.
      * @details Example:
      * @code
      * ip::address addr = 127, 0, 0, 1;
      * @endcode
      */
      address(const std::array<unsigned char, 4> &data);

      /**
      * @brief Create an IP adderss representation from a
      * unsigned 32 bit integer.
      * @param value the integer representation of an IP address.
      */
      explicit address(const uint32_t &value);

      /**
      * @brief Implicit conversion to an unsigned 32 bit integer.
      */
      uint32_t operator()() const;

      /**
      * @brief Access operator.
      * @param index the index to access.
      */
      reference operator[](const int &index) noexcept(false);

      /**
      * @brief Const version of the access operator.
      */
      value_type operator[](const int &index) const noexcept(false);

      /**
      * @brief Prefix increment operator.
      */
      void operator++();

      /**
      * @brief Postfix increment operator.
      */
      ::ip::address& operator++(int);

      /**
      * @brief Prefix decrement operator.
      */
      void operator--();

      /**
      * @brief Prefix decrement operator.
      */
      ::ip::address& operator--(int);

      iterator begin();
      iterator end();
      const_iterator begin() const;
      const_iterator end() const;
      private:
      std::array<value_type, 4> data_;
      ;

      bool operator<(const ip::address &first, const ip::address &second);
      bool operator==(const ip::address &first, const ip::address &second);
      std::ostream& operator<<(std::ostream& output, const ip::address &address);
      address from_string(const std::string &view);
      std::string to_string(const address& address);



      ip_address.cpp



      #include <ip_address.h>

      #include <iterator>
      #include <iostream>
      #include <sstream>
      #include <regex>
      #include <vector>
      #include <string>

      #pragma region Utilities
      template<typename Output>
      void split(const std::string &s, char delim, Output result)
      std::stringstream ss(s);
      std::string item;
      while (std::getline(ss, item, delim))
      *(result++) = item;



      std::vector<std::string> split(const std::string &s, char delim)
      std::vector<std::string> elems;
      split(s, delim, std::back_inserter(elems));
      return elems;


      #pragma endregion

      ip::invalid_format_exception::invalid_format_exception(const std::string& invalid_format)
      : invalid_format_(invalid_format)



      char const* ip::invalid_format_exception::what() const

      std::ostringstream oss;
      oss << "Invalid IP address format: " << invalid_format_;
      return oss.str().c_str();


      ip::address::address(const value_type & first, const value_type & second, const value_type & third, const value_type & fourth)

      data_[0] = first;
      data_[1] = second;
      data_[2] = third;
      data_[3] = fourth;


      ip::address::address(const std::array<unsigned char, 4>& data)

      data_ = data;


      ip::address::address(const uint32_t& value)

      data_[0] = value >> 24 & 0xFF;
      data_[1] = value >> 16 & 0xFF;
      data_[2] = value >> 8 & 0xFF;
      data_[3] = value & 0xFF;


      uint32_t ip::address::operator()() const
      data_[2] << 8

      ip::address::reference ip::address::operator[](const int& index)

      return data_.at(index);


      ip::address::value_type ip::address::operator[](const int& index) const

      return data_.at(index);


      void ip::address::operator++()

      auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

      return data < 255;
      );

      if(location != std::rend(data_))

      const auto r_index = std::distance(data_.rbegin(), location);
      auto index = 4 - r_index - 1;
      data_[index]++;



      ::ip::address& ip::address::operator++(int)

      auto result(*this);
      ++(*this);
      return result;


      void ip::address::operator--()

      auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

      return data < 255;
      );

      if (location != std::rend(data_))

      const auto r_index = std::distance(data_.rbegin(), location);
      auto index = 4 - r_index - 1;
      data_[index]--;



      ::ip::address& ip::address::operator--(int)

      auto result(*this);
      --(*this);
      return result;


      ip::address::iterator ip::address::begin()

      return data_.begin();


      ip::address::const_iterator ip::address::end() const

      return data_.end();


      bool ip::operator<(const ip::address& first, const ip::address& second)

      return (uint32_t)first() < (uint32_t)second();


      bool ip::operator==(const ip::address& first, const ip::address& second)

      return (uint32_t)first() == (uint32_t) second();


      ip::address::const_iterator ip::address::begin() const

      return data_.begin();


      ip::address::iterator ip::address::end()

      return data_.end();


      std::ostream& ip::operator<<(std::ostream& output, const ip::address& address)

      std::copy(address.begin(), address.end()-1,
      std::ostream_iterator<short>(output, "."));
      output << +address[3];
      return output;


      ip::address ip::from_string(const std::string &view)

      auto parts = split(view, '.');
      if (parts.size() != 4)

      throw invalid_format_exception(view);


      return
      (ip::address::value_type)std::stoi(parts[0]),
      (ip::address::value_type)std::stoi(parts[1]),
      (ip::address::value_type)std::stoi(parts[2]),
      (ip::address::value_type)std::stoi(parts[3])
      ;


      std::string ip::to_string(const address& address)

      std::ostringstream string_stream;
      string_stream << address;
      return string_stream.str();









      share







      New contributor




      Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I've recently picked up a book that gives various "modern" C++ challenges/solutions. One of the first ones I did was on modeling an IPv4 address in C++. Below is the full implementation; it's also on Github. Any suggestions? My goal is to make this as modern as possible and I'm not sure if there are some C++17 features I'm not taking advantage of.



      ip_address.h



      #pragma once

      #include <array>
      #include <string>
      #include <string_view>
      #include <stdint.h>

      namespace ip

      /**
      * @brief Thrown when there is an invalid ip address passed via
      * string.
      */
      class invalid_format_exception : public std::exception

      std::string invalid_format_;
      public:
      invalid_format_exception(const std::string &invalid_format);
      char const* what() const override;
      ;

      /**
      * Class that models a IPv4 address.
      */
      class address

      public:

      #pragma region Type definitions
      using value_type = uint8_t;
      using reference = value_type & ;
      using pointer = value_type * ;
      using iterator = std::array<value_type, 4>::iterator;
      using const_iterator = std::array<value_type, 4>::const_iterator;
      using reverse_iterator = std::array<value_type, 4>::reverse_iterator;
      using const_reverse_iterator = std::array<value_type, 4>::const_reverse_iterator;
      using size_type = std::array<value_type, 4>::size_type;
      #pragma endregion

      /**
      * @brief Create an IP address representation from the
      * four parts of the address definition.
      * @param first the first part of the address
      * @param second the second part of the address
      * @param third the third part of the address.
      * @param fourth the fourth part of the address.
      * @details Example:
      * @code
      * ip::address addr(127, 0, 0, 1);
      * @endcode
      */
      address(const value_type& first, const value_type &second,
      const value_type &third, const value_type& fourth);

      /**
      * @brief Create an IP address representaiton from an
      * array.
      * @param data the data array.
      * @details Example:
      * @code
      * ip::address addr = 127, 0, 0, 1;
      * @endcode
      */
      address(const std::array<unsigned char, 4> &data);

      /**
      * @brief Create an IP adderss representation from a
      * unsigned 32 bit integer.
      * @param value the integer representation of an IP address.
      */
      explicit address(const uint32_t &value);

      /**
      * @brief Implicit conversion to an unsigned 32 bit integer.
      */
      uint32_t operator()() const;

      /**
      * @brief Access operator.
      * @param index the index to access.
      */
      reference operator[](const int &index) noexcept(false);

      /**
      * @brief Const version of the access operator.
      */
      value_type operator[](const int &index) const noexcept(false);

      /**
      * @brief Prefix increment operator.
      */
      void operator++();

      /**
      * @brief Postfix increment operator.
      */
      ::ip::address& operator++(int);

      /**
      * @brief Prefix decrement operator.
      */
      void operator--();

      /**
      * @brief Prefix decrement operator.
      */
      ::ip::address& operator--(int);

      iterator begin();
      iterator end();
      const_iterator begin() const;
      const_iterator end() const;
      private:
      std::array<value_type, 4> data_;
      ;

      bool operator<(const ip::address &first, const ip::address &second);
      bool operator==(const ip::address &first, const ip::address &second);
      std::ostream& operator<<(std::ostream& output, const ip::address &address);
      address from_string(const std::string &view);
      std::string to_string(const address& address);



      ip_address.cpp



      #include <ip_address.h>

      #include <iterator>
      #include <iostream>
      #include <sstream>
      #include <regex>
      #include <vector>
      #include <string>

      #pragma region Utilities
      template<typename Output>
      void split(const std::string &s, char delim, Output result)
      std::stringstream ss(s);
      std::string item;
      while (std::getline(ss, item, delim))
      *(result++) = item;



      std::vector<std::string> split(const std::string &s, char delim)
      std::vector<std::string> elems;
      split(s, delim, std::back_inserter(elems));
      return elems;


      #pragma endregion

      ip::invalid_format_exception::invalid_format_exception(const std::string& invalid_format)
      : invalid_format_(invalid_format)



      char const* ip::invalid_format_exception::what() const

      std::ostringstream oss;
      oss << "Invalid IP address format: " << invalid_format_;
      return oss.str().c_str();


      ip::address::address(const value_type & first, const value_type & second, const value_type & third, const value_type & fourth)

      data_[0] = first;
      data_[1] = second;
      data_[2] = third;
      data_[3] = fourth;


      ip::address::address(const std::array<unsigned char, 4>& data)

      data_ = data;


      ip::address::address(const uint32_t& value)

      data_[0] = value >> 24 & 0xFF;
      data_[1] = value >> 16 & 0xFF;
      data_[2] = value >> 8 & 0xFF;
      data_[3] = value & 0xFF;


      uint32_t ip::address::operator()() const
      data_[2] << 8

      ip::address::reference ip::address::operator[](const int& index)

      return data_.at(index);


      ip::address::value_type ip::address::operator[](const int& index) const

      return data_.at(index);


      void ip::address::operator++()

      auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

      return data < 255;
      );

      if(location != std::rend(data_))

      const auto r_index = std::distance(data_.rbegin(), location);
      auto index = 4 - r_index - 1;
      data_[index]++;



      ::ip::address& ip::address::operator++(int)

      auto result(*this);
      ++(*this);
      return result;


      void ip::address::operator--()

      auto location = std::find_if(data_.rbegin(), data_.rend(), [](const unsigned char& data)

      return data < 255;
      );

      if (location != std::rend(data_))

      const auto r_index = std::distance(data_.rbegin(), location);
      auto index = 4 - r_index - 1;
      data_[index]--;



      ::ip::address& ip::address::operator--(int)

      auto result(*this);
      --(*this);
      return result;


      ip::address::iterator ip::address::begin()

      return data_.begin();


      ip::address::const_iterator ip::address::end() const

      return data_.end();


      bool ip::operator<(const ip::address& first, const ip::address& second)

      return (uint32_t)first() < (uint32_t)second();


      bool ip::operator==(const ip::address& first, const ip::address& second)

      return (uint32_t)first() == (uint32_t) second();


      ip::address::const_iterator ip::address::begin() const

      return data_.begin();


      ip::address::iterator ip::address::end()

      return data_.end();


      std::ostream& ip::operator<<(std::ostream& output, const ip::address& address)

      std::copy(address.begin(), address.end()-1,
      std::ostream_iterator<short>(output, "."));
      output << +address[3];
      return output;


      ip::address ip::from_string(const std::string &view)

      auto parts = split(view, '.');
      if (parts.size() != 4)

      throw invalid_format_exception(view);


      return
      (ip::address::value_type)std::stoi(parts[0]),
      (ip::address::value_type)std::stoi(parts[1]),
      (ip::address::value_type)std::stoi(parts[2]),
      (ip::address::value_type)std::stoi(parts[3])
      ;


      std::string ip::to_string(const address& address)

      std::ostringstream string_stream;
      string_stream << address;
      return string_stream.str();







      c++ c++11 ip-address





      share







      New contributor




      Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 mins ago









      Developer PaulDeveloper Paul

      1061




      1061




      New contributor




      Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Developer Paul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Developer Paul is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216894%2fmodeling-an-ip-address%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Developer Paul is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Developer Paul is a new contributor. Be nice, and check out our Code of Conduct.












          Developer Paul is a new contributor. Be nice, and check out our Code of Conduct.











          Developer Paul is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216894%2fmodeling-an-ip-address%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar