RyanHub – file viewer
filename: src/nbt/Tag.cpp
branch: feature/world
back to repo
/*
 *
 *               _____  _                 _
 *              /  ___|| |               | |
 *              \ `--. | |_  _ __   __ _ | |_   ___   ___
 *               `--. \| __|| '__| / _` || __| / _ \ / __|
 *              /\__/ /| |_ | |   | (_| || |_ | (_) |\__ \
 *              \____/  \__||_|    \__,_| \__| \___/ |___/
 *
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Copyright (C) 2025 Armen Deroian
 *
 */

#include "Tag.h"

#include "ArrayTag.h"
#include "CompoundTag.h"
#include "ListTag.h"
#include "PrimitiveTag.h"
#include "StringTag.h"

namespace stratos::nbt {
bool isValidType(const int type, const bool allowEnd) {
    return (allowEnd ? 0 : 1) <= type && type <= 12;
}
std::unique_ptr<Tag> Tag::clone() && {
    return std::move(*this).moveClone();
}
std::unique_ptr<Tag> Tag::create(const TagType type) {
    switch (type) {
    case TagType::Byte:
        return internal::makeUnique<ByteTag>();
    case TagType::Short:
        return internal::makeUnique<ShortTag>();
    case TagType::Int:
        return internal::makeUnique<IntTag>();
    case TagType::Long:
        return internal::makeUnique<LongTag>();
    case TagType::Float:
        return internal::makeUnique<FloatTag>();
    case TagType::Double:
        return internal::makeUnique<DoubleTag>();
    case TagType::ByteArray:
        return internal::makeUnique<ByteArrayTag>();
    case TagType::String:
        return internal::makeUnique<StringTag>();
    case TagType::List:
        return internal::makeUnique<ListTag>();
    case TagType::Compound:
        return internal::makeUnique<CompoundTag>();
    case TagType::IntArray:
        return internal::makeUnique<IntArrayTag>();
    case TagType::LongArray:
        return internal::makeUnique<LongArrayTag>();
    case TagType::End:
        return nullptr; // End tag is not creatable
    default:
        throw std::invalid_argument("Invalid tag type");
    }
}
bool                 operator==(const Tag& lhs, const Tag& rhs) { return typeid(lhs) == typeid(rhs) && lhs.equals(rhs); }
bool                 operator!=(const Tag& lhs, const Tag& rhs) { return !(lhs == rhs); }
} // namespace stratos::nbt