slint/api/sixtyfps-cpp/include/sixtyfps_resource.h
Simon Hausmann 5bae6e01a5 Prepare for the ability to embed image data
The Image's source property used to be a string. Now it is a Resource
enum, which can either be None or an absolute file path to the image on
disk. This also replaces the internal Image type.

The compiler internally resolves the img bang expression to a resource
reference, which shall remain just an absolute path. For now the target
generator passes that through, but in the future the target generator
may choose a target specific way of embedding the data and thus
generating a different Resource type in the final code (through
compile_expression in the cpp and rust generator).

The C++ binding is a bit messy as cbindgen doesn't really support
exporting enums that can be constructed on the C++ side. So instead we
use cbindgen to merely export the type internally and only use the tag
from it then. The public API is then a custom Resource type that is
meant to be binary compatible.
2020-06-09 22:54:29 +02:00

65 lines
1.4 KiB
C++

#pragma once
#include <string_view>
#include "sixtyfps_resource_internal.h"
#include "sixtyfps_string.h"
namespace sixtyfps {
union ResourceData {
SharedString absolute_file_path;
ResourceData() { }
~ResourceData() { }
};
struct Resource
{
public:
using Tag = internal::types::Resource::Tag;
Resource() : tag(Tag::None) { }
Resource(const SharedString &file_path) : tag(Tag::AbsoluteFilePath)
{
new (&data.absolute_file_path) SharedString(file_path);
}
Resource(const Resource &other) : tag(other.tag)
{
switch (tag) {
case Tag::None:
break;
case Tag::AbsoluteFilePath:
new (&data.absolute_file_path) SharedString(other.data.absolute_file_path);
}
}
~Resource() { destroy(); }
Resource &operator=(const Resource &other)
{
if (this == &other)
return *this;
destroy();
tag = other.tag;
switch (tag) {
case Tag::None:
break;
case Tag::AbsoluteFilePath:
new (&data.absolute_file_path) SharedString(other.data.absolute_file_path);
}
return *this;
}
private:
void destroy()
{
switch (tag) {
case Tag::None:
break;
case Tag::AbsoluteFilePath:
data.absolute_file_path.~SharedString();
}
}
Tag tag;
ResourceData data;
};
}