diff --git a/developer-handbook.md b/developer-handbook.md index c2c942b5..e87b99e0 100644 --- a/developer-handbook.md +++ b/developer-handbook.md @@ -386,16 +386,16 @@ struct NostalgiaGraphic { }; template -constexpr ox::Error model(T *h, NostalgiaPalette *pal) noexcept { +constexpr ox::Error model(T *h, ox::CommonPtrWith auto *pal) noexcept { h->template setTypeInfo(); - // it is also possible to provide the type name and number of fields as function arguments + // it is also possible to provide the type name and type version as function arguments //h->setTypeInfo("net.drinkingtea.nostalgia.core.NostalgiaPalette", 1); oxReturnError(h->field("colors", &pal->colors)); return OxError(0); } template -constexpr ox::Error model(T *h, NostalgiaGraphic *ng) noexcept { +constexpr ox::Error model(T *h, ox::CommonPtrWith auto *ng) noexcept { h->template setTypeInfo(); oxReturnError(h->field("bpp", &ng->bpp)); oxReturnError(h->field("rows", &ng->rows)); @@ -416,7 +416,7 @@ The model system also provides for unions: class FileAddress { template - friend constexpr Error model(T*, FileAddress*) noexcept; + friend constexpr Error model(T*, ox::CommonPtrWith auto*) noexcept; public: static constexpr auto TypeName = "net.drinkingtea.ox.FileAddress"; @@ -435,21 +435,31 @@ class FileAddress { }; template -constexpr Error model(T *h, FileAddress::Data *obj) noexcept { +constexpr Error model(T *h, ox::CommonPtrWith auto *obj) noexcept { h->template setTypeInfo(); - oxReturnError(h->field("path", SerStr(&obj->path))); - oxReturnError(h->field("constPath", SerStr(&obj->path))); + oxReturnError(h->fieldCString("path", &obj->path)); + oxReturnError(h->fieldCString("constPath", &obj->path)); oxReturnError(h->field("inode", &obj->inode)); return OxError(0); } template -constexpr Error model(T *h, FileAddress *fa) noexcept { - h->template setTypeInfo(); - oxReturnError(h->field("type", bit_cast(&fa->m_type))); - oxReturnError(h->field("data", UnionView(&fa->m_data, static_cast(fa->m_type)))); +constexpr Error model(T *io, ox::CommonPtrWith auto *fa) noexcept { + io->template setTypeInfo(); + // cannot read from object in Reflect operation + if constexpr(ox_strcmp(T::opType(), OpType::Reflect) == 0) { + int8_t type = 0; + oxReturnError(io->field("type", &type)); + oxReturnError(io->field("data", UnionView(&fa->m_data, 0))); + } else { + auto type = static_cast(fa->m_type); + oxReturnError(io->field("type", &type)); + fa->m_type = static_cast(type); + oxReturnError(io->field("data", UnionView(&fa->m_data, static_cast(fa->m_type)))); + } return OxError(0); } + ``` There are also macros in `````` for simplifying the declaration of models: @@ -511,7 +521,9 @@ ox::Result loadPalette2(const Buffer &buff) noexcept { ox::Result loadPalette3(const Buffer &buff) noexcept { NostalgiaPalette pal; - oxReturnError(ox::readMC(buff.data(), buff.size(), &pal)); + std::size_t sz = 0; + oxReturnError(ox::readMC(buff.data(), buff.size(), &pal, &sz)); + buffer.resize(sz); return pal; } ``` @@ -523,7 +535,9 @@ ox::Result loadPalette3(const Buffer &buff) noexcept { ox::Result writeSpritePalette1(NostalgiaPalette *pal) noexcept { ox::Buffer buffer(ox::units::MB); - oxReturnError(ox::writeMC(buffer.data(), buffer.size(), pal)); + std::size_t sz = 0; + oxReturnError(ox::writeMC(buffer.data(), buffer.size(), pal, &sz)); + buffer.resize(sz); return std::move(buffer); }