VR-Forces 5.0.1 Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
settingsManager.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
6 // \file settingsManager.h
7 // \brief Manages settings saved out in an XML file
8 // The DtSettingsManager contains DtSettingsObjects which are seralized into
9 // a settings file using boost::serialize. This manager is not a data storage
10 // class -- it simply maintains a copy on behalf of a client which provides
11 // a setting. Such clients are called producers and provide functions which
12 // the manager can use to get the latest value. Values are only updated upon
13 // lookup and immediately before the file is written out to disk. Producers
14 // are not required -- independent settings can also be added to the manager.
15 // Changes to settings objects can be tracked by registering as a consumer
16 // for a given object.
17 
18 #pragma once
19 
21 #include <boost/shared_ptr.hpp>
22 #include "vlutil/vlFilename.h"
23 
24 #include <tbb/recursive_mutex.h>
25 #include <tbb/mutex.h>
26 
27 #include <map>
28 #include <set>
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 {
54  DtSettingsConsumer() : consumerFcn(0), usr(0), deleted(new bool(false)) {};
55  DtSettingsConsumer(DtSettingsConsumerFcn fcn, void* u) : consumerFcn(fcn), usr(u), deleted(new bool(false)) {};
57 
59  {
60  consumerFcn = orig.consumerFcn;
61  usr = orig.usr;
62  deleted = orig.deleted;
63 
64  return *this;
65  }
66 
67  bool operator<(const DtSettingsConsumer& rhs) const
68  {
69  return consumerFcn < rhs.consumerFcn || (!(rhs.consumerFcn < consumerFcn) && usr < rhs.usr);
70  }
71 
72  bool operator>(const DtSettingsConsumer& rhs) const
73  {
74  return (rhs < *this);
75  }
76 
77  bool operator==(const DtSettingsConsumer& rhs) const
78  {
79  return ((consumerFcn == rhs.consumerFcn) &&
80  (usr == rhs.usr));
81  }
82 
83  bool operator!=(const DtSettingsConsumer& rhs) const
84  {
85  return !(*this == rhs);
86  }
87 
89  void* usr;
90  boost::shared_ptr<bool> deleted;
91 };
92 
93 // Definition for multi-map of consumers
94 typedef std::map<std::string, std::set<DtSettingsConsumer> > DtSettingsConsumersMultiMap;
95 
96 // Definition for map of DtSettingsObjects
97 typedef std::map<std::string, DtSettingsObject*> DtSettingsMap ;
98 
101 {
102 public:
103  // Constructor
104  DtSettingsManager(const std::string& filename);
105 
106  // Destructor
108 
109  // Calls read()
110  virtual void init();
111 
112  // Opens and parses the settings file
113  virtual bool read();
114 
115  // Updates all settings objects via their associated producer functions and
116  // then writes out the settings file.
117  virtual bool write();
118 
119  // DtSettingsObjectFactory
121  static DtSettingsObjectFactory* factory();
122  static void setFactory(DtSettingsObjectFactory* factory);
124 
125  // \brief Returns a pointer to the named settings object from the map.
126  // If the object does not exist but a producer has been registered for it,
127  // a new instance of the object will be created and returned.
128  // If the object has been read in from the settings file but does not yet
129  // have a producer associated with it, its value will still be returned.
130  // If the object does not exist in the map and there is no producer
131  // associated with it, null (0) is returned.
132  // Note that the return type is const because clients should only write
133  // settings data via their DtSettingsProducerFcn which they register with
134  // registerProducer.
135  virtual const DtSettingsObject* lookupSetting(const std::string& name);
136 
137  // Updates an existing DtSettingsObject from the one provided by using
138  // its setFrom method. If there is no DtSettingsObject in the DtSettingsMap
139  // whose name corresponds to the name of the object provided, or if their
140  // types do not match, false is returned and the setting is left unchanged.
141  virtual bool setSetting(const DtSettingsObject* settingsObject);
142 
143  // Returns the key list of the settings map.
144  virtual std::list<std::string> settingsMapKeys();
145 
146  // \brief Registers a producer function as the source for the given setting
147  // Associates the given DtSettingsProducerFcn with the given settings name.
148  // All three arguments are copied into a DtSettingsProducer which is stored
149  // in myProducersMap.
150  // Whenever lookupSetting is called, this function will be used to update
151  // the settings object with the most current data from the producer before
152  // it is returned to the caller. If a producer with the same name is
153  // already registered in myProducersMap, the associated settings object
154  // in mySettingsMap is no longer considered valid and will be deleted.
155  virtual void registerProducer(const std::string& type, const std::string& name,
156  DtSettingsProducerFcn producerFcn, void* usr = 0);
157  virtual void unregisterProducer(const std::string& type, const std::string& name,
158  DtSettingsProducerFcn producerFcn, void* usr = 0);
159 
160  // DtSettingsConsumerFcn's are called whenever a named DtSettingsObject
161  // changes. The arguments are copied into a DtSettingsConsumer used to
162  // update the myConsumersMultiMap.
163  // @{
164  virtual void registerConsumer(const std::string& name,
165  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
166  virtual void unregisterConsumer(const std::string& name,
167  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
168  // @}
169 
170  // Registers an independent setting that does not have an associated
171  // producer function. Independent settings are generally only used when
172  // a given setting will never be associated with other settings grouped
173  // into a subclass of DtSettingsObject.
174  // @{
175  virtual void addIndependentBool(const std::string& name, bool value);
176  virtual void addIndependentInt(const std::string& name, int value);
177  virtual void addIndependentDouble(const std::string& name, double value);
178  virtual void addIndependentString(const std::string& name, const std::string& value);
179  // @}
180 
181 protected:
182  // Settings file to read/write
183  std::string myFilename;
184 
185  // Map of DtSettingsObjects
187 
188  // Map of DtSettingsProducers
190 
191  // Map of DtSettingsConsumers
193 
194  // DtSettingsObjectFactory
196 
197  tbb::mutex myConsumerMutex;
198  tbb::recursive_mutex myProducerMutex;
199 };
DtSettingsConsumer & operator=(const DtSettingsConsumer &orig)
Definition: settingsManager.h:58
DtSettingsConsumer(const DtSettingsConsumer &orig)
Definition: settingsManager.h:56
Definition: settingsManager.h:37
bool operator>(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:72
tbb::recursive_mutex myProducerMutex
Definition: settingsManager.h:198
boost::shared_ptr< bool > deleted
Definition: settingsManager.h:90
void * usr
Definition: settingsManager.h:89
bool operator<(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:67
Definition: settingsObject.h:27
DtSettingsConsumersMultiMap myConsumersMultiMap
Definition: settingsManager.h:192
void * usr
Definition: settingsManager.h:41
DtSettingsMap mySettingsMap
Definition: settingsManager.h:186
Definition: settingsObjectFactory.h:30
void(* DtSettingsConsumerFcn)(const DtSettingsObject *object, void *usr)
Definition: settingsManager.h:48
static DtSettingsObjectFactory * theSettingsObjectFactory
Definition: settingsManager.h:195
void(* DtSettingsProducerFcn)(DtSettingsObject *object, void *usr)
Definition: settingsManager.h:34
DtSettingsProducerFcn producerFcn
Definition: settingsManager.h:40
Definition: settingsManager.h:100
DtSettingsConsumer()
Definition: settingsManager.h:54
std::map< std::string, DtSettingsProducer > DtSettingsProducersMap
Definition: settingsManager.h:45
DtSettingsConsumerFcn consumerFcn
Definition: settingsManager.h:88
std::map< std::string, DtSettingsObject * > DtSettingsMap
Definition: settingsManager.h:97
DtSettingsConsumer(DtSettingsConsumerFcn fcn, void *u)
Definition: settingsManager.h:55
std::map< std::string, std::set< DtSettingsConsumer > > DtSettingsConsumersMultiMap
Definition: settingsManager.h:94
DT_DLL_DEBUGDRAWRENDERER void init(makVrv::DtDe &de)
Work function for the plugin initialization.
std::string type
Definition: settingsManager.h:39
#define DT_DLL_makSettingsManager
Definition: makSettingsManagerDefines.h:24
DtSettingsProducersMap myProducersMap
Definition: settingsManager.h:189
bool operator!=(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:83
Definition: settingsManager.h:52
std::string myFilename
Definition: settingsManager.h:183
bool operator==(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:77
tbb::mutex myConsumerMutex
Definition: settingsManager.h:197

Document ID: Generated on Mon Jun 20 00:38:30 EDT 2022 from SVN revision 244029
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)