Skip to content

Class endstone::nbt::Tag

ClassList > endstone > nbt > Tag

A single NBT tag that may hold any of the supported payload types. More...

  • #include <endstone/nbt/tag.h>

Public Types

Type Name
typedef std::variant< std::monostate, ByteTag, ShortTag, IntTag, LongTag, FloatTag, DoubleTag, StringTag, ByteArrayTag, IntArrayTag, ListTag, CompoundTag > Storage

Public Functions

Type Name
Tag () noexcept
Tag (const ByteTag & v)
Tag (ByteTag && v)
Tag (const ShortTag & v)
Tag (ShortTag && v)
Tag (const IntTag & v)
Tag (IntTag && v)
Tag (const LongTag & v)
Tag (LongTag && v)
Tag (const FloatTag & v)
Tag (FloatTag && v)
Tag (const DoubleTag & v)
Tag (DoubleTag && v)
Tag (const StringTag & v)
Tag (StringTag && v)
Tag (const ByteArrayTag & v)
Tag (ByteArrayTag && v)
Tag (const IntArrayTag & v)
Tag (IntArrayTag && v)
Tag (const ListTag & v)
Tag (ListTag && v)
Tag (const CompoundTag & v)
Tag (CompoundTag && v)
Tag & at (const std::string & key)
Access the child tag with the given key, treating this tag as a compound, with bounds checking.
const Tag & at (const std::string & key) const
Access the child tag with the given key, treating this tag as a compound, with bounds checking.
Tag & at (std::size_t index)
Access the child tag at the given index, treating this tag as a list, with bounds checking.
const Tag & at (std::size_t index) const
Access the child tag at the given index, treating this tag as a list, with bounds checking.
bool contains (const std::string & key) noexcept const
Check whether this tag is a compound containing the given key.
std::string dump (std::endian byte_order=std::endian::little, bool network=false) const
Serialize this tag to binary NBT with an empty root name.
std::string dump (const std::string & name, std::endian byte_order=std::endian::little, bool network=false) const
Serialize this tag to binary NBT with a root name.
std::pair< CompoundTag::iterator, bool > emplace (Args &&... args)
Construct an entry in place in this compound.
ListTag & emplace_back (Args &&... args)
Construct a child tag in place at the end of this list.
bool empty () noexcept const
Check whether this tag holds no elements.
T & get ()
Get the payload as the given tag type.
const T & get () const
Get the payload as the given tag type.
T * get_if () noexcept
Get a pointer to the payload if this tag holds the given type.
const T * get_if () noexcept const
Get a pointer to the payload if this tag holds the given type.
Tag & operator[] (const std::string & key)
Access the child tag with the given key, treating this tag as a compound.
Tag & operator[] (std::size_t index)
Access the child tag at the given index, treating this tag as a list.
std::size_t size () noexcept const
Get the number of elements held by this tag.
Type type () noexcept const
Get the type of payload currently held by this tag.
decltype(auto) visit (Fn && visitor) const
Apply a visitor to the payload currently held by this tag.
decltype(auto) visit (Fn && visitor)
Apply a visitor to the payload currently held by this tag.

Detailed Description

A Tag behaves as a tagged union over every NBT type (byte, short, int, long, float, double, string, byte array, int array, list and compound). A default-constructed Tag is empty (the End type). Indexing or inserting into an empty Tag promotes it to a ListTag or CompoundTag as appropriate.

Public Types Documentation

typedef Storage

using endstone::nbt::Tag::Storage =  std::variant<std::monostate, ByteTag, ShortTag, IntTag, LongTag, FloatTag, DoubleTag, StringTag, ByteArrayTag, IntArrayTag, ListTag, CompoundTag>;

Public Functions Documentation

function Tag [1/23]

inline endstone::nbt::Tag::Tag () noexcept

function Tag [2/23]

inline endstone::nbt::Tag::Tag (
    const  ByteTag & v
) 

function Tag [3/23]

inline endstone::nbt::Tag::Tag (
    ByteTag && v
) 

function Tag [4/23]

inline endstone::nbt::Tag::Tag (
    const  ShortTag & v
) 

function Tag [5/23]

inline endstone::nbt::Tag::Tag (
    ShortTag && v
) 

function Tag [6/23]

inline endstone::nbt::Tag::Tag (
    const  IntTag & v
) 

function Tag [7/23]

inline endstone::nbt::Tag::Tag (
    IntTag && v
) 

function Tag [8/23]

inline endstone::nbt::Tag::Tag (
    const  LongTag & v
) 

function Tag [9/23]

inline endstone::nbt::Tag::Tag (
    LongTag && v
) 

function Tag [10/23]

inline endstone::nbt::Tag::Tag (
    const  FloatTag & v
) 

function Tag [11/23]

inline endstone::nbt::Tag::Tag (
    FloatTag && v
) 

function Tag [12/23]

inline endstone::nbt::Tag::Tag (
    const  DoubleTag & v
) 

function Tag [13/23]

inline endstone::nbt::Tag::Tag (
    DoubleTag && v
) 

function Tag [14/23]

inline endstone::nbt::Tag::Tag (
    const  StringTag & v
) 

function Tag [15/23]

inline endstone::nbt::Tag::Tag (
    StringTag && v
) 

function Tag [16/23]

inline endstone::nbt::Tag::Tag (
    const  ByteArrayTag & v
) 

function Tag [17/23]

inline endstone::nbt::Tag::Tag (
    ByteArrayTag && v
) 

function Tag [18/23]

inline endstone::nbt::Tag::Tag (
    const  IntArrayTag & v
) 

function Tag [19/23]

inline endstone::nbt::Tag::Tag (
    IntArrayTag && v
) 

function Tag [20/23]

inline endstone::nbt::Tag::Tag (
    const  ListTag & v
) 

function Tag [21/23]

inline endstone::nbt::Tag::Tag (
    ListTag && v
) 

function Tag [22/23]

inline endstone::nbt::Tag::Tag (
    const  CompoundTag & v
) 

function Tag [23/23]

inline endstone::nbt::Tag::Tag (
    CompoundTag && v
) 

function at [¼]

Access the child tag with the given key, treating this tag as a compound, with bounds checking.

inline Tag & endstone::nbt::Tag::at (
    const std::string & key
) 

An empty tag is promoted to a CompoundTag.

Parameters:

  • key the key to access

Returns:

a reference to the child tag

Exception:

  • std::runtime_error if this tag is not a compound
  • std::out_of_range if the key is not present

function at [2/4]

Access the child tag with the given key, treating this tag as a compound, with bounds checking.

inline const  Tag & endstone::nbt::Tag::at (
    const std::string & key
) const

Parameters:

  • key the key to access

Returns:

a const reference to the child tag

Exception:

  • std::runtime_error if this tag is not a compound
  • std::out_of_range if the key is not present

function at [¾]

Access the child tag at the given index, treating this tag as a list, with bounds checking.

inline Tag & endstone::nbt::Tag::at (
    std::size_t index
) 

An empty tag is promoted to a ListTag.

Parameters:

  • index the index to access

Returns:

a reference to the child tag

Exception:

  • std::runtime_error if this tag is not a list
  • std::out_of_range if the index is out of range

function at [4/4]

Access the child tag at the given index, treating this tag as a list, with bounds checking.

inline const  Tag & endstone::nbt::Tag::at (
    std::size_t index
) const

Parameters:

  • index the index to access

Returns:

a const reference to the child tag

Exception:

  • std::runtime_error if this tag is not a list
  • std::out_of_range if the index is out of range

function contains

Check whether this tag is a compound containing the given key.

inline bool endstone::nbt::Tag::contains (
    const std::string & key
) noexcept const

Parameters:

  • key the key to look up

Returns:

true if this tag is a compound and contains the key, false otherwise


function dump [½]

Serialize this tag to binary NBT with an empty root name.

inline std::string endstone::nbt::Tag::dump (
    std::endian byte_order=std::endian::little,
    bool network=false
) const

Parameters:

  • byte_order std::endian::little (Bedrock) or std::endian::big (Java).
  • network If true, use Bedrock network varint encoding.

Returns:

Binary NBT data.

See also: endstone::nbt::dump(), endstone::nbt::load()


function dump [2/2]

Serialize this tag to binary NBT with a root name.

inline std::string endstone::nbt::Tag::dump (
    const std::string & name,
    std::endian byte_order=std::endian::little,
    bool network=false
) const

Parameters:

  • name The root tag name.
  • byte_order std::endian::little (Bedrock) or std::endian::big (Java).
  • network If true, use Bedrock network varint encoding.

Returns:

Binary NBT data.


function emplace

Construct an entry in place in this compound.

template<class... Args>
inline std::pair< CompoundTag::iterator, bool > endstone::nbt::Tag::emplace (
    Args &&... args
) 

An empty tag is promoted to a CompoundTag.

Parameters:

  • args the arguments forwarded to construct the key/value entry

Returns:

a pair of an iterator to the entry and a bool that is true if a new entry was inserted

Exception:

  • std::runtime_error if this tag is not a compound

function emplace_back

Construct a child tag in place at the end of this list.

template<class... Args>
inline ListTag & endstone::nbt::Tag::emplace_back (
    Args &&... args
) 

An empty tag is promoted to a ListTag.

Parameters:

  • args the arguments forwarded to the element constructor

Returns:

a reference to this tag's underlying list

Exception:

  • std::runtime_error if this tag is not a list

function empty

Check whether this tag holds no elements.

inline bool endstone::nbt::Tag::empty () noexcept const

Returns:

true if size() is 0


function get [½]

Get the payload as the given tag type.

template<typename  T>
inline T & endstone::nbt::Tag::get () 

Template parameters:

  • T the tag type to retrieve

Returns:

a reference to the held value

Exception:

  • std::runtime_error if this tag does not currently hold a T

function get [2/2]

Get the payload as the given tag type.

template<typename  T>
inline const  T & endstone::nbt::Tag::get () const

Template parameters:

  • T the tag type to retrieve

Returns:

a const reference to the held value

Exception:

  • std::runtime_error if this tag does not currently hold a T

function get_if [½]

Get a pointer to the payload if this tag holds the given type.

template<typename  T>
inline T * endstone::nbt::Tag::get_if () noexcept

Template parameters:

  • T the tag type to retrieve

Returns:

a pointer to the held value, or nullptr if this tag does not hold a T


function get_if [2/2]

Get a pointer to the payload if this tag holds the given type.

template<typename  T>
inline const  T * endstone::nbt::Tag::get_if () noexcept const

Template parameters:

  • T the tag type to retrieve

Returns:

a const pointer to the held value, or nullptr if this tag does not hold a T


function operator[]

Access the child tag with the given key, treating this tag as a compound.

inline Tag & endstone::nbt::Tag::operator[] (
    const std::string & key
) 

An empty tag is promoted to a CompoundTag, and a missing key is inserted with an empty value.

Parameters:

  • key the key to access

Returns:

a reference to the child tag

Exception:

  • std::runtime_error if this tag is not a compound

function operator[]

Access the child tag at the given index, treating this tag as a list.

inline Tag & endstone::nbt::Tag::operator[] (
    std::size_t index
) 

An empty tag is promoted to a ListTag.

Parameters:

  • index the index to access

Returns:

a reference to the child tag

Exception:

  • std::runtime_error if this tag is not a list

function size

Get the number of elements held by this tag.

inline std::size_t endstone::nbt::Tag::size () noexcept const

For a list, compound or array this is the element count; for a string it is the character count; scalar tags report 1 and an empty tag reports 0.

Returns:

the element count


function type

Get the type of payload currently held by this tag.

inline Type endstone::nbt::Tag::type () noexcept const

Returns:

the tag type, or Type::End if the tag is empty


function visit [½]

Apply a visitor to the payload currently held by this tag.

template<typename  Fn>
inline decltype ( auto ) endstone::nbt::Tag::visit (
    Fn && visitor
) const

Template parameters:

  • Fn the visitor type, callable with any of the tag payload types (including std::monostate)

Parameters:

  • visitor the visitor to invoke

Returns:

whatever the visitor returns


function visit [2/2]

Apply a visitor to the payload currently held by this tag.

template<typename  Fn>
inline decltype ( auto ) endstone::nbt::Tag::visit (
    Fn && visitor
) 

Template parameters:

  • Fn the visitor type, callable with any of the tag payload types (including std::monostate)

Parameters:

  • visitor the visitor to invoke

Returns:

whatever the visitor returns


Friends Documentation

friend operator!=

inline bool endstone::nbt::Tag::operator!= (
    const  Tag & a,
    const  Tag & b
) noexcept

friend operator==

inline bool endstone::nbt::Tag::operator== (
    const  Tag & a,
    const  Tag & b
) noexcept


The documentation for this class was generated from the following file include/endstone/nbt/tag.h