xorp

range.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/range.hh,v 1.11 2008/10/02 21:57:32 bms Exp $
00023 
00024 #ifndef __LIBXORP_RANGE_HH__
00025 #define __LIBXORP_RANGE_HH__
00026 
00027 
00028 
00029 class IPv4;
00030 class IPv6;
00031 
00039 template <class T>
00040 class Range {
00041 public:
00045     Range()             {}
00046 
00050     explicit Range(T value) { _low = _high = value; }
00051 
00055     explicit Range(T low, T high) {
00056     _low = low;
00057     _high = high;
00058     }
00059 
00060     const T& low() const { return _low; }
00061     const T& high() const { return _high; }
00062 
00063 protected:
00064     T _low;
00065     T _high;
00066 };
00067 
00074 class U32Range: public Range<uint32_t> {
00075 public:
00079     U32Range()          { Range<uint32_t>::_low = 
00080                        Range<uint32_t>::_high = 0; }
00081 
00085     U32Range(const char *from_cstr) {
00086     string from_string = string(from_cstr);
00087     string::size_type delim = from_string.find("..", 0);
00088     if (delim == string::npos) {
00089         _low = _high = strtoul(from_cstr, NULL, 10);
00090     } else if (delim > 0 && (from_string.length() - delim > 2)) {
00091         _low = strtoul(from_string.substr(0, delim).c_str(), NULL, 10);
00092         _high = strtoul(from_string.substr(delim + 2, from_string.length()).c_str(), NULL, 10);
00093     } else {
00094         xorp_throw(InvalidString, "Syntax error");
00095     }
00096     }
00097 
00103     string str() const {
00104     ostringstream os;
00105     os << _low;
00106     if (_low < _high)
00107         os << ".." << _high;
00108     return os.str();
00109     }
00110 };
00111 
00120 inline bool operator==(const uint32_t& lhs, const U32Range& rhs) {
00121     return (lhs >= rhs.low() && lhs <= rhs.high());
00122 }
00123 
00124 
00133 inline bool operator!=(const uint32_t& lhs, const U32Range& rhs) {
00134     return (lhs < rhs.low() || lhs > rhs.high());
00135 }
00136 
00145 inline bool operator<(const uint32_t& lhs, const U32Range& rhs) {
00146     return (lhs < rhs.low());
00147 }
00148 
00157 inline bool operator<=(const uint32_t& lhs, const U32Range& rhs) {
00158     return (lhs <= rhs.high());
00159 }
00160 
00169 inline bool operator>(const uint32_t& lhs, const U32Range& rhs) {
00170     return (lhs > rhs.high());
00171 }
00172 
00181 inline bool operator>=(const uint32_t& lhs, const U32Range& rhs) {
00182     return (lhs >= rhs.low());
00183 }
00184 
00185 
00192 template <class T>
00193 class IPvXRange: public Range<T> {
00194 public:
00198     IPvXRange()         {}
00199 
00203     IPvXRange(const char *from_cstr) {
00204     string from_string = string(from_cstr);
00205     string::size_type delim = from_string.find("..", 0);
00206     if (delim == string::npos)
00207         Range<T>::_low = Range<T>::_high = T(from_cstr);
00208     else if (delim > 0 && (from_string.length() - delim > 2)) {
00209         Range<T>::_low = T(from_string.substr(0, delim).c_str());
00210         Range<T>::_high = T(from_string.substr(delim + 2, 
00211                                from_string.length())
00212                             .c_str());
00213     } else {
00214         xorp_throw(InvalidString, "Syntax error");
00215     }
00216     }
00217 
00223     string str() const {
00224     ostringstream os;
00225     os << Range<T>::_low.str();
00226     if (Range<T>::_low < Range<T>::_high)
00227         os << ".." << Range<T>::_high.str();
00228     return os.str();
00229     }
00230 };
00231 
00232 typedef IPvXRange<IPv4> IPv4Range;
00233 typedef IPvXRange<IPv6> IPv6Range;
00234 
00235 #endif // __LIBXORP_RANGE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations