CSettingsInterface.h

Go to the documentation of this file.
00001 
00036 #pragma once
00037 
00038 #include <Windows.h>
00039 #include "SettingsInterface.h"
00040 
00041 // STL
00042 #include <algorithm>
00043 #include <map>
00044 #include <string>
00045 #include <vector>
00046 
00047 // Templated helper class to store string - parameter mappings, access attributes and allowed values
00048 template <class T>
00049 class RtvcParameterValue
00050 {
00051 public:
00052         RtvcParameterValue()
00053         {;}
00054 
00055         ~RtvcParameterValue()
00056         {;}
00057 
00058         bool contains(const std::string& sName)
00059         {
00060                 return (m_mAddresses.find(sName) != m_mAddresses.end());
00061         }
00062 
00063         std::vector<std::string> getParameterNames()
00064         {
00065                 std::vector<std::string> vResults;
00066                 for (std::map<std::string, T*>::iterator it = m_mAddresses.begin(); it != m_mAddresses.end(); it++)
00067                 {
00068                         vResults.push_back(it->first);
00069                 }
00070                 return vResults;
00071         }
00072 
00073         T* getParamAddress(const std::string& sName)
00074         {
00075                 std::map<std::string, T*>::iterator it = m_mAddresses.find(sName);
00076                 if (it != m_mAddresses.end())
00077                 {
00078                         return it->second;
00079                 }
00080                 else
00081                 {
00082                         return NULL;
00083                 }
00084         }
00085 
00086         bool setParameterValue(const std::string& sName, T newValue)
00087         {
00088                 std::map<std::string, std::vector<T> >::iterator it = m_mAllowedValues.find(sName);
00089                 if (it != m_mAllowedValues.end())
00090                 {
00091                         // If access through this interface is read only then exit
00092                         if (m_mReadOnly[sName] == true)
00093                         {
00094                                 return false;
00095                         }
00096 
00097                         // Get allowed values if they exist
00098                         std::vector<T> vAllowedValues = it->second;
00099                         if (vAllowedValues.size() != 0)
00100                         {
00101                                 // Only these values are allowed
00102                                 std::vector<T>::iterator it = std::find(vAllowedValues.begin(), vAllowedValues.end(), newValue);
00103                                 if (it != vAllowedValues.end())
00104                                 {
00105                                         // Get pointer to address
00106                                         T* pValue = getParamAddress(sName);
00107                                         // Store previous value
00108                                         m_mPreviousValues[sName] = *pValue;
00109                                         *pValue = newValue;
00110                                         return true;
00111                                 }
00112                                 else
00113                                 {
00114                                         // Allowed values does not contain value
00115                                         return false;
00116                                 }
00117                         }
00118                         else
00119                         {
00120                                 T* pValue = getParamAddress(sName);
00121                                 // Store previous value
00122                                 m_mPreviousValues[sName] = *pValue;
00123                                 *pValue = newValue;
00124                                 return true;
00125                         }
00126                 }
00127                 else
00128                 {
00129                         return false;
00130                 }               
00131         }
00132 
00133         // Reverts the parameters value back to the previous one if it exists in the map
00134         // Parameters get stored in the previousmap on setting
00135         bool revertToPreviousValue(const std::string& sName)
00136         {
00137                 std::map<std::string, T>::iterator it = m_mPreviousValues.find(sName);
00138                 if (it != m_mPreviousValues.end())
00139                 {
00140                         // We found the previous value
00141                         // Get pointer to address
00142                         T* pValue = getParamAddress(sName);
00143                         *pValue = it->second;
00144                         return true;
00145                 }
00146                 else
00147                 {
00148                         return false;
00149                 }
00150         }
00151 
00152         // This method should be called by child classes to initialise the parameter
00153         void addParam(const std::string& sName, T* pAddr, T defaultValue, bool bReadOnly, std::vector<T>& vAllowedValues)
00154         {
00155                 // Check if it exists already
00156                 if (m_mAddresses.find(sName) == m_mAddresses.end())
00157                 {
00158                         m_mAddresses[sName] = pAddr;
00159                         m_mAllowedValues[sName] = vAllowedValues; 
00160                         m_mReadOnly[sName] = bReadOnly;
00161                         // Set initial value
00162                         *pAddr = defaultValue;
00163                 }
00164         }
00165 
00166         int getSize(){ return m_vAddresses.size();}
00167 private:
00168         std::map<std::string, T*> m_mAddresses;
00169         std::map<std::string, T> m_mPreviousValues;
00170         std::map<std::string, bool> m_mReadOnly;
00171         std::map<std::string, std::vector<T> > m_mAllowedValues;
00172 };
00173 
00174 class CSettingsInterface :
00175         public ISettingsInterface
00176 {
00177 public:
00178         CSettingsInterface(void);
00179         virtual ~CSettingsInterface(void);
00185         virtual bool revertParameter(const char* szParamName);
00186 
00194         STDMETHODIMP GetParameter( const char* szParamName, int nBufferSize, char* szValue, int* pLength);
00200         STDMETHODIMP SetParameter( const char* szParamName, const char* szValue);
00206         STDMETHODIMP GetParameterSettings(char* szResult, int nSize);
00207 
00208 protected:
00216         virtual void initParameters() = 0; 
00217 
00224         virtual void addParameter(const char* szParamName, int* pAddr, int nDefaultValue, bool bReadOnly = false, std::vector<int> vAllowedValues = std::vector<int>());
00231         virtual void addParameter(const char* szParamName, std::string* pAddr, std::string szDefaultValue, bool bReadOnly= false, std::vector<std::string> vAllowedValues = std::vector<std::string>());
00238         virtual void addParameter(const char* szParamName, bool* pAddr, bool bDefaultValue, bool bReadOnly = false, std::vector<bool> vAllowedValues = std::vector<bool>());
00245         virtual void addParameter(const char* szParamName, double* pAddr, double dDefaultValue, bool bReadOnly = false, std::vector<double> vAllowedValues = std::vector<double>());
00246 
00247 private:
00248 
00249         enum RTVC_PARAM_TYPES
00250         {
00251                 RTVC_STRING,
00252                 RTVC_INT,
00253                 RTVC_BOOL,
00254                 RTVC_DOUBLE
00255         };
00256 
00257         typedef std::map<std::string, CSettingsInterface::RTVC_PARAM_TYPES> PARAM_TYPE_MAP;
00258 
00259 
00260         std::vector<std::string> m_vParameters;
00261         
00262         RtvcParameterValue<int> m_intParams;
00263         RtvcParameterValue<double> m_doubleParams;
00264         RtvcParameterValue<bool> m_boolParams;
00265         RtvcParameterValue<std::string> m_stringParams;
00266 
00267         PARAM_TYPE_MAP m_mParamTypes;
00268 };
00269 

Generated on Fri Mar 13 14:12:38 2009 for RTVC by  doxygen 1.5.3