TypeTracker.hpp

Track class types abstractly to dynamically call correct function overloads.

TypeTracker is a templated class that must be declared with all of the types that can possibly be tracked. For example:

TypeTracker<int, std::string, double> tt;

…would create a TypeTracker that can manage the three types listed and convert back.

Todo:

Should use std::is_convertible<X,Y>::value to determine if casting on base type is allowed.

Functions should be able to have fixed type values mixed in.

Note

Status: BETA

struct TrackedInfo_Base
#include <TypeTracker.hpp>

The proxy base class of any type to be tracked.

Subclassed by TrackedInfo_Value< REAL_T, ID >

Public Functions

virtual size_t GetTypeID() const noexcept = 0
inline virtual ~TrackedInfo_Base()
virtual Ptr<TrackedInfo_Base> Clone() const = 0
struct TrackedVar
#include <TypeTracker.hpp>

The actual TrackedVar object that manages a Ptr to the value.

Public Functions

inline TrackedVar(Ptr<TrackedInfo_Base> _ptr)
inline TrackedVar(const TrackedVar &_in)

Copy constructor; use judiciously since it copies the contents!

inline TrackedVar(TrackedVar &&_in)

Move constructor takes control of the pointer.

inline ~TrackedVar()

Cleanup ptr on destruct.

inline TrackedVar &operator=(const TrackedVar &_in)

Move assignment hands over control of the pointer.

inline TrackedVar &operator=(TrackedVar &&_in)

Move assignment hands over control of the pointer.

inline size_t GetTypeID() const noexcept

Public Members

Ptr<TrackedInfo_Base> ptr
template<typename REAL_T, size_t ID>
struct TrackedInfo_Value : public TrackedInfo_Base
#include <TypeTracker.hpp>

TrackedInfo_Value store both the real type and an ID for it (to be identified from the base class for each conversion back.)

Public Types

using real_t = REAL_T
using this_t = TrackedInfo_Value<REAL_T, ID>

Public Functions

inline TrackedInfo_Value(const REAL_T &in)
inline TrackedInfo_Value(REAL_T &&in)
TrackedInfo_Value(const TrackedInfo_Value&) = default
TrackedInfo_Value(TrackedInfo_Value&&) = default
TrackedInfo_Value &operator=(const TrackedInfo_Value&) = default
TrackedInfo_Value &operator=(TrackedInfo_Value&&) = default
inline virtual size_t GetTypeID() const noexcept override
inline virtual Ptr<TrackedInfo_Base> Clone() const override

Build a copy of this TrackedInfo_Value; recipient is in charge of deletion.

Public Members

REAL_T value
template<typename ...TYPES>
class TypeTracker
#include <TypeTracker.hpp>

Dynamic functions that are indexed by parameter types; calls lookup the correct function to forward arguments into.

Public Types

using this_t = TypeTracker<TYPES...>
template<typename REAL_T>
using wrap_t = TrackedInfo_Value<REAL_T, get_type_index<REAL_T, TYPES...>()>
template<typename T>
using var_decoy = TrackedVar

var_decoy converts any variable into a TrackedVar (used to have correct number of vars)

Public Functions

TypeTracker() = default
TypeTracker(const TypeTracker&) = default
TypeTracker(TypeTracker&&) = default
TypeTracker &operator=(const TypeTracker&) = default
TypeTracker &operator=(TypeTracker&&) = default
inline ~TypeTracker()
template<typename ...Ts>
inline this_t &AddFunction(std::function<void(Ts...)> fun)

Add a new std::function that this TypeTracker should call if the appropriate types are passed in.

template<typename ...Ts>
inline this_t &AddFunction(void (*fun)(Ts...))

Add a new function pointer that this TypeTracker should call if the appropriate types are passed in.

template<typename LAMBDA_T>
inline this_t &AddFunction(const LAMBDA_T &fun)

Add a new lambda function that this TypeTracker should call if the appropriate types are passed in.

template<typename ...Ts>
inline void RunFunction(Ts&&... args)

Run the appropriate function based on the argument types received.

template<typename ...Ts>
inline void operator()(Ts&&... args)

Call TypeTracker as a function (refers call to RunFunction)

Public Static Functions

static inline constexpr size_t GetNumTypes()

How many types are we working with?

static inline constexpr size_t GetNumCombos(size_t vals = 2)

How many combinations of V types are there?

static inline constexpr size_t GetCumCombos(size_t vals = 2)

How many combinations are the of the given number of types OR FEWER?

template<typename T>
static inline constexpr size_t GetID()

Each type should have a unique ID.

template<typename T1, typename T2, typename ...Ts>
static inline constexpr size_t GetID()

Each set of types should have an ID unique within that number of types.

template<typename ...Ts>
static inline constexpr size_t GetComboID()

A ComboID should be unique across all size combinations.

static inline size_t GetTrackedID(const TrackedVar &tt)

A Tracked ID is simply the unique ID of the type being tracked.

template<typename ...Ts>
static inline size_t GetTrackedID(const TrackedVar &tt1, const TrackedVar &tt2, const Ts&... ARGS)

Or set of types being tracked…

template<typename ...Ts>
static inline constexpr size_t GetTrackedComboID(const Ts&... ARGS)

A tracked COMBO ID, is an ID for this combination of types, unique among all possible type combinations. Consistent with GetComboID with the same underlying types.

template<typename REAL_T>
static inline TrackedVar Convert(const REAL_T &val)

Convert an input value into a TrackedInfo_Value maintaining the value (universal version)

template<typename TEST_T>
static inline bool IsType(TrackedVar &tt)

Test if the tracked type is TEST_T.

template<typename REAL_T>
static inline REAL_T ToType(TrackedVar &tt)

Convert the tracked type back to REAL_T. Assert that this is type safe!

template<typename OUT_T>
static inline OUT_T Cast(TrackedVar &tt)

Cast the tracked type to OUT_T. Try to do so even if NOT original type!

Protected Attributes

std::unordered_map<size_t, Ptr<GenericFunction>> fun_map

fun_map is a hash table that maps a set of inputs to the appropriate function.