VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #include <list>
30 
32 
33 // Definition for producer functions which are used with registerProducer
34 typedef void (*DtSettingsProducerFcn)(DtSettingsObject* object, void* usr);
35 
36 // Holds producer details
38 {
39  std::string type;
41  void* usr;
42 };
43 
44 // Definition for map of producers
45 typedef std::map<std::string, DtSettingsProducer> DtSettingsProducersMap;
46 
47 // Definition for comsumer functions which are used with registerConsumer
48 typedef void (*DtSettingsConsumerFcn)(const DtSettingsObject* object,
49  void* usr);
50 
51 // Holds consumer details
53 {
55  void* usr;
56 };
57 
58 // Definition for multi-map of consumers
59 typedef std::multimap<std::string, DtSettingsConsumer>
61 
62 // Definition for map of DtSettingsObjects
63 typedef std::map<std::string, DtSettingsObject*> DtSettingsMap ;
64 
67 {
68 public:
69  // Constructor
70  DtSettingsManager(std::string filename);
71 
72  // Destructor
74 
75  // Calls read()
76  virtual void init();
77 
78  // Opens and parses the settings file
79  virtual bool read();
80 
81  // Updates all settings objects via their associated producer functions and
82  // then writes out the settings file.
83  virtual bool write();
84 
85  // DtSettingsObjectFactory
87  static DtSettingsObjectFactory* factory();
88  static void setFactory(DtSettingsObjectFactory* factory);
90 
91  // \brief Returns a pointer to the named settings object from the map.
92  // If the object does not exist but a producer has been registered for it,
93  // a new instance of the object will be created and returned.
94  // If the object has been read in from the settings file but does not yet
95  // have a producer associated with it, its value will still be returned.
96  // If the object does not exist in the map and there is no producer
97  // associated with it, null (0) is returned.
98  // Note that the return type is const because clients should only write
99  // settings data via their DtSettingsProducerFcn which they register with
100  // registerProducer.
101  virtual const DtSettingsObject* lookupSetting(std::string name);
102 
103  // Updates an existing DtSettingsObject from the one provided by using
104  // its setFrom method. If there is no DtSettingsObject in the DtSettingsMap
105  // whose name corresponds to the name of the object provided, or if their
106  // types do not match, false is returned and the setting is left unchanged.
107  virtual bool setSetting(const DtSettingsObject* settingsObject);
108 
109  // Returns the key list of the settings map.
110  virtual std::list<std::string> settingsMapKeys();
111 
112  // \brief Registers a producer function as the source for the given setting
113  // Associates the given DtSettingsProducerFcn with the given settings name.
114  // All three arguments are copied into a DtSettingsProducer which is stored
115  // in myProducersMap.
116  // Whenever lookupSetting is called, this function will be used to update
117  // the settings object with the most current data from the producer before
118  // it is returned to the caller. If a producer with the same name is
119  // already registered in myProducersMap, the associated settings object
120  // in mySettingsMap is no longer considered valid and will be deleted.
121  virtual void registerProducer(std::string type, std::string name,
122  DtSettingsProducerFcn producerFcn, void* usr = 0);
123  virtual void unregisterProducer(std::string type, std::string name,
124  DtSettingsProducerFcn producerFcn, void* usr = 0);
125 
126  // DtSettingsConsumerFcn's are called whenever a named DtSettingsObject
127  // changes. The arguments are copied into a DtSettingsConsumer used to
128  // update the myConsumersMultiMap.
129  // @{
130  virtual void registerConsumer(std::string name,
131  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
132  virtual void unregisterConsumer(std::string name,
133  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
134  // @}
135 
136  // Registers an independent setting that does not have an associated
137  // producer function. Independent settings are generally only used when
138  // a given setting will never be associated with other settings grouped
139  // into a subclass of DtSettingsObject.
140  // @{
141  virtual void addIndependentBool(std::string name, bool value);
142  virtual void addIndependentInt(std::string name, int value);
143  virtual void addIndependentDouble(std::string name, double value);
144  virtual void addIndependentString(std::string name, std::string value);
145  // @}
146 
147 protected:
148  // Settings file to read/write
149  std::string myFilename;
150 
151  // Map of DtSettingsObjects
153 
154  // Map of DtSettingsProducers
156 
157  // Map of DtSettingsConsumers
159 
160  // DtSettingsObjectFactory
162 };
163 
164 #endif

Document ID: Generated on Tue Mar 8 22:13:38 EST 2016 from SVN revision 162938
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)