MeshLib Documentation
Loading...
Searching...
No Matches
MRConfig.h
Go to the documentation of this file.
1#pragma once
2#include "MRMeshFwd.h"
3#include "MRColor.h"
4#include "MRSerializer.h"
5#include "MRPch/MRJson.h"
6#include "MRVector2.h"
7#include "MRLog.h"
8#include <filesystem>
9#include <string>
10
11namespace MR
12{
13
14using FileNamesStack = std::vector<std::filesystem::path>;
15
16class Config
17{
18public:
19 Config( Config const& ) = delete;
20 void operator=( Config const& ) = delete;
21
23
24 // looks up (~/.local/share/<appname>/config.json) or (AppData<appname>\config.json)
25 // creates directory if not presented
26 MRMESH_API void reset( std::string appName );
27
28 MRMESH_API const std::string& getAppName() const;
29
30 // writes current config to file. (imlicitly called from destructor)
32
33private:
34 Config();
35
36 // looks for presented *.json file
37 void reset( const std::filesystem::path& filePath );
38
39
40 // stores configuration depends on constructor call: (<filepath>) or
41 // (~/.local/share/<appname>/config.json) or (AppData<appname>\config.json)
42 ~Config();
43
44public:
45 // returns true if MRColor with presented key exists
46 MRMESH_API bool hasBool( const std::string& key ) const;
47 // returns MRColor with presented key
48 MRMESH_API bool getBool( const std::string& key, bool defaultValue = false ) const;
49 // sets MRColor for presented key
50 MRMESH_API void setBool( const std::string& key, bool keyValue );
51
52 // returns true if MRColor with presented key exists
53 MRMESH_API bool hasColor( const std::string& key ) const;
54 // returns MRColor with presented key
55 MRMESH_API Color getColor( const std::string& key, const Color& defaultValue = Color::black() ) const;
56 // sets MRColor for presented key
57 MRMESH_API void setColor( const std::string& key, const Color& keyValue );
58
59 // returns true if 'recently used' files exist
60 MRMESH_API bool hasFileStack( const std::string& key ) const;
61 // returns 'recently used' files list
62 MRMESH_API FileNamesStack getFileStack( const std::string& key, const FileNamesStack& defaultValue = FileNamesStack() ) const;
63 // sets 'recently used' files list
64 MRMESH_API void setFileStack( const std::string& key, const FileNamesStack& keyValue );
65
66 // returns true if Vector2i with presented key exists
67 MRMESH_API bool hasVector2i( const std::string& key ) const;
68 // returns Vector2i with presented key
69 MRMESH_API Vector2i getVector2i( const std::string& key, const Vector2i& defaultValue = Vector2i() ) const;
70 // sets Vector2i for presented key
71 MRMESH_API void setVector2i( const std::string& key, const Vector2i& keyValue );
72
73 // Decription of a enumeration as a map between [0...N) and N strings
74 typedef std::vector<const char*> Enum;
75
76 // returns true if given enumeration value with this key exists and is correct
77 MRMESH_API bool hasEnum( const Enum &enumeration, const std::string& key ) const;
78 // returns custom enumeration value
79 MRMESH_API int getEnum( const Enum& enumeration, const std::string& key, int defaultValue = 0 ) const;
80 // sets custom enumeration value
81 MRMESH_API void setEnum( const Enum& enumeration, const std::string& key, int keyValue );
82
83 // returns true if json value with this key exists
84 MRMESH_API bool hasJsonValue( const std::string& key );
85 // returns custom json value
86 MRMESH_API Json::Value getJsonValue( const std::string& key, const Json::Value& defaultValue = {} );
87 // sets custom json value
88 MRMESH_API void setJsonValue( const std::string& key, const Json::Value& keyValue );
89
90private:
91 std::string appName_;
92
93 Json::Value config_;
94 std::filesystem::path filePath_;
95 // prolong logger life
96 std::shared_ptr<spdlog::logger> loggerHandle_ = Logger::instance().getSpdLogger();
97};
98
99} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRConfig.h:17
MRMESH_API bool getBool(const std::string &key, bool defaultValue=false) const
MRMESH_API Color getColor(const std::string &key, const Color &defaultValue=Color::black()) const
MRMESH_API void setEnum(const Enum &enumeration, const std::string &key, int keyValue)
std::vector< const char * > Enum
Definition MRConfig.h:74
MRMESH_API void reset(std::string appName)
MRMESH_API bool hasColor(const std::string &key) const
MRMESH_API bool hasVector2i(const std::string &key) const
MRMESH_API void writeToFile()
MRMESH_API void setVector2i(const std::string &key, const Vector2i &keyValue)
MRMESH_API int getEnum(const Enum &enumeration, const std::string &key, int defaultValue=0) const
MRMESH_API const std::string & getAppName() const
MRMESH_API void setJsonValue(const std::string &key, const Json::Value &keyValue)
MRMESH_API bool hasBool(const std::string &key) const
MRMESH_API FileNamesStack getFileStack(const std::string &key, const FileNamesStack &defaultValue=FileNamesStack()) const
MRMESH_API bool hasFileStack(const std::string &key) const
MRMESH_API Json::Value getJsonValue(const std::string &key, const Json::Value &defaultValue={})
MRMESH_API Vector2i getVector2i(const std::string &key, const Vector2i &defaultValue=Vector2i()) const
static MRMESH_API Config & instance()
MRMESH_API void setBool(const std::string &key, bool keyValue)
void operator=(Config const &)=delete
MRMESH_API void setFileStack(const std::string &key, const FileNamesStack &keyValue)
MRMESH_API bool hasEnum(const Enum &enumeration, const std::string &key) const
MRMESH_API void setColor(const std::string &key, const Color &keyValue)
MRMESH_API bool hasJsonValue(const std::string &key)
Config(Config const &)=delete
static MRMESH_API Logger & instance()
MRMESH_API const std::shared_ptr< spdlog::logger > & getSpdLogger() const
store this pointer if need to prolong logger life time (necessary to log something from destructors)
Definition MRCameraOrientationPlugin.h:8
std::vector< std::filesystem::path > FileNamesStack
Definition MRIOFilesMenuItems.h:14
Definition MRColor.h:9
static constexpr Color black() noexcept
Definition MRColor.h:28