|
|
|
#include <cstdio>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "../include/opaque.hh"
|
|
|
|
|
|
|
|
|
|
|
|
static void print_view(std::string_view ptr)
|
|
|
|
{
|
|
|
|
//std::string_view ptr{string};
|
|
|
|
|
|
|
|
size_t sz;
|
|
|
|
#ifdef DIRECT_WRITE
|
|
|
|
// Bypass all caching of the output and immediately invoke `write()` for everything
|
|
|
|
if(__builtin_expect((sz = ptr.size()) > 0, true)) {
|
|
|
|
write(1, "> ", 2);
|
|
|
|
write(1, ptr.data(), sz);
|
|
|
|
} else write(1, "! <null>\n", 8);
|
|
|
|
write(1, "\n", 1);
|
|
|
|
#else
|
|
|
|
// All the output from the program is cached and `write()` is only called once at the end.
|
|
|
|
if(__builtin_expect((sz = ptr.size()) > 0, true)) {
|
|
|
|
printf("> %.*s\n", (int)sz, ptr.data());
|
|
|
|
} else puts("! <null>\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline void print(const std::string& string)
|
|
|
|
{
|
|
|
|
std::string_view sv{string};
|
|
|
|
return print_view(sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class moh_hck {
|
|
|
|
Template,
|
|
|
|
Ref,
|
|
|
|
Owned,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Example using `make_opaque_handle`, like would be done for a C interface
|
|
|
|
template<moh_hck Create = moh_hck::Owned>
|
|
|
|
void use_moh()
|
|
|
|
{
|
|
|
|
std::string* str = new std::string(__PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
if constexpr(Create != moh_hck::Owned) {
|
|
|
|
constexpr auto _h = [](std::string* ptr, auto op) noexcept {
|
|
|
|
if(ptr) {
|
|
|
|
switch(op) {
|
|
|
|
case opaque_handle_operation::Clone: return new std::string(*ptr);
|
|
|
|
case opaque_handle_operation::Delete: delete ptr; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return static_cast<std::string*>(nullptr);
|
|
|
|
};
|
|
|
|
|
|
|
|
if constexpr(Create == moh_hck::Template) {
|
|
|
|
const opaque_handle v{make_opaque_handle<std::string, _h>(str)};
|
|
|
|
print(*v);
|
|
|
|
} else /*if constexpr(Create == moh_hck::Ref)*/ {
|
|
|
|
const opaque_handle v{make_opaque_handle<std::string>(str, _h)};
|
|
|
|
print(*v);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const opaque_handle v{make_opaque_handle<std::string>(str, [](std::string* ptr, auto op) noexcept {
|
|
|
|
if(ptr) {
|
|
|
|
switch(op) {
|
|
|
|
case opaque_handle_operation::Clone: return new std::string(*ptr);
|
|
|
|
case opaque_handle_operation::Delete: delete ptr; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return static_cast<std::string*>(nullptr);
|
|
|
|
})};
|
|
|
|
print(*v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
const opaque_handle v{make_opaque_object_handle(std::string{"Hello world"})};
|
|
|
|
puts("Auto-downcasting to `const std::string&`:");
|
|
|
|
print(*v);
|
|
|
|
puts("\nConverting view directly:");
|
|
|
|
print_view(std::string_view{v.cast<std::string>()}); // Explicit cast needed, *v will choose the wrong type if not.
|
|
|
|
|
|
|
|
puts("\nUsing opaque_make_handle():");
|
|
|
|
use_moh<moh_hck::Template>();
|
|
|
|
use_moh<moh_hck::Ref>();
|
|
|
|
use_moh<moh_hck::Owned>();
|
|
|
|
return 0;
|
|
|
|
}
|