All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cpp-ue4.model-source.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>licenseInfo}}
#include "{{classname}}.h"

#include "{{unrealModuleName}}Module.h"
#include "{{modelNamePrefix}}Helpers.h"

#include "Templates/SharedPointer.h"

{{#cppNamespaceDeclarations}}
namespace {{this}}
{
{{/cppNamespaceDeclarations}}
{{#models}}{{#model}}
{{#isEnum}}
inline FString ToString(const {{classname}}::Values& Value)
{
	{{#allowableValues}}
	switch (Value)
	{
	{{#enumVars}}
	case {{classname}}::Values::{{name}}:
		return TEXT("{{{value}}}");
	{{/enumVars}}
	}
	{{/allowableValues}}

	UE_LOG(Log{{unrealModuleName}}, Error, TEXT("Invalid {{classname}}::Values Value (%d)"), (int)Value);
	return TEXT("");
}

FString {{classname}}::EnumToString(const {{classname}}::Values& EnumValue)
{
	return ToString(EnumValue);
}

inline bool FromString(const FString& EnumAsString, {{classname}}::Values& Value)
{
	static TMap StringToEnum = { {{#allowableValues}}{{#enumVars}}
		{ TEXT("{{{value}}}"), {{classname}}::Values::{{name}} },{{/enumVars}}{{/allowableValues}} };

	const auto Found = StringToEnum.Find(EnumAsString);
	if(Found)
		Value = *Found;

	return Found != nullptr;
}

bool {{classname}}::EnumFromString(const FString& EnumAsString, {{classname}}::Values& EnumValue)
{
	return FromString(EnumAsString, EnumValue);
}

inline void WriteJsonValue(JsonWriter& Writer, const {{classname}}::Values& Value)
{
	WriteJsonValue(Writer, ToString(Value));
}

inline bool TryGetJsonValue(const TSharedPtr& JsonValue, {{classname}}::Values& Value)
{
	{{#allowableValues}}
	FString TmpValue;
	if (JsonValue->TryGetString(TmpValue))
	{
		if(FromString(TmpValue, Value))
			return true;
	}
	{{/allowableValues}}
	return false;
}

{{/isEnum}}
{{#hasEnums}}
{{#vars}}
{{#isEnum}}
inline FString ToString(const {{classname}}::{{{enumName}}}& Value)
{
	{{#allowableValues}}
	switch (Value)
	{
	{{#enumVars}}
	case {{classname}}::{{{enumName}}}::{{name}}:
		return TEXT("{{{value}}}");
	{{/enumVars}}
	}
	{{/allowableValues}}

	UE_LOG(Log{{unrealModuleName}}, Error, TEXT("Invalid {{classname}}::{{{enumName}}} Value (%d)"), (int)Value);
	return TEXT("");
}

FString {{classname}}::EnumToString(const {{classname}}::{{{enumName}}}& EnumValue)
{
	return ToString(EnumValue);
}

inline bool FromString(const FString& EnumAsString, {{classname}}::{{{enumName}}}& Value)
{
	static TMap StringToEnum = { {{#allowableValues}}{{#enumVars}}
		{ TEXT("{{{value}}}"), {{classname}}::{{{enumName}}}::{{name}} },{{/enumVars}}{{/allowableValues}} };

	const auto Found = StringToEnum.Find(EnumAsString);
	if(Found)
		Value = *Found;

	return Found != nullptr;
}

bool {{classname}}::EnumFromString(const FString& EnumAsString, {{classname}}::{{{enumName}}}& EnumValue)
{
	return FromString(EnumAsString, EnumValue);
}

inline void WriteJsonValue(JsonWriter& Writer, const {{classname}}::{{{enumName}}}& Value)
{
	WriteJsonValue(Writer, ToString(Value));
}

inline bool TryGetJsonValue(const TSharedPtr& JsonValue, {{classname}}::{{{enumName}}}& Value)
{
	{{#allowableValues}}
	FString TmpValue;
	if (JsonValue->TryGetString(TmpValue))
	{
		if(FromString(TmpValue, Value))
			return true;
	}
	{{/allowableValues}}
	return false;
}

{{/isEnum}}
{{/vars}}
{{/hasEnums}}
void {{classname}}::WriteJson(JsonWriter& Writer) const
{
	{{#isString}}
	WriteJsonValue(Writer, Value);
	{{/isString}}
	{{^isString}}
	Writer->WriteObjectStart();
    {{#vars}}
	{{#required}}
	Writer->WriteIdentifierPrefix(TEXT("{{baseName}}")); WriteJsonValue(Writer, {{name}});
	{{/required}}
	{{^required}}
	if ({{name}}.IsSet())
	{
		Writer->WriteIdentifierPrefix(TEXT("{{baseName}}")); WriteJsonValue(Writer, {{name}}.GetValue());
	}
	{{/required}}
    {{/vars}}
	Writer->WriteObjectEnd();
	{{/isString}}
}

bool {{classname}}::FromJson(const TSharedPtr& JsonValue)
{
	{{#isString}}
	return TryGetJsonValue(JsonValue, Value);
	{{/isString}}
	{{^isString}}
	const TSharedPtr* Object;
	if (!JsonValue->TryGetObject(Object))
		return false;

	bool ParseSuccess = true;

	{{#vars}}
	{{#required}}
	{{^isWriteOnly}}ParseSuccess &= {{/isWriteOnly}}TryGetJsonValue(*Object, TEXT("{{baseName}}"), {{name}});
	{{/required}}
	{{^required}}
	ParseSuccess &= TryGetJsonValue(*Object, TEXT("{{baseName}}"), {{name}});
	{{/required}}
	{{/vars}}

	return ParseSuccess;
	{{/isString}}
}

{{/model}}
{{/models}}
{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy