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
25 // looks up (~/.local/share/<appname>/config.json) or (AppData<appname>\config.json)
26 // creates directory if not presented
27 MRMESH_API void reset( std::string appName );
28
29 MRMESH_API const std::string& getAppName() const;
30
31 // writes current config to file. (implicitly called from destructor)
33
34private:
35 Config();
36
37 // looks for presented *.json file
38 void reset( const std::filesystem::path& filePath );
39
40
41 // stores configuration depends on constructor call: (<filepath>) or
42 // (~/.local/share/<appname>/config.json) or (AppData<appname>\config.json)
43 ~Config();
44
45public:
46 // returns true if MRColor with presented key exists
47 MRMESH_API bool hasBool( const std::string& key ) const;
48 // returns MRColor with presented key
49 MRMESH_API bool getBool( const std::string& key, bool defaultValue = false ) const;
50 // sets MRColor for presented key
51 MRMESH_API void setBool( const std::string& key, bool keyValue );
52
53 // returns true if MRColor with presented key exists
54 MRMESH_API bool hasColor( const std::string& key ) const;
55 // returns MRColor with presented key
56 MRMESH_API Color getColor( const std::string& key, const Color& defaultValue = Color::black() ) const;
57 // sets MRColor for presented key
58 MRMESH_API void setColor( const std::string& key, const Color& keyValue );
59
60 // returns true if 'recently used' files exist
61 MRMESH_API bool hasFileStack( const std::string& key ) const;
62 // returns 'recently used' files list
63 MRMESH_API FileNamesStack getFileStack( const std::string& key, const FileNamesStack& defaultValue = FileNamesStack() ) const;
64 // sets 'recently used' files list
65 MRMESH_API void setFileStack( const std::string& key, const FileNamesStack& keyValue );
66
67 // returns true if Vector2i with presented key exists
68 MRMESH_API bool hasVector2i( const std::string& key ) const;
69 // returns Vector2i with presented key
70 MRMESH_API Vector2i getVector2i( const std::string& key, const Vector2i& defaultValue = Vector2i() ) const;
71 // sets Vector2i for presented key
72 MRMESH_API void setVector2i( const std::string& key, const Vector2i& keyValue );
73
74 // Decription of a enumeration as a map between [0...N) and N strings
75 typedef std::vector<const char*> Enum;
76
77 // returns true if given enumeration value with this key exists and is correct
78 MRMESH_API bool hasEnum( const Enum &enumeration, const std::string& key ) const;
79 // returns custom enumeration value
80 MRMESH_API int getEnum( const Enum& enumeration, const std::string& key, int defaultValue = 0 ) const;
81 // sets custom enumeration value
82 MRMESH_API void setEnum( const Enum& enumeration, const std::string& key, int keyValue );
83
84 MRMESH_API bool hasViewportMask( const std::string& key ) const;
85 MRMESH_API ViewportMask getViewportMask( const std::string& key, ViewportMask defaultValue = {} ) const;
86 MRMESH_API void setViewportMask( const std::string& key, ViewportMask newValue );
87
88 // returns true if json value with this key exists
89 MRMESH_API bool hasJsonValue( const std::string& key );
90 // returns custom json value
91 MRMESH_API Json::Value getJsonValue( const std::string& key, const Json::Value& defaultValue = {} );
92 // sets custom json value
93 MRMESH_API void setJsonValue( const std::string& key, const Json::Value& keyValue );
94
95private:
96 std::string appName_;
97
98 Json::Value config_;
99 std::filesystem::path filePath_;
100 // prolong logger life
101 std::shared_ptr<spdlog::logger> loggerHandle_ = Logger::instance().getSpdLogger();
102};
103
104} // 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
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:75
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 bool hasViewportMask(const std::string &key) const
MRMESH_API FileNamesStack getFileStack(const std::string &key, const FileNamesStack &defaultValue=FileNamesStack()) const
MRMESH_API ViewportMask getViewportMask(const std::string &key, ViewportMask defaultValue={}) const
MRMESH_API bool hasFileStack(const std::string &key) const
MRMESH_API void setViewportMask(const std::string &key, ViewportMask newValue)
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)
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