Last active
March 18, 2019 10:51
-
-
Save wi7a1ian/430a605eae2b7f7666b84dc4ee200e56 to your computer and use it in GitHub Desktop.
String DTO for C++ API #cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
class IData : public IBase { | |
public: | |
using Ptr = std::unique_ptr<typename IData<T>, SDeleter>; | |
public: | |
virtual const T* Get(uint32_t& size) const noexcept = 0; | |
}; | |
using IString = IData<uchar>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StringContainer : public IString { | |
public: | |
StringContainer(const uchar* str) | |
{ | |
assert(str != nullptr); | |
if (str != nullptr) | |
{ | |
m_str = str; | |
} | |
} | |
StringContainer(const ustring& str) : m_str(str) | |
{ | |
// Nop | |
} | |
StringContainer(ustring&& str) : m_str(std::move(str)) | |
{ | |
// Nop | |
} | |
void Free() override { delete this; } | |
const uchar* Get(uint32_t& size) const noexcept | |
{ | |
size = static_cast<uint32_t>(m_str.size()); | |
assert(size > 0); | |
return (size > 0) ? &m_str.at(0) : nullptr; | |
} | |
private: | |
KOTL::ustring m_str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment