49 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#pragma once
 | 
						|
#include <string>
 | 
						|
#include "convert.h"
 | 
						|
namespace refl {
 | 
						|
	inline bool Convert::ToString(Any& dst, const Any& src)
 | 
						|
	{
 | 
						|
		if (src.cls == &TypeInfo<char>::StaticClass) {
 | 
						|
			std::construct_at(dst.CastTo<std::string*>(), src.CastTo<const char*>());
 | 
						|
			return true;
 | 
						|
		}
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
	inline bool Convert::ToInt32(Any& dst, const Any& src)
 | 
						|
	{
 | 
						|
		if (src.cls == &TypeInfo<float>::StaticClass) {
 | 
						|
			*dst.CastTo<int*>() = *src.CastTo<float*>();
 | 
						|
			return true;
 | 
						|
		}
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
	inline bool Convert::ToFloat(Any& dst, const Any& src)
 | 
						|
	{
 | 
						|
		if (src.cls == &TypeInfo<int>::StaticClass) {
 | 
						|
			*dst.CastTo<float*>() = *src.CastTo<int*>();
 | 
						|
			return true;
 | 
						|
		}
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
	inline bool Convert::Construct(Any& dst, const Any& src)
 | 
						|
	{
 | 
						|
		if (dst.cls->CtorObject((void*)dst.ptr, src)) {
 | 
						|
			return true;
 | 
						|
		}
 | 
						|
		auto it = ClassMap.find(dst.cls);
 | 
						|
		if (it == ClassMap.end()) {
 | 
						|
			return false;
 | 
						|
		}
 | 
						|
		return it->second(dst, src);
 | 
						|
	}
 | 
						|
	inline ConvertMap Convert::BuildClassMap()
 | 
						|
	{
 | 
						|
		ConvertMap classMap;
 | 
						|
		classMap.emplace(&TypeInfo<std::string>::StaticClass, &ToString);
 | 
						|
		classMap.emplace(&TypeInfo<int>::StaticClass, &ToInt32);
 | 
						|
		classMap.emplace(&TypeInfo<uint32_t>::StaticClass, &ToInt32);
 | 
						|
		classMap.emplace(&TypeInfo<float>::StaticClass, &ToFloat);
 | 
						|
		return classMap;
 | 
						|
	}
 | 
						|
} |