VR-Forces 4.0.4 Class Documentation
include/makSettingsManager/settingsManager.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002 ** Copyright (c) 2008 MAK Technologies, Inc.
00003 ** All rights reserved.
00004 *******************************************************************************/
00005 /*******************************************************************************
00006 ** $RCSfile: settingsManager.h,v $ $Revision: 1.4 $ $State: Exp $
00007 *******************************************************************************/
00008 
00009 // \file settingsManager.h
00010 // \brief Manages settings saved out in an XML file
00011 // The DtSettingsManager contains DtSettingsObjects which are seralized into
00012 // a settings file using boost::serialize. This manager is not a data storage
00013 // class -- it simply maintains a copy on behalf of a client which provides
00014 // a setting. Such clients are called producers and provide functions which
00015 // the manager can use to get the latest value. Values are only updated upon
00016 // lookup and immediately before the file is written out to disk. Producers
00017 // are not required -- independent settings can also be added to the manager.
00018 // Changes to settings objects can be tracked by registering as a consumer
00019 // for a given object.
00020 
00021 #ifndef settingsManager_H_
00022 #define settingsManager_H_
00023 
00024 #include "makSettingsManager/settingsObject.h"
00025 
00026 #include "vlutil/vlFilename.h"
00027 
00028 #include <map>
00029 
00030 class DtSettingsObjectFactory;
00031 
00032 // Definition for producer functions which are used with registerProducer
00033 typedef void (*DtSettingsProducerFcn)(DtSettingsObject* object, void* usr);
00034 
00035 // Holds producer details
00036 struct DtSettingsProducer
00037 {
00038    std::string type;
00039    DtSettingsProducerFcn producerFcn;
00040    void* usr;
00041 };
00042 
00043 // Definition for map of producers
00044 typedef std::map<std::string, DtSettingsProducer> DtSettingsProducersMap;
00045 
00046 // Definition for comsumer functions which are used with registerConsumer
00047 typedef void (*DtSettingsConsumerFcn)(const DtSettingsObject* object, 
00048    void* usr);
00049 
00050 // Holds consumer details
00051 struct DtSettingsConsumer
00052 {
00053    DtSettingsConsumerFcn consumerFcn;
00054    void* usr;
00055 };
00056 
00057 // Definition for multi-map of consumers
00058 typedef std::multimap<std::string, DtSettingsConsumer>
00059    DtSettingsConsumersMultiMap;
00060 
00061 // Definition for map of DtSettingsObjects
00062 typedef std::map<std::string, DtSettingsObject*> DtSettingsMap ;
00063 
00064 #include "makSettingsManager/makSettingsManagerDefines.h"
00065 class DT_DLL_makSettingsManager DtSettingsManager
00066 {
00067 public:   
00068    // Constructor
00069    DtSettingsManager(std::string filename);
00070  
00071    // Destructor
00072    ~DtSettingsManager();
00073 
00074    // Calls read()
00075    virtual void init();
00076    
00077    // Opens and parses the settings file
00078    virtual bool read();
00079    
00080    // Updates all settings objects via their associated producer functions and
00081    // then writes out the settings file.
00082    virtual bool write();
00083 
00084    // DtSettingsObjectFactory
00086    static DtSettingsObjectFactory* factory();
00087    static void setFactory(DtSettingsObjectFactory* factory);
00089    
00090    // \brief Returns a pointer to the named settings object from the map.
00091    // If the object does not exist but a producer has been registered for it,
00092    // a new instance of the object will be created and returned.
00093    // If the object has been read in from the settings file but does not yet 
00094    // have a producer associated with it, its value will still be returned.
00095    // If the object does not exist in the map and there is no producer
00096    // associated with it, null (0) is returned.
00097    // Note that the return type is const because clients should only write 
00098    // settings data via their DtSettingsProducerFcn which they register with
00099    // registerProducer. 
00100    virtual const DtSettingsObject* lookupSetting(std::string name);
00101 
00102    // Updates an existing DtSettingsObject from the one provided by using
00103    // its setFrom method. If there is no DtSettingsObject in the DtSettingsMap
00104    // whose name corresponds to the name of the object provided, or if their
00105    // types do not match, false is returned and the setting is left unchanged.
00106    virtual bool setSetting(const DtSettingsObject* settingsObject);
00107 
00108    // Returns the key list of the settings map.
00109    virtual std::list<std::string>  settingsMapKeys();
00110 
00111    // \brief Registers a producer function as the source for the given setting
00112    // Associates the given DtSettingsProducerFcn with the given settings name.
00113    // All three arguments are copied into a DtSettingsProducer which is stored
00114    // in myProducersMap.
00115    // Whenever lookupSetting is called, this function will be used to update
00116    // the settings object with the most current data from the producer before
00117    // it is returned to the caller. If a producer with the same name is
00118    // already registered in myProducersMap, the associated settings object
00119    // in mySettingsMap is no longer considered valid and will be deleted.
00120    virtual void registerProducer(std::string type, std::string name,
00121       DtSettingsProducerFcn producerFcn, void* usr = 0);
00122    virtual void unregisterProducer(std::string type, std::string name,
00123       DtSettingsProducerFcn producerFcn, void* usr = 0);
00124 
00125    // DtSettingsConsumerFcn's are called whenever a named DtSettingsObject
00126    // changes. The arguments are copied into a DtSettingsConsumer used to
00127    // update the myConsumersMultiMap. 
00128    // @{
00129    virtual void registerConsumer(std::string name,
00130       DtSettingsConsumerFcn consumerFcn, void* usr = 0);  
00131    virtual void unregisterConsumer(std::string name,
00132       DtSettingsConsumerFcn consumerFcn, void* usr = 0);
00133    // @}
00134 
00135    // Registers an independent setting that does not have an associated
00136    // producer function. Independent settings are generally only used when
00137    // a given setting will never be associated with other settings grouped
00138    // into a subclass of DtSettingsObject.
00139    // @{
00140    virtual void addIndependentBool(std::string name, bool value);
00141    virtual void addIndependentInt(std::string name, int value);
00142    virtual void addIndependentDouble(std::string name, double value);
00143    virtual void addIndependentString(std::string name, std::string value);
00144    // @}
00145 
00146 protected:
00147    // Settings file to read/write
00148    std::string myFilename;
00149    
00150    // Map of DtSettingsObjects
00151    DtSettingsMap mySettingsMap;
00152    
00153    // Map of DtSettingsProducers
00154    DtSettingsProducersMap myProducersMap;
00155    
00156    // Map of DtSettingsConsumers
00157    DtSettingsConsumersMultiMap myConsumersMultiMap;
00158    
00159    // DtSettingsObjectFactory
00160    static DtSettingsObjectFactory* theSettingsObjectFactory;
00161 };
00162 
00163 #endif

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)