#ifndef _utl_Validated_h_ #define _utl_Validated_h_ #include namespace utl { /** \class Validated Validated.h "utl/Validated.h" \brief Wrapper class for initially unset data This class in intended as replacement of lazy evaluation through pointers, i.e. when zero value of a pointer to some field indicated that this field has not been initialized yet. This was usualy done in such way: \code class Foo { public: Foo() : fField(0) { } int GetField() const { if (!fField) FetchField(); return *fField; } private: void FetchField() const { fField = new int(GetFromSomewhere()); } mutable int* fField; }; \endcode This can now be replaced with more elegant construct which does not trash the heap allocator with small memory chunks: \code class Foo { public: //Foo() // ctor not needed, default is invalid value int GetField() const { if (!fField.IsValid()) FetchField(); return fField.Get(); } private: void FetchField() const { fField = GetFromSomewhere(); } mutable utl::Validated fField; }; \endcode \author Darko Veberic \date 03 Nov 2008 \version $Id$ \ingroup stl */ template class Validated : public VValidated { public: Validated() : fIsValid(false) { } Validated(const T& value) : fIsValid(true), fValue(value) { } bool IsValid() const { return fIsValid; } void SetValid(const bool valid = true) { fIsValid = valid; } T& Get() { return fValue; } const T& Get() const { return fValue; } void Set(const T& value) { fIsValid = true; fValue = value; } Validated& operator=(const T& value) { Set(value); return *this; } private: bool fIsValid; T fValue; }; } #endif