ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> boost
boost
[boost] Ranged type mini-library submission
by christopher diggins other posts by this author
May 14 2004 9:08PM messages near this date
RE: [boost] Re: Latest ScopeGuard | Re: [boost] Ranged type mini-library submission
There has been some discussion previously on this list for a ranged type. My
personal desire was to have new double types in the ranges [0.0, 1.0) and
[0.0, 1.0] among other things. There are several other uses that I can
imagine. Feel free to rip this apart or do whatever you want.

#include <float.h> 
#include <assert.h> 

namespace boost
{
  // a simple range class
  template<class policy> 
  class range {
    public:
      typedef range<policy>  self;
      typedef typename policy::value value;
      range() : m(min()) { }
      range(const self& x) : m(x) { }
      range(const value& x) { m = rangeAssert(x); }
      static const value min() { return policy::min(); }
      static const value max() { return policy::max(); }
      operator value() { return m; }
      self& operator=(const self& x) { m = x; return *this; }
      self& operator=(const value& x) { m = rangeAssert(x); return *this; }
      const value& rangeAssert(const value& x) const { assert(x <= max());
assert(x > = min()); return x; };
    private:
      value m;
  };

  // some range policies
  struct RangePolicyZeroToOne {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 1.0; };
  };

  struct RangePolicyZeroToBelowOne {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 1.0 - DBL_EPSILON; };
  };

  struct RangePolicyPercent {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 100.0; };
  };

  struct RangePolicyDegrees {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 360.0 - DBL_EPSILON; };
  };

  struct RangePolicyRadians {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return (2 * PI) - DBL_EPSILON; };
  };

  template <typename T, T min_T, T max_T> 
  struct StaticRange {
    typedef T value;
    static const value min() { return min_T; };
    static const value max() { return max_T; };
  }

  // some sample types made with ranges
  typedef range<RangePolicyZeroToOne>  Probability;
  typedef range<RangePolicyZeroToBelowOne>  Distribution;
  typedef range<RangePolicyPercent>  Percent;
  typedef range<RangePolicyDegrees>  Degrees;
  typedef range<RangePolicyRadians>  Radians;
  typedef range<RangePolicyStatic<int, 0, 11>  > Month;
  typedef range<RangePolicyStatic<int, 0, 364>  > DayOfTheYear;
  typedef range<RangePolicyStatic<int, 0, 6>  > DayOfTheWeek;
  typedef range<RangePolicyStatic<char, 'a', 'z'>  > LowerCaseLetter;
  typedef range<RangePolicyStatic<char, 'A', 'Z'>  > UpperCaseLetter;
  typedef range<RangePolicyStatic<char, '0', '9'>  > NumberChar;
};

by Christopher Diggins
licensed under the Boost software license

http://www.cdiggins.com
http://www.heron-language.com

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thread:
christopher diggins
christopher diggins
christopher diggins
Jeff Garland
christopher diggins
Rob Stewart
Jeff Garland
christopher diggins
christopher diggins
Robert Bell
christopher diggins
Jeff Garland
christopher diggins
christopher diggins
Jeff Garland
christopher diggins
christopher diggins
Robert Bell
Robert Bell
christopher diggins
Christoph Ludwig
christopher diggins
Daryle Walker
christopher diggins
Christoph Ludwig
christopher diggins
Rob Stewart
christopher diggins
Pavol Droba
Jeff Garland
christopher diggins
Victor A. Wagner Jr.
Jeff Garland
Victor A. Wagner Jr.
Jeff Garland
Jeff Garland
Victor A. Wagner Jr.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved