Declarations
template <class ConstrainedFunc> class constrained_value;
Overview
constrained_value は,制約付きの値型を実現するためのクラスです. 代入演算子をオーバーロードすることによって,値の代入や四則演算が行われるたびに, 値が制約を満たしているか自動的にチェックします.制約は ConstrainedFunc に制約条件用のポリシークラスを指定することによってよって与えます. 独自に制約条件用のポリシークラスを定義する場合には,以下の型, およびメソッドを定義する必要があります.
class constrained_func { public: typedef ... value_type; static void assign(value_type& dest, const value_type& src); };
尚,現在のところ代入された値が [Min, Max] の範囲外である場合には,std::ranged_error を例外として送出する ranged_integer と言う制約条件用ポリシークラスを提供しています.
template <int Min, int Max> class ranged_integer { public: typedef int value_type; const static value_type default_value = Min; static void assign(value_type& dest, const value_type& src); };
代入,四則演算,および比較については,同じ ConstrainedFunc を適用しているオブジェクト, または,基となる型 (integer, double, ...) と行うことができます.
constrained_value は,C++ クックブック (p.194 - 197) をベースに実装しています.
Example
#include <iostream> #include "clx/constrained_value.h" int main(int argc, char* argv[]) { clx::constrained_value<clx::ranged_integer<0, 100> > x; clx::constrained_value<clx::ranged_integer<0, 100> > y; clx::constrained_value<clx::ranged_integer<0, 50> > z; try { std::cout << "1st example." << std::endl; x = 32; std::cout << "x = " << x << std::endl; y = 8; std::cout << "y = " << y << std::endl; y += x + 51 - (2 * 4); std::cout << "y = " << y << std::endl; } catch (std::range_error& e) { std::cerr << "exception: " << e.what() << std::endl; } std::cout << std::endl; try { std::cout << "2nd example." << std::endl; x = 32; std::cout << "x = " << x << std::endl; z = 8; std::cout << "z = " << z << std::endl; z += z.value() + 51 - (2 * 4); std::cout << "z = " << z << std::endl; } catch (std::range_error& e) { std::cerr << "exception: " << e.what() << std::endl; } return 0; }
Result 1st example. x = 32 y = 8 y = 83 2nd example. x = 32 z = 8 exception: out of valied range
Template Parameters
- ConstrainedFunc
- 制約条件を記述したポリシークラスを指定します.
Related Types
typedef ConstrainedFunc func; typedef typename ConstrainedFunc::value_type value_type;
Construction and Member Functions
constrained_value(); constrained_value(const constrained_value& x); explicit constrained_value(const value_type& x); virtual ~constrained_value(); const value_type& value() const;
Operators
constrained_value& operator=(const constrained_value& x); constrained_value& operator+=(const constrained_value& x); constrained_value& operator-=(const constrained_value& x); constrained_value& operator*=(const constrained_value& x); constrained_value& operator/=(const constrained_value& x); constrained_value& operator%=(const constrained_value& x); constrained_value& operator=(const value_type& x); constrained_value& operator+=(const value_type& x); constrained_value& operator-=(const value_type& x); constrained_value& operator*=(const value_type& x); constrained_value& operator/=(const value_type& x); constrained_value& operator%=(const value_type& x); constrained_value& operator>>=(int x); constrained_value& operator<<=(int x);
Unary Operators
constrained_value operator-(); constrained_value operator+(); constrained_value operator!(); constrained_value operator~();
Binary Operators
friend constrained_value operator+(constrained_value x, const constrained_value& y); friend constrained_value operator-(constrained_value x, const constrained_value& y); friend constrained_value operator*(constrained_value x, const constrained_value& y); friend constrained_value operator/(constrained_value x, const constrained_value& y); friend constrained_value operator%(constrained_value x, const constrained_value& y); friend constrained_value operator+(constrained_value x, const value_type& y); friend constrained_value operator-(constrained_value x, const value_type& y); friend constrained_value operator*(constrained_value x, const value_type& y); friend constrained_value operator/(constrained_value x, const value_type& y); friend constrained_value operator%(constrained_value x, const value_type& y); friend constrained_value operator+(const value_type& y, constrained_value x); friend constrained_value operator-(const value_type& y, constrained_value x); friend constrained_value operator*(const value_type& y, constrained_value x); friend constrained_value operator/(const value_type& y, constrained_value x); friend constrained_value operator%(const value_type& y, constrained_value x); friend constrained_value operator>>(constrained_value x, int y); friend constrained_value operator<<(constrained_value x, int y);
Stream Operators
template <class Ch, class Tr> friend std::basic_ostream<Ch, Tr>& operator<<( std::basic_ostream<Ch, Tr>& sout, constrained_value x); template <class Ch, class Tr> friend std::basic_istream<Ch, Tr>& operator>>( std::basic_istream<Ch, Tr>& sin, constrained_value x);
Comparison Operators
friend bool operator<(const constrained_value& x, const constrained_value& y); friend bool operator>(const constrained_value& x, const constrained_value& y); friend bool operator<=(const constrained_value& x, const constrained_value& y); friend bool operator>=(const constrained_value& x, const constrained_value& y); friend bool operator==(const constrained_value& x, const constrained_value& y); friend bool operator!=(const constrained_value& x, const constrained_value& y); friend bool operator<(const constrained_value& x, const value_type& y); friend bool operator>(const constrained_value& x, const value_type& y); friend bool operator<=(const constrained_value& x, const value_type& y); friend bool operator>=(const constrained_value& x, const value_type& y); friend bool operator==(const constrained_value& x, const value_type& y); friend bool operator!=(const constrained_value& x, const value_type& y); friend bool operator<(const value_type& y, const constrained_value& x); friend bool operator>(const value_type& y, const constrained_value& x); friend bool operator<=(const value_type& y, const constrained_value& x); friend bool operator>=(const value_type& y, const constrained_value& x); friend bool operator==(const value_type& y, const constrained_value& x); friend bool operator!=(const value_type& y, const constrained_value& x);
References
- D. Ryan Stephens, Christopher Diggins, Janathan Turkanis, and Jeff Cogswell (株式会社クイープ訳), "C++ クックブック," 株式会社オライリー・ジャパン, 2006.