You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

#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;
#if DIRECT_WRITE
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
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);
}
void use_moh()
{
std::string str{"Hello from 2!"};
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);
};
opaque_handle v{make_opaque_handle<std::string, _h>(&str)};
print(*v);
}
int main()
{
const opaque_handle v{make_opaque_object_handle(std::string{"Hello world"})};
print(*v); //XXX: Why does converting it to string_view here break it?
use_moh();
return 0;
}