Skip to content

Runtime Modifiable Classes

Doug Binks edited this page Sep 29, 2013 · 2 revisions

RCC++ uses virtual functions so that behaviour can be swapped at runtime by changing the object.

Not all code needs to use virtual functions however, as include file tracking and runtime source dependencies can be used to extend the type of code which can be modified at runtime, however these increase turn-around times and need more care in using.

The Console Example project has a simple runtime modifiable class defined as follows.

IUpdateable.h

#pragma once

#include "../../RuntimeObjectSystem/IObject.h"

struct IUpdateable : public IObject
{
    virtual void Update( float deltaTime ) = 0;
};

RuntimeObject01.cpp:

#include "../../RuntimeObjectSystem/ObjectInterfacePerModule.h"

#include "../../RuntimeObjectSystem/IObject.h"
#include "IUpdateable.h"
#include "InterfaceIds.h"
#include <iostream>


class RuntimeObject01 : public TInterface<IID_IUPDATEABLE,IUpdateable>
{
public:
    virtual void Update( float deltaTime )
    {
	    std::cout << "Runtime Object 01 update called!\n";
    }
};

REGISTERCLASS(RuntimeObject01);

The IUpdateable class defines an interface for the Update function. TInterface<IID_IUPDATEABLE,IUpdateable> is a template class which takes an enum and a class to derive from, and implements the GetInterace() method for getting hold of a pointer to a particular interface (fast dynamic casting via an enum). The REGISTERCLASS macro registers the class with a constructor so that the runtime factory system can swap objects at runtime.

Singletons

RCC++ can be used to ensure certain classes never have more than one instance created, and this can be automatically instantiated using the macro:

REGISTERSINGLETON( CLASSNAME, bIsAutoConstructSingleton );

which is defined in the header:

#include "ObjectInterfacePerModule.h"

This is used as per the REGISTERCLASS macro. The second boolean parameter can be set to true to have the class automatically constructed and initialized by the RuntimeObjectSystem. This should only be used if initialization order is not important.

Calling Construct for a singleton will return only one instance. If the singleton is not registered as auto-construct it should be initialized on first creation.

The demo example SimpleTest has two singleton examples - the MainMenu and MainObject classes.