Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

verifier.hh

00001 /*
00002 *  Name:      verifier.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Invariant Base verifier
00005 *  Date:      $Date: 2003/08/08 23:30:33 $
00006 *  Revision:  $Revision: 1.2 $
00007 *
00008 *  Copyright (C) 1994-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _MPCL_INVARIANT_VERIFIER__
00027 #define _MPCL_INVARIANT_VERIFIER__
00028 
00029 #include <cstdarg>
00030 #include "../text/string.hh"
00031 #include "../util/collection/map.hh"
00032 #include "../util/variable_argument_array.hh"
00033 #include "defs.hh"
00034 
00035 
00037 namespace mpcl
00038 {
00039 
00041   namespace invariant
00042   {
00043 
00044     using text::TString;
00045     using util::collection::TMap;
00046     using util::TVariableArgumentArray;
00047 
00054     template <typename TParameter>
00055     class IVerifier
00056     {
00057 
00058       protected:
00059 
00061         class TInvariant
00062         {
00063 
00064           public:
00065 
00067             TString   yMessage;
00068 
00070             int   iConformanceLevel;
00071 
00072 
00073           public:
00074 
00075             //
00076             //  C O N S T R U C T O R S
00077             //
00078 
00084             TInvariant (const char* pkcMESSAGE, int iCONFORMANCE_LEVEL) :
00085               yMessage          (pkcMESSAGE)                            ,
00086               iConformanceLevel (iCONFORMANCE_LEVEL)                    {}
00087 
00088         };  // class TInvariant
00089 
00090 
00091       protected:
00092 
00094         int   iDefaultConformanceLevel;
00095 
00096 
00097       public:
00098 
00103         TMap<long int, TInvariant>   tInvariantMap;
00104 
00109         mutable TVariableArgumentArray<TParameter>   tArgumentArray;
00110 
00115         TConformanceLevelSet   tConformanceLevelSet;
00116 
00117 
00118       public:
00119 
00120         //
00121         //  C O N S T R U C T O R S
00122         //
00123 
00128         IVerifier (int iDEFAULT_CONFORMANCE_LEVEL)              :
00129           iDefaultConformanceLevel (iDEFAULT_CONFORMANCE_LEVEL) ,
00130           tInvariantMap        ()                               ,
00131           tArgumentArray       ()                               ,
00132           tConformanceLevelSet ()                               {}
00133 
00135         virtual ~IVerifier (void) {}
00136 
00143         void addInvariant ( long int    liINVARIANT_IDENTIFIER ,
00144                             int         iCONFORMANCE_LEVEL     ,
00145                             const char* pkcMESSAGE             )
00146         {
00147           tInvariantMap.bind ( liINVARIANT_IDENTIFIER                      ,
00148                                TInvariant (pkcMESSAGE, iCONFORMANCE_LEVEL) );
00149         }
00150 
00156         void addInvariant (long int liINVARIANT_IDENTIFIER, const char* pkcMESSAGE)
00157         {
00158           tInvariantMap.bind ( liINVARIANT_IDENTIFIER                            ,
00159                                TInvariant (pkcMESSAGE, iDefaultConformanceLevel) );
00160         }
00161 
00166         void addConformanceLevel (int iCONFORMANCE_LEVEL)
00167         {
00168           if ( tConformanceLevelSet.end() == tConformanceLevelSet.find (iCONFORMANCE_LEVEL) )
00169           {
00170             tConformanceLevelSet.insert (iCONFORMANCE_LEVEL);
00171           }
00172         }
00173 
00178         void removeConformanceLevel (int iCONFORMANCE_LEVEL)
00179         {
00180           if ( tConformanceLevelSet.end() != tConformanceLevelSet.find (iCONFORMANCE_LEVEL) )
00181           {
00182             tConformanceLevelSet.erase (iCONFORMANCE_LEVEL);
00183           }
00184         }
00185 
00187         void clearConformanceLevelSet (void)
00188         {
00189           tConformanceLevelSet.clear();
00190         }
00191 
00192 
00194         void resetConformanceLevelSet (void)
00195         {
00196           tConformanceLevelSet.erase();
00197           tConformanceLevelSet.insert (iDefaultConformanceLevel);
00198         }
00199 
00200 
00201       public:
00202 
00203         //
00204         //  S E L E C T O R S
00205         //
00206 
00214         void require (long int liINVARIANT_IDENTIFIER...) const
00215         {
00216           va_list   tVa_list;
00217 
00218           va_start (tVa_list, liINVARIANT_IDENTIFIER);
00219           requireList (liINVARIANT_IDENTIFIER, tVa_list);
00220           va_end (tVa_list);
00221         }
00222 
00229         void requireList (long int liINVARIANT_IDENTIFIER, va_list tVA_LIST) const
00230         {
00231           const TInvariant&   rktInvariant (tInvariantMap [liINVARIANT_IDENTIFIER]);
00232 
00233           //
00234           //  Only checks invariants that belong to an active
00235           //  conformance-level.
00236           //
00237           tArgumentArray.initialize (tVA_LIST);
00238           if ( tConformanceLevelSet.end() != tConformanceLevelSet.find (rktInvariant.iConformanceLevel) )
00239           {
00240             if ( !verify (liINVARIANT_IDENTIFIER) )
00241             {
00242               va_end (tVA_LIST);
00243               throw TViolationException ( rktInvariant.iConformanceLevel , 
00244                                           rktInvariant.yMessage.c_str()  );
00245             }
00246           }
00247         }
00248 
00255         virtual bool verify (long int liINVARIANT_IDENTIFIER) const = 0;
00256 
00257     };  // class IVerifier
00258 
00259   }  // namespace invariant
00260 
00261 }  // namespace mpcl
00262 
00263   
00264 #endif  // not _MPCL_INVARIANT_VERIFIER__

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18