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
46 ~Config() = default;
47
48public:
49
51 MRMESH_API bool hasBool( const std::string& key ) const;
52
54 MRMESH_API bool getBool( const std::string& key, bool defaultValue = false ) const;
55
57 MRMESH_API void setBool( const std::string& key, bool keyValue );
58
59
61 MRMESH_API bool hasInt( const std::string& key ) const;
62
64 MRMESH_API int getInt( const std::string& key, int defaultValue = 0 ) const;
65
67 MRMESH_API void setInt( const std::string& key, int keyValue );
68
69
71 MRMESH_API bool hasColor( const std::string& key ) const;
72
74 MRMESH_API Color getColor( const std::string& key, const Color& defaultValue = Color::black() ) const;
75
77 MRMESH_API void setColor( const std::string& key, const Color& keyValue );
78
80 MRMESH_API bool hasFileStack( const std::string& key ) const;
81
83 MRMESH_API FileNamesStack getFileStack( const std::string& key, const FileNamesStack& defaultValue = FileNamesStack() ) const;
84
86 MRMESH_API void setFileStack( const std::string& key, const FileNamesStack& keyValue );
87
88
90 MRMESH_API bool hasVector2i( const std::string& key ) const;
91
93 MRMESH_API Vector2i getVector2i( const std::string& key, const Vector2i& defaultValue = Vector2i() ) const;
94
96 MRMESH_API void setVector2i( const std::string& key, const Vector2i& keyValue );
97
99 typedef std::vector<const char*> Enum;
100
101
103 MRMESH_API bool hasEnum( const Enum &enumeration, const std::string& key ) const;
104
106 MRMESH_API int getEnum( const Enum& enumeration, const std::string& key, int defaultValue = 0 ) const;
107
109 MRMESH_API void setEnum( const Enum& enumeration, const std::string& key, int keyValue );
110
111 MRMESH_API bool hasViewportMask( const std::string& key ) const;
112 MRMESH_API ViewportMask getViewportMask( const std::string& key, ViewportMask defaultValue = {} ) const;
113 MRMESH_API void setViewportMask( const std::string& key, ViewportMask newValue );
114
116 MRMESH_API bool hasJsonValue( const std::string& key );
117
119 MRMESH_API Json::Value getJsonValue( const std::string& key, const Json::Value& defaultValue = {} );
120
122 MRMESH_API void setJsonValue( const std::string& key, const Json::Value& keyValue );
123
125 Json::Value toJson() const { return config_; }
126
128 void fromJson( const Json::Value& config ) { config_ = config; }
129private:
130 std::string appName_;
131
132 Json::Value config_;
133 std::filesystem::path filePath_;
135 std::shared_ptr<spdlog::logger> loggerHandle_ = Logger::instance().getSpdLogger();
138 mutable std::unordered_set<std::string> reportedMissingKeys_;
139};
140
141}
#define MRMESH_API
Definition MRMeshFwd.h:85
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:99
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:125
void fromJson(const Json::Value &config)
replace current config content with given one
Definition MRConfig.h:128
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; must be called explicitly, the destructor does not do it
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 strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
Definition MRColor.h:12