From 6c7b51dd971dd3855d6a43b4ef95fe2c0138099e Mon Sep 17 00:00:00 2001 From: Igor Nazarov Date: Wed, 21 Jun 2017 13:57:25 +0300 Subject: [PATCH] TizenRefApp-8727 [Gallery] Implement CString - Implemented CString and ConstCString classes; - Variant:CStr replaced with new CString implementation. Change-Id: I699b9f5a85ed195c32276cc267c7b68986538f45 --- ucl/inc/ucl/misc/CString.h | 55 ++++++++++++++++++ ucl/inc/ucl/misc/CString.hpp | 93 +++++++++++++++++++++++++++++++ ucl/inc/ucl/misc/ConstCString.h | 53 ++++++++++++++++++ ucl/inc/ucl/misc/ConstCString.hpp | 86 ++++++++++++++++++++++++++++ ucl/inc/ucl/misc/Variant.h | 19 +------ ucl/inc/ucl/misc/Variant.hpp | 39 ------------- ucl/src/appfw/UIApp.cpp | 8 +-- ucl/src/misc/Variant.cpp | 18 +++--- 8 files changed, 302 insertions(+), 69 deletions(-) create mode 100644 ucl/inc/ucl/misc/CString.h create mode 100644 ucl/inc/ucl/misc/CString.hpp create mode 100644 ucl/inc/ucl/misc/ConstCString.h create mode 100644 ucl/inc/ucl/misc/ConstCString.hpp diff --git a/ucl/inc/ucl/misc/CString.h b/ucl/inc/ucl/misc/CString.h new file mode 100644 index 0000000..396f37d --- /dev/null +++ b/ucl/inc/ucl/misc/CString.h @@ -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 index 0000000..086b3c2 --- /dev/null +++ b/ucl/inc/ucl/misc/CString.hpp @@ -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 index 0000000..7f600f0 --- /dev/null +++ b/ucl/inc/ucl/misc/ConstCString.h @@ -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 index 0000000..50df605 --- /dev/null +++ b/ucl/inc/ucl/misc/ConstCString.hpp @@ -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(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); + } +} diff --git a/ucl/inc/ucl/misc/Variant.h b/ucl/inc/ucl/misc/Variant.h index 3bfa6d6..6ad0b23 100644 --- a/ucl/inc/ucl/misc/Variant.h +++ b/ucl/inc/ucl/misc/Variant.h @@ -21,7 +21,7 @@ #include #include -#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; diff --git a/ucl/inc/ucl/misc/Variant.hpp b/ucl/inc/ucl/misc/Variant.hpp index 04c6650..fe6d977 100644 --- a/ucl/inc/ucl/misc/Variant.hpp +++ b/ucl/inc/ucl/misc/Variant.hpp @@ -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(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 : diff --git a/ucl/src/appfw/UIApp.cpp b/ucl/src/appfw/UIApp.cpp index 4012eca..4e1a4de 100644 --- a/ucl/src/appfw/UIApp.cpp +++ b/ucl/src/appfw/UIApp.cpp @@ -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) { diff --git a/ucl/src/misc/Variant.cpp b/ucl/src/misc/Variant.cpp index 4509118..f723f34 100644 --- a/ucl/src/misc/Variant.cpp +++ b/ucl/src/misc/Variant.cpp @@ -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 // -- 2.34.1