Knoodle
Knoodle is a Free Open-Source alternative for graph-based texture designers. It is a tool for creating procedural textures using a node-based graph editor. It is designed to be easy to use, to be able to create complex textures with a minimum of effort, and to be pluggable to any game engine and front-end.
 
Loading...
Searching...
No Matches
config_manager.hpp
1/**************************************************************************/
2/* config_manager.hpp */
3/**************************************************************************/
4/* This file is part of: */
5/* Knoodle */
6/* https://knoodlegraph.org */
7/**************************************************************************/
8/* Copyright (c) 2025 Knoodle contributors (vide AUTHORS.md) */
9/* */
10/* Permission is hereby granted, free of charge, to any person obtaining */
11/* a copy of this software and associated documentation files (the */
12/* "Software"), to deal in the Software without restriction, including */
13/* without limitation the rights to use, copy, modify, merge, publish, */
14/* distribute, sublicense, and/or sell copies of the Software, and to */
15/* permit persons to whom the Software is furnished to do so, subject to */
16/* the following conditions: */
17/* */
18/* The above copyright notice and this permission notice shall be */
19/* included in all copies or substantial portions of the Software. */
20/* */
21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28/**************************************************************************/
29
30#pragma once
31
32#include <filesystem>
33#include <optional>
34#include <set>
35#include <string>
36#include <string_view>
37#include <unordered_map>
38#include "common.hpp"
39#include "core_api.hpp"
40
41namespace kn {
42/**
43 * Manages the configuration of the application.
44 * The configuration is read from a file and stored in memory.
45 */
46class KN_CORE_API ConfigManager {
47 ConfigManager() = default;
48
49 public:
50 ~ConfigManager() = default;
51
52 static ConfigManager& get_instance();
53
54 /**
55 * Loads the configuration from a file and append it to the current configuration.
56 * @param file_path The path to the file to load.
57 * @return True if the file was successfully loaded, false otherwise.
58 */
59 bool load_config(const std::filesystem::path& file_path);
60
61 /** Return a set of all the registered sections. */
62 [[nodiscard]] std::set<std::string> get_sections() const;
63
64 /**
65 * Returns a value by key.
66 * @param key The key to search for.
67 * @return The value associated with the key, if it exists.
68 */
69 [[nodiscard]] std::optional<std::string> get_value(const std::string_view& key) const;
70
71 /**
72 * Returns a value by key as integer.
73 * @param key The key to search for.
74 * @return The value associated with the key, if it exists.
75 */
76 [[nodiscard]] std::optional<int32_t> get_int_value(const std::string_view& key) const;
77
78 /**
79 * Returns a value by key as real_t.
80 * @param key The key to search for.
81 * @return The value associated with the key, if it exists.
82 */
83 [[nodiscard]] std::optional<real_t> get_real_value(const std::string_view& key) const;
84
85 /**
86 * Returns a value by key as boolean.
87 * @param key The key to search for.
88 * @return The value associated with the key, if it exists.
89 */
90 [[nodiscard]] std::optional<bool> get_bool_value(const std::string_view& key) const;
91
92 protected:
93 /**
94 * Parses a line from the configuration file.
95 * @param line The line to parse.
96 */
97 virtual void parse_line(const std::string_view& line);
98 /**
99 * Parses a key-value pair from a line.
100 * @param line The line to parse.
101 */
102 virtual void parse_key_value(const std::string_view& line);
103 /**
104 * Parses a comment from a line.
105 * @param line The line to parse.
106 */
107 virtual void parse_comment(const std::string_view& line);
108 /**
109 * Parses a section from a line.
110 * @param line The line to parse.
111 */
112 virtual void parse_section(const std::string_view& line);
113 /**
114 * Parses a blank line from a line.
115 * @param line The line to parse.
116 */
117 virtual void parse_blank(const std::string_view& line);
118 /**
119 * Parses a line from the configuration file.
120 * @param line The line to parse.
121 */
122
123 private:
124 std::unordered_map<std::string, std::string> _config;
125
126 std::string _current_section;
127};
128} // namespace kn
Definition config_manager.hpp:46