IpAddress is a class for containing an IP address.
INCLUDEPATH += \ ../../Classes/CppIpAddress SOURCES += \ ../../Classes/CppIpAddress/ipaddress.cpp HEADERS += \ ../../Classes/CppIpAddress/ipaddress.h OTHER_FILES += \ ../../Classes/CppIpAddress/Licence.txt
//--------------------------------------------------------------------------- /* IpAddress, class for containing an IP address Copyright 2011-2015 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> namespace ribi { ///IpAddress guarantees to contain a valid IP address struct IpAddress { ///IpAddress constructor throws an exception when ///ip_address is not a valid IP address explicit IpAddress(const std::string& ip_address); ///IpAddress is a base class, so its destructor must be virtual virtual ~IpAddress() noexcept {} ///Get the IP address as a std::string const std::string& Get() const noexcept { return m_ip_address; } ///Obtain the IpAddress version static std::string GetVersion() noexcept; ///Obtain the IpAddress version history static std::vector<std::string> GetVersionHistory() noexcept; private: ///The std::string guaranteed to hold a valid IP address std::string m_ip_address; }; ///SafeIpAddress contains a valid or invalid IP address struct SafeIpAddress //: public IpAddress { ///SafeIpAddress constructor does not throw an exception ///ip_address is not a valid IP address SafeIpAddress(const std::string& ip_address) noexcept; SafeIpAddress(const SafeIpAddress&) = delete; SafeIpAddress& operator=(const SafeIpAddress&) = delete; ///Get the possibe IP address as a std::string const std::string& Get() const noexcept { return m_ip_address; } private: ~SafeIpAddress() noexcept {} friend void boost::checked_delete<>(SafeIpAddress*); friend void boost::checked_delete<>(const SafeIpAddress*); ///The std::string that might hold a valid IP address std::string m_ip_address; }; bool operator==(const IpAddress& lhs,const IpAddress& rhs) noexcept; bool operator==(const SafeIpAddress& lhs,const SafeIpAddress& rhs) noexcept; } //~namespace ribi #endif // IPADDRESS_H
//--------------------------------------------------------------------------- /* IpAddress, class for containing an IP address Copyright 2011-2015 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 //--------------------------------------------------------------------------- #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include "ipaddress.h" #include <stdexcept> #include <boost/xpressive/xpressive.hpp> #pragma GCC diagnostic pop ribi::IpAddress::IpAddress(const std::string& ip_address) : m_ip_address{ip_address} { const boost::xpressive::sregex regex_ip_address = boost::xpressive::sregex::compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"); if(!boost::xpressive::regex_match(ip_address,regex_ip_address)) { throw std::logic_error("Invalid IP address"); } } std::string ribi::IpAddress::GetVersion() noexcept { return "1.1"; } std::vector<std::string> ribi::IpAddress::GetVersionHistory() noexcept { return { "2011-06-08: version 1.0: initial version", "2013-09-02: version 1.1: replaced Boost.Regex by Boost.Xpressive" }; } ribi::SafeIpAddress::SafeIpAddress(const std::string& ip_address) noexcept : m_ip_address(ip_address) { } bool ribi::operator==(const IpAddress& lhs,const IpAddress& rhs) noexcept { return lhs.Get() == rhs.Get(); } bool ribi::operator==(const SafeIpAddress& lhs,const SafeIpAddress& rhs) noexcept { return lhs.Get() == rhs.Get(); }