#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_SUPPORT_SETTINGS_REGISTRYSETTINGSSTORE_H #define NUCLEX_SUPPORT_SETTINGS_REGISTRYSETTINGSSTORE_H #include "Nuclex/Support/Config.h" #if defined(NUCLEX_SUPPORT_WINDOWS) #include "Nuclex/Support/Settings/SettingsStore.h" #include // for std::intptr_t namespace Nuclex { namespace Support { namespace Settings { // ------------------------------------------------------------------------------------------- // /// Stores application settings in the Windows registry /// /// /// With this implementation of the settings store, you can read and write settings /// from and into the Windows registry. The registry is a giant multi-leveled database /// of properties that stores vital operating system data together with application /// specific settings. /// /// /// The registry is not commonly accessed or understood by the user, there is no built-in /// documentation mechanism, it's not portable beyond Windows operating systems and /// you're prone to leave orphaned settings behind when uninstalling. Thus, unless you're /// having specific reason to interface with the registry, it's usually a bad idea that /// will only make your application harder to maintain and harder to port. /// /// /// Any changes made to the settings are immediately reflected in the registry. If you /// need transient changes, you should create a and /// copy all settings over, then make the changes in the memory settings store. /// /// /// If you're storing any live data (i.e. resume indices or such) for your application, /// also notice that even on Windows 10 systems, the registry is amazingly slow and /// seemingly harmless operations may take several milliseconds to complete. /// /// /// /// void test() { /// RegistrySettingsStore settings(u8"HKCU/MyCompany/MyGame"); /// /// // Retrieve() returns std::optional<T>, so you can either /// // check if the value was present with .has_value() and .value() or /// // directly provide a default via .value_or() as shown below /// /// std::uint32_t resolutionX = settings.Retrieve<std::uint32_t>( /// u8"Video", u8"ResolutionX").value_or(1920) /// ); /// std::uint32_t resolutionY = settings.Retrieve<std::uint32_t>( /// u8"Video", u8"ResolutionY").value_or(1080) /// ); /// /// settings.Store<bool>(std::string(), u8"FirstLaunch", false); /// } /// /// /// /// In place of "HKCU", you can also use the other registry hives or /// their shortcuts, for example "HKEY_LOCAL_MACHINE" or /// "HKLM". Do note that write access to "HKLM" requires /// administative privileges. For normal application settings, /// "HKEY_CURRENT_USER" aka "HKCU" is the intended place. /// /// class NUCLEX_SUPPORT_TYPE RegistrySettingsStore : public SettingsStore { /// Deletes the specified registry key and all its subkeys /// Path of the key that will be deleted /// True if the key existed and was deleted, false otherwise /// /// You can use this method to eliminate a key again if you want to remove your /// application's settings from the registry. It is also used by unit tests to /// ensure no garbage from previous tests is left to interfere with testing. /// public: NUCLEX_SUPPORT_API static bool DeleteKey(const std::string ®istryPath); /// /// Initializes a new registry settings store with settings storing under /// the specified registry key /// /// /// Absolute path of the registry key that will be accessed. This must include /// the registry hive in short or long form. /// /// /// Whether the registry key should be opened for writing (default). /// /// /// /// Any registry path must begin with the hive, for example: /// "HKCU/HKEY_CURRENT_USER/SOFTWARE/MyCompany/MyApplication" or, /// another example using the long form registry hive, /// "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/MyService". /// Paths are encoded as UTF-8 with forward slashes. /// /// /// By setting the argument to 'false', access flags /// are passed to the Windows registry API that may allow reading from some keys /// that would otherwise require administrative privileges to access. If you set /// this parameter to 'false', it's a good idea to declare the registry settings /// store as const to ensure you won't access any of the write methods. /// /// public: NUCLEX_SUPPORT_API RegistrySettingsStore( const std::string ®istryPath, bool writable = true ); /// Frees all resources owned by the .ini settings store public: NUCLEX_SUPPORT_API ~RegistrySettingsStore() override; /// Returns a list of all categories contained in the store /// A list of all categories present in the store currently public: NUCLEX_SUPPORT_API std::vector GetAllCategories() const override; /// Returns a list of all properties found within a category /// Name of the category whose properties will be returned /// A list of all properties present in the specified category /// /// If the root level of properties should be listed, pass an empty string as /// the category name. Specifying the name of a category that doesn't exist will /// simply return an empty list (because ) /// public: NUCLEX_SUPPORT_API std::vector GetAllProperties( const std::string &categoryName = std::string() ) const override; /// Deletes an entire category with all its properties from the store /// Name of the category that will be deleted /// True if the category existed and was deleted, false otherwise public: NUCLEX_SUPPORT_API bool DeleteCategory( const std::string &categoryName ) override; /// Deletes the specified property from the store /// /// Name of the category from which the property will be deleted /// /// Name of the property that will be deleted /// True if the property existed and was deleted, false otherwise public: NUCLEX_SUPPORT_API bool DeleteProperty( const std::string &categoryName, const std::string &propertyName ) override; /// Retrieves the value of a boolean property from the store /// Category from which the property will be read /// Name of the property whose value will be read /// The value of the requested property or nothing if it didn't exist protected: NUCLEX_SUPPORT_API std::optional RetrieveBooleanProperty( const std::string &categoryName, const std::string &propertyName ) const override; /// Retrieves the value of a 32 bit integer property from the store /// Category from which the property will be read /// Name of the property whose value will be read /// The value of the requested property or nothing if it didn't exist protected: NUCLEX_SUPPORT_API std::optional RetrieveUInt32Property( const std::string &categoryName, const std::string &propertyName ) const override; /// Retrieves the value of a 32 bit integer property from the store /// Category from which the property will be read /// Name of the property whose value will be read /// The value of the requested property or nothing if it didn't exist protected: NUCLEX_SUPPORT_API std::optional RetrieveInt32Property( const std::string &categoryName, const std::string &propertyName ) const override; /// Retrieves the value of a 64 bit integer property from the store /// Category from which the property will be read /// Name of the property whose value will be read /// The value of the requested property or nothing if it didn't exist protected: NUCLEX_SUPPORT_API std::optional RetrieveUInt64Property( const std::string &categoryName, const std::string &propertyName ) const override; /// Retrieves the value of a 64 bit integer property from the store /// Category from which the property will be read /// Name of the property whose value will be read /// The value of the requested property or nothing if it didn't exist protected: NUCLEX_SUPPORT_API std::optional RetrieveInt64Property( const std::string &categoryName, const std::string &propertyName ) const override; /// Retrieves the value of a string property from the store /// Category from which the property will be read /// Name of the property whose value will be read /// The value of the requested property or nothing if it didn't exist protected: NUCLEX_SUPPORT_API std::optional RetrieveStringProperty( const std::string &categoryName, const std::string &propertyName ) const override; /// Stores or updates a boolean property in the settings store /// Category from which the property will be read /// Name of the property whose value will be read /// Value that will be stored protected: NUCLEX_SUPPORT_API void StoreBooleanProperty( const std::string &categoryName, const std::string &propertyName, bool value ) override; /// Stores or updates a 32 bit integer property in the settings store /// Category from which the property will be read /// Name of the property whose value will be read /// Value that will be stored protected: NUCLEX_SUPPORT_API void StoreUInt32Property( const std::string &categoryName, const std::string &propertyName, std::uint32_t value ) override; /// Stores or updates a 32 bit integer property in the settings store /// Category from which the property will be read /// Name of the property whose value will be read /// Value that will be stored protected: NUCLEX_SUPPORT_API void StoreInt32Property( const std::string &categoryName, const std::string &propertyName, std::int32_t value ) override; /// Stores or updates a 64 bit integer property in the settings store /// Category from which the property will be read /// Name of the property whose value will be read /// Value that will be stored protected: NUCLEX_SUPPORT_API void StoreUInt64Property( const std::string &categoryName, const std::string &propertyName, std::uint64_t value ) override; /// Stores or updates a 64 bit integer property in the settings store /// Category from which the property will be read /// Name of the property whose value will be read /// Value that will be stored protected: NUCLEX_SUPPORT_API void StoreInt64Property( const std::string &categoryName, const std::string &propertyName, std::int64_t value ) override; /// Stores or updates a string property in the settings store /// Category from which the property will be read /// Name of the property whose value will be read /// Value that will be stored protected: NUCLEX_SUPPORT_API void StoreStringProperty( const std::string &categoryName, const std::string &propertyName, const std::string &value ) override; /// A registry key handle (HKEY) for the opened settings root key private: std::intptr_t settingsKeyHandle; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Settings #endif // defined(NUCLEX_SUPPORT_WINDOWS) #endif // NUCLEX_SUPPORT_SETTINGS_REGISTRYSETTINGSSTORE_H