Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) IpAddress

 

IpAddress is a class for containing an IP address.

 

 

 

 

 

ipaddress.h

 

//---------------------------------------------------------------------------
/*
IpAddress, class for containing an IP address
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppIpAddress.htm
//---------------------------------------------------------------------------
#ifndef IPADDRESS_H
#define IPADDRESS_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/noncopyable.hpp>
//---------------------------------------------------------------------------
///IpAddress guarantees to contain a valid IP address
struct IpAddress
{
  ///IpAddress constructor throws an exception when
  ///ip_address is not a valid IP address
  IpAddress(const std::string& ip_address);

  ///IpAddress is a base class, so its destructor must be virtual
  virtual ~IpAddress()  {}


  ///Set the IP address. Will throws an exception when
  ///the ip_address is not a valid IP address
  void Set(const std::string& ip_address);

  ///Get the IP address as a std::string
  const std::string& Get() const { return m_ip_address; }

  ///Obtain the IpAddress version
  static const std::string GetVersion();

  ///Obtain the IpAddress version history
  static const std::vector<std::string> GetVersionHistory();

  private:
  ///The std::string guaranteed to hold a valid IP address
  std::string m_ip_address;
};
//---------------------------------------------------------------------------
///SafeIpAddress guarantees to contain a valid IP address
///and only be of a complete type
struct SafeIpAddress : public IpAddress, boost::noncopyable
{
  ///SafeIpAddress constructor throws an exception when
  ///ip_address is not a valid IP address
  SafeIpAddress(const std::string& ip_address);

  private:
  ///To prevent the following trouble,
  ///cited from http://www.boost.org/libs/utility/checked_delete.html:
  ///The C++ Standard allows, in 5.3.5/5, pointers to incomplete
  ///class types to be deleted with a delete-expression.
  ///When the class has a non-trivial destructor, or a class-specific operator
  ///delete, the behavior is undefined. Some compilers issue a warning when an
  ///incomplete type is deleted, but unfortunately, not all do, and programmers
  ///sometimes ignore or disable warnings.
  ~SafeIpAddress()  {}
  ///Template syntax from Herb Sutter. Exceptional C++ style. 2005.
  ///ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.
  friend void boost::checked_delete<>(SafeIpAddress*);
};
//---------------------------------------------------------------------------
///Test for equal IP addresses
bool operator==(const IpAddress& lhs,const IpAddress& rhs);
///Test for equal IP addresses
bool operator==(const SafeIpAddress& lhs,const SafeIpAddress& rhs);
//---------------------------------------------------------------------------
#endif // IPADDRESS_H

 

 

 

 

 

ipaddress.cpp

 

//---------------------------------------------------------------------------
/*
IpAddress, class for containing an IP address
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppIpAddress.htm
//---------------------------------------------------------------------------
#include <stdexcept>
//---------------------------------------------------------------------------
#include <boost/regex.hpp>
//---------------------------------------------------------------------------
#include "ipaddress.h"
//---------------------------------------------------------------------------
IpAddress::IpAddress(const std::string& ip_address)
{
  Set(ip_address);
}
//---------------------------------------------------------------------------
const std::string IpAddress::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> IpAddress::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-06-08: version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
void IpAddress::Set(const std::string& ip_address)
{
  const boost::regex regex_ip_address("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
  if(!boost::regex_match(ip_address,regex_ip_address))
  {
    throw std::logic_error("Invalid IP address");
  }
  m_ip_address = ip_address;
}
//---------------------------------------------------------------------------
SafeIpAddress::SafeIpAddress(const std::string& ip_address)
  : IpAddress(ip_address)
{
}
//---------------------------------------------------------------------------
bool operator==(const IpAddress& lhs,const IpAddress& rhs)
{
  return lhs.Get() == rhs.Get();
}
//---------------------------------------------------------------------------
bool operator==(const SafeIpAddress& lhs,const SafeIpAddress& rhs)
{
  return lhs.Get() == rhs.Get();
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml