00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
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             
00077             
00078 
00084             TInvariant (const char* pkcMESSAGE, int iCONFORMANCE_LEVEL) :
00085               yMessage          (pkcMESSAGE)                            ,
00086               iConformanceLevel (iCONFORMANCE_LEVEL)                    {}
00087 
00088         };  
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         
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         
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           
00235           
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     };  
00258 
00259   }  
00260 
00261 }  
00262 
00263   
00264 #endif  // not _MPCL_INVARIANT_VERIFIER__