VR-Forces 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) 2025 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 "vlutil/vlFilename.h"
22 
23 #include <tbb/mutex.h>
24 #include <map>
25 #include <set>
26 #include <list>
27 #include <mutex>
28 
30 
31 // Definition for producer functions which are used with registerProducer
32 typedef void (*DtSettingsProducerFcn)(DtSettingsObject* object, void* usr);
33 
34 // Holds producer details
36 {
37  std::string type;
39  void* usr;
40 };
41 
42 // Definition for map of producers
43 typedef std::map<std::string, DtSettingsProducer> DtSettingsProducersMap;
44 
45 // Definition for comsumer functions which are used with registerConsumer
46 typedef void (*DtSettingsConsumerFcn)(const DtSettingsObject* object,
47  void* usr);
48 
49 // Holds consumer details
51 {
52  DtSettingsConsumer() : consumerFcn(0), usr(0), deleted(new bool(false)) {};
53  DtSettingsConsumer(DtSettingsConsumerFcn fcn, void* u) : consumerFcn(fcn), usr(u), deleted(new bool(false)) {};
55 
57  {
58  consumerFcn = orig.consumerFcn;
59  usr = orig.usr;
60  deleted = orig.deleted;
61 
62  return *this;
63  }
64 
65  bool operator<(const DtSettingsConsumer& rhs) const
66  {
67  return consumerFcn < rhs.consumerFcn || (!(rhs.consumerFcn < consumerFcn) && usr < rhs.usr);
68  }
69 
70  bool operator>(const DtSettingsConsumer& rhs) const
71  {
72  return (rhs < *this);
73  }
74 
75  bool operator==(const DtSettingsConsumer& rhs) const
76  {
77  return ((consumerFcn == rhs.consumerFcn) &&
78  (usr == rhs.usr));
79  }
80 
81  bool operator!=(const DtSettingsConsumer& rhs) const
82  {
83  return !(*this == rhs);
84  }
85 
87  void* usr;
88  std::shared_ptr<bool> deleted;
89 };
90 
91 // Definition for multi-map of consumers
92 typedef std::map<std::string, std::set<DtSettingsConsumer> > DtSettingsConsumersMultiMap;
93 
94 // Definition for map of DtSettingsObjects
95 typedef std::map<std::string, DtSettingsObject*> DtSettingsMap ;
96 
99 {
100 public:
101  // Constructor
102  DtSettingsManager(const std::string& filename);
103 
104  // Destructor
106 
107  // Calls read()
108  virtual void init();
109 
110  // Opens and parses the settings file
111  virtual bool read();
112 
113  // Updates all settings objects via their associated producer functions and
114  // then writes out the settings file.
115  virtual bool write();
116 
117  // DtSettingsObjectFactory
119  static DtSettingsObjectFactory* factory();
120  static void setFactory(DtSettingsObjectFactory* factory);
122 
123  // \brief Returns a pointer to the named settings object from the map.
124  // If the object does not exist but a producer has been registered for it,
125  // a new instance of the object will be created and returned.
126  // If the object has been read in from the settings file but does not yet
127  // have a producer associated with it, its value will still be returned.
128  // If the object does not exist in the map and there is no producer
129  // associated with it, null (0) is returned.
130  // Note that the return type is const because clients should only write
131  // settings data via their DtSettingsProducerFcn which they register with
132  // registerProducer.
133  virtual const DtSettingsObject* lookupSetting(const std::string& name);
134 
135  // Updates an existing DtSettingsObject from the one provided by using
136  // its setFrom method. If there is no DtSettingsObject in the DtSettingsMap
137  // whose name corresponds to the name of the object provided, or if their
138  // types do not match, false is returned and the setting is left unchanged.
139  virtual bool setSetting(const DtSettingsObject* settingsObject);
140 
141  // Returns the key list of the settings map.
142  virtual std::list<std::string> settingsMapKeys();
143 
144  // \brief Registers a producer function as the source for the given setting
145  // Associates the given DtSettingsProducerFcn with the given settings name.
146  // All three arguments are copied into a DtSettingsProducer which is stored
147  // in myProducersMap.
148  // Whenever lookupSetting is called, this function will be used to update
149  // the settings object with the most current data from the producer before
150  // it is returned to the caller. If a producer with the same name is
151  // already registered in myProducersMap, the associated settings object
152  // in mySettingsMap is no longer considered valid and will be deleted.
153  virtual void registerProducer(const std::string& type, const std::string& name,
154  DtSettingsProducerFcn producerFcn, void* usr = 0);
155  virtual void unregisterProducer(const std::string& type, const std::string& name,
156  DtSettingsProducerFcn producerFcn, void* usr = 0);
157 
158  // DtSettingsConsumerFcn's are called whenever a named DtSettingsObject
159  // changes. The arguments are copied into a DtSettingsConsumer used to
160  // update the myConsumersMultiMap.
161  // @{
162  virtual void registerConsumer(const std::string& name,
163  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
164  virtual void unregisterConsumer(const std::string& name,
165  DtSettingsConsumerFcn consumerFcn, void* usr = 0);
166  // @}
167 
168  // Registers an independent setting that does not have an associated
169  // producer function. Independent settings are generally only used when
170  // a given setting will never be associated with other settings grouped
171  // into a subclass of DtSettingsObject.
172  // @{
173  virtual void addIndependentBool(const std::string& name, bool value);
174  virtual void addIndependentInt(const std::string& name, int value);
175  virtual void addIndependentDouble(const std::string& name, double value);
176  virtual void addIndependentString(const std::string& name, const std::string& value);
177  // @}
178 
179 protected:
180  // Settings file to read/write
181  std::string myFilename;
182 
183  // Map of DtSettingsObjects
185 
186  // Map of DtSettingsProducers
188 
189  // Map of DtSettingsConsumers
191 
192  // DtSettingsObjectFactory
194 
195  tbb::mutex myConsumerMutex;
196  std::recursive_mutex myProducerMutex;
197 };
std::recursive_mutex myProducerMutex
Definition: settingsManager.h:196
std::shared_ptr< bool > deleted
Definition: settingsManager.h:88
DtSettingsConsumer & operator=(const DtSettingsConsumer &orig)
Definition: settingsManager.h:56
DtSettingsConsumer(const DtSettingsConsumer &orig)
Definition: settingsManager.h:54
Definition: settingsManager.h:35
bool operator>(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:70
void * usr
Definition: settingsManager.h:87
bool operator<(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:65
Definition: settingsObject.h:27
DtSettingsConsumersMultiMap myConsumersMultiMap
Definition: settingsManager.h:190
void * usr
Definition: settingsManager.h:39
DtSettingsMap mySettingsMap
Definition: settingsManager.h:184
Definition: settingsObjectFactory.h:30
void(* DtSettingsConsumerFcn)(const DtSettingsObject *object, void *usr)
Definition: settingsManager.h:46
static DtSettingsObjectFactory * theSettingsObjectFactory
Definition: settingsManager.h:193
void(* DtSettingsProducerFcn)(DtSettingsObject *object, void *usr)
Definition: settingsManager.h:32
DtSettingsProducerFcn producerFcn
Definition: settingsManager.h:38
Definition: settingsManager.h:98
DtSettingsConsumer()
Definition: settingsManager.h:52
std::map< std::string, DtSettingsProducer > DtSettingsProducersMap
Definition: settingsManager.h:43
DtSettingsConsumerFcn consumerFcn
Definition: settingsManager.h:86
std::map< std::string, DtSettingsObject * > DtSettingsMap
Definition: settingsManager.h:95
DtSettingsConsumer(DtSettingsConsumerFcn fcn, void *u)
Definition: settingsManager.h:53
std::map< std::string, std::set< DtSettingsConsumer > > DtSettingsConsumersMultiMap
Definition: settingsManager.h:92
DT_DLL_DEBUGDRAWRENDERER void init(makVrv::DtDe &de)
Work function for the plugin initialization.
std::string type
Definition: settingsManager.h:37
#define DT_DLL_makSettingsManager
Definition: makSettingsManagerDefines.h:24
DtSettingsProducersMap myProducersMap
Definition: settingsManager.h:187
bool operator!=(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:81
Definition: settingsManager.h:50
std::string myFilename
Definition: settingsManager.h:181
bool operator==(const DtSettingsConsumer &rhs) const
Definition: settingsManager.h:75
tbb::mutex myConsumerMutex
Definition: settingsManager.h:195

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)