From ff2a2dbed0545f9c71eee3c87f99c74a67292624 Mon Sep 17 00:00:00 2001
From: EndrII <EndrIIMail@gmail.com>
Date: Sun, 21 Aug 2022 22:25:59 +0300
Subject: [PATCH] fix service

---
 CMakeLists.txt |  2 +-
 isettings.h    | 14 +++++++-------
 qaservice.h    |  9 ++++-----
 settings.cpp   |  8 ++++++++
 settings.h     | 26 ++++++++++++++++++++------
 5 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b1b6de3..644ea49 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -61,7 +61,7 @@ add_library(${PROJECT_NAME} ${SOURCE_CPP})
 target_link_libraries(${PROJECT_NAME} PUBLIC Qt${QT_VERSION_MAJOR}::Core)
 target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
 
-setVersion(1 5 1)
+setVersion(1 5 2)
 
 initAll()
 make_directory("${CMAKE_CURRENT_SOURCE_DIR}/Distro")
diff --git a/isettings.h b/isettings.h
index 43092f3..f3904e2 100644
--- a/isettings.h
+++ b/isettings.h
@@ -35,9 +35,9 @@ enum class SettingsSaveMode: quint64 {
  *
  * @note The all child classes should be initialized before used.
  *
- * ```
- *     auto settingsInstance = Setting::init<Setting>();
- * ```
+ * @code{cpp}
+ *     auto settingsInstance = Setting::initService<Setting>();
+ * @endcode
  *
  * @see ISettings::init method.
  *
@@ -187,8 +187,6 @@ protected:
      */
     void clearCache();
 
-
-
     /**
      * @brief settingsMap This method returns initialized settings map.
      * Settings map contains pairs with settings key and default value.
@@ -204,9 +202,11 @@ private:
     QHash<QString, QVariant> _cache;
     QHash<QString, QVariant> *_defaultConfig = nullptr;
 
-//    static ISettings* _settings;
+    friend class Service<ISettings>;
+};
+
+
 };
-} ;
 
 
 #endif // ISETTINGS_H
diff --git a/qaservice.h b/qaservice.h
index 617fe08..89d959b 100644
--- a/qaservice.h
+++ b/qaservice.h
@@ -66,8 +66,7 @@ template<class Base>
 class Service
 {
 public:
-    Service() {
-    };
+    Service() {};
 
     /**
      * @brief initService This method initialize the @a Base object as a service.
@@ -79,8 +78,8 @@ public:
      */
     template<class BaseClass = Base, class... Args>
     static Base* initService(Args&&... args) {
-        auto instanceObj = privateInstance();
-        if(instanceObj->_data) {
+        Service<Base>* instanceObj = privateInstance();
+        if(!instanceObj->_data) {
             instanceObj->_data = new BaseClass(std::forward<Args>(args)...);
         }
 
@@ -121,7 +120,7 @@ public:
      * @see autoInstance
      */
     static void deinitService() {
-        auto instanceObj = privateInstance();
+        Service<Base>* instanceObj = privateInstance();
         if(instanceObj->_data) {
             delete instanceObj->_data;
             instanceObj->_data = nullptr;
diff --git a/settings.cpp b/settings.cpp
index 62b1269..c9c6173 100644
--- a/settings.cpp
+++ b/settings.cpp
@@ -43,6 +43,14 @@ void Settings::setValueImplementation(const QString key, const QVariant &value)
     return _settings->setValue(key, value);
 }
 
+bool Settings::ignoreToRest(const QString &) const {
+    return false;
+}
+
+QHash<QString, QVariant> Settings::defaultSettings() {
+    return {};
+}
+
 bool Settings::isBool(const QString &) const {
     return false;
 }
diff --git a/settings.h b/settings.h
index 3740a1c..5439d01 100644
--- a/settings.h
+++ b/settings.h
@@ -20,9 +20,21 @@ namespace QuasarAppUtils {
  *
  * Example of initialisation :
  *
- *  ```
- *     auto settingsInstance = Setting::init();
- *  ```
+ *  @code{cpp}
+ *     auto settingsInstance = QuasarAppUtils::Setting::initService();
+ *
+ *     // on any another site you can use the instance method for get settings instance object.
+ *     auto settingsInstance = QuasarAppUtils::Setting::instance();
+ *
+ *     // and you can destroy setting object if they are not needed anymore
+ *     QuasarAppUtils::Setting::deinitService();
+ *  @endcode
+ *
+ *  **Or** you can use the Settings::autoInstance method.
+ *
+ *  @code{cpp}
+ *     auto settingsInstance = QuasarAppUtils::Setting::autoInstance();
+ *  @endcode
  *
  *  @see Settings::init
  */
@@ -51,9 +63,11 @@ public:
 
 protected:
 
-    void syncImplementation();
-    QVariant getValueImplementation(const QString &key, const QVariant &def);
-    void setValueImplementation(const QString key, const QVariant &value);
+    void syncImplementation() override;
+    QVariant getValueImplementation(const QString &key, const QVariant &def) override;
+    void setValueImplementation(const QString key, const QVariant &value) override;
+    bool ignoreToRest(const QString &) const override;
+    QHash<QString, QVariant> defaultSettings() override;
 
     /**
      * @brief isBool This method should be return true if the key's type is bool.