stlab.adobe.com Adobe Systems Incorporated

dirty_value.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003     Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004     or a copy at http://stlab.adobe.com/licenses.html)
00005 */
00006 
00007 /*************************************************************************************************/
00008 
00009 #ifndef ADOBE_DIRTY_VALUE_HPP
00010 #define ADOBE_DIRTY_VALUE_HPP
00011 
00012 /*************************************************************************************************/
00013 
00014 #include <adobe/config.hpp>
00015 
00016 /*************************************************************************************************/
00017 
00018 namespace adobe {
00019 
00020 /*************************************************************************************************/
00021 
00022 template <typename T>
00023 struct dirty_value
00024 {
00025     typedef T value_type;
00026 
00027     dirty_value(const value_type& x = value_type()) :
00028         value_m(x), dirty_m(false)
00029     { }
00030 
00031     dirty_value(const dirty_value& rhs) :
00032         value_m(rhs.value_m), dirty_m(false)
00033     { }
00034 
00035     dirty_value& operator = (const value_type& rhs)
00036     {
00037         if (value_m == rhs)
00038             return *this;
00039 
00040         value_m = rhs;
00041         dirty_m = true;
00042         return *this;
00043     }
00044 
00045     dirty_value& operator = (const dirty_value& rhs)
00046     { return *this = rhs.value_m; }
00047 
00048     const value_type& operator*() const
00049     { return value_m; }
00050     const value_type* operator->() const
00051     { return &value_m; }
00052     operator const value_type& () const
00053     { return value_m; }
00054 
00055     void clean()
00056     { dirty_m = false; }
00057 
00058     bool is_dirty() const
00059     { return dirty_m; }
00060 
00061     struct cleaner
00062     {
00063         typedef dirty_value first_argument_type;
00064         typedef void        result_type;
00065 
00066         inline result_type operator()(first_argument_type& x) const
00067         { x.clean(); }
00068     };
00069 
00070     value_type value_m;
00071     bool       dirty_m;
00072 };
00073 
00074 /*************************************************************************************************/
00075 
00076 /*
00077     NOTE (fbrereto) : We cannot use boost::totaly_ordered to implement
00078     these operations portably. The problem is that we do not know if T
00079     satifies the requirments for totally ordered or not - dirty_value is
00080     only totally ordered if T is. By splitting the operations out to
00081     seperate template functions they are only instantiated if and where
00082     they are used.
00083 */
00084 
00085 template <typename T>
00086 inline bool operator<(const dirty_value<T>& x, const dirty_value<T>& y)
00087 { return *x < *y; }
00088 
00089 template <typename T>
00090 inline bool operator>(const dirty_value<T>& x, const dirty_value<T>& y)
00091 { return y < x; }
00092 
00093 template <typename T>
00094 inline bool operator<=(const dirty_value<T>& x, const dirty_value<T>& y)
00095 { return !(y < x); }
00096 
00097 template <typename T>
00098 inline bool operator>=(const dirty_value<T>& x, const dirty_value<T>& y)
00099 { return !(x < y); }
00100 
00101 template <typename T>
00102 inline bool operator==(const dirty_value<T>& x, const dirty_value<T>& y)
00103 { return *x == *y; }
00104 
00105 template <typename T>
00106 inline bool operator!=(const dirty_value<T>& x, const dirty_value<T>& y)
00107 { return !(x == y); }
00108 
00109 /*************************************************************************************************/
00110 
00111 } // namespace adobe
00112 
00113 /*************************************************************************************************/
00114 
00115 // ADOBE_DIRTY_VALUE_HPP
00116 #endif
00117 
00118 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google