AaronCameron.net
What's your point?
Not a Member? - Login or Create an Account...MC Offline
Tuesday the 21st of May 2013 @ 11:16pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cConfigurationTemplate.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 
00026 #include "cConfigurationTemplate.h"
00027 #include "cConfParsingErrors.h"
00028 #include "cConfParsingError.h"
00029 #include "cConfFailedValidationException.h"
00030 
00031 namespace n2l
00032 {
00033     /* ************************************************************ */
00034     cConfigurationTemplate::cConfigurationTemplate() :
00035         mAllowUnvalidatedKeys(true)
00036     {
00037     }
00038 
00039 
00040     /* ************************************************************ */
00041     cConfigurationTemplate::~cConfigurationTemplate()
00042     {
00043     }
00044 
00045 
00046     /* ************************************************************ */
00047     const cConfigurationTemplate::tConstIterator cConfigurationTemplate::begin() const
00048     {
00049         return mValidators.begin();
00050     }
00051 
00052 
00053     /* ************************************************************ */
00054     const cConfigurationTemplate::tIterator cConfigurationTemplate::begin()
00055     {
00056         return mValidators.begin();
00057     }
00058 
00059 
00060     /* ************************************************************ */
00061     const cConfigurationTemplate::tConstIterator cConfigurationTemplate::end() const
00062     {
00063         return mValidators.end();
00064     }
00065 
00066 
00067     /* ************************************************************ */
00068     const cConfigurationTemplate::tIterator cConfigurationTemplate::end()
00069     {
00070         return mValidators.end();
00071     }
00072 
00073     /* ************************************************************ */
00074     void cConfigurationTemplate::validate( cConfiguration & ioConfig ) const
00075     {
00076         static const cConfValidatorNoValidator * const NoValidatorValidator =
00077                                                         new cConfValidatorNoValidator();
00078         cConfParsingErrors errors;
00079 
00080         // Prefill all defaults
00081         applyDefaults( ioConfig );
00082 
00083         // Check values one at a time, keying by the validator
00084         for (tConstIterator validator = begin(); validator!=end(); ++validator) {
00085             cConfiguration::tConstIterator confPair = ioConfig.find(validator->first);
00086             if (confPair==ioConfig.end()) {
00087                 tRequiredContainer::const_iterator requirement = mRequired.find(validator->first);
00088                 if (requirement->second == Requirement_Required)
00089                     errors.add( cConfParsingError(  cConfParsingError::ErrorType_ValidationFailed,
00090                                                     "Missing required value",
00091                                                     validator->first ) );
00092             } else {
00093                 // Validate it.
00094                 const cConfValidatorInterface * failedValidation;
00095                 if ((failedValidation=validator->second->valueOk(confPair->second)))
00096                     errors.add( cConfParsingError(  cConfParsingError::ErrorType_ValidationFailed,
00097                                                     failedValidation->describe(),
00098                                                     validator->first,
00099                                                     confPair->second ) );
00100             }
00101         }
00102         // Reverse scan the entries against the validator if optional keys are
00103         // not allowed
00104         if (!allowUnvalidatedKeys()) {
00105             for (cConfiguration::tConstIterator setting = ioConfig.begin();
00106                 setting!=ioConfig.end(); ++setting)
00107                 if (!isset(setting->first)) 
00108                     errors.add( cConfParsingError(  cConfParsingError::ErrorType_ValidationFailed,
00109                                                     NoValidatorValidator->describe(),
00110                                                     setting->first,
00111                                                     setting->second ) );
00112         }
00113         if (!errors.empty())
00114             throw cConfFailedValidationException( errors );
00115     }
00116 
00117 
00118     /* ************************************************************ */
00119     void cConfigurationTemplate::applyDefaults( cConfiguration & ioConfig ) const
00120     {
00121         for (tDefaultsContainer::const_iterator defaultIt = mDefaults.begin();
00122             defaultIt!=mDefaults.end(); ++defaultIt)
00123             if (!ioConfig.isSet(defaultIt->first))
00124                 ioConfig[defaultIt->first] = defaultIt->second;
00125     }
00126 
00127 
00128     /* ************************************************************ */
00129     void cConfigurationTemplate::addPair(   const tString & iKey,
00130                                             const cAutoPtr<cConfValidatorInterface> iValidator,
00131                                             const tBool iValueRequired )
00132     {
00133         mValidators[iKey] = iValidator;
00134         if (iValueRequired)
00135             mRequired[iKey] = Requirement_Required;
00136         else mRequired[iKey] = Requirement_Optional;
00137     }
00138 
00139 
00140     /* ************************************************************ */
00141     void cConfigurationTemplate::addPairWithDefault(const tString & iKey,
00142                                                     const cAutoPtr<cConfValidatorInterface> iValidator,
00143                                                     const cDynVar & iDefault )
00144     {
00145         mValidators[iKey] = iValidator;
00146         mRequired[iKey] = Requirement_Override;
00147         mDefaults[iKey] = iDefault;
00148     }
00149 
00150 
00151     /* ************************************************************ */
00152     const cConfValidatorInterface * const cConfigurationTemplate::pairIsOk(
00153                                                     const tString & iName,
00154                                                     const cDynVar & iValue ) const
00155     {
00156         static const cConfValidatorNoValidator * const NoValidatorValidator =
00157                                                         new cConfValidatorNoValidator();
00158         tConstIterator validator = mValidators.find( iName );
00159         if (validator==mValidators.end()) {
00160             if (!allowUnvalidatedKeys())
00161                 return NoValidatorValidator;
00162             return 0;
00163         }
00164         return validator->second->valueOk( iValue );
00165     }
00166     
00167 
00168     /* ************************************************************ */
00169     const tBool cConfigurationTemplate::isset( const tString & iName ) const
00170     {
00171         tConstIterator validator = mValidators.find( iName );
00172         return (validator!=end());
00173     }
00174 
00175 
00176     /* ************************************************************ */
00177     const tBool cConfigurationTemplate::empty() const
00178     {
00179         return mValidators.empty();
00180     }
00181 
00182 
00183     /* ************************************************************ */
00184     const cConfigurationTemplate::tSize cConfigurationTemplate::size() const
00185     {
00186         return mValidators.size();
00187     }
00188 
00189 
00190     /* ************************************************************ */
00191     void cConfigurationTemplate::clear()
00192     {
00193         return mValidators.clear();
00194     }
00195 
00196 
00197     /* ************************************************************ */
00198     void cConfigurationTemplate::setUnvalidatedKeysAllowed( const tBool iAllowed )
00199     {
00200         mAllowUnvalidatedKeys = iAllowed;
00201     }
00202 
00203 
00204     /* ************************************************************ */
00205     const tBool cConfigurationTemplate::allowUnvalidatedKeys() const
00206     {
00207         return mAllowUnvalidatedKeys;
00208     }
00209 
00210 
00211     /* ************************************************************ */
00212     const tString cConfigurationTemplate::dump() const
00213     {
00214         tString temp("<configuration template>\n");
00215         tConstIterator validator = begin();
00216         const tConstIterator EndValidator = end();
00217         for (; EndValidator!=validator; ++validator)
00218             temp += (tString("\t") + validator->first + " =\t" +
00219                 validator->second->describe() + "\n");
00220         temp += "</configuration template>";
00221         return temp;
00222     }
00223 
00224 
00225     /* ************************************************************ */
00226     cConfigurationTemplate::cConfValidatorNoValidator::~cConfValidatorNoValidator()
00227     {
00228     }
00229 
00230 
00231     /* ************************************************************ */
00232     const cConfValidatorInterface * const cConfigurationTemplate::cConfValidatorNoValidator::valueOk(
00233                                                     const cDynVar & ) const
00234     {
00235         // This is a meta-validator of sorts, it doesnt actually check
00236         // anything, so just return false;
00237         return this;
00238     }
00239 
00240 
00241     /* ************************************************************ */
00242     const tString cConfigurationTemplate::cConfValidatorNoValidator::describe() const
00243     {
00244         static const tString Message("No validator for this key");
00245         return Message;
00246     }
00247 
00248 }
©2013 Aaron Cameron