Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
BlockState Class Reference

Represents the state of a single block (previously known as "block meta"). More...

#include <BlockState.h>

Public Member Functions

 BlockState ()
 Creates a new instance with an empty map. More...
 
 BlockState (const AString &aKey, const AString &aValue)
 Creates a new instance consisting of a single key-value pair. More...
 
 BlockState (std::initializer_list< std::pair< const AString, AString >> aKeysAndValues)
 Creates a new instance initialized with several (hard-coded) key-value pairs. More...
 
 BlockState (const std::map< AString, AString > &aKeysAndValues)
 Creates a new instance initialized with several (dynamic) key-value pairs. More...
 
 BlockState (std::map< AString, AString > &&aKeysAndValues)
 Creates a new instance initialized with several (dynamic) key-value pairs. More...
 
 BlockState (const BlockState &aCopyFrom, std::initializer_list< std::pair< const AString, AString >> aAdditionalKeysAndValues)
 Creates a copy of the specified BlockState with the (hard-coded) additional keys and values added to it. More...
 
 BlockState (const BlockState &aCopyFrom, const std::map< AString, AString > &aAdditionalKeysAndValues)
 Creates a copy of the specified BlockState with the (dynamic) additional keys and values added to it. More...
 
bool operator!= (const BlockState &aOther) const
 Fast inequality check. More...
 
bool operator< (const BlockState &aOther) const
 Less-than comparison. More...
 
bool operator== (const BlockState &aOther) const
 Fast equality check. More...
 
const AStringvalue (const AString &aKey) const
 Returns the value at the specified key. More...
 

Protected Member Functions

UInt32 initializeChecksum ()
 Normalizes mState and calculates the checksum from it. More...
 
UInt32 partialChecksum (const AString &aString)
 Calculates the partial checksum of a single string. More...
 
void removeEmptyKeys ()
 Removes all the keys from mState that have an empty value. More...
 

Protected Attributes

UInt32 mChecksum
 The checksum used for the fast equality check. More...
 
std::map< AString, AStringmState
 The state, represented as a string->string map. More...
 

Detailed Description

Represents the state of a single block (previously known as "block meta").

The state consists of a map of string -> string, plus a mechanism for fast equality checks between two BlockState instances. Once a BlockState instance is created, it is then immutable - there's no way of changing it, only by creating a (modified) copy. A BlockState instance can be created from hard-coded data or from dynamic data: BlockState bs({{"key1", "value1"}, {key2", "value2"}}); // Hard-coded

Definition at line 19 of file BlockState.h.

Constructor & Destructor Documentation

BlockState::BlockState ( )

Creates a new instance with an empty map.

Definition at line 8 of file BlockState.cpp.

BlockState::BlockState ( const AString aKey,
const AString aValue 
)

Creates a new instance consisting of a single key-value pair.

If the value is empty, it is not stored wihin the map.

Definition at line 18 of file BlockState.cpp.

BlockState::BlockState ( std::initializer_list< std::pair< const AString, AString >>  aKeysAndValues)

Creates a new instance initialized with several (hard-coded) key-value pairs.

Any key with an empty value is not stored within the map.

Definition at line 28 of file BlockState.cpp.

BlockState::BlockState ( const std::map< AString, AString > &  aKeysAndValues)

Creates a new instance initialized with several (dynamic) key-value pairs.

Makes a copy of aKeysAndValues for this object. Any key with an empty value is not stored within the map.

Definition at line 38 of file BlockState.cpp.

BlockState::BlockState ( std::map< AString, AString > &&  aKeysAndValues)

Creates a new instance initialized with several (dynamic) key-value pairs.

Any key with an empty value is not stored within the map.

Definition at line 48 of file BlockState.cpp.

BlockState::BlockState ( const BlockState aCopyFrom,
std::initializer_list< std::pair< const AString, AString >>  aAdditionalKeysAndValues 
)

Creates a copy of the specified BlockState with the (hard-coded) additional keys and values added to it.

Any key in aAdditionalKeysAndValues that is already present in aCopyFrom is overwritten with the aAdditionalKeysAndValues' one. Any key with an empty value is not stored in the map. (it's possible to erase a key from aCopyFrom by setting it to empty string in aAdditionalKeysAndValues).

Definition at line 58 of file BlockState.cpp.

BlockState::BlockState ( const BlockState aCopyFrom,
const std::map< AString, AString > &  aAdditionalKeysAndValues 
)

Creates a copy of the specified BlockState with the (dynamic) additional keys and values added to it.

Any key in aAdditionalKeysAndValues that is already present in aCopyFrom is overwritten with the aAdditionalKeysAndValues' one. Any key with an empty value is not stored in the map. (it's possible to erase a key from aCopyFrom by setting it to empty string in aAdditionalKeysAndValues).

Definition at line 72 of file BlockState.cpp.

Member Function Documentation

UInt32 BlockState::initializeChecksum ( )
protected

Normalizes mState and calculates the checksum from it.

Removes all the empty values from mState. Used only from constructors.

Definition at line 163 of file BlockState.cpp.

bool BlockState::operator!= ( const BlockState aOther) const
inline

Fast inequality check.

Definition at line 62 of file BlockState.h.

bool BlockState::operator< ( const BlockState aOther) const

Less-than comparison.

Definition at line 86 of file BlockState.cpp.

bool BlockState::operator== ( const BlockState aOther) const

Fast equality check.

Definition at line 132 of file BlockState.cpp.

UInt32 BlockState::partialChecksum ( const AString aString)
protected

Calculates the partial checksum of a single string.

Used from within initializeChecksum().

Definition at line 201 of file BlockState.cpp.

void BlockState::removeEmptyKeys ( )
protected

Removes all the keys from mState that have an empty value.

Definition at line 182 of file BlockState.cpp.

const AString & BlockState::value ( const AString aKey) const

Returns the value at the specified key.

If the key is not present, returns an empty string.

Definition at line 148 of file BlockState.cpp.

Member Data Documentation

UInt32 BlockState::mChecksum
protected

The checksum used for the fast equality check.

This is calculated upon creation.

Definition at line 79 of file BlockState.h.

std::map<AString, AString> BlockState::mState
protected

The state, represented as a string->string map.

Definition at line 75 of file BlockState.h.


The documentation for this class was generated from the following files: