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
12namespace MR
13{
14
15using FileNamesStack = std::vector<std::filesystem::path>;
16
17class Config
18{
19public:
20 Config( Config const& ) = delete;
21 void operator=( Config const& ) = delete;
22
24
27 MRMESH_API void reset( std::string appName );
28
29 MRMESH_API const std::string& getAppName() const;
30
33
34private:
35 Config();
36
38 void reset( const std::filesystem::path& filePath );
39
40
43 ~Config();
44
45public:
46
48 MRMESH_API bool hasBool( const std::string& key ) const;
49
51 MRMESH_API bool getBool( const std::string& key, bool defaultValue = false ) const;
52
54 MRMESH_API void setBool( const std::string& key, bool keyValue );
55
56
58 MRMESH_API bool hasColor( const std::string& key ) const;
59
61 MRMESH_API Color getColor( const std::string& key, const Color& defaultValue = Color::black() ) const;
62
64 MRMESH_API void setColor( const std::string& key, const Color& keyValue );
65
67 MRMESH_API bool hasFileStack( const std::string& key ) const;
68
70 MRMESH_API FileNamesStack getFileStack( const std::string& key, const FileNamesStack& defaultValue = FileNamesStack() ) const;
71
73 MRMESH_API void setFileStack( const std::string& key, const FileNamesStack& keyValue );
74
75
77 MRMESH_API bool hasVector2i( const std::string& key ) const;
78
80 MRMESH_API Vector2i getVector2i( const std::string& key, const Vector2i& defaultValue = Vector2i() ) const;
81
83 MRMESH_API void setVector2i( const std::string& key, const Vector2i& keyValue );
84
86 typedef std::vector<const char*> Enum;
87
88
90 MRMESH_API bool hasEnum( const Enum &enumeration, const std::string& key ) const;
91
93 MRMESH_API int getEnum( const Enum& enumeration, const std::string& key, int defaultValue = 0 ) const;
94
96 MRMESH_API void setEnum( const Enum& enumeration, const std::string& key, int keyValue );
97
98 MRMESH_API bool hasViewportMask( const std::string& key ) const;
99 MRMESH_API ViewportMask getViewportMask( const std::string& key, ViewportMask defaultValue = {} ) const;
100 MRMESH_API void setViewportMask( const std::string& key, ViewportMask newValue );
101
103 MRMESH_API bool hasJsonValue( const std::string& key );
104
106 MRMESH_API Json::Value getJsonValue( const std::string& key, const Json::Value& defaultValue = {} );
107
109 MRMESH_API void setJsonValue( const std::string& key, const Json::Value& keyValue );
110
112 Json::Value toJson() const { return config_; }
113
115 void fromJson( const Json::Value& config ) { config_ = config; }
116private:
117 std::string appName_;
118
119 Json::Value config_;
120 std::filesystem::path filePath_;
121 // prolong logger life
122 std::shared_ptr<spdlog::logger> loggerHandle_ = Logger::instance().getSpdLogger();
123};
124
125} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:80
Definition MRConfig.h:18
MRMESH_API bool getBool(const std::string &key, bool defaultValue=false) const
returns MRColor with presented key
MRMESH_API Color getColor(const std::string &key, const Color &defaultValue=Color::black()) const
returns MRColor with presented key
MRMESH_API void setEnum(const Enum &enumeration, const std::string &key, int keyValue)
sets custom enumeration value
std::vector< const char * > Enum
Description of a enumeration as a map between [0...N) and N strings.
Definition MRConfig.h:86
MRMESH_API void reset(std::string appName)
MRMESH_API bool hasColor(const std::string &key) const
returns true if MRColor with presented key exists
MRMESH_API bool hasVector2i(const std::string &key) const
returns true if Vector2i with presented key exists
MRMESH_API void writeToFile()
writes current config to file. (implicitly called from destructor)
Json::Value toJson() const
returns json with content of this config
Definition MRConfig.h:112
void fromJson(const Json::Value &config)
replace current config content with given one
Definition MRConfig.h:115
MRMESH_API void setVector2i(const std::string &key, const Vector2i &keyValue)
sets Vector2i for presented key
MRMESH_API int getEnum(const Enum &enumeration, const std::string &key, int defaultValue=0) const
returns custom enumeration value
MRMESH_API const std::string & getAppName() const
MRMESH_API void setJsonValue(const std::string &key, const Json::Value &keyValue)
sets custom json value
MRMESH_API bool hasBool(const std::string &key) const
returns true if MRColor with presented key exists
MRMESH_API bool hasViewportMask(const std::string &key) const
MRMESH_API FileNamesStack getFileStack(const std::string &key, const FileNamesStack &defaultValue=FileNamesStack()) const
returns 'recently used' files list
MRMESH_API ViewportMask getViewportMask(const std::string &key, ViewportMask defaultValue={}) const
MRMESH_API bool hasFileStack(const std::string &key) const
returns true if 'recently used' files exist
MRMESH_API void setViewportMask(const std::string &key, ViewportMask newValue)
MRMESH_API Json::Value getJsonValue(const std::string &key, const Json::Value &defaultValue={})
returns custom json value
MRMESH_API Vector2i getVector2i(const std::string &key, const Vector2i &defaultValue=Vector2i()) const
returns Vector2i with presented key
static MRMESH_API Config & instance()
MRMESH_API void setBool(const std::string &key, bool keyValue)
sets MRColor for presented key
void operator=(Config const &)=delete
MRMESH_API void setFileStack(const std::string &key, const FileNamesStack &keyValue)
sets 'recently used' files list
MRMESH_API bool hasEnum(const Enum &enumeration, const std::string &key) const
returns true if given enumeration value with this key exists and is correct
MRMESH_API void setColor(const std::string &key, const Color &keyValue)
sets MRColor for presented key
MRMESH_API bool hasJsonValue(const std::string &key)
returns true if json value with this key exists
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)
stores mask of viewport unique identifiers
Definition MRViewportId.h:42
Definition MRCameraOrientationPlugin.h:8
std::vector< std::filesystem::path > FileNamesStack
Definition MRIOFilesMenuItems.h:14
Definition MRMesh/MRColor.h:9
static constexpr Color black() noexcept
Definition MRMesh/MRColor.h:26