Created
April 3, 2020 18:02
-
-
Save PsichiX/11eea575a564d2cc50e7e313eccfc737 to your computer and use it in GitHub Desktop.
Data Driven Template engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import './serialization.chrobry' | |
inject | |
``` | |
#pragma once | |
#include <string> | |
#include <sstream> | |
``` | |
extern 'int' 'float' { | |
impl Display | |
``` | |
std::string Display(%{ $TYPENAME }% self) { return std::to_string(self); } | |
``` | |
impl Clone | |
``` | |
%{ $TYPENAME }% Clone(%{ $TYPENAME }% self) { return self; } | |
``` | |
} | |
extern 'std::string' { | |
impl Display | |
``` | |
std::string Display(const std::string& self) { return self; } | |
``` | |
impl Clone | |
``` | |
std::string Clone(const std::string& self) { return self; } | |
``` | |
} | |
@Display | |
@Clone | |
struct Foo { | |
a: 'int', | |
b: 'std::string', | |
c: Status, | |
d: 'float', | |
} | |
@Display | |
@Clone | |
enum Status { | |
Ok, | |
Error, | |
} | |
impl struct Display | |
``` | |
std::string Display(const %{ $TYPENAME }%& self) { | |
std::stringstream result("%{ $TYPENAME }% {\n"); | |
%{ | |
for $name $type in fields [where $type impl Display] | |
``` | |
result << "%{ $name }%: " << Display(self.%{ $name }%) << "\n"; | |
``` | |
}% | |
result << " }"; | |
return result; | |
} | |
``` | |
impl enum Display | |
``` | |
std::string Display(%{ $TYPENAME }% self) { | |
std::stringstream result("%{ $TYPENAME }%::"); | |
%{ | |
match $name in fields | |
``` | |
result << "%{ $name }%"; | |
``` | |
}% | |
return result; | |
} | |
``` | |
impl struct Clone [where fields impl Clone Default] | |
``` | |
%{ $TYPENAME }% Clone(const %{ $TYPENAME }%& self) { | |
%{ $TYPENAME }% result = { | |
%{ | |
for $name $type in fields | |
``` | |
Clone(self.%{ $name }%), | |
``` | |
}% | |
}; | |
return result; | |
} | |
``` | |
impl enum Clone | |
``` | |
%{ $TYPENAME }% Clone(%{ $TYPENAME }% self) { | |
return self; | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment