[nostalgia] Update model example code in Developer Handbook
This commit is contained in:
		| @@ -386,16 +386,16 @@ struct NostalgiaGraphic { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| template<typename T> | template<typename T> | ||||||
| constexpr ox::Error model(T *h, NostalgiaPalette *pal) noexcept { | constexpr ox::Error model(T *h, ox::CommonPtrWith<NostalgiaPalette> auto *pal) noexcept { | ||||||
| 	h->template setTypeInfo<NostalgiaPalette>(); | 	h->template setTypeInfo<NostalgiaPalette>(); | ||||||
| 	// 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); | 	//h->setTypeInfo("net.drinkingtea.nostalgia.core.NostalgiaPalette", 1); | ||||||
| 	oxReturnError(h->field("colors", &pal->colors)); | 	oxReturnError(h->field("colors", &pal->colors)); | ||||||
| 	return OxError(0); | 	return OxError(0); | ||||||
| } | } | ||||||
|  |  | ||||||
| template<typename T> | template<typename T> | ||||||
| constexpr ox::Error model(T *h, NostalgiaGraphic *ng) noexcept { | constexpr ox::Error model(T *h, ox::CommonPtrWith<NostalgiaGraphic> auto *ng) noexcept { | ||||||
| 	h->template setTypeInfo<NostalgiaGraphic>(); | 	h->template setTypeInfo<NostalgiaGraphic>(); | ||||||
| 	oxReturnError(h->field("bpp", &ng->bpp)); | 	oxReturnError(h->field("bpp", &ng->bpp)); | ||||||
| 	oxReturnError(h->field("rows", &ng->rows)); | 	oxReturnError(h->field("rows", &ng->rows)); | ||||||
| @@ -416,7 +416,7 @@ The model system also provides for unions: | |||||||
| class FileAddress { | class FileAddress { | ||||||
|  |  | ||||||
| 	template<typename T> | 	template<typename T> | ||||||
| 	friend constexpr Error model(T*, FileAddress*) noexcept; | 	friend constexpr Error model(T*, ox::CommonPtrWith<FileAddress> auto*) noexcept; | ||||||
|  |  | ||||||
| 	public: | 	public: | ||||||
| 		static constexpr auto TypeName = "net.drinkingtea.ox.FileAddress"; | 		static constexpr auto TypeName = "net.drinkingtea.ox.FileAddress"; | ||||||
| @@ -435,21 +435,31 @@ class FileAddress { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| template<typename T> | template<typename T> | ||||||
| constexpr Error model(T *h, FileAddress::Data *obj) noexcept { | constexpr Error model(T *h, ox::CommonPtrWith<FileAddress::Data> auto *obj) noexcept { | ||||||
| 	h->template setTypeInfo<FileAddress::Data>(); | 	h->template setTypeInfo<FileAddress::Data>(); | ||||||
| 	oxReturnError(h->field("path", SerStr(&obj->path))); | 	oxReturnError(h->fieldCString("path", &obj->path)); | ||||||
| 	oxReturnError(h->field("constPath", SerStr(&obj->path))); | 	oxReturnError(h->fieldCString("constPath", &obj->path)); | ||||||
| 	oxReturnError(h->field("inode", &obj->inode)); | 	oxReturnError(h->field("inode", &obj->inode)); | ||||||
| 	return OxError(0); | 	return OxError(0); | ||||||
| } | } | ||||||
|  |  | ||||||
| template<typename T> | template<typename T> | ||||||
| constexpr Error model(T *h, FileAddress *fa) noexcept { | constexpr Error model(T *io, ox::CommonPtrWith<FileAddress> auto *fa) noexcept { | ||||||
| 	h->template setTypeInfo<FileAddress>(); | 	io->template setTypeInfo<FileAddress>(); | ||||||
| 	oxReturnError(h->field("type", bit_cast<int8_t*>(&fa->m_type))); | 	// cannot read from object in Reflect operation | ||||||
| 	oxReturnError(h->field("data", UnionView(&fa->m_data, static_cast<int>(fa->m_type)))); | 	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<int8_t>(fa->m_type); | ||||||
|  | 		oxReturnError(io->field("type", &type)); | ||||||
|  | 		fa->m_type = static_cast<FileAddressType>(type); | ||||||
|  | 		oxReturnError(io->field("data", UnionView(&fa->m_data, static_cast<int>(fa->m_type)))); | ||||||
|  | 	} | ||||||
| 	return OxError(0); | 	return OxError(0); | ||||||
| } | } | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| There are also macros in ```<ox/model/def.hpp>``` for simplifying the declaration of models: | There are also macros in ```<ox/model/def.hpp>``` for simplifying the declaration of models: | ||||||
| @@ -511,7 +521,9 @@ ox::Result<NostalgiaPalette> loadPalette2(const Buffer &buff) noexcept { | |||||||
|  |  | ||||||
| ox::Result<NostalgiaPalette> loadPalette3(const Buffer &buff) noexcept { | ox::Result<NostalgiaPalette> loadPalette3(const Buffer &buff) noexcept { | ||||||
| 	NostalgiaPalette pal; | 	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; | 	return pal; | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
| @@ -523,7 +535,9 @@ ox::Result<NostalgiaPalette> loadPalette3(const Buffer &buff) noexcept { | |||||||
|  |  | ||||||
| ox::Result<ox::Buffer> writeSpritePalette1(NostalgiaPalette *pal) noexcept { | ox::Result<ox::Buffer> writeSpritePalette1(NostalgiaPalette *pal) noexcept { | ||||||
| 	ox::Buffer buffer(ox::units::MB); | 	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); | 	return std::move(buffer); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user