MeshLib C++ Docs
Loading...
Searching...
No Matches
MRConfig.h
Go to the documentation of this file.
1#pragma once
3#include "MRMeshFwd.h"
4#include "MRColor.h"
5#include "MRSerializer.h"
6#include "MRPch/MRJson.h"
7#include "MRVector2.h"
8#include "MRLog.h"
9#include <filesystem>
10#include <string>
11#include <unordered_set>
12
13namespace MR
14{
17
18
19using FileNamesStack = std::vector<std::filesystem::path>;
20
21class Config
22{
23public:
24 Config( Config const& ) = delete;
25 void operator=( Config const& ) = delete;
26
28
31 MRMESH_API void reset( std::string appName );
32
34 MRMESH_API const std::string& getAppName() const;
35
38
39private:
40 Config();
41
43 void reset( const std::filesystem::path& filePath );
44
45
48 ~Config();
49
50public:
51
53 MRMESH_API bool hasBool( const std::string& key ) const;
54
56 MRMESH_API bool getBool( const std::string& key, bool defaultValue = false ) const;
57
59 MRMESH_API void setBool( const std::string& key, bool keyValue );
60
61
63 MRMESH_API bool hasInt( const std::string& key ) const;
64
66 MRMESH_API int getInt( const std::string& key, int defaultValue = 0 ) const;
67
69 MRMESH_API void setInt( const std::string& key, int keyValue );
70
71
73 MRMESH_API bool hasColor( const std::string& key ) const;
74
76 MRMESH_API Color getColor( const std::string& key, const Color& defaultValue = Color::black() ) const;
77
79 MRMESH_API void setColor( const std::string& key, const Color& keyValue );
80
82 MRMESH_API bool hasFileStack( const std::string& key ) const;
83
85 MRMESH_API FileNamesStack getFileStack( const std::string& key, const FileNamesStack& defaultValue = FileNamesStack() ) const;
86
88 MRMESH_API void setFileStack( const std::string& key, const FileNamesStack& keyValue );
89
90
92 MRMESH_API bool hasVector2i( const std::string& key ) const;
93
95 MRMESH_API Vector2i getVector2i( const std::string& key, const Vector2i& defaultValue = Vector2i() ) const;
96
98 MRMESH_API void setVector2i( const std::string& key, const Vector2i& keyValue );
99
101 typedef std::vector<const char*> Enum;
102
103
105 MRMESH_API bool hasEnum( const Enum &enumeration, const std::string& key ) const;
106
108 MRMESH_API int getEnum( const Enum& enumeration, const std::string& key, int defaultValue = 0 ) const;
109
111 MRMESH_API void setEnum( const Enum& enumeration, const std::string& key, int keyValue );
112
113 MRMESH_API bool hasViewportMask( const std::string& key ) const;
114 MRMESH_API ViewportMask getViewportMask( const std::string& key, ViewportMask defaultValue = {} ) const;
115 MRMESH_API void setViewportMask( const std::string& key, ViewportMask newValue );
116
118 MRMESH_API bool hasJsonValue( const std::string& key );
119
121 MRMESH_API Json::Value getJsonValue( const std::string& key, const Json::Value& defaultValue = {} );
122
124 MRMESH_API void setJsonValue( const std::string& key, const Json::Value& keyValue );
125
127 Json::Value toJson() const { return config_; }
128
130 void fromJson( const Json::Value& config ) { config_ = config; }
131private:
132 std::string appName_;
133
134 Json::Value config_;
135 std::filesystem::path filePath_;
137 std::shared_ptr<spdlog::logger> loggerHandle_ = Logger::instance().getSpdLogger();
140 mutable std::unordered_set<std::string> reportedMissingKeys_;
141};
142
143}
#define MRMESH_API
Definition MRMeshFwd.h:80
static Logger & instance()
const std::shared_ptr< spdlog::logger > & getSpdLogger() const
store this pointer if need to prolong logger life time (necessary to log something from destructors)
stores mask of viewport unique identifiers
Definition MRViewportId.h:42
Json::Value getJsonValue(const std::string &key, const Json::Value &defaultValue={})
returns custom json value
int getEnum(const Enum &enumeration, const std::string &key, int defaultValue=0) const
returns custom enumeration value
bool hasFileStack(const std::string &key) const
returns true if 'recently used' files exist
std::vector< const char * > Enum
Description of a enumeration as a map between [0...N) and N strings.
Definition MRConfig.h:101
void setBool(const std::string &key, bool keyValue)
sets bool for presented key
FileNamesStack getFileStack(const std::string &key, const FileNamesStack &defaultValue=FileNamesStack()) const
returns 'recently used' files list
Json::Value toJson() const
returns json with content of this config
Definition MRConfig.h:127
void fromJson(const Json::Value &config)
replace current config content with given one
Definition MRConfig.h:130
static constexpr Color black() noexcept
Definition MRColor.h:29
bool hasJsonValue(const std::string &key)
returns true if json value with this key exists
Vector2i getVector2i(const std::string &key, const Vector2i &defaultValue=Vector2i()) const
returns Vector2i with presented key
void setInt(const std::string &key, int keyValue)
sets int for presented key
void setColor(const std::string &key, const Color &keyValue)
sets MRColor for presented key
static Config & instance()
bool hasVector2i(const std::string &key) const
returns true if Vector2i with presented key exists
bool hasColor(const std::string &key) const
returns true if MRColor with presented key exists
bool hasEnum(const Enum &enumeration, const std::string &key) const
returns true if given enumeration value with this key exists and is correct
Color getColor(const std::string &key, const Color &defaultValue=Color::black()) const
returns MRColor with presented key
void setEnum(const Enum &enumeration, const std::string &key, int keyValue)
sets custom enumeration value
int getInt(const std::string &key, int defaultValue=0) const
returns int with presented key
void setVector2i(const std::string &key, const Vector2i &keyValue)
sets Vector2i for presented key
const std::string & getAppName() const
the name of the application, UTF8 encoded
std::vector< std::filesystem::path > FileNamesStack
Definition MRIOFilesMenuItems.h:17
bool hasViewportMask(const std::string &key) const
bool hasInt(const std::string &key) const
returns true if int with presented key exists
void reset(std::string appName)
void writeToFile()
writes current config to file. (implicitly called from destructor)
void setViewportMask(const std::string &key, ViewportMask newValue)
void setFileStack(const std::string &key, const FileNamesStack &keyValue)
sets 'recently used' files list
bool hasBool(const std::string &key) const
returns true if bool with presented key exists
void operator=(Config const &)=delete
void setJsonValue(const std::string &key, const Json::Value &keyValue)
sets custom json value
bool getBool(const std::string &key, bool defaultValue=false) const
returns bool with presented key
Config(Config const &)=delete
ViewportMask getViewportMask(const std::string &key, ViewportMask defaultValue={}) const
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRColor.h:12