--- /dev/null
+/*
+ * 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__
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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__
--- /dev/null
+/*
+ * 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);
+ }
+}
#include <vector>
#include <initializer_list>
-#include "ucl/util/types.h"
+#include "ConstCString.h"
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;
float asFloat() const noexcept;
double asDouble() const noexcept;
- CStr asString() const noexcept;
+ ConstCString asString() const noexcept;
Variant *asArray() noexcept;
const Variant *asArray() const noexcept;
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 :
}
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;
m_window = Window::Builder().
setType(winType).
- setName({paramValue.asString()}).
+ setName(paramValue.asString().get()).
setIsOwner(true).
build();
if (!m_window) {
{
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);
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;
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 //