TizenRefApp-8727 [Gallery] Implement CString 63/135263/6
authorIgor Nazarov <i.nazarov@samsung.com>
Wed, 21 Jun 2017 10:57:25 +0000 (13:57 +0300)
committerIgor Nazarov <i.nazarov@samsung.com>
Thu, 22 Jun 2017 12:28:27 +0000 (15:28 +0300)
- Implemented CString and ConstCString classes;
- Variant:CStr replaced with new CString implementation.

Change-Id: I699b9f5a85ed195c32276cc267c7b68986538f45

ucl/inc/ucl/misc/CString.h [new file with mode: 0644]
ucl/inc/ucl/misc/CString.hpp [new file with mode: 0644]
ucl/inc/ucl/misc/ConstCString.h [new file with mode: 0644]
ucl/inc/ucl/misc/ConstCString.hpp [new file with mode: 0644]
ucl/inc/ucl/misc/Variant.h
ucl/inc/ucl/misc/Variant.hpp
ucl/src/appfw/UIApp.cpp
ucl/src/misc/Variant.cpp

diff --git a/ucl/inc/ucl/misc/CString.h b/ucl/inc/ucl/misc/CString.h
new file mode 100644 (file)
index 0000000..396f37d
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __UCL_MISC_CSTRING_H__
+#define __UCL_MISC_CSTRING_H__
+
+#include "ucl/util/types.h"
+
+namespace ucl {
+
+       class CString final : public NonCopyable {
+       public:
+               friend void swap(CString &x, CString &y) noexcept;
+
+               static CString dup(const char *ptr);
+               static CString takeover(char *ptr) noexcept;
+
+               CString() noexcept;
+               CString(std::nullptr_t) noexcept;
+               CString(CString &&s) noexcept;
+               CString(const std::string &s);
+               ~CString() noexcept;
+
+               CString &operator=(CString s) noexcept;
+
+               bool isEmpty() const;
+
+               char *release() noexcept;
+
+               char *get() const noexcept;
+
+       private:
+               explicit CString(char *ptr) noexcept;
+
+       private:
+               char *m_ptr;
+       };
+}
+
+#include "CString.hpp"
+
+#endif // __UCL_MISC_CSTRING_H__
diff --git a/ucl/inc/ucl/misc/CString.hpp b/ucl/inc/ucl/misc/CString.hpp
new file mode 100644 (file)
index 0000000..086b3c2
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ucl/util/helpers.h"
+
+namespace ucl {
+
+       // CString //
+
+       inline CString CString::dup(const char *const ptr)
+       {
+               return CString(strDupSafe(ptr));
+       }
+
+       inline CString CString::takeover(char *const ptr) noexcept
+       {
+               return CString(ptr);
+       }
+
+       inline CString::CString(char *const ptr) noexcept :
+               m_ptr(ptr)
+       {
+       }
+
+       inline CString::CString() noexcept :
+               m_ptr(nullptr)
+       {
+       }
+
+       inline CString::CString(std::nullptr_t) noexcept :
+               CString()
+       {
+       }
+
+       inline CString::CString(CString &&s) noexcept :
+               m_ptr(s.m_ptr)
+       {
+               s.m_ptr = nullptr;
+       }
+
+       inline CString::CString(const std::string &s) :
+               m_ptr(s.empty() ? nullptr : strdup(s.c_str()))
+       {
+       }
+
+       inline CString::~CString() noexcept
+       {
+               free(m_ptr);
+       }
+
+       inline CString &CString::operator=(CString s) noexcept
+       {
+               swap(*this, s);
+               return *this;
+       }
+
+       inline bool CString::isEmpty() const
+       {
+               return !m_ptr;
+       }
+
+       inline char *CString::release() noexcept
+       {
+               char *const result = m_ptr;
+               m_ptr = nullptr;
+               return result;
+       }
+
+       inline char *CString::get() const noexcept
+       {
+               return m_ptr;
+       }
+
+       // Non-member functions //
+
+       inline void swap(CString &x, CString &y) noexcept
+       {
+               std::swap(x.m_ptr, y.m_ptr);
+       }
+}
diff --git a/ucl/inc/ucl/misc/ConstCString.h b/ucl/inc/ucl/misc/ConstCString.h
new file mode 100644 (file)
index 0000000..7f600f0
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __UCL_MISC_CONST_CSTRING_H__
+#define __UCL_MISC_CONST_CSTRING_H__
+
+#include "CString.h"
+
+namespace ucl {
+
+       class ConstCString final : public NonCopyable {
+       public:
+               friend void swap(ConstCString &x, ConstCString &y) noexcept;
+
+               static ConstCString wrap(const char *ptr) noexcept;
+
+               ConstCString() noexcept;
+               ConstCString(std::nullptr_t) noexcept;
+               ConstCString(ConstCString &&s) noexcept;
+               ConstCString(CString &&s) noexcept;
+               ~ConstCString() noexcept;
+
+               ConstCString &operator=(ConstCString s) noexcept;
+
+               bool isEmpty() const;
+
+               const char *get() const noexcept;
+
+       private:
+               explicit ConstCString(const char *ptr) noexcept;
+
+       private:
+               const char *m_ptr;
+               bool m_isOwner;
+       };
+}
+
+#include "ConstCString.hpp"
+
+#endif // __UCL_MISC_CONST_CSTRING_H__
diff --git a/ucl/inc/ucl/misc/ConstCString.hpp b/ucl/inc/ucl/misc/ConstCString.hpp
new file mode 100644 (file)
index 0000000..50df605
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace ucl {
+
+       // ConstCString //
+
+       inline ConstCString ConstCString::wrap(const char *ptr) noexcept
+       {
+               return ConstCString(ptr);
+       }
+
+       inline ConstCString::ConstCString(const char *ptr) noexcept :
+               m_ptr(ptr),
+               m_isOwner(false)
+       {
+       }
+
+       inline ConstCString::ConstCString() noexcept :
+               m_ptr(nullptr),
+               m_isOwner(false)
+       {
+       }
+
+       inline ConstCString::ConstCString(std::nullptr_t) noexcept :
+               ConstCString()
+       {
+       }
+
+       inline ConstCString::ConstCString(ConstCString &&s) noexcept :
+               m_ptr(s.m_ptr),
+               m_isOwner(s.m_isOwner)
+       {
+               s.m_isOwner = false;
+       }
+
+       inline ConstCString::ConstCString(CString &&s) noexcept :
+               m_ptr(s.release()),
+               m_isOwner(true)
+       {
+       }
+
+       inline ConstCString::~ConstCString() noexcept
+       {
+               if (m_isOwner) {
+                       free(const_cast<char *>(m_ptr));
+               }
+       }
+
+       inline ConstCString &ConstCString::operator=(ConstCString s) noexcept
+       {
+               swap(*this, s);
+               return *this;
+       }
+
+       inline bool ConstCString::isEmpty() const
+       {
+               return !m_ptr;
+       }
+
+       inline const char *ConstCString::get() const noexcept
+       {
+               return m_ptr;
+       }
+
+       // Non-member functions //
+
+       inline void swap(ConstCString &x, ConstCString &y) noexcept
+       {
+               std::swap(x.m_ptr, y.m_ptr);
+               std::swap(x.m_isOwner, y.m_isOwner);
+       }
+}
index 3bfa6d6ccb3abf23d19d37f7736938bd9714184c..6ad0b23552d6d134b9d08a75fc3fd20f7e71de87 100644 (file)
@@ -21,7 +21,7 @@
 #include <vector>
 #include <initializer_list>
 
-#include "ucl/util/types.h"
+#include "ConstCString.h"
 
 namespace ucl {
 
@@ -49,21 +49,6 @@ namespace ucl {
                        TYPE_ARRAY
                };
 
-       private:
-               // Helper to wrap "const char *" and manage its lifetime
-               class CStr : NonCopyable {
-               public:
-                       CStr(const char *ptr);
-                       CStr(const char *ptr, bool isOwner) noexcept;
-                       CStr(CStr &&s);
-                       ~CStr() noexcept;
-                       const char *get() const noexcept;
-                       operator const char *() const noexcept;
-               private:
-                       const char *const m_ptr;
-                       bool m_isOwner;
-               };
-
        public:
                friend void swap(Variant &x, Variant &y) noexcept;
 
@@ -106,7 +91,7 @@ namespace ucl {
                float asFloat() const noexcept;
                double asDouble() const noexcept;
 
-               CStr asString() const noexcept;
+               ConstCString asString() const noexcept;
 
                Variant *asArray() noexcept;
                const Variant *asArray() const noexcept;
index 04c66504eb7e98ab2dccc70769476c4188c460c7..fe6d977196075082acde71093ee9529bc36c0f12 100644 (file)
@@ -28,45 +28,6 @@ namespace ucl { namespace { namespace impl {
 
 namespace ucl {
 
-       // Variant::CStr //
-
-       inline Variant::CStr::CStr(const char *const ptr) :
-               m_ptr(strdup(ptr)),
-               m_isOwner(true)
-       {
-       }
-
-       inline Variant::CStr::CStr(const char *const ptr,
-                       const bool isOwner) noexcept :
-               m_ptr(ptr),
-               m_isOwner(isOwner)
-       {
-       }
-
-       inline Variant::CStr::CStr(CStr &&s) :
-               m_ptr(s.m_ptr),
-               m_isOwner(s.m_isOwner)
-       {
-               s.m_isOwner = false;
-       }
-
-       inline Variant::CStr::~CStr() noexcept
-       {
-               if (m_isOwner) {
-                       free(const_cast<char *>(m_ptr));
-               }
-       }
-
-       inline const char *Variant::CStr::get() const noexcept
-       {
-               return m_ptr;
-       }
-
-       inline Variant::CStr::operator const char *() const noexcept
-       {
-               return m_ptr;
-       }
-
        // Variant //
 
        inline Variant::Variant() noexcept :
index 4012ecac3be2ceee8ee60ddbe705816a31d59abf..4e1a4deca3be6e2c3af7ceefdae809180cd9d033 100644 (file)
@@ -123,13 +123,13 @@ namespace ucl {
                }
 
                if (appParams.get(AppParam::ACCELERATION_PREFERENECE, paramValue)) {
-                       const char *const accelPreference = paramValue.asString();
+                       const auto accelPreference = paramValue.asString();
                        if (isEmpty(accelPreference)) {
                                LOG_RETURN(RES_INVALID_DATA,
                                                "Invalid parameter ACCELERATION_PREFERENECE: %s",
-                                               accelPreference);
+                                               accelPreference.get());
                        }
-                       elm_config_accel_preference_set(accelPreference);
+                       elm_config_accel_preference_set(accelPreference.get());
                }
 
                return RES_OK;
@@ -157,7 +157,7 @@ namespace ucl {
 
                m_window = Window::Builder().
                                setType(winType).
-                               setName({paramValue.asString()}).
+                               setName(paramValue.asString().get()).
                                setIsOwner(true).
                                build();
                if (!m_window) {
index 4509118fd27c2387c981d566215d4975fe3847f2..f723f34b5c627195c57132faf942af1aae4768c7 100644 (file)
@@ -27,9 +27,9 @@ namespace ucl {
        {
                if (aString) {
                        const int realLength = ((length < 0) ? strlen(aString) : length);
-                       const UInt size = (realLength + 1);
-                       if (size <= sizeof(m_aSmallStr.buffer)) {
-                               strncpy(m_aSmallStr.buffer, aString, size);
+                       if (realLength < sizeof(m_aSmallStr.buffer)) {
+                               strncpy(m_aSmallStr.buffer, aString, realLength);
+                               m_aSmallStr.buffer[realLength] = '\0';
                                m_type |= impl::FLAG_SMALL_STR;
                        } else {
                                m_aString.data = strndup(aString, realLength);
@@ -164,14 +164,14 @@ namespace ucl {
                return 0.0;
        }
 
-       Variant::CStr Variant::asString() const noexcept
+       ConstCString Variant::asString() const noexcept
        {
                char strBuff[impl::TMP_STR_BUFF_SIZE] = {};
                switch (m_type) {
                case TYPE_NIL:
-                       return {"", false};
+                       return ConstCString::wrap("");
                case TYPE_BOOLEAN:
-                       return {(m_aBool.value ? "true" : "false"), false};
+                       return ConstCString::wrap(m_aBool.value ? "true" : "false");
                case TYPE_INTEGER:
                        snprintf(strBuff, sizeof(strBuff), "%d", m_anInt.value);
                        break;
@@ -183,14 +183,14 @@ namespace ucl {
                        snprintf(strBuff, sizeof(strBuff), "%f", m_aDouble.value);
                        break;
                case impl::TYPE_SMALL_STR:
-                       return m_aSmallStr.buffer;
+                       return ConstCString::wrap(m_aSmallStr.buffer);
                case TYPE_STRING:
-                       return {m_aString.data, false};
+                       return ConstCString::wrap(m_aString.data);
                case TYPE_ARRAY:
                        snprintf(strBuff, sizeof(strBuff), "%d", m_anArray.length);
                        break;
                }
-               return strBuff;
+               return CString::dup(strBuff);
        }
 
        // Non-member functions //