VR-Forces 4.1 Class Documentation
settingsManager.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2008 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 /*******************************************************************************
6 ** $RCSfile: settingsManager.h,v $ $Revision: 1.4 $ $State: Exp $
7 *******************************************************************************/
8 
9 // \file settingsManager.h
10 // \brief Manages settings saved out in an XML file
11 // The DtSettingsManager contains DtSettingsObjects which are seralized into
12 // a settings file using boost::serialize. This manager is not a data storage
13 // class -- it simply maintains a copy on behalf of a client which provides
14 // a setting. Such clients are called producers and provide functions which
15 // the manager can use to get the latest value. Values are only updated upon
16 // lookup and immediately before the file is written out to disk. Producers
17 // are not required -- independent settings can also be added to the manager.
18 // Changes to settings objects can be tracked by registering as a consumer
19 // for a given object.
20 
21 #ifndef settingsManager_H_
22 #define settingsManager_H_
23 
25 
26 #include "vlutil/vlFilename.h"
27 
28 #include <map>
29 
31 
32 // Definition for producer functions which are used with registerProducer
33 typedef void (*DtSettingsProducerFcn)(DtSettingsObject* object, void* usr);
34 
35 // Holds producer details
37 {
38  std::string type;
40  void* usr;
41 };
42 
43 // Definition for map of producers
44 typedef std::map<std::string, DtSettingsProducer> DtSettingsProducersMap;
45 
46 // Definition for comsumer functions which are used with registerConsumer
47 typedef void (*DtSettingsConsumerFcn)(const DtSettingsObject* object,
48  void* usr);
49 
50 // Holds consumer details
52 {
54  void* usr;
55 };
56 
57 // Definition for multi-map of consumers
58 typedef std::multimap<std::string, DtSettingsConsumer>
60 
61 // Definition for map of DtSettingsObjects
62 typedef std::map<std::string, DtSettingsObject*> DtSettingsMap ;
63 
66 {
67 public:
68  // Constructor
69  DtSettingsManager(std::string filename);
70 
71  // Destructor
73 
74  // Calls read()
75  virtual void init();
76 
77  // Opens and parses the settings file
78  virtual bool read();
79 
80  // Updates all settings objects via their associated producer functions and
81  // then writes out the settings file.
82  virtual bool write();
83 
84  // DtSettingsObjectFactory
86  static DtSettingsObjectFactory* factory();
87  static void setFactory(DtSettingsObjectFactory* factory);
89 
90  // \brief Returns a pointer to the named settings object from the map.
91  // If the object does not exist but a producer has been registered for it,
92  // a new instance of the object will be created and returned.
93  // If the object has been read in from the settings file but does not yet
94  // have a producer associated with it, its value will still be returned.
95  // If the object does not exist in the map and there is no producer
96  // associated with it, null (0) is returned.
97  // Note that the return type is const because clients should only write
98  // settings data via their DtSettingsProducerFcn which they register with
99  // registerProducer.
100  virtual const DtSettingsObject* lookupSetting(std::string name);
101 
102  // Updates an existing DtSettingsObject from the one provided by using
103  // its setFrom method. If there is no DtSettingsObject in the DtSettingsMap
104  // whose name corresponds to the name of the object provided, or if their
105  // types do not match, false is returned and the setting is left unchanged.
106  virtual bool setSetting(const DtSettingsObject* settingsObject);
107 
108  // Returns the key list of the settings map.
109  virtual std::list<std::string> settingsMapKeys();
110 
111  // \brief Registers a producer function as the source for the given setting
112  // Associates the given DtSettingsProducerFcn with the given settings name.
113  // All three arguments are copied into a DtSettingsProducer which is stored
114  // in myProducersMap.
115  // Whenever lookupSetting is called, this function will be used to update
116  // the settings object with the most current data from the producer before
117  // it is returned to the caller. If a producer with the same name is
118  // already registered in myProducersMap, the associated settings object
119  // in mySettingsMap is no longer considered valid and will be deleted.
120  virtual void registerProducer(std::string type, std::string name,
121  DtSettingsProducerFcn producerFcn, void* usr = 0);
122  virtual void unregisterProducer(std::string type, std::string name,
123  DtSettingsProducerFcn producerFcn, void* usr = 0);
124 
125  // DtSettingsConsumerFcn's are called whenever a named DtSettingsObject
126  // changes. The arguments are copied into a DtSettingsConsumer used to
127  // update the myConsumersMultiMap.
128  // @{
129  virtual void registerConsumer(std::string name,
130  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
131  virtual void unregisterConsumer(std::string name,
132  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
133  // @}
134 
135  // Registers an independent setting that does not have an associated
136  // producer function. Independent settings are generally only used when
137  // a given setting will never be associated with other settings grouped
138  // into a subclass of DtSettingsObject.
139  // @{
140  virtual void addIndependentBool(std::string name, bool value);
141  virtual void addIndependentInt(std::string name, int value);
142  virtual void addIndependentDouble(std::string name, double value);
143  virtual void addIndependentString(std::string name, std::string value);
144  // @}
145 
146 protected:
147  // Settings file to read/write
148  std::string myFilename;
149 
150  // Map of DtSettingsObjects
152 
153  // Map of DtSettingsProducers
155 
156  // Map of DtSettingsConsumers
158 
159  // DtSettingsObjectFactory
161 };
162 
163 #endif

Document ID: Generated on Tue Jan 29 18:21:16 EST 2013 from SVN revision 123193
Copyright © 2005-2013 VT MÄK Inc. All Rights Reserved (www.mak.com)