TT Client Library 1.0.0
C++ library for Tandem Tales clients.
Loading...
Searching...
No Matches
tt::MessageFactory Class Reference

A helper class for deserializing messages. More...

#include <tt/io/MessageFactory.h>

Public Types

using Creator = std::function<std::unique_ptr<tt::Message>(const nlohmann::json&)>
 Type alias to simplify code.

Public Member Functions

void registerType (const std::string &type, Creator creator)
 Register a type with the message factory.
std::unique_ptr< tt::Message > create (const std::string &type, const nlohmann::json &j) const
 Looks up the creator of the desired type to deserialize json.

Static Public Member Functions

static MessageFactory & instance ()
 MessageFactory singleton.

Detailed Description

A helper class for deserializing messages.

This class helps the client create an object of the correct Message type for incoming JSON.

Custom clients should not need to use this class or any of its member functions, as message receiving is already built into the Client class.

Member Function Documentation

◆ create()

std::unique_ptr< tt::Message > tt::MessageFactory::create ( const std::string & type,
const nlohmann::json & j ) const
inline

Looks up the creator of the desired type to deserialize json.

Uses the createFromJson functions generated for each type.

Parameters
typethe name of the type
jjson serialization of the message
Returns
a pointer to the message created

◆ registerType()

void tt::MessageFactory::registerType ( const std::string & type,
Creator creator )
inline

Register a type with the message factory.

This is called when the first tt::Client is created.

Parameters
typethe name of the type
creatora function to create an object of that type

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