76 lines
1.1 KiB
C++
Executable File
76 lines
1.1 KiB
C++
Executable File
// TJP COPYRIGHT HEADER
|
|
|
|
#pragma once
|
|
|
|
#include <tjp/core/string/StringView.h>
|
|
#include <tjp/core/containers/Vector.h>
|
|
#include <tjp/core/containers/Optional.hpp>
|
|
|
|
namespace tjp::core::internet {
|
|
|
|
using DataBlock = Vector<char>;
|
|
using DataView = StringView;
|
|
|
|
struct Data {
|
|
mutable Optional<DataBlock> block;
|
|
mutable Optional<String> string;
|
|
|
|
Data()
|
|
{}
|
|
|
|
Data(DataBlock &&block_) :
|
|
block(std::move(block_))
|
|
{}
|
|
|
|
Data(String &&string_) :
|
|
string(std::move(string_))
|
|
{}
|
|
|
|
operator String &() const
|
|
{
|
|
if (!string.has_value())
|
|
{
|
|
if (block.has_value())
|
|
string = String(block->data(), block->size());
|
|
else
|
|
string = String();
|
|
}
|
|
|
|
return *string;
|
|
}
|
|
|
|
operator DataView () const
|
|
{
|
|
if (block.has_value())
|
|
return StringView(block->data(), block->size());
|
|
|
|
if (string.has_value())
|
|
return *string;
|
|
|
|
return {};
|
|
} ;
|
|
|
|
const char *data() const
|
|
{
|
|
DataView view = *this;
|
|
return view.data();
|
|
}
|
|
|
|
const char *c_str() const
|
|
{
|
|
String &view = *this;
|
|
return view.c_str();
|
|
}
|
|
|
|
const auto size() const
|
|
{
|
|
DataView view = *this;
|
|
return view.size();
|
|
}
|
|
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|