From: <ric...@us...> - 2008-08-23 07:00:17
|
Revision: 899 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=899&view=rev Author: rich_sposato Date: 2008-08-23 07:00:13 +0000 (Sat, 23 Aug 2008) Log Message: ----------- Added pre-conditions and post-conditions. Changed name of class. Modified Paths: -------------- trunk/include/loki/Checker.h Modified: trunk/include/loki/Checker.h =================================================================== --- trunk/include/loki/Checker.h 2008-08-09 15:35:12 UTC (rev 898) +++ trunk/include/loki/Checker.h 2008-08-23 07:00:13 UTC (rev 899) @@ -4,9 +4,9 @@ // Copyright (c) 2008 Rich Sposato // The copyright on this file is protected under the terms of the MIT license. // -// Permission to use, copy, modify, distribute and sell this software for any -// purpose is hereby granted without fee, provided that the above copyright -// notice appear in all copies and that both that copyright notice and this +// Permission to use, copy, modify, distribute and sell this software for any +// purpose is hereby granted without fee, provided that the above copyright +// notice appear in all copies and that both that copyright notice and this // permission notice appear in supporting documentation. // // The author makes no representations about the suitability of this software @@ -31,14 +31,14 @@ namespace Loki { -/** @par Checker and StaticChecker Overview - The Checker and StaticChecker classes have two purposes: +/** @par ContractChecker and StaticChecker Overview + The ContractChecker and StaticChecker classes have two purposes: - provide a mechanism by which programmers can determine which functions violate class/data invariants, - and determine which exception safety a function provides. @par Class & Data Invariants - The Checker and StaticChecker utilities define invariants as "expressions that + The ContractChecker and StaticChecker define invariants as "expressions that are true for particular data". They uses a function which returns true if all data are valid, and returns false if any datum is invalid. This is called the validator function, and the host class or function provides a pointer to it. @@ -52,19 +52,21 @@ Years ago, David Abrahams formalized a framework for assessing the exception safety level a function provides. His framework describes three levels of guarantees. Any function which does not provide any of these levels is - considered unsafe. Checker and StaticChecker determine a function's safety - level through the use of policy classes. Checker's policy classes can show if - a function provides any of these three guarantees. (Caveat: Checker can't - detect leaks directly by itself, but it can call a validator which does.) - StaticChecker's policy classes only provide direct checking for the no-throw - and invariant guarantees. With some finesse, a programmer can write a - validator for StaticChecker that checks for the Strong guarantee. + considered unsafe. ContractChecker and StaticChecker determine a function's + safety level through the use of policy classes. Checker's policy classes can + show if a function provides any of these three guarantees. Since there is no + universal way to detect leaks, this facility provides no mechanism for finding + leaks, but users may create their own validators which do. StaticChecker's + policy classes only provide direct checking for the no-throw and invariant + guarantees. With some finesse, a programmer can write a validator for + StaticChecker that checks for the Strong guarantee. - No-throw guarantee: A function will not throw any exceptions. - Strong guarantee: A function will not change data if an exception occurs. - (Also called the no-change guarantee.) + (Which I call the no-change guarantee.) - Basic guarantee: A function will not leak resources and data will remain - in a valid state if an exception occurs. (Also called a no-leak guarantee.) + in a valid state if an exception occurs. (Which I call either the no-leak + or no-break guarantee depending on context.) */ // ---------------------------------------------------------------------------- @@ -72,9 +74,9 @@ /** @class CheckForNoThrow @par Exception Safety Level: - This exception-checking policy class for Checker asserts if an exception exists. - Host classes can use this to show that some member functions provide the no-throw - exception safety guarantees. + This exception-checking policy class for ContractChecker asserts if an + exception exists. Host classes can use this to show that a member function + provides the no-throw exception safety guarantees. @par Requirements For Host Class: This policy imposes no requirements on a host class. @@ -86,9 +88,11 @@ inline explicit CheckForNoThrow( const Host * ) {} - inline void Check( const Host * ) const + inline bool Check( const Host * ) const { - assert( !::std::uncaught_exception() ); + const bool okay = ( !::std::uncaught_exception() ); + assert( okay ); + return okay; } }; @@ -97,10 +101,10 @@ /** @class CheckForNoChange @par Exception Safety Level: - This exception-checking policy class for Checker asserts only if a copy of the - host differs from the host object when an exception occurs. Host classes can - use this policy to show which member functions provide the strong exception - guarantee. + This exception-checking policy class for ContractChecker asserts only if a + copy of the host differs from the host object when an exception occurs. Host + classes can use this policy to show which member functions provide the strong + exception guarantee. @par Requirements: This policy requires hosts to provide both the copy-constructor and the @@ -116,12 +120,12 @@ inline explicit CheckForNoChange( const Host * host ) : m_compare( *host ) {} - inline void Check( const Host * host ) const + inline bool Check( const Host * host ) const { - if ( ::std::uncaught_exception() ) - { - assert( m_compare == *host ); - } + const bool okay = ( !::std::uncaught_exception() ) + || ( m_compare == *host ); + assert( okay ); + return okay; } private: @@ -133,10 +137,10 @@ /** @class CheckForNoChangeOrThrow @par Exception Safety Level: - This exception-checking policy class for Checker asserts either if a copy of - the host differs from the original host object, or if an exception occurs. - Host classes can use this policy to show which member functions provide the - no-throw exception guarantee, and would never change data anyway. + This exception-checking policy class for ContractChecker asserts either if a + copy of the host differs from the original host object, or if an exception + occurs. Host classes can use this policy to show which member functions provide + the no-throw exception guarantee, and would never change data anyway. @par Requirements For Host Class: This policy requires hosts to provide both the copy-constructor and the @@ -151,10 +155,13 @@ inline explicit CheckForNoChangeOrThrow( const Host * host ) : m_compare( *host ) {} - inline void Check( const Host * host ) const + inline bool Check( const Host * host ) const { - assert( !::std::uncaught_exception() ); - assert( m_compare == *host ); + bool okay = ( !::std::uncaught_exception() ); + assert( okay ); + okay = ( m_compare == *host ); + assert( okay ); + return okay; } private: @@ -166,8 +173,7 @@ /** @class CheckForEquality @par Exception Safety Level: - This exception-checking policy class for Checker asserts only if a copy of the - host differs from the host object regardless of whether an exception occurs. + This exception-checking policy class for ContractChecker asserts if a copy of the host differs from the host object regardless of whether an exception occurs. Host classes can use this policy to show which member functions never change data members, and thereby provide the strong exception safety level by default. @@ -184,9 +190,11 @@ inline explicit CheckForEquality( const Host * host ) : m_compare( *host ) {} - inline void Check( const Host * host ) const + inline bool Check( const Host * host ) const { - assert( m_compare == *host ); + const bool okay = ( m_compare == *host ); + assert( okay ); + return okay; } private: @@ -198,10 +206,10 @@ /** @class CheckForNothing @par Exception Safety Level: - This exception-checking policy class for Checker does nothing when called. - Host classes can use this to show which member functions provide neither the - strong nor no-throw exception guarantees. The best guarantee such functions - can provide is that nothing gets leaked. + This exception-checking policy class for ContractChecker does nothing when + called. Host classes can use this to show which member functions provide + neither the strong nor no-throw exception guarantees. The best guarantee such + functions can provide is that nothing gets leaked. @par Requirements For Host Class: This policy imposes no requirements on a host class. @@ -212,15 +220,18 @@ { public: inline explicit CheckForNothing( const Host * ) {} - inline void Check( const Host * ) const {} + inline bool Check( const Host * ) const { return true; } }; // ---------------------------------------------------------------------------- -/** @class Checker - This class checks if a host class violated an invariant. This asserts if any - check for an invariant failed. It can also demonstrate which functions provide - which exception safety level. +/** @class ContractChecker + This class determines if a function violated any class invariant, but it also + determines if a function fulfills its contract with client code. In the + "Design by Contract" paradigm, each function has certain pre-conditions and + post-conditions which may differ from the class invariants. This asserts if a + check for an invariant fails as well as if any pre- or post-condition fails. + It also demonstrate which exception safety level a function provides. @par Usage -# Implement a function that checks each class invariant. The function must @@ -229,18 +240,24 @@ - The function should return true if everything is okay, but false if something is wrong. - Or it could assert if anything is wrong. + - Ideally, it should be private. + -# Implement similar functions to check for pre-conditions and post-conditions. + Functions which verify pre-conditions and post-conditions do not need to + check all class invariants, just conditions specific to certain public + functions in the host class. -# Declare some typedef's inside the class declaration like these. Make one typedef for each exception policy you use. I typedef'ed the CheckForNothing policy as CheckInvariants because even if a function can't provide either the no-throw nor the no-change policies, it should still make sure the object remains in a valid state. - - typedef ::Loki::Checker< Host, ::Loki::CheckForNoThrow > CheckForNoThrow; - - typedef ::Loki::Checker< Host, ::Loki::CheckForNoChange > CheckForNoChange; - - typedef ::Loki::Checker< Host, ::Loki::CheckForEquality > CheckForEquality; - - typedef ::Loki::Checker< Host, ::Loki::CheckForNothing > CheckInvariants; + - typedef ::Loki::ContractChecker< Host, ::Loki::CheckForNoThrow > CheckForNoThrow; + - typedef ::Loki::ContractChecker< Host, ::Loki::CheckForNoChange > CheckForNoChange; + - typedef ::Loki::ContractChecker< Host, ::Loki::CheckForEquality > CheckForEquality; + - typedef ::Loki::ContractChecker< Host, ::Loki::CheckForNothing > CheckInvariants; -# Construct a checker near the top of each member function - except in the validator member function. Pass the this pointer and the address of your - validator function into the checker's constructor. + validator function into the checker's constructor. You may also pass in pointers + to function which check pre- and post-conditions. - If the function never throws, then use the CheckForNoThrow policy. - If the function never changes any data members, then use CheckForEquality policy. @@ -248,8 +265,8 @@ data remains unchanged when any exceptions occur, then use the CheckForNoChange policy. - Otherwise use the CheckInvariants policy. - -# Recompile a debug version of your program, run it, and look for which - assertions failed. + -# Recompile a debug version of your program, run the program and all the unit + tests, and look for which assertions failed. */ template @@ -257,7 +274,7 @@ class Host, template < class > class ExceptionPolicy > -class Checker : public ExceptionPolicy< Host > +class ContractChecker : public ExceptionPolicy< Host > { /// Shorthand for the ExceptionPolicy class. typedef ExceptionPolicy< Host > Ep; @@ -269,55 +286,78 @@ /** The constructor makes sure the host is valid at the time the checker was created, thus insuring the host object was not corrupt from the start. + @par host Pointer to host object. + @par validator Pointer to function that checks class invariants. + @par pre Optional pointer to function that checks pre-conditions. + @par post Optional pointer to function that checks post-conditions. */ - inline Checker( const Host * host, Validator validator ) : + inline ContractChecker( const Host * host, Validator validator, + Validator pre = 0, Validator post = 0 ) : Ep( host ), m_host( host ), - m_validator( validator ) + m_validator( validator ), + m_pre( pre ), + m_post( post ) { - Check(); + assert( Check() ); + if ( 0 != m_pre ) + assert( ( m_host->*( m_pre ) )() ); } /** The destructor checks if any Host invariants failed, and then calls the ExceptionPolicy's Check function to determine what to do in case of an exception. */ - inline ~Checker( void ) + inline ~ContractChecker( void ) { - Check(); - Ep::Check( m_host ); + assert( Check() ); + if ( 0 != m_post ) + assert( ( m_host->*( m_post ) )() ); + assert( Ep::Check( m_host ) ); } - /** This first checks the invariants for Checker, and then calls the - validator function for the host to make sure no class invariants - were broken by the host within the Host's member function body. The - host member function can call Check directly to verify the object - remains valid at any time. + /** This first checks the invariants for ContractChecker, and then calls the + validator function for the host to make sure no class invariants were + broken by the host within the Host's member function body. The host + member function can call Check directly to verify the object remains valid + at any time. This does not care if the pre- and post-condition validator + pointers are null since a host class may pass in NULL pointers for either + to indicate the pre-conditions or post-conditions are the same as the + overall class invariants. */ - inline void Check( void ) const + inline bool Check( void ) const { assert( 0 != this ); assert( 0 != m_host ); assert( 0 != m_validator ); // Now that this confirms the pointers to the host and validation // functions are not null, go ahead and validate the host object. - assert( ( m_host->*( m_validator ) )() ); + const bool okay = ( m_host->*( m_validator ) )(); + assert( okay ); + return okay; } private: /// Default constructor is not implemented. - Checker( void ); + ContractChecker( void ); /// Copy constructor is not implemented. - Checker( const Checker & ); + ContractChecker( const ContractChecker & ); /// Copy-assignment operator is not implemented. - Checker & operator = ( const Checker & ); + ContractChecker & operator = ( const ContractChecker & ); /// Pointer to the host object. const Host * m_host; - /// Pointer to member function that checks Host object's invariants. + /// Pointer to member function that checks Host object's invariants. Validator m_validator; + + /// Pointer to member function that checks Host object's pre-conditions. + Validator m_pre; + + /// Pointer to member function that checks Host object's post-conditions. + Validator m_post; + }; // ---------------------------------------------------------------------------- @@ -332,9 +372,11 @@ class CheckStaticForNoThrow { public: - static inline void Check( void ) + inline bool Check( void ) { - assert( !::std::uncaught_exception() ); + const bool okay = !::std::uncaught_exception(); + assert( okay ); + return okay; } }; @@ -350,7 +392,7 @@ class CheckStaticForNothing { public: - static inline void Check( void ) {} + inline bool Check( void ) { return true; } }; // ---------------------------------------------------------------------------- @@ -373,15 +415,15 @@ you use. I typedef'ed the CheckForNothing policy as CheckInvariants because even if a function can't provide the no-throw guarantee, it should still make sure that static data remains in a valid state. - - typedef ::Loki::StaticChecker< ::Loki::CheckForNoThrow > CheckStaticForNoThrow; - - typedef ::Loki::StaticChecker< ::Loki::CheckForNothing > CheckStaticInvariants; + - typedef ::Loki::StaticChecker< ::Loki::CheckForNoThrow > CheckStaticForNoThrow; + - typedef ::Loki::StaticChecker< ::Loki::CheckForNothing > CheckStaticInvariants; -# Construct a checker near the top of each member function - except in the validator member function. Pass the address of your validator function into the checker's constructor. - If the function never throws, then use the CheckForNoThrow policy. - Otherwise use the CheckInvariants policy. - -# Recompile a debug version of your program, run it, and look for which - assertions failed. + -# Recompile a debug version of your program, run it, and see if an assertion + fails. */ template @@ -400,12 +442,20 @@ /** The constructor makes sure the host is valid at the time the checker was created, thus insuring the host object was not corrupt from the start. + @par validator Pointer to function that checks class invariants. + @par pre Optional pointer to function that checks pre-conditions. + @par post Optional pointer to function that checks post-conditions. */ - inline explicit StaticChecker( Validator validator ) : + inline explicit StaticChecker( Validator validator, + Validator pre = 0, Validator post = 0 ) : Ep(), - m_validator( validator ) + m_validator( validator ), + m_pre( pre ), + m_post( post ) { - Check(); + assert( Check() ); + if ( 0 != m_pre ) + assert( m_pre() ); } /** The destructor checks if any Host invariants failed, and then calls the @@ -414,22 +464,29 @@ */ inline ~StaticChecker( void ) { - Check(); - Ep::Check(); + assert( Check() ); + if ( 0 != m_post ) + assert( m_post() ); + assert( Ep::Check() ); } /** This first checks its own invariants, and then calls the validator function to make sure no invariants were broken by the function which - created this checker. That function can call Check directly to verify - the data remains valid at any time. + created this checker. That function can call Check directly to verify the + data remains valid at any time. This does not care if the pre- and post- + condition validator pointers are null since a host class may pass in NULL + pointers for either to indicate the pre-conditions or post-conditions are + the same as the overall class invariants. */ - inline void Check( void ) const + inline bool Check( void ) const { assert( 0 != this ); assert( 0 != m_validator ); // Now that this confirms the pointers to the host and validation // functions are not null, go ahead and validate the host object. - assert( ( m_validator )() ); + const bool okay = m_validator(); + assert( okay ); + return okay; } private: @@ -441,9 +498,15 @@ /// Copy-assignment operator is not implemented. StaticChecker & operator = ( const StaticChecker & ); - /// Pointer to member function that checks Host object's invariants. + /// Pointer to member function that checks Host object's invariants. Validator m_validator; + /// Pointer to member function that checks Host object's pre-conditions. + Validator m_pre; + + /// Pointer to member function that checks Host object's post-conditions. + Validator m_post; + }; // ---------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2009-09-26 19:40:28
|
Revision: 1017 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1017&view=rev Author: rich_sposato Date: 2009-09-26 19:40:20 +0000 (Sat, 26 Sep 2009) Log Message: ----------- Added ability to compare host to memento. Modified Paths: -------------- trunk/include/loki/Checker.h Modified: trunk/include/loki/Checker.h =================================================================== --- trunk/include/loki/Checker.h 2009-08-16 20:33:52 UTC (rev 1016) +++ trunk/include/loki/Checker.h 2009-09-26 19:40:20 UTC (rev 1017) @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // The Loki Library -// Copyright (c) 2008 Rich Sposato +// Copyright (c) 2008, 2009 Rich Sposato // The copyright on this file is protected under the terms of the MIT license. // // Permission to use, copy, modify, distribute and sell this software for any @@ -32,15 +32,25 @@ { /** @par ContractChecker and StaticChecker Overview - The ContractChecker and StaticChecker classes have two purposes: - - provide a mechanism by which programmers can determine which functions + ContractChecker and StaticChecker classes provide a mechanism to enforce + "Design by Contract" programming practices. According to Design by Contract, + each function and class provides a contract with client code. Each contract + says: + - what operations the class or function performs, + - what input it needs, + - what output it provides, + - what exception safety level it guarantees, and + - what constraints it enforces on its data. + + ContractChecker and StaticChecker encourage Design by Contract by: + - providing a mechanism by which programmers can determine if functions violate class/data invariants, - - and determine which exception safety a function provides. + - and determining which exception safety level a function provides. @par Class & Data Invariants - The ContractChecker and StaticChecker define invariants as "expressions that - are true for particular data". They call a function which returns true if all - data are valid, and returns false if any datum is invalid. This is called the + ContractChecker and StaticChecker define invariants as "expressions that are + true for particular data". They call a function which returns true if all data + are valid, and returns false if any datum is invalid. This is called the validator function, and the host class or function provides a pointer to it. The validator could also assert for any invariant which fails rather than return false. If the validator is a static member function, you can use it @@ -68,25 +78,76 @@ in a valid state if an exception occurs. (Which I call either the no-leak or no-break guarantee depending on context.) - @par Writing Your Own Policies + @par Provided Exception Policies + Loki provides several exception policies for use with ContractChecker. + - CheckForNoChangeOrThrow + - CheckForNoThrow + - CheckForNoChange + - CheckForEquality + - CheckForNothing + Loki also provides these two policies for StaticChecker. + - CheckStaticForNoThrow + - CheckStaticForNothing + + @par Writing Your Own Policies for ContractChecker Loki provides several exception policies for ContractChecker. These policies assert if an object changed or a function threw an exception. If you prefer - policies that log failures to a file, pop-up a message box, notify your unit- - test framework, or whatever else, you can easily write your own policies. - Your policy class should have two functions, a constructor and a Check - function, both of which accept a pointer to const instance of the host class. - Your policy class will become a base class of ContractChecker. Check should - return true if all is okay, and false if any failure was detected. Check - should never throw any exceptions since it is called from ContractChecker's - destructor. You may add other functions to the policy class. This code - snippet shows the signatures for the two required functions. + policies that log failures to a file, send an email, file a bug report, pop-up + a message box, notify your unit-test framework, or whatever else, you can + easily write your own policies. Please follow these guidelines when writing + your own policies: + - Each policy class must provide three public functions, a constructor, a destructor, and a Check function. + - The destructor could be implied. (Not actually written, and provided by the compiler.) + - The constructor and Check functions accept a pointer to const instance of the host class. + - Your policy class will become a base class of ContractChecker. + - Check should return true if all is okay, and false for any failures. + - Check should never throw any exceptions since it is called by a destructor. + - You may add other functions or features to your policy class. + + @par Using a Memento + Sometimes copying or comparing the host object is very expensive. If you do + not want ContractChecker to copy the host object, you can provide an optional + template parameter called Memento. The memento stores a little information on + the host object's state so that when the Host's function ends, ContractChecker + will use the memento to determine if the host object changed. A Host class can + declare a Memento class as an internal class. These guidelines will help you + design a memento class: + - Your exception policy's constructor should not copy the host object but use a memento to store info about the host. + - The memento should provide a constructor, an equality operator, and a destructor. + - The memento's destructor could be implied. (Not actually written, and provided by the compiler.) + - The memento's constructor and equality operator must accept a reference to a const Host object. + - The memento's equality operator should return false if the memento differs from the host. + + This code snippet shows the template parameters for an exception policy that + uses a Memento and the signatures for the three required functions for a + ContractChecker policy. @code + template < class Host, class Memento > class YourPolicy - { public: + { + public: explicit YourPolicy( const Host * ); bool Check( const Host * ) const; + ~YourPolicy(); + private: + Memento m_compare; } @endcode + + The following code snippet shows the template parameters and function + declarations for an exception policy that does not use a Memento. + @code + template < class Host > + class YourPolicy< Host, void > + { + public: + explicit YourPolicy( const Host * ); + bool Check( const Host * ) const; + ~YourPolicy(); + } + @endcode + + @par Writing Your Own Policies for StaticChecker Loki provides two exception policies for StaticChecker - one that asserts if an exception occurred, and one that does not care about exceptions. You can make your own policy to log failures, send an email, file a bug report, or do @@ -101,8 +162,37 @@ bool Check() const; } @endcode + + @par Requirements for Host Object + CheckForNoThrow and CheckForNothing impose no restrictions on the host class. + The policies for StaticChecker impose no restrictions either. All other + policies require the Host class to either provide a Memento class or have a + public copy-constructor, destructor, and equality operator. If the Host class + provides a Memento class, then follow the guidelines listed above in the + section called "Using a Memento". + + @par Writing Your Own Policies for StaticChecker + Loki provides two exception policies for StaticChecker - one that asserts if + an exception occurred, and one that does not care about exceptions. Please + follow these guidelines when writing policies: + - Each policy needs a default constructor, a destructor, and a function named Check. + - The constructor and destructor may be implied. + - Make sure your Check function never throws any exceptions. + - Any additional functions or features of the policy are up to you. + + This code snippet shows the signatures for the three required functions for a + StaticChecker policy. + @code + class YourPolicy + { public: + YourPolicy(); + bool Check() const; + ~YourPolicy(); + } + @endcode */ + // ---------------------------------------------------------------------------- /** @class CheckForNoThrow @@ -110,13 +200,16 @@ @par Exception Safety Level: This exception-checking policy class for ContractChecker asserts if an exception exists. Host classes can use this to show that a member function - provides the no-throw exception safety guarantees. + provides the no-throw exception safety guarantees. Since this policy does not + care if the host object changed, use this policy for operations which change + the host object but never throw. @par Requirements For Host Class: - This policy imposes no requirements on a host class. + This policy imposes no requirements on a host class. This ignores the Memento + template parameter. */ -template < class Host > +template < class Host, class Memento > class CheckForNoThrow { public: @@ -133,6 +226,8 @@ // ---------------------------------------------------------------------------- +template < class Host, class Memento > class CheckForNoChange; + /** @class CheckForNoChange @par Exception Safety Level: @@ -144,11 +239,10 @@ @par Requirements: This policy requires hosts to provide both the copy-constructor and the equality operator, and is intended for classes with value semantics. - equality operator. */ template < class Host > -class CheckForNoChange +class CheckForNoChange< Host, void > { public: @@ -167,22 +261,48 @@ Host m_compare; }; +template < class Host, class Memento > +class CheckForNoChange +{ +public: + + inline explicit CheckForNoChange( const Host * host ) : + m_compare( *host ) {} + + inline bool Check( const Host * host ) const + { + const bool okay = ( !::std::uncaught_exception() ) + || ( m_compare == *host ); + assert( okay ); + return okay; + } + +private: + Memento m_compare; +}; + // ---------------------------------------------------------------------------- +template < class Host, class Memento > class CheckForNoChangeOrThrow; + /** @class CheckForNoChangeOrThrow + This policy comes in two forms - one uses a memento, and one does not. The + memento form does not copy the host object, but stores info about the host in + a memento for later comparison with the host. The other form copies the host + object to a temporary and then compares that to the original. @par Exception Safety Level: This exception-checking policy class for ContractChecker asserts either if a copy of the host differs from the original host object, or if an exception occurs. Host classes can use this policy to show which member functions provide - the no-throw exception guarantee, and would never change data anyway. + the no-throw exception guarantee and never change data anyway. @par Requirements For Host Class: This policy requires hosts to provide both the copy-constructor and the equality operator, and is intended for classes with value semantics. */ -template < class Host > +template < class Host, class Memento > class CheckForNoChangeOrThrow { public: @@ -200,11 +320,34 @@ } private: + Memento m_compare; +}; + +template < class Host > +class CheckForNoChangeOrThrow< Host, void > +{ +public: + + inline explicit CheckForNoChangeOrThrow( const Host * host ) : + m_compare( *host ) {} + + inline bool Check( const Host * host ) const + { + bool okay = ( !::std::uncaught_exception() ); + assert( okay ); + okay = ( m_compare == *host ); + assert( okay ); + return okay; + } + +private: Host m_compare; }; // ---------------------------------------------------------------------------- +template < class Host, class Memento > class CheckForEquality; + /** @class CheckForEquality @par Exception Safety Level: @@ -216,9 +359,13 @@ @par Requirements For Host Class: This policy requires hosts to provide both the copy-constructor and the equality operator, and is intended for classes with value semantics. + + @par Requirements For Memento Class: + This policy requires Memento to provide a constructor and an equality operator + that accept a reference to a const host. */ -template < class Host > +template < class Host, class Memento > class CheckForEquality { public: @@ -234,6 +381,25 @@ } private: + Memento m_compare; +}; + +template < class Host > +class CheckForEquality< Host, void > +{ +public: + + inline explicit CheckForEquality( const Host * host ) : + m_compare( *host ) {} + + inline bool Check( const Host * host ) const + { + const bool okay = ( m_compare == *host ); + assert( okay ); + return okay; + } + +private: Host m_compare; }; @@ -245,13 +411,15 @@ This exception-checking policy class for ContractChecker does nothing when called. Host classes can use this to show which member functions provide neither the strong nor no-throw exception guarantees. The best guarantee such - functions can provide is that nothing gets leaked. + functions can provide is that nothing gets leaked. Use this policy for any + function that may throw exceptions and will change the host object. @par Requirements For Host Class: - This policy imposes no requirements on a host class. + This policy imposes no requirements on a host class. This ignores the Memento + template parameter. */ -template < class Host > +template < class Host, class Memento > class CheckForNothing { public: @@ -308,12 +476,13 @@ template < class Host, - template < class > class ExceptionPolicy + template < class, class > class ExceptionPolicy, + class Memento = void > -class ContractChecker : public ExceptionPolicy< Host > +class ContractChecker : public ExceptionPolicy< Host, Memento > { /// Shorthand for the ExceptionPolicy class. - typedef ExceptionPolicy< Host > Ep; + typedef ExceptionPolicy< Host, Memento > Ep; public: @@ -405,15 +574,15 @@ exception policies for ContractChecker, you might want to also write a struct similiar to CheckFor to conveniently declare all your policies. */ -template < class Host > +template < class Host, class Memento = void > struct CheckFor { // These lines declare checkers for non-static functions in a host class. - typedef ContractChecker< Host, CheckForNoChangeOrThrow > NoChangeOrThrow; - typedef ContractChecker< Host, CheckForNoThrow > NoThrow; - typedef ContractChecker< Host, CheckForNoChange > NoChange; - typedef ContractChecker< Host, CheckForEquality > Equality; - typedef ContractChecker< Host, CheckForNothing > Invariants; + typedef ContractChecker< Host, CheckForNoChangeOrThrow, Memento > NoChangeOrThrow; + typedef ContractChecker< Host, CheckForNoThrow, Memento > NoThrow; + typedef ContractChecker< Host, CheckForNoChange, Memento > NoChange; + typedef ContractChecker< Host, CheckForEquality, Memento > Equality; + typedef ContractChecker< Host, CheckForNothing, Memento > Invariants; }; // ---------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2011-10-17 19:57:41
|
Revision: 1152 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1152&view=rev Author: rich_sposato Date: 2011-10-17 19:57:36 +0000 (Mon, 17 Oct 2011) Log Message: ----------- Removed semicolon to remove compiler warning. Modified Paths: -------------- trunk/include/loki/Checker.h Modified: trunk/include/loki/Checker.h =================================================================== --- trunk/include/loki/Checker.h 2011-10-17 19:56:59 UTC (rev 1151) +++ trunk/include/loki/Checker.h 2011-10-17 19:57:36 UTC (rev 1152) @@ -773,6 +773,6 @@ // ---------------------------------------------------------------------------- -}; // end namespace Loki +} // end namespace Loki #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2011-11-04 00:53:55
|
Revision: 1171 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1171&view=rev Author: rich_sposato Date: 2011-11-04 00:53:49 +0000 (Fri, 04 Nov 2011) Log Message: ----------- Changed names to be more descriptive. Updated comments. Modified Paths: -------------- trunk/include/loki/Checker.h Modified: trunk/include/loki/Checker.h =================================================================== --- trunk/include/loki/Checker.h 2011-10-24 18:07:46 UTC (rev 1170) +++ trunk/include/loki/Checker.h 2011-11-04 00:53:49 UTC (rev 1171) @@ -60,13 +60,13 @@ - and determining which exception safety level a function provides. @par Class & Data Invariants - ContractChecker and StaticChecker define invariants as "expressions that are - true for particular data". They call a function which returns true if all data - are valid, and returns false if any datum is invalid. This is called the - validator function, and the host class or function provides a pointer to it. - The validator could also assert for any invariant which fails rather than - return false. If the validator is a static member function, you can use it - with checkers in any function, but especially standalone functions and class + ContractChecker and StaticChecker define invariants as "conditions that are + true for particular objects". They call a function which returns true if all + conditions are valid, and returns false if any condition is invalid. This is + called the validator function, and the host class or function provides a + pointer to it. The validator may assert for any invariant which fails rather + than return false. If the validator is a static member function, you can use + it with checkers in any function, but especially standalone functions and class static functions. If the validator is a non-static member function, you can use it only within non-static member functions. @@ -92,22 +92,27 @@ @par Provided Exception Policies Loki provides several exception policies for use with ContractChecker. - - CheckForNoChangeOrThrow + - CheckForNoThrowOrChange - CheckForNoThrow + - CheckForNoChangeOnThrow - CheckForNoChange - - CheckForEquality - CheckForNothing Loki also provides these two policies for StaticChecker. - CheckStaticForNoThrow - CheckStaticForNothing - @par Writing Your Own Policies for ContractChecker - Loki provides several exception policies for ContractChecker. These policies - assert if an object changed or a function threw an exception. If you prefer - policies that log failures to a file, send an email, file a bug report, pop-up - a message box, notify your unit-test framework, or whatever else, you can - easily write your own policies. Please follow these guidelines when writing - your own policies: + @par Reasons to Write Your Own Policies for ContractChecker + Loki provides several exception policies for ContractChecker that assert if an + object changed or a function threw an exception. These are good reasons to + write your own policies: + - you want to log failures to a file, + - you want to create a bug report, + - you want to stop your unit-test framework, + - your classes don't have copy-constructors or equality operators, + - or whatever else. + + @par Guidelines for Writing Your Own Policies for ContractChecker + Please follow these guidelines when writing your own policies: - Each policy class must provide three public functions, a constructor, a destructor, and a Check function. - The destructor could be implied. (Not actually written, and provided by the compiler.) - The constructor and Check functions accept a pointer to const instance of the host class. @@ -238,15 +243,16 @@ // ---------------------------------------------------------------------------- -template < class Host, class Memento > class CheckForNoChange; +template < class Host, class Memento > class CheckForNoChangeOnThrow; -/** @class CheckForNoChange +/** @class CheckForNoChangeOnThrow @par Exception Safety Level: This exception-checking policy class for ContractChecker asserts only if a copy of the host differs from the host object when an exception occurs. Host classes can use this policy to show which member functions provide the strong - exception guarantee. + exception guarantee. Such functions provide transaction semantics - either the + action succeeds or the object rollsback to its previous state. @par Requirements: This policy requires hosts to provide both the copy-constructor and the @@ -254,11 +260,11 @@ */ template < class Host > -class CheckForNoChange< Host, void > +class CheckForNoChangeOnThrow< Host, void > { public: - inline explicit CheckForNoChange( const Host * host ) : + inline explicit CheckForNoChangeOnThrow( const Host * host ) : m_compare( *host ) {} inline bool Check( const Host * host ) const @@ -274,11 +280,11 @@ }; template < class Host, class Memento > -class CheckForNoChange +class CheckForNoChangeOnThrow { public: - inline explicit CheckForNoChange( const Host * host ) : + inline explicit CheckForNoChangeOnThrow( const Host * host ) : m_compare( *host ) {} inline bool Check( const Host * host ) const @@ -295,9 +301,9 @@ // ---------------------------------------------------------------------------- -template < class Host, class Memento > class CheckForNoChangeOrThrow; +template < class Host, class Memento > class CheckForNoThrowOrChange; -/** @class CheckForNoChangeOrThrow +/** @class CheckForNoThrowOrChange This policy comes in two forms - one uses a memento, and one does not. The memento form does not copy the host object, but stores info about the host in a memento for later comparison with the host. The other form copies the host @@ -315,11 +321,11 @@ */ template < class Host, class Memento > -class CheckForNoChangeOrThrow +class CheckForNoThrowOrChange { public: - inline explicit CheckForNoChangeOrThrow( const Host * host ) : + inline explicit CheckForNoThrowOrChange( const Host * host ) : m_compare( *host ) {} inline bool Check( const Host * host ) const @@ -336,11 +342,11 @@ }; template < class Host > -class CheckForNoChangeOrThrow< Host, void > +class CheckForNoThrowOrChange< Host, void > { public: - inline explicit CheckForNoChangeOrThrow( const Host * host ) : + inline explicit CheckForNoThrowOrChange( const Host * host ) : m_compare( *host ) {} inline bool Check( const Host * host ) const @@ -358,9 +364,9 @@ // ---------------------------------------------------------------------------- -template < class Host, class Memento > class CheckForEquality; +template < class Host, class Memento > class CheckForNoChange; -/** @class CheckForEquality +/** @class CheckForNoChange @par Exception Safety Level: This exception-checking policy class for ContractChecker asserts if a copy of @@ -378,11 +384,11 @@ */ template < class Host, class Memento > -class CheckForEquality +class CheckForNoChange { public: - inline explicit CheckForEquality( const Host * host ) : + inline explicit CheckForNoChange( const Host * host ) : m_compare( *host ) {} inline bool Check( const Host * host ) const @@ -397,11 +403,11 @@ }; template < class Host > -class CheckForEquality< Host, void > +class CheckForNoChange< Host, void > { public: - inline explicit CheckForEquality( const Host * host ) : + inline explicit CheckForNoChange( const Host * host ) : m_compare( *host ) {} inline bool Check( const Host * host ) const @@ -468,18 +474,18 @@ -# Add one of these lines at the top of various class member functions to construct a checker near the top of each public function. You may also pass in pointers to functions which check pre- and post-conditions. - - CheckFor::NoChangeOrThrow checker( this, &Host::IsValid ); + - CheckFor::NoThrowOrChange checker( this, &Host::IsValid ); + - CheckFor::NoChangeOnThrow checker( this, &Host::IsValid ); - CheckFor::NoThrow checker( this, &Host::IsValid ); - CheckFor::NoChange checker( this, &Host::IsValid ); - - CheckFor::Equality checker( this, &Host::IsValid ); - CheckFor::Invariants checker( this, &Host::IsValid ); -# Use these guidelines to decide which policy to use inside which function: - If the function never throws, then use the CheckForNoThrow policy. - - If the function never changes any data members, then use CheckForEquality + - If the function never changes any data members, then use CheckForNoChange policy. - If the function's normal execution flow changes data, but must make sure data remains unchanged when any exceptions occur, then use the - CheckForNoChange policy. + CheckForNoChangeOnThrow policy. - Otherwise use the CheckInvariants policy. -# Recompile a debug version of your program, run the program and all the unit tests, and look for which assertions failed. @@ -569,10 +575,10 @@ /// Pointer to member function that checks Host object's invariants. Validator m_validator; - /// Pointer to member function that checks Host object's pre-conditions. + /// Pointer to member function that checks Host function's pre-conditions. Validator m_pre; - /// Pointer to member function that checks Host object's post-conditions. + /// Pointer to member function that checks Host function's post-conditions. Validator m_post; }; @@ -590,10 +596,10 @@ struct CheckFor { // These lines declare checkers for non-static functions in a host class. - typedef ContractChecker< Host, CheckForNoChangeOrThrow, Memento > NoChangeOrThrow; + typedef ContractChecker< Host, CheckForNoThrowOrChange, Memento > NoThrowOrChange; + typedef ContractChecker< Host, CheckForNoChangeOnThrow, Memento > NoChangeOnThrow; typedef ContractChecker< Host, CheckForNoThrow, Memento > NoThrow; typedef ContractChecker< Host, CheckForNoChange, Memento > NoChange; - typedef ContractChecker< Host, CheckForEquality, Memento > Equality; typedef ContractChecker< Host, CheckForNothing, Memento > Invariants; }; @@ -745,10 +751,10 @@ /// Pointer to member function that checks Host object's invariants. Validator m_validator; - /// Pointer to member function that checks Host object's pre-conditions. + /// Pointer to member function that checks Host function's pre-conditions. Validator m_pre; - /// Pointer to member function that checks Host object's post-conditions. + /// Pointer to member function that checks Host function's post-conditions. Validator m_post; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2011-11-07 23:35:34
|
Revision: 1174 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1174&view=rev Author: rich_sposato Date: 2011-11-07 23:35:28 +0000 (Mon, 07 Nov 2011) Log Message: ----------- Added typedef for Validator function types. Modified Paths: -------------- trunk/include/loki/Checker.h Modified: trunk/include/loki/Checker.h =================================================================== --- trunk/include/loki/Checker.h 2011-11-07 23:31:36 UTC (rev 1173) +++ trunk/include/loki/Checker.h 2011-11-07 23:35:28 UTC (rev 1174) @@ -601,6 +601,8 @@ typedef ContractChecker< Host, CheckForNoThrow, Memento > NoThrow; typedef ContractChecker< Host, CheckForNoChange, Memento > NoChange; typedef ContractChecker< Host, CheckForNothing, Memento > Invariants; + + typedef bool ( Host:: * Validator )( void ) const; }; // ---------------------------------------------------------------------------- @@ -775,6 +777,7 @@ typedef StaticChecker< CheckStaticForNoThrow > NoThrow; typedef StaticChecker< CheckStaticForNothing > Invariants; + typedef bool ( * Validator )( void ); }; // ---------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2012-04-02 05:49:27
|
Revision: 1178 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1178&view=rev Author: rich_sposato Date: 2012-04-02 05:49:21 +0000 (Mon, 02 Apr 2012) Log Message: ----------- Put braces around assert statement. Modified Paths: -------------- trunk/include/loki/Checker.h Modified: trunk/include/loki/Checker.h =================================================================== --- trunk/include/loki/Checker.h 2011-11-07 23:56:57 UTC (rev 1177) +++ trunk/include/loki/Checker.h 2012-04-02 05:49:21 UTC (rev 1178) @@ -524,7 +524,9 @@ { assert( Check() ); if ( 0 != m_pre ) + { assert( ( m_host->*( m_pre ) )() ); + } } /** The destructor checks if any Host invariants failed, and then calls the @@ -535,7 +537,9 @@ { assert( Check() ); if ( 0 != m_post ) + { assert( ( m_host->*( m_post ) )() ); + } assert( Ep::Check( m_host ) ); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |