Debian Codegraph @

src/Base/Parameter.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Library General Public License (LGPL) *
10 * as published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * for detail see the LICENCE text file. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Library General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Library General Public *
20 * License along with FreeCAD; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 * USA *
23 * *
24 ***************************************************************************/
25
26 /**
27 * \file Parameter.h
28 * \brief The classes defined here are used to interface with the XML-based
29 * FreeCAD config files: user.cfg and system.cfg files. It can parse, get,
30 * and store the parameters/configurations for the user's preferences.
31 * 3rd party Xerces-C++ XML parser is used to parse and write the XML.
32 */
33
34 #ifndef BASE_PARAMETER_H
35 #define BASE_PARAMETER_H
36
37 // Python stuff
38 using PyObject = struct _object;
39
40 #include <FCConfig.h>
41
42 #ifdef FC_OS_MACOSX
43 # undef toupper
44 # undef tolower
45 # undef isupper
46 # undef islower
47 # undef isspace
48 # undef isalpha
49 # undef isalnum
50 #endif
51
52 #include <map>
53 #include <vector>
54 #include <boost/signals2.hpp>
55 #include <xercesc/util/XercesDefs.hpp>
56
57 #include "Handle.h"
58 #include "Observer.h"
59 #include "Color.h"
60
61 #ifdef _MSC_VER
62 # pragma warning(disable : 4251)
63 # pragma warning(disable : 4503)
64 # pragma warning(disable : 4786) // specifier longer then 255 chars
65 # pragma warning(disable : 4290) // not implemented throw specification
66 # pragma warning(disable : 4275)
67 #endif
68
69 #ifndef XERCES_CPP_NAMESPACE_BEGIN
70 # define XERCES_CPP_NAMESPACE_QUALIFIER
71 namespace XERCES_CPP_NAMESPACE
72 {
73 class DOMNode;
74 class DOMElement;
75 class DOMDocument;
76 class XMLFormatTarget;
77 class InputSource;
78 } // namespace XERCES_CPP_NAMESPACE
79 #else
80 XERCES_CPP_NAMESPACE_BEGIN
81 class DOMNode;
82 class DOMElement;
83 class DOMDocument;
84 class XMLFormatTarget;
85 class InputSource;
86 XERCES_CPP_NAMESPACE_END
87 #endif
88
89 class ParameterManager;
90
91 /** The parameter container class
92 * This is the base class of all classes handle parameter.
93 * The class contains a map of key-value pairs in a grouping
94 * structure, not unlike the windows registry.
95 * It allows the user to set and retrieve values of the
96 * type float, long and string. Also it handles importing
97 * and exporting groups of parameters and enables streaming
98 * to a persistent medium via XML.
99 * \par
100 * Its main task is making user parameter persistent, saving
101 * last used values in dialog boxes, setting and retrieving all
102 * kind of preferences and so on.
103 * @see ParameterManager
104 */
105 class BaseExport ParameterGrp: public Base::Handled, public Base::Subject<const char*>
106 {
107 public:
108 ParameterGrp(const ParameterGrp&) = delete;
109 ParameterGrp(ParameterGrp&&) = delete;
110 ParameterGrp& operator=(const ParameterGrp&) = delete;
111 ParameterGrp& operator=(ParameterGrp&&) = delete;
112
113 /** @name Copying & Inserting */
114 //@{
115
116 /**
117 * Overwrites another group with this one.
118 *
119 * @param[out] Group The group to overwrite.
120 */
121 void copyTo(const Base::Reference<ParameterGrp>& Group);
122
123 /**
124 * Inserts items from this group into another.
125 *
126 * @param[out] Group The group to insert into.
127 *
128 * @note
129 * Inserts new and replaces existing items.
130 */
131 void insertTo(const Base::Reference<ParameterGrp>& Group);
132
133 /**
134 * Exports this group to a given file.
135 *
136 * @param[out] FileName The path to the file.
137 */
138 void exportTo(const char* FileName);
139
140 /**
141 * Overwrites this group with the given file.
142 *
143 * @param[in] FileName The path to the file.
144 */
145 void importFrom(const char* FileName);
146
147 /**
148 * Inserts items from the given file.
149 *
150 * @param[in] FileName The path to the file.
151 *
152 * @note
153 * Inserts new and replaces existing items.
154 */
155 void insert(const char* FileName);
156
157 /**
158 * Removes items from this group that are present in the given file.
159 *
160 * @param[in] FileName The path to the file.
161 *
162 * @note
163 * Only removes items that have the same value.
164 */
165 void revert(const char* FileName);
166
167 /**
168 * Removes items from this group that are present in the other.
169 *
170 * @param[in] Group The group to compare with.
171 *
172 * @note
173 * Only removes items that have the same value.
174 */
175 void revert(const Base::Reference<ParameterGrp>& Group);
176
177 //@}
178
179 /** @name methods for group handling */
180 //@{
181
182 /**
183 * Returns or creates a sub-group with the given name.
184 *
185 * @param[in] Name Name of the sub-group.
186 * @returns A handle to the sub-group.
187 */
188 Base::Reference<ParameterGrp> GetGroup(const char* Name);
189
190 /**
191 * Returns all sub-groups.
192 *
193 * @returns A vector of handles to the sub-groups.
194 */
195 std::vector<Base::Reference<ParameterGrp>> GetGroups();
196
197 /**
198 * Tests if this group is empty.
199 */
200 bool IsEmpty() const;
201
202 /**
203 * Tests if a sub-group exists.
204 *
205 * @param[in] Name Name of the sub-group.
206 */
207 bool HasGroup(const char* Name) const;
208
209 /// type of the handle
210 using handle = Base::Reference<ParameterGrp>;
211
212 /**
213 * Removes a sub-group.
214 *
215 * @param[in] Name Name of the sub-group.
216 */
217 void RemoveGrp(const char* Name);
218
219 /**
220 * Renames a sub-group.
221 *
222 * @param[in] OldName The current name of the sub-group.
223 * @param[in] NewName The new name the sub-group will have.
224 * @returns Whether or not the renaming succeeded.
225 *
226 * @note Does nothing if a sub-group with the new name already exists.
227 */
228 bool RenameGrp(const char* OldName, const char* NewName);
229
230 /**
231 * Empties this group.
232 *
233 * @param[in] notify Whether to notify on deleted parameters using the Observer interface.
234 */
235 void Clear(bool notify = false);
236
237 //@}
238
239 /** @name methods for generic attribute handling */
240 //@{
241
242 enum class ParamType
243 {
244 FCInvalid = 0,
245 FCText = 1,
246 FCBool = 2,
247 FCInt = 3,
248 FCUInt = 4,
249 FCFloat = 5,
250 FCGroup = 6,
251 };
252 static const char* TypeName(ParamType type);
253 static ParamType TypeValue(const char*);
254
255 /**
256 * Sets the value of an attribute.
257 *
258 * @param[in] Type The type of the attribute.
259 * @param[in] Name The name of the attribute.
260 * @param[in] Value The value to be set.
261 */
262 void SetAttribute(ParamType Type, const char* Name, const char* Value);
263
264 /**
265 * Removes an attribute.
266 *
267 * @param[in] Type The type of the attribute.
268 * @param[in] Name The name of the attribute.
269 */
270 void RemoveAttribute(ParamType Type, const char* Name);
271
272 /**
273 * Returns the value of the attribute.
274 *
275 * If the attribute can't be found, \n
276 * the fallback value is returned.
277 *
278 * @param[in] Type The type of the attribute.
279 * @param[in] Name The name of the attribute.
280 * @param[out] Value The value of attribute or the fallback value.
281 * @param[in] Default The fallback value.
282 */
283 const char* GetAttribute(ParamType Type, const char* Name, std::string& Value, const char* Default) const;
284
285 /**
286 * Returns all attributes with the given type.
287 *
288 * Optionally filters them to only include attributes \n
289 * with names that contain a certain string.
290 *
291 * @param[in] Type The type of attributes to be returned
292 * @param[in] sFilter String that has to be present in the names of the attributes.
293 * @returns Vector of attribute name & value pairs.
294 */
295 std::vector<std::pair<std::string, std::string>> GetAttributeMap(
296 ParamType Type,
297 const char* sFilter = nullptr
298 ) const;
299
300
301 /**
302 * Returns all parameters.
303 *
304 * Optionally filters them to only include attributes \n
305 * with names that contain a certain string.
306 *
307 * @param[in] sFilter String that has to be present in the names of the attributes.
308 * @returns Vector of attribute type & name pairs.
309 */
310 std::vector<std::pair<ParamType, std::string>> GetParameterNames(
311 const char* sFilter = nullptr
312 ) const;
313
314 //@}
315
316 /** @name methods for bool handling */
317 //@{
318 /// read bool values or give default
319 bool GetBool(const char* Name, bool bPreset = false) const;
320 /// set a bool value
321 void SetBool(const char* Name, bool bValue);
322 /// get a vector of all bool values in this group
323 std::vector<bool> GetBools(const char* sFilter = nullptr) const;
324 /// get a map with all bool values and the keys of this group
325 std::vector<std::pair<std::string, bool>> GetBoolMap(const char* sFilter = nullptr) const;
326 /// remove a bool value from this group
327 void RemoveBool(const char* Name);
328 //@}
329
330 /** @name methods for Int handling */
331 //@{
332 /// read bool values or give default
333 long GetInt(const char* Name, long lPreset = 0) const;
334 /// set a int value
335 void SetInt(const char* Name, long lValue);
336 /// get a vector of all int values in this group
337 std::vector<long> GetInts(const char* sFilter = nullptr) const;
338 /// get a map with all int values and the keys of this group
339 std::vector<std::pair<std::string, long>> GetIntMap(const char* sFilter = nullptr) const;
340 /// remove a int value from this group
341 void RemoveInt(const char* Name);
342 //@}
343
344 /** @name methods for Unsigned Int handling */
345 //@{
346 /// read uint values or give default
347 unsigned long GetUnsigned(const char* Name, unsigned long lPreset = 0) const;
348 /// set a uint value
349 void SetUnsigned(const char* Name, unsigned long lValue);
350 /// get a vector of all uint values in this group
351 std::vector<unsigned long> GetUnsigneds(const char* sFilter = nullptr) const;
352 /// get a map with all uint values and the keys of this group
353 std::vector<std::pair<std::string, unsigned long>> GetUnsignedMap(
354 const char* sFilter = nullptr
355 ) const;
356 /// remove a uint value from this group
357 void RemoveUnsigned(const char* Name);
358 //@}
359
360 /** @name methods for Colors handling, colors are persisted as packed uints */
361 //@{
362 /// read color value or give default
363 Base::Color GetColor(const char* Name, Base::Color lPreset = Base::Color(1.0, 1.0, 1.0)) const;
364 /// set a color value
365 void SetColor(const char* Name, Base::Color lValue);
366 /// get a vector of all color values in this group
367 std::vector<Base::Color> GetColors(const char* sFilter = nullptr) const;
368 /// get a map with all color values and the keys of this group
369 std::vector<std::pair<std::string, Base::Color>> GetColorMap(const char* sFilter = nullptr) const;
370 /// remove a color value from this group
371 void RemoveColor(const char* Name);
372 //@}
373
374
375 /** @name methods for Float handling */
376 //@{
377 /// set a float value
378 double GetFloat(const char* Name, double dPreset = 0.0) const;
379 /// read float values or give default
380 void SetFloat(const char* Name, double dValue);
381 /// get a vector of all float values in this group
382 std::vector<double> GetFloats(const char* sFilter = nullptr) const;
383 /// get a map with all float values and the keys of this group
384 std::vector<std::pair<std::string, double>> GetFloatMap(const char* sFilter = nullptr) const;
385 /// remove a float value from this group
386 void RemoveFloat(const char* Name);
387 //@}
388
389
390 /** @name methods for String handling */
391 //@{
392 /// set a string value
393 void SetASCII(const char* Name, const char* sValue);
394 /// set a string value
395 void SetASCII(const char* Name, const std::string& sValue)
396 {
397 SetASCII(Name, sValue.c_str());
398 }
399 /// read a string values
400 std::string GetASCII(const char* Name, const char* pPreset = nullptr) const;
401 /// remove a string value from this group
402 void RemoveASCII(const char* Name);
403 /** Return all string elements in this group as a vector of strings
404 * Its also possible to set a filter criteria.
405 * @param sFilter only strings which name includes sFilter are put in the vector
406 * @return std::vector of std::strings
407 */
408 std::vector<std::string> GetASCIIs(const char* sFilter = nullptr) const;
409 /// Same as GetASCIIs() but with key,value map
410 std::vector<std::pair<std::string, std::string>> GetASCIIMap(const char* sFilter = nullptr) const;
411 //@}
412
413 friend class ParameterManager;
414
415 /// returns the name
416 const char* GetGroupName() const
417 {
418 return _cName.c_str();
419 }
420
421 /// return the full path of this group
422 std::string GetPath() const;
423 void GetPath(std::string&) const;
424
425 /** Notifies all observers for all entries except of sub-groups.
426 */
427 void NotifyAll();
428
429 ParameterGrp* Parent() const
430 {
431 return _Parent;
432 }
433 ParameterManager* Manager() const
434 {
435 return _Manager;
436 }
437
438 protected:
439 /// constructor is protected (handle concept)
440 ParameterGrp(
441 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* GroupNode = nullptr,
442 const char* sName = nullptr,
443 ParameterGrp* Parent = nullptr
444 );
445 /// destructor is protected (handle concept)
446 ~ParameterGrp() override;
447 /// helper function for GetGroup
448 Base::Reference<ParameterGrp> _GetGroup(const char* Name);
449 bool ShouldRemove() const;
450
451 void _Reset();
452
453 void _SetAttribute(ParamType Type, const char* Name, const char* Value);
454 void _Notify(ParamType Type, const char* Name, const char* Value);
455
456 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* FindNextElement(
457 XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* Prev,
458 const char* Type
459 ) const;
460
461 /** Find an element specified by Type and Name
462 * Search in the parent element Start for the first occurrence of an
463 * element of Type and with the attribute Name=Name. On success it returns
464 * the pointer to that element, otherwise NULL
465 * If the names not given it returns the first occurrence of Type.
466 */
467 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* FindElement(
468 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start,
469 const char* Type,
470 const char* Name = nullptr
471 ) const;
472
473 /** Find an element specified by Type and Name or create it if not found
474 * Search in the parent element Start for the first occurrence of an
475 * element of Type and with the attribute Name=Name. On success it returns
476 * the pointer to that element, otherwise it creates the element and returns the pointer.
477 */
478 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* FindOrCreateElement(
479 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start,
480 const char* Type,
481 const char* Name
482 );
483
484 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* CreateElement(
485 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start,
486 const char* Type,
487 const char* Name
488 );
489
490 /** Find an attribute specified by Name
491 */
492 XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* FindAttribute(
493 XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* Node,
494 const char* Name
495 ) const;
496
497 /// DOM Node of the Base node of this group
498 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* _pGroupNode;
499 /// the own name
500 std::string _cName;
501 /// map of already exported groups
502 std::map<std::string, Base::Reference<ParameterGrp>> _GroupMap;
503 ParameterGrp* _Parent = nullptr;
504 ParameterManager* _Manager = nullptr;
505 /// Means this group xml element has not been added to its parent yet.
506 bool _Detached = false;
507 /** Indicate this group is currently being cleared
508 *
509 * This is used to prevent anynew value/sub-group to be added in observer
510 */
511 bool _Clearing = false;
512 };
513
514 /** The parameter serializer class
515 * This is a helper class to serialize a parameter XML document.
516 * Does loading and saving the DOM document from and to files.
517 * In sub-classes the load and saving of XML documents can be
518 * customized.
519 * @see ParameterManager
520 */
521 class BaseExport ParameterSerializer
522 {
523 public:
524 explicit ParameterSerializer(std::string fn);
525 ParameterSerializer(const ParameterSerializer&) = delete;
526 ParameterSerializer(ParameterSerializer&&) = delete;
527 virtual ~ParameterSerializer();
528
529 virtual void SaveDocument(const ParameterManager&);
530 virtual int LoadDocument(ParameterManager&);
531 virtual bool LoadOrCreateDocument(ParameterManager&);
532 const std::string& GetFileName() const
533 {
534 return filename;
535 }
536
537 ParameterSerializer& operator=(const ParameterSerializer&) = delete;
538 ParameterSerializer& operator=(ParameterSerializer&&) = delete;
539
540 private:
541 std::string filename;
542 };
543
544 /** The parameter manager class
545 * This class manages a parameter XML document.
546 * Does loading, saving and handling the DOM document.
547 * @see ParameterGrp
548 */
549 class BaseExport ParameterManager: public ParameterGrp
550 {
551 public:
552 /// Create a reference counted ParameterManager
553 static Base::Reference<ParameterManager> Create();
554 static void Init();
555 static void Terminate();
556
557 /** Signal on parameter changes
558 *
559 * The signal is triggered on adding, removing, renaming or modifying on
560 * all individual parameters and group. The signature of the signal is
561 * \code
562 * void (ParameterGrp *param, ParamType type, const char *name, const char *value)
563 * \endcode
564 * where 'param' is the parameter group causing the change, 'type' is the
565 * type of the parameter, 'name' is the name of the parameter, and 'value'
566 * is the current value.
567 *
568 * The possible values of 'type' are, 'FCBool', 'FCInt', 'FCUint',
569 * 'FCFloat', 'FCText', and 'FCParamGroup'. The notification is triggered
570 * when value is changed, in which case 'value' contains the new value in
571 * text form, or, when the parameter is removed, in which case 'value' is
572 * empty.
573 *
574 * For 'FCParamGroup' type, the observer will be notified in the following events.
575 * - Group creation: both 'name' and 'value' contain the name of the new group
576 * - Group removal: both 'name' and 'value' are empty
577 * - Group rename: 'name' is the new name, and 'value' is the old name
578 */
579 boost::signals2::signal<
580 void(ParameterGrp* /*param*/, ParamType /*type*/, const char* /*name*/, const char* /*value*/)>
581 signalParamChanged;
582
583 int LoadDocument(const char* sFileName);
584 int LoadDocument(const XERCES_CPP_NAMESPACE_QUALIFIER InputSource&);
585 bool LoadOrCreateDocument(const char* sFileName);
586 void SaveDocument(const char* sFileName) const;
587 void SaveDocument(XERCES_CPP_NAMESPACE_QUALIFIER XMLFormatTarget* pFormatTarget) const;
588 void CreateDocument();
589 void CheckDocument() const;
590
591 /** @name Parameter serialization */
592 //@{
593 /// Sets a serializer. The ParameterManager takes ownership of the serializer.
594 void SetSerializer(ParameterSerializer*);
595 /// Returns true if a serializer is set, otherwise false is returned.
596 bool HasSerializer() const;
597 /// Returns the filename of the serialize.
598 const std::string& GetSerializeFileName() const;
599 /// Loads an XML document by calling the serializer's load method.
600 int LoadDocument();
601 /// Loads or creates an XML document by calling the serializer's load method.
602 bool LoadOrCreateDocument();
603 /// Saves an XML document by calling the serializer's save method.
604 void SaveDocument() const;
605 void SetIgnoreSave(bool value);
606 bool IgnoreSave() const;
607 //@}
608
609 private:
610 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* _pDocument {nullptr};
611 ParameterSerializer* paramSerializer {nullptr};
612
613 bool gIgnoreSave;
614 bool gDoNamespaces;
615 bool gDoSchema;
616 bool gSchemaFullChecking;
617 bool gDoCreate;
618
619
620 const XMLCh* gOutputEncoding;
621 const XMLCh* gMyEOLSequence;
622
623 bool gSplitCdataSections;
624 bool gDiscardDefaultContent;
625 bool gUseFilter;
626 bool gFormatPrettyPrint;
627
628 private:
629 ParameterManager();
630
631 public:
632 ~ParameterManager() override;
633 ParameterManager(const ParameterManager&) = delete;
634 ParameterManager(ParameterManager&&) = delete;
635
636 ParameterManager& operator=(const ParameterManager&) = delete;
637 ParameterManager& operator=(ParameterManager&&) = delete;
638 };
639
640 /** python wrapper function
641 */
642 BaseExport PyObject* GetPyObject(const Base::Reference<ParameterGrp>& hcParamGrp);
643
644
645 #endif // BASE_PARAMETER_H