This commit is contained in:
2024-12-24 16:49:34 +08:00
parent 3eeb1f6cb6
commit 5219351df1
14 changed files with 945 additions and 109 deletions

View File

@ -1,2 +1,3 @@
# Result
*.hpp
VxTypes.hpp
VxTypes.cpp

View File

@ -0,0 +1,347 @@
struct VxVector2 {
CKFLOAT x, y;
VxVector2() : x(0.0f), y(0.0f) {}
VxVector2(CKFLOAT _x, CKFLOAT _y) : x(_x), y(_y) {}
YYCC_DEF_CLS_COPY_MOVE(VxVector2);
CKFLOAT& operator[](size_t i) {
switch (i) {
case 0: return x;
case 1: return y;
default: throw LogicException("Invalid index for VxVector2::operator[].");
}
}
const CKFLOAT& operator[](size_t i) const {
switch (i) {
case 0: return x;
case 1: return y;
default: throw LogicException("Invalid index for VxVector2::operator[].");
}
}
VxVector2& operator+=(const VxVector2& rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
friend VxVector2 operator+(const VxVector2& lhs, const VxVector2& rhs) {
return VxVector2(lhs.x + rhs.x, lhs.y + rhs.y);
}
VxVector2& operator-=(const VxVector2& rhs) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
friend VxVector2 operator-(const VxVector2& lhs, const VxVector2& rhs) {
return VxVector2(lhs.x - rhs.x, lhs.y - rhs.y);
}
VxVector2& operator*=(CKFLOAT rhs) {
x *= rhs;
y *= rhs;
return *this;
}
friend VxVector2 operator*(const VxVector2& lhs, CKFLOAT rhs) {
return VxVector2(lhs.x * rhs, lhs.y * rhs);
}
friend VxVector2 operator*(CKFLOAT lhs, const VxVector2& rhs) {
return VxVector2(lhs * rhs.x, lhs * rhs.y);
}
friend CKFLOAT operator*(const VxVector2& lhs, const VxVector2& rhs) {
return (lhs.x * rhs.x + lhs.y * rhs.y);
}
VxVector2& operator/=(CKFLOAT rhs) {
if (rhs == 0.0f) return *this;
x /= rhs;
y /= rhs;
return *this;
}
friend VxVector2 operator/(const VxVector2& lhs, CKFLOAT rhs) {
if (rhs == 0.0f) return VxVector2(0.0f, 0.0f);
return VxVector2(lhs.x / rhs, lhs.y / rhs);
}
bool operator==(const VxVector2& rhs) const {
return (x == rhs.x && y == rhs.y);
}
auto operator<=>(const VxVector2& rhs) const {
if (auto cmp = x <=> rhs.x; cmp != 0) return cmp;
return y <=> rhs.y;
}
CKFLOAT SquaredLength() const {
return (x * x + y * y);
}
CKFLOAT Length() const {
return std::sqrt(SquaredLength());
}
void Normalized() {
CKFLOAT len = Length();
if (len == 0.0f) return;
x /= len;
y /= len;
}
VxVector2 Normalize() const {
CKFLOAT len = Length();
if (len == 0.0f) return VxVector2();
return VxVector2(x / len, y / len);
}
};
struct VxVector3 {
CKFLOAT x, y, z;
VxVector3() : x(0.0f), y(0.0f), z(0.0f) {}
VxVector3(CKFLOAT _x, CKFLOAT _y, CKFLOAT _z) : x(_x), y(_y), z(_z) {}
YYCC_DEF_CLS_COPY_MOVE(VxVector3);
CKFLOAT& operator[](size_t i) {
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
default: throw LogicException("Invalid index for VxVector3::operator[].");
}
}
const CKFLOAT& operator[](size_t i) const {
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
default: throw LogicException("Invalid index for VxVector3::operator[].");
}
}
VxVector3& operator+=(const VxVector3& rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
friend VxVector3 operator+(const VxVector3& lhs, const VxVector3& rhs) {
return VxVector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
VxVector3& operator-=(const VxVector3& rhs) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
friend VxVector3 operator-(const VxVector3& lhs, const VxVector3& rhs) {
return VxVector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
}
VxVector3& operator*=(CKFLOAT rhs) {
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
friend VxVector3 operator*(const VxVector3& lhs, CKFLOAT rhs) {
return VxVector3(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
}
friend VxVector3 operator*(CKFLOAT lhs, const VxVector3& rhs) {
return VxVector3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
}
friend CKFLOAT operator*(const VxVector3& lhs, const VxVector3& rhs) {
return (lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z);
}
VxVector3& operator/=(CKFLOAT rhs) {
if (rhs == 0.0f) return *this;
x /= rhs;
y /= rhs;
z /= rhs;
return *this;
}
friend VxVector3 operator/(const VxVector3& lhs, CKFLOAT rhs) {
if (rhs == 0.0f) return VxVector3(0.0f, 0.0f, 0.0f);
return VxVector3(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs);
}
bool operator==(const VxVector3& rhs) const {
return (x == rhs.x && y == rhs.y && z == rhs.z);
}
auto operator<=>(const VxVector3& rhs) const {
if (auto cmp = x <=> rhs.x; cmp != 0) return cmp;
if (auto cmp = y <=> rhs.y; cmp != 0) return cmp;
return z <=> rhs.z;
}
CKFLOAT SquaredLength() const {
return (x * x + y * y + z * z);
}
CKFLOAT Length() const {
return std::sqrt(SquaredLength());
}
void Normalized() {
CKFLOAT len = Length();
if (len == 0.0f) return;
x /= len;
y /= len;
z /= len;
}
VxVector3 Normalize() const {
CKFLOAT len = Length();
if (len == 0.0f) return VxVector3();
return VxVector3(x / len, y / len, z / len);
}
};
struct VxVector4 {
CKFLOAT x, y, z, w;
VxVector4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {}
VxVector4(CKFLOAT _x, CKFLOAT _y, CKFLOAT _z, CKFLOAT _w) : x(_x), y(_y), z(_z), w(_w) {}
YYCC_DEF_CLS_COPY_MOVE(VxVector4);
CKFLOAT& operator[](size_t i) {
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default: throw LogicException("Invalid index for VxVector4::operator[].");
}
}
const CKFLOAT& operator[](size_t i) const {
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default: throw LogicException("Invalid index for VxVector4::operator[].");
}
}
VxVector4& operator+=(const VxVector4& rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
w += rhs.w;
return *this;
}
friend VxVector4 operator+(const VxVector4& lhs, const VxVector4& rhs) {
return VxVector4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w);
}
VxVector4& operator-=(const VxVector4& rhs) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
w -= rhs.w;
return *this;
}
friend VxVector4 operator-(const VxVector4& lhs, const VxVector4& rhs) {
return VxVector4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w);
}
VxVector4& operator*=(CKFLOAT rhs) {
x *= rhs;
y *= rhs;
z *= rhs;
w *= rhs;
return *this;
}
friend VxVector4 operator*(const VxVector4& lhs, CKFLOAT rhs) {
return VxVector4(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs, lhs.w * rhs);
}
friend VxVector4 operator*(CKFLOAT lhs, const VxVector4& rhs) {
return VxVector4(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z, lhs * rhs.w);
}
friend CKFLOAT operator*(const VxVector4& lhs, const VxVector4& rhs) {
return (lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w);
}
VxVector4& operator/=(CKFLOAT rhs) {
if (rhs == 0.0f) return *this;
x /= rhs;
y /= rhs;
z /= rhs;
w /= rhs;
return *this;
}
friend VxVector4 operator/(const VxVector4& lhs, CKFLOAT rhs) {
if (rhs == 0.0f) return VxVector4(0.0f, 0.0f, 0.0f, 0.0f);
return VxVector4(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs, lhs.w / rhs);
}
bool operator==(const VxVector4& rhs) const {
return (x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w);
}
auto operator<=>(const VxVector4& rhs) const {
if (auto cmp = x <=> rhs.x; cmp != 0) return cmp;
if (auto cmp = y <=> rhs.y; cmp != 0) return cmp;
if (auto cmp = z <=> rhs.z; cmp != 0) return cmp;
return w <=> rhs.w;
}
CKFLOAT SquaredLength() const {
return (x * x + y * y + z * z + w * w);
}
CKFLOAT Length() const {
return std::sqrt(SquaredLength());
}
void Normalized() {
CKFLOAT len = Length();
if (len == 0.0f) return;
x /= len;
y /= len;
z /= len;
w /= len;
}
VxVector4 Normalize() const {
CKFLOAT len = Length();
if (len == 0.0f) return VxVector4();
return VxVector4(x / len, y / len, z / len, w / len);
}
};
struct VxQuaternion {
CKFLOAT x, y, z, w;
VxQuaternion() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {} // set your custom init.
VxQuaternion(CKFLOAT _x, CKFLOAT _y, CKFLOAT _z, CKFLOAT _w) : x(_x), y(_y), z(_z), w(_w) {}
YYCC_DEF_CLS_COPY_MOVE(VxQuaternion);
CKFLOAT& operator[](size_t i) {
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default: throw LogicException("Invalid index for VxQuaternion::operator[].");
}
}
const CKFLOAT& operator[](size_t i) const {
switch (i) {
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
default: throw LogicException("Invalid index for VxQuaternion::operator[].");
}
}
bool operator==(const VxQuaternion& rhs) const {
return (x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w);
}
auto operator<=>(const VxQuaternion& rhs) const {
if (auto cmp = x <=> rhs.x; cmp != 0) return cmp;
if (auto cmp = y <=> rhs.y; cmp != 0) return cmp;
if (auto cmp = z <=> rhs.z; cmp != 0) return cmp;
return w <=> rhs.w;
}
};
struct VxColor {
CKFLOAT r, g, b, a;
VxColor() : r(0.0f), g(0.0f), b(0.0f), a(0.0f) {} // set your custom init.
VxColor(CKFLOAT _r, CKFLOAT _g, CKFLOAT _b, CKFLOAT _a) : r(_r), g(_g), b(_b), a(_a) {}
YYCC_DEF_CLS_COPY_MOVE(VxColor);
CKFLOAT& operator[](size_t i) {
switch (i) {
case 0: return r;
case 1: return g;
case 2: return b;
case 3: return a;
default: throw LogicException("Invalid index for VxColor::operator[].");
}
}
const CKFLOAT& operator[](size_t i) const {
switch (i) {
case 0: return r;
case 1: return g;
case 2: return b;
case 3: return a;
default: throw LogicException("Invalid index for VxColor::operator[].");
}
}
bool operator==(const VxColor& rhs) const {
return (r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a);
}
auto operator<=>(const VxColor& rhs) const {
if (auto cmp = r <=> rhs.r; cmp != 0) return cmp;
if (auto cmp = g <=> rhs.g; cmp != 0) return cmp;
if (auto cmp = b <=> rhs.b; cmp != 0) return cmp;
return a <=> rhs.a;
}
};

View File

@ -0,0 +1,178 @@
{% import 'VxTypes.shared.jinja' as shared %}
{#
For friend operator overload, we do not need add CLASSNAME:: prefix for it.
Because they are not a part of that class.
#}
#pragma region {{ sname }}
{#- Ctor type 1 - Default ctor #}
{{ sname }}::{{ sname }}() : {{- shared.initialize_list_builder(svars, False) -}} {}
{#- Ctor type 2 - User specified ctor #}
{{ sname }}::{{ sname }}({{- shared.argument_list_builder(svars) -}}) : {{- shared.initialize_list_builder(svars, True) -}} {}
{#- Offset operator #}
CKFLOAT& {{ sname }}::operator[](size_t i) {
switch (i) {
{%- for item in svars %}
case {{ loop.index0 }}: return item;
{%- endfor %}
default: throw LogicException("Invalid index for {{ sname }}::operator[].");
}
}
const CKFLOAT& {{ sname }}::operator[](size_t i) const {
switch (i) {
{%- for item in svars %}
case {{ loop.index0 }}: return {{ item }};
{%- endfor %}
default: throw LogicException("Invalid index for {{ sname }}::operator[].");
}
}
{#- Equal operator #}
bool {{ sname }}::operator==(const {{ sname }}& rhs) const {
return (
{%- for item in svars %}
lhs.{{ item }} * rhs.{{ item }} {% if not loop.last %} && {% endif %}
{%- endfor %}
);
}
{#- Spaceship operator #}
auto {{ sname }}::operator<=>(const {{ sname }}& rhs) const {
{%- for item in svars[:-1] %}
if (auto cmp = {{ item }} <=> rhs.{{ item }}; cmp != 0) return cmp;
{%- endfor %}
return svars[-1] <=> rhs.svars[-1];
}
{#- BEGIN VECTOR SPECIFIC #}
{%- if is_vector %}
{#- Add, minus operators #}
{#- Unary operators #}
{{ sname }} {{ sname }}::operator+() const {
return *this;
}
{{ sname }} {{ sname }}::operator-() const {
return {{ sname }}(
{%- for item in svars %}
-{{ item }} {%- if not loop.last %}, {% endif %}
{%- endfor %}
);
}
{#- Additive operators #}
{{ sname }}& {{ sname }}::operator+=(const {{ sname }}& rhs) {
{%- for item in svars %}
{{ item }} += rhs.{{ item }};
{%- endfor %}
return *this;
}
{{ sname }} operator+(const {{ sname }}& lhs, const {{ sname }}& rhs) {
return {{ sname }}(
{%- for item in svars %}
lhs.{{ item }} + rhs.{{ item }} {%- if not loop.last %}, {% endif %}
{%- endfor %}
);
}
{{ sname }}& {{ sname }}::operator-=(const {{ sname }}& rhs) {
{%- for item in svars %}
{{ item }} -= rhs.{{ item }};
{%- endfor %}
return *this;
}
{{ sname }} operator-(const {{ sname }}& lhs, const {{ sname }}& rhs) {
return {{ sname }}(
{%- for item in svars %}
lhs.{{ item }} - rhs.{{ item }} {%- if not loop.last %}, {% endif %}
{%- endfor %}
);
}
{#- Mul operator #}
{{ sname }}& {{ sname }}::operator*=(CKFLOAT rhs) {
{%- for item in svars %}
{{ item }} *= rhs;
{%- endfor %}
return *this;
}
{{ sname }} operator*(const {{ sname }}& lhs, CKFLOAT rhs) {
return {{ sname }}(
{%- for item in svars %}
lhs.{{ item }} * rhs {%- if not loop.last %}, {% endif %}
{%- endfor %}
);
}
{{ sname }} operator*(CKFLOAT lhs, const {{ sname }}& rhs) {
return {{ sname }}(
{%- for item in svars %}
lhs.{{ item }} * rhs {%- if not loop.last %}, {% endif %}
{%- endfor %}
);
}
CKFLOAT operator*(const {{ sname }}& lhs, const {{ sname }}& rhs) {
return (
{%- for item in svars %}
lhs.{{ item }} * rhs.{{ item }} {%- if not loop.last %} + {% endif %}
{%- endfor %}
);
}
{#- Div operator #}
{{ sname }}& {{ sname }}::operator/=(CKFLOAT rhs) {
if (rhs == 0.0f) return *this;
{%- for item in svars %}
{{ item }} /= rhs;
{%- endfor %}
return *this;
}
{{ sname }} operator/(const {{ sname }}& lhs, CKFLOAT rhs) {
if (rhs == 0.0f) return {{ sname }}(0.0f, 0.0f);
return {{ sname }}(
{%- for item in svars %}
lhs.{{ item }} / rhs {%- if not loop.last %}, {% endif %}
{%- endfor %}
);
}
{#- Length functions #}
CKFLOAT {{ sname }}::SquaredLength() const {
return (
{%- for item in svars %}
{{ item }} * {{ item }} {%- if not loop.last %} + {% endif %}
{%- endfor %}
);
}
CKFLOAT {{ sname }}::Length() const {
return std::sqrt(SquaredLength());
}
{#- Normalize functions #}
void {{ sname }}::Normalized() {
CKFLOAT len = Length();
if (len == 0.0f) return;
{%- for item in svars %}
{{ item }} /= len;
{%- endfor %}
}
{{ sname }} {{ sname }}::Normalize() const {
CKFLOAT len = Length();
if (len == 0.0f) return {{ sname }}();
return {{ sname }}(
{%- for item in svars %}
{{ item }} / len {%- if not loop.last %} + {% endif %}
{%- endfor %}
);
}
{%- endif %}
{#- END VECTOR SPECIFIC #}
{#- User custom region #}
/* ===== BEGIN USER CUSTOM ===== */
/* ===== END USER CUSTOM ===== */
#pragma endregion

View File

@ -0,0 +1,65 @@
{% import 'VxTypes.shared.jinja' as shared %}
struct {{ sname }} {
{#- Variable declaration #}
CKFLOAT {{ ", ".join(svars) }};
{#- Ctor type 1 - Default ctor #}
{{ sname }}();
{#- Ctor type 2 - User specified ctor #}
{{ sname }}({{- shared.argument_list_builder(svars) -}});
{#- Default copy ctor, move ctor, copy assigner, move assigner #}
YYCC_DEF_CLS_COPY_MOVE({{ sname }});
{#- Offset operator #}
CKFLOAT& operator[](size_t i);
const CKFLOAT& operator[](size_t i) const;
{#- Equal operator #}
bool operator==(const {{ sname }}& rhs) const;
{#- Spaceship operator #}
auto operator<=>(const {{ sname }}& rhs) const;
{#- BEGIN VECTOR SPECIFIC #}
{%- if is_vector %}
{#- Add, minus operators #}
{#- Unary operators #}
{{ sname }} operator+() const;
{{ sname }} operator-() const;
{#- Additive operators #}
{{ sname }}& operator+=(const {{ sname }}& rhs);
friend {{ sname }} operator+(const {{ sname }}& lhs, const {{ sname }}& rhs);
{{ sname }}& operator-=(const {{ sname }}& rhs);
friend {{ sname }} operator-(const {{ sname }}& lhs, const {{ sname }}& rhs);
{#- Mul operator #}
{{ sname }}& operator*=(CKFLOAT rhs);
friend {{ sname }} operator*(const {{ sname }}& lhs, CKFLOAT rhs);
friend {{ sname }} operator*(CKFLOAT lhs, const {{ sname }}& rhs);
friend CKFLOAT operator*(const {{ sname }}& lhs, const {{ sname }}& rhs);
{#- Div operator #}
{{ sname }}& operator/=(CKFLOAT rhs);
friend {{ sname }} operator/(const {{ sname }}& lhs, CKFLOAT rhs);
{#- Length functions #}
CKFLOAT SquaredLength() const;
CKFLOAT Length() const;
{#- Normalize functions #}
void Normalized();
{{ sname }} Normalize() const;
{%- endif %}
{#- END VECTOR SPECIFIC #}
{#- User custom region #}
/* ===== BEGIN USER CUSTOM ===== */
/* ===== END USER CUSTOM ===== */
};

View File

@ -0,0 +1,21 @@
{#
The macro to generate C++ ctor argument list
It produce like this: `CKFLOAT _x, CKFLOAT _y, CKFLOAT _z, CKFLOAT _w`
#}
{% macro argument_list_builder(svars) %}
{%- for item in svars -%}
CKFLOAT _{{- item -}}{%- if not loop.last %}, {% endif -%}
{%- endfor -%}
{% endmacro %}
{#
The macro to generate C++ ctor initialize list
It produce like this: `x(0.0f), y(0.0f), z(0.0f), w(0.0f)`
or this: `x(_x), y(_y), z(_z), w(_w)`
according to user request.
#}
{% macro initialize_list_builder(svars, is_user) %}
{%- for item in svars -%}
{{- item -}}({%- if is_user -%}_{{- item -}}{%- else -%}0.0f{%- endif -%}){%- if not loop.last %}, {% endif -%}
{%- endfor -%}
{% endmacro %}

View File

@ -0,0 +1,63 @@
import os
import io
import typing
import jinja2
g_HppTemplateFile: str = 'VxTypes.hpp.jinja'
g_CppTemplateFile: str = 'VxTypes.cpp.jinja'
g_ResultHppFile: str = 'VxTypes.hpp'
g_ResultCppFile: str = 'VxTypes.cpp'
def get_root_directory() -> str:
return os.path.dirname(__file__)
class TemplateRender:
m_Loader: jinja2.BaseLoader
m_Environment: jinja2.Environment
m_HppTemplate: jinja2.Template
m_CppTemplate: jinja2.Template
m_OutputHpp: io.TextIOWrapper
m_OutputCpp: io.TextIOWrapper
def __init__(self, output_hpp_path: str, output_cpp_path: str) -> None:
self.m_Loader = jinja2.FileSystemLoader(get_root_directory())
self.m_Environment = jinja2.Environment(loader=self.m_Loader)
self.m_HppTemplate = self.m_Environment.get_template(g_HppTemplateFile)
self.m_CppTemplate = self.m_Environment.get_template(g_CppTemplateFile)
self.m_OutputHpp = open(os.path.join(get_root_directory(), output_hpp_path), 'w', encoding='utf-8')
self.m_OutputCpp = open(os.path.join(get_root_directory(), output_cpp_path), 'w', encoding='utf-8')
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.m_OutputHpp.close()
self.m_OutputCpp.close()
def __render(self, sname: str, is_vector: bool, svars: tuple[str]) -> None:
template_argument: dict[str, typing.Any] = {
'sname': sname,
'is_vector': is_vector,
'svars': svars
}
self.m_OutputHpp.write(self.m_HppTemplate.render(**template_argument))
self.m_OutputCpp.write(self.m_CppTemplate.render(**template_argument))
def render_vector(self, sname: str, svars: tuple[str]) -> None:
self.__render(sname, True, svars)
def render_others(self, sname: str, svars: tuple[str]) -> None:
self.__render(sname, False, svars)
if __name__ == '__main__':
with TemplateRender(g_ResultHppFile, g_ResultCppFile) as render:
render.render_vector('VxVector2', ('x', 'y', ))
render.render_vector('VxVector3', ('x', 'y', 'z', ))
render.render_vector('VxVector4', ('x', 'y', 'z', 'w', ))
render.render_others('VxQuaternion', ('x', 'y', 'z', 'w', ))
render.render_others('VxColor', ('r', 'g', 'b', 'a', ))