This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/509-update-cxx-support-to-cxx14
in repository https://gitbox.apache.org/repos/asf/celix.git

commit b95c012041e836c7d4b2d03c73b0e9aaf5936a10
Author: Pepijn Noltes <[email protected]>
AuthorDate: Sun Oct 1 14:53:50 2023 +0200

    #509: Add missing IsString check in Properties to allow prop set with 
std::string
---
 libs/utils/include/celix/Properties.h | 104 +++++-----------------------------
 1 file changed, 14 insertions(+), 90 deletions(-)

diff --git a/libs/utils/include/celix/Properties.h 
b/libs/utils/include/celix/Properties.h
index 68107fee..90710158 100644
--- a/libs/utils/include/celix/Properties.h
+++ b/libs/utils/include/celix/Properties.h
@@ -101,16 +101,15 @@ namespace celix {
      * @note Not thread safe.
      */
     class Properties {
+    private:
+        template<typename T>
+        using IsString = std::is_same<std::decay_t<T>, std::string>; //Util to 
check if T is a std::string.
     public:
         using const_iterator = PropertiesIterator;
 
         class ValueRef {
         public:
-#if __cplusplus >= 201703L //C++17 or higher
-            ValueRef(std::shared_ptr<celix_properties_t> _props, 
std::string_view _key) : props{std::move(_props)}, stringKey{}, 
charKey{_key.data()} {}
-#else
             ValueRef(std::shared_ptr<celix_properties_t> _props, std::string 
_key) : props{std::move(_props)}, stringKey{std::move(_key)}, charKey{nullptr} 
{}
-#endif
             ValueRef(std::shared_ptr<celix_properties_t> _props, const char* 
_key) : props{std::move(_props)}, stringKey{}, charKey{_key} {}
 
             ValueRef(const ValueRef&) = default;
@@ -118,16 +117,6 @@ namespace celix {
             ValueRef& operator=(const ValueRef&) = default;
             ValueRef& operator=(ValueRef&&) = default;
 
-#if __cplusplus >= 201703L //C++17 or higher
-            ValueRef& operator=(std::string_view value) {
-                if (charKey == nullptr) {
-                    celix_properties_set(props.get(), stringKey.c_str(), 
value.data());
-                } else {
-                    celix_properties_set(props.get(), charKey, value.data());
-                }
-                return *this;
-            }
-#else
             ValueRef& operator=(const std::string& value) {
                 if (charKey == nullptr) {
                     celix_properties_set(props.get(), stringKey.c_str(), 
value.c_str());
@@ -136,7 +125,6 @@ namespace celix {
                 }
                 return *this;
             }
-#endif
 
             const char* getValue() const {
                 if (charKey == nullptr) {
@@ -172,19 +160,11 @@ namespace celix {
         Properties(const Properties& rhs) :
             cProps{celix_properties_copy(rhs.cProps.get()), 
[](celix_properties_t* p) { celix_properties_destroy(p); }} {}
 
-#if __cplusplus >= 201703L //C++17 or higher
-        Properties(std::initializer_list<std::pair<std::string_view, 
std::string_view>> list) : cProps{celix_properties_create(), 
[](celix_properties_t* p) { celix_properties_destroy(p); }} {
-            for(auto &entry : list) {
-                set(entry.first, entry.second);
-            }
-        }
-#else
         Properties(std::initializer_list<std::pair<std::string, std::string>> 
list) : cProps{celix_properties_create(), [](celix_properties_t* p) { 
celix_properties_destroy(p); }} {
             for(auto &entry : list) {
                 set(entry.first, entry.second);
             }
         }
-#endif
 
         /**
          * @brief Wraps C properties, but does not take ownership -> dtor will 
not destroy properties
@@ -204,21 +184,6 @@ namespace celix {
             return cProps.get();
         }
 
-#if __cplusplus >= 201703L //C++17 or higher
-        /**
-         * @brief Get the value for a property key
-         */
-        ValueRef operator[](std::string_view key) {
-            return ValueRef{cProps, key};
-        }
-
-        /**
-         * @brief Get the value for a property key
-         */
-        ValueRef operator[](std::string_view key) const {
-            return ValueRef{cProps, key};
-        }
-#else
         /**
          * @brief Get the value for a property key
          */
@@ -232,7 +197,6 @@ namespace celix {
         ValueRef operator[](std::string key) const {
             return ValueRef{cProps, std::move(key)};
         }
-#endif
 
         /**
          * @brief begin iterator
@@ -266,54 +230,6 @@ namespace celix {
             return iter;
         }
 
-#if __cplusplus >= 201703L //C++17 or higher
-        /**
-         * @brief Get the value for a property key or return the defaultValue 
if the key does not exists.
-         */
-        std::string get(std::string_view key, std::string_view defaultValue = 
{}) const {
-            const char* found = celix_properties_get(cProps.get(), key.data(), 
nullptr);
-            return found == nullptr ? std::string{defaultValue} : 
std::string{found};
-        }
-
-        /**
-         * @brief Get the value as long for a property key or return the 
defaultValue if the key does not exists.
-         */
-        long getAsLong(std::string_view key, long defaultValue) const {
-            return celix_properties_getAsLong(cProps.get(), key.data(), 
defaultValue);
-        }
-
-        /**
-         * @brief Get the value as double for a property key or return the 
defaultValue if the key does not exists.
-         */
-        double getAsDouble(std::string_view key, double defaultValue) const {
-            return celix_properties_getAsDouble(cProps.get(), key.data(), 
defaultValue);
-        }
-
-        /**
-         * @brief Get the value as bool for a property key or return the 
defaultValue if the key does not exists.
-         */
-        bool getAsBool(std::string_view key, bool defaultValue) const {
-            return celix_properties_getAsBool(cProps.get(), key.data(), 
defaultValue);
-        }
-
-        /**
-         * @brief Sets a T&& property. Will use (std::) to_string to convert 
the value to string.
-         */
-        template<typename T>
-        void set(std::string_view key, T&& value) {
-            if constexpr (std::is_same_v<std::decay_t<T>, bool>) {
-                celix_properties_setBool(cProps.get(), key.data(), value);
-            } else if constexpr (std::is_same_v<std::decay_t<T>, 
std::string_view>) {
-                celix_properties_set(cProps.get(), key.data(), value.data());
-            } else if constexpr (std::is_convertible_v<T, std::string_view>) {
-                std::string_view view{value};
-                celix_properties_set(cProps.get(), key.data(), view.data());
-            } else {
-                using namespace std;
-                celix_properties_set(cProps.get(), key.data(), 
to_string(value).c_str());
-            }
-        }
-#else
         /**
          * @brief Get the value for a property key or return the defaultValue 
if the key does not exists.
          */
@@ -368,11 +284,20 @@ namespace celix {
          * @brief Sets a T property. Will use (std::) to_string to convert the 
value to string.
          */
         template<typename T>
-        void set(const std::string& key, T&& value) {
+        typename std::enable_if<!::celix::Properties::IsString<T>::value>::type
+        set(const std::string& key, T&& value) {
             using namespace std;
             celix_properties_set(cProps.get(), key.c_str(), 
to_string(value).c_str());
         }
-#endif
+
+        /**
+         * @brief Sets a String property.
+         */
+        template<typename T>
+        typename std::enable_if<::celix::Properties::IsString<T>::value>::type
+        set(const std::string& key, T&& value) {
+            celix_properties_set(cProps.get(), key.c_str(), value.c_str());
+        }
 
         /**
          * @brief Returns the nr of properties.
@@ -419,7 +344,6 @@ namespace celix {
 
         std::shared_ptr<celix_properties_t> cProps;
     };
-
 }
 
 inline std::ostream& operator<<(std::ostream& os, const 
::celix::Properties::ValueRef& ref)

Reply via email to