Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Last active March 18, 2019 10:51
Show Gist options
  • Save wi7a1ian/430a605eae2b7f7666b84dc4ee200e56 to your computer and use it in GitHub Desktop.
Save wi7a1ian/430a605eae2b7f7666b84dc4ee200e56 to your computer and use it in GitHub Desktop.
String DTO for C++ API #cpp
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>;
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