[ox/claw] Fix Claw read to check for type/version compatibility
This commit is contained in:
		
							
								
								
									
										9
									
								
								deps/ox/src/ox/claw/read.hpp
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										9
									
								
								deps/ox/src/ox/claw/read.hpp
									
									
									
									
										vendored
									
									
								
							| @@ -19,6 +19,9 @@ | ||||
|  | ||||
| namespace ox { | ||||
|  | ||||
| constexpr auto Error_ClawTypeMismatch = 200; | ||||
| constexpr auto Error_ClawTypeVersionMismatch = 200; | ||||
|  | ||||
| struct ClawHeader { | ||||
| 	String typeName; | ||||
| 	int typeVersion = -1; | ||||
| @@ -38,6 +41,12 @@ Result<Buffer> stripClawHeader(const ox::Buffer &buff) noexcept; | ||||
| template<typename T> | ||||
| Error readClaw(const char *buff, std::size_t buffLen, T *val) { | ||||
| 	oxRequire(header, readClawHeader(buff, buffLen)); | ||||
| 	if (header.typeName != getModelTypeName<T>()) { | ||||
| 		return OxError(Error_ClawTypeMismatch, "Claw Read: Type mismatch"); | ||||
| 	} | ||||
| 	if (header.typeVersion != getModelTypeVersion<T>()) { | ||||
| 		return OxError(Error_ClawTypeVersionMismatch, "Claw Read: Type Verion mismatch"); | ||||
| 	} | ||||
| 	switch (header.fmt) { | ||||
| 		case ClawFormat::Metal: | ||||
| 		{ | ||||
|   | ||||
							
								
								
									
										28
									
								
								deps/ox/src/ox/model/typenamecatcher.hpp
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										28
									
								
								deps/ox/src/ox/model/typenamecatcher.hpp
									
									
									
									
										vendored
									
									
								
							| @@ -20,12 +20,14 @@ namespace ox { | ||||
| struct TypeNameCatcher { | ||||
|  | ||||
| 	const char *name = ""; | ||||
| 	int version = 0; | ||||
|  | ||||
| 	constexpr TypeNameCatcher() noexcept = default; | ||||
|  | ||||
| 	template<typename T = std::nullptr_t> | ||||
| 	constexpr void setTypeInfo(const char *n = T::TypeName, int = 0) noexcept { | ||||
| 	constexpr void setTypeInfo(const char *n = T::TypeName, int v = 0) noexcept { | ||||
| 		this->name = n; | ||||
| 		this->version = v; | ||||
| 	} | ||||
|  | ||||
| 	template<typename T> | ||||
| @@ -44,6 +46,30 @@ struct TypeNameCatcher { | ||||
|  | ||||
| }; | ||||
|  | ||||
| template<typename T> | ||||
| constexpr int getModelTypeVersion() noexcept { | ||||
| 	auto a = std::allocator<T>(); | ||||
| 	auto t = a.allocate(1); | ||||
| 	TypeNameCatcher nc; | ||||
| 	oxIgnoreError(model(&nc, t)); | ||||
| 	a.deallocate(t, 1); | ||||
| 	return nc.version; | ||||
| } | ||||
|  | ||||
| template<typename T> | ||||
| constexpr int getModelTypeVersion(T *val) noexcept { | ||||
| 	TypeNameCatcher nc; | ||||
| 	oxIgnoreError(model(&nc, val)); | ||||
| 	return nc.version; | ||||
| } | ||||
|  | ||||
| template<typename T> | ||||
| consteval int requireModelTypeVersion() noexcept { | ||||
| 	constexpr auto version = getModelTypeVersion<T>(); | ||||
| 	static_assert(version != 0, "TypeName is required"); | ||||
| 	return version; | ||||
| } | ||||
|  | ||||
| template<typename T, typename Str = const char*> | ||||
| constexpr Str getModelTypeName() noexcept { | ||||
| 	auto a = std::allocator<T>(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user