1 #pragma once
2 
3 #include <string>
4 #include <map>
5 #include <mutex>
6 #include "filesystem_def.h"
7 #include "xml/Node.h"
8 
9 namespace Framework
10 {
11 	class CConfig
12 	{
13 	public:
14 		typedef fs::path PathType;
15 
16 											CConfig(const PathType&, bool readonly = false);
17 											CConfig(const CConfig&) = delete;
18 		virtual								~CConfig();
19 
20 		CConfig&							operator =(const CConfig&) = delete;
21 
22 		static std::string					MakePreferenceName(const std::string&, const std::string& = "", const std::string& = "", const std::string& = "");
23 
24 		void								RegisterPreferenceInteger(const char*, int);
25 		void								RegisterPreferenceBoolean(const char*, bool);
26 		void								RegisterPreferenceString(const char*, const char*);
27 		void								RegisterPreferencePath(const char*, const PathType&);
28 
29 		int									GetPreferenceInteger(const char*);
30 		bool								GetPreferenceBoolean(const char*);
31 		const char*							GetPreferenceString(const char*);
32 		PathType							GetPreferencePath(const char*);
33 
34 		bool								SetPreferenceInteger(const char*, int);
35 		bool								SetPreferenceBoolean(const char*, bool);
36 		bool								SetPreferenceString(const char*, const char*);
37 		bool								SetPreferencePath(const char*, const PathType&);
38 
39 		void								Save();
40 		PathType							GetConfigPath() const;
41 
42 	private:
43 		enum PREFERENCE_TYPE
44 		{
45 			TYPE_INTEGER,
46 			TYPE_BOOLEAN,
47 			TYPE_STRING,
48 			TYPE_PATH,
49 		};
50 
51 		class CPreference
52 		{
53 		public:
54 											CPreference(const char*, PREFERENCE_TYPE);
55 			virtual							~CPreference() = default;
56 			const char*						GetName() const;
57 			PREFERENCE_TYPE					GetType() const;
58 			const char*						GetTypeString() const;
59 			virtual void					Serialize(Framework::Xml::CNode*) const;
60 
61 		private:
62 			std::string						m_name;
63 			PREFERENCE_TYPE					m_type;
64 		};
65 
66 		class CPreferenceInteger : public CPreference
67 		{
68 		public:
69 			enum
70 			{
71 				PREFERENCE_TYPE_ID = TYPE_INTEGER
72 			};
73 
74 											CPreferenceInteger(const char*, int);
75 			virtual							~CPreferenceInteger() = default;
76 			int								GetValue() const;
77 			void							SetValue(int);
78 			void							Serialize(Framework::Xml::CNode*) const override;
79 
80 		private:
81 			int								m_value;
82 		};
83 
84 		class CPreferenceBoolean : public CPreference
85 		{
86 		public:
87 			enum
88 			{
89 				PREFERENCE_TYPE_ID = TYPE_BOOLEAN
90 			};
91 
92 											CPreferenceBoolean(const char*, bool);
93 			virtual							~CPreferenceBoolean() = default;
94 			bool							GetValue() const;
95 			void							SetValue(bool);
96 			void							Serialize(Framework::Xml::CNode*) const override;
97 
98 		private:
99 			bool							m_value;
100 		};
101 
102 		class CPreferenceString : public CPreference
103 		{
104 		public:
105 			enum
106 			{
107 				PREFERENCE_TYPE_ID = TYPE_STRING
108 			};
109 
110 											CPreferenceString(const char*, const char*);
111 			virtual							~CPreferenceString() = default;
112 			const char*						GetValue() const;
113 			void							SetValue(const char*);
114 			void							Serialize(Framework::Xml::CNode*) const override;
115 
116 		private:
117 			std::string						m_value;
118 		};
119 
120 		class CPreferencePath : public CPreference
121 		{
122 		public:
123 			enum
124 			{
125 				PREFERENCE_TYPE_ID = TYPE_PATH
126 			};
127 
128 											CPreferencePath(const char*, const PathType&);
129 			virtual							~CPreferencePath() = default;
130 			PathType						GetValue() const;
131 			void							SetValue(const PathType&);
132 			void							Serialize(Framework::Xml::CNode*) const override;
133 
134 		private:
135 			PathType						m_value;
136 		};
137 
138 		typedef std::shared_ptr<CPreference> PreferencePtr;
139 		typedef std::map<std::string, PreferencePtr> PreferenceMapType;
140 
141 		void								Load();
142 		void								InsertPreference(const PreferencePtr&);
143 
144 		template <typename Type> std::shared_ptr<Type>			FindPreference(const char*);
145 
146 		template <typename Type>
CastPreference(const PreferencePtr & preference)147 		static std::shared_ptr<Type> CastPreference(const PreferencePtr& preference)
148 		{
149 			if(preference->GetType() != Type::PREFERENCE_TYPE_ID)
150 			{
151 				return std::shared_ptr<Type>();
152 			}
153 			return std::static_pointer_cast<Type>(preference);
154 		}
155 
156 		PreferenceMapType					m_preferences;
157 		std::mutex							m_mutex;
158 		PathType							m_path;
159 		bool								m_readonly;
160 	};
161 }
162