ASPiK SDK
preferences.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #pragma once
6 
7 #include "../ipreference.h"
8 #include "../iapplication.h"
9 
10 //------------------------------------------------------------------------
11 namespace VSTGUI {
12 namespace Standalone {
13 
14 static const char* DefaultPreferencesGroupSeparator = "::";
15 
16 //------------------------------------------------------------------------
18 {
19 public:
20  Preferences (Preferences&&) = default;
21  Preferences& operator=(Preferences&&) = default;
22 
23  Preferences (const std::initializer_list<const char*>& groups, const char* groupSeparator = DefaultPreferencesGroupSeparator)
24  : groupSeparator (groupSeparator)
25  {
26  for (auto& g : groups)
27  groupKey += UTF8String (g) + groupSeparator;
28  }
29 
30  Preferences (const UTF8String& inGroupKey = "", const char* groupSeparator = DefaultPreferencesGroupSeparator)
31  : groupKey (inGroupKey)
32  , groupSeparator (groupSeparator)
33  {
34  if (!groupKey.empty ())
35  groupKey += groupSeparator;
36  }
37 
38  inline Preferences subGroupPreferences (const UTF8String& subGroup) const
39  {
40  return Preferences (groupKey + subGroup, groupSeparator);
41  }
42 
43  inline bool set (const UTF8String& key, const UTF8String& value) const
44  {
45  if (!groupKey.empty ())
46  return preferences.set (groupKey + key, value);
47  return preferences.set (key, value);
48  }
49 
50  inline Optional<UTF8String> get (const UTF8String& key) const
51  {
52  if (!groupKey.empty ())
53  return preferences.get (groupKey + key);
54  return preferences.get (key);
55  }
56 
57  template<typename T>
58  inline bool setNumber (const UTF8String& key, T value) const
59  {
60  return set (key, toString (value));
61  }
62 
63  template<typename T>
64  inline bool setFloat (const UTF8String& key, T value, uint32_t precision = 8) const
65  {
66  std::ostringstream sstream;
67  sstream.imbue (std::locale::classic ());
68  sstream.precision (static_cast<std::streamsize> (precision));
69  sstream << value;
70  return set (key, UTF8String (sstream.str ()));
71  }
72 
73  template<typename T>
74  inline Optional<T> getNumber (const UTF8String& key) const
75  {
76  if (auto p = get (key))
77  {
78  return UTF8StringView (*p).toNumber<T> ();
79  }
80  return {};
81  }
82 
83  inline bool setPoint (const UTF8String& key, CPoint p, uint32_t precision = 8) const
84  {
85  std::ostringstream sstream;
86  sstream.imbue (std::locale::classic ());
87  sstream.precision (static_cast<std::streamsize> (precision));
88  sstream << '{';
89  sstream << p.x;
90  sstream << ';';
91  sstream << p.y;
92  sstream << '}';
93  return set (key, UTF8String (sstream.str ()));
94  }
95 
96  inline Optional<CPoint> getPoint (const UTF8String& key) const
97  {
98  if (auto p = get (key))
99  {
100  std::istringstream sstream (p->getString ());
101  sstream.imbue (std::locale::classic ());
102  uint8_t c;
103  sstream >> c;
104  if (sstream.fail () || c != '{')
105  return {};
106  CCoord x;
107  sstream >> x;
108  sstream >> c;
109  if (sstream.fail () || c != ';')
110  return {};
111  CCoord y;
112  sstream >> y;
113  sstream >> c;
114  if (sstream.fail () || c != '}')
115  return {};
116  return {CPoint (x, y)};
117  }
118  return {};
119  }
120 
121  inline const UTF8String& getGroupKey () const { return groupKey; }
122  inline const UTF8String& getGroupSeparator () const { return groupSeparator; }
123 private:
124  Preferences (const Preferences&) = default;
125  Preferences& operator=(const Preferences&) = default;
126 
128  UTF8String groupKey;
129  UTF8String groupSeparator;
130 };
131 
132 //------------------------------------------------------------------------
133 } // Standalone
134 } // VSTGUI
virtual IPreference & getPreferences() const =0
Definition: customcontrols.cpp:8
virtual bool set(const UTF8String &key, const UTF8String &value)=0
a view on a null terminated UTF-8 String
Definition: cstring.h:172
static IApplication & instance()
Definition: application.cpp:451
Definition: preferences.h:17
holds an UTF8 encoded string and a platform representation of it
Definition: cstring.h:56
Definition: optional.h:18
virtual Optional< UTF8String > get(const UTF8String &key)=0
Point structure.
Definition: cpoint.h:17
Definition: ipreference.h:22