From b6d496b1e83853cd272a8ffdea273c59bbf4b87c Mon Sep 17 00:00:00 2001 From: YongDo-Hyun Date: Sat, 27 Dec 2025 13:05:30 +0300 Subject: feat: add numeric tag creation methods and corresponding tests for value assignments Signed-off-by: YongDo-Hyun --- src/tag.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/value.cpp | 19 +------------------ 2 files changed, 49 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/tag.cpp b/src/tag.cpp index 0bff2c6440..6175a36293 100644 --- a/src/tag.cpp +++ b/src/tag.cpp @@ -45,6 +45,24 @@ std::unique_ptr tag::clone() && return std::move(*this).move_clone(); } +namespace +{ + template + std::unique_ptr create_numeric_tag(tag_type type, T val) + { + switch(type) + { + case tag_type::Byte: return make_unique(static_cast(val)); + case tag_type::Short: return make_unique(static_cast(val)); + case tag_type::Int: return make_unique(static_cast(val)); + case tag_type::Long: return make_unique(static_cast(val)); + case tag_type::Float: return make_unique(static_cast(val)); + case tag_type::Double: return make_unique(static_cast(val)); + default: throw std::invalid_argument("Invalid numeric tag type"); + } + } +} + std::unique_ptr tag::create(tag_type type) { switch(type) @@ -66,6 +84,36 @@ std::unique_ptr tag::create(tag_type type) } } +std::unique_ptr tag::create(tag_type type, int8_t val) +{ + return create_numeric_tag(type, val); +} + +std::unique_ptr tag::create(tag_type type, int16_t val) +{ + return create_numeric_tag(type, val); +} + +std::unique_ptr tag::create(tag_type type, int32_t val) +{ + return create_numeric_tag(type, val); +} + +std::unique_ptr tag::create(tag_type type, int64_t val) +{ + return create_numeric_tag(type, val); +} + +std::unique_ptr tag::create(tag_type type, float val) +{ + return create_numeric_tag(type, val); +} + +std::unique_ptr tag::create(tag_type type, double val) +{ + return create_numeric_tag(type, val); +} + bool operator==(const tag& lhs, const tag& rhs) { if(typeid(lhs) != typeid(rhs)) diff --git a/src/value.cpp b/src/value.cpp index f07e28d82c..bf0ffcad78 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -66,21 +66,6 @@ void value::set(tag&& t) //Primitive assignment namespace // helper functions local to this translation unit { - template - std::unique_ptr make_numeric_tag(tag_type type, T val) - { - switch(type) - { - case tag_type::Byte: return std::unique_ptr(new tag_byte(val)); - case tag_type::Short: return std::unique_ptr(new tag_short(val)); - case tag_type::Int: return std::unique_ptr(new tag_int(val)); - case tag_type::Long: return std::unique_ptr(new tag_long(val)); - case tag_type::Float: return std::unique_ptr(new tag_float(val)); - case tag_type::Double: return std::unique_ptr(new tag_double(val)); - default: return nullptr; - } - } - template void assign_numeric_impl(std::unique_ptr& tag_ptr, T val, tag_type default_type) @@ -88,9 +73,7 @@ namespace // helper functions local to this translation unit using nbt::tag_type; if(!tag_ptr) { - tag_ptr = make_numeric_tag(default_type, val); - if(!tag_ptr) - throw std::invalid_argument("Invalid default_type"); + tag_ptr = tag::create(default_type, val); return; } -- cgit 0.0.5-2-1-g0f52