#include <CConfigurableObject.h>
class CConfigurableObject {CConfigurableObject();
CConfigurableObject(const CConfigurableObject& rhs);
virtual ~CConfigurableObject();
CConfigurableObject& operator=(const CConfigurableObject& rsh);
const int operator==(const CConfigurableObject& rhs);
const int operator!=(const CConfigurableObject& rhs);
void Attach(CItemConfiguration* pConfiguration, bool dynamic = true);
void configure(std::string name, std::string value);
const std::string getName();
std::string cget(std::string name);
CItemConfiguration::ConfigurationArray cget();
virtual =0 void onAttach();
}
CConfigurableObject
is an abstract base class
for objects
that contain configurations via the
CItemConfiguration
class. The class embeds an CItemConfiguration
and provides a mechanism for initializing the configuration as it is
attached.
Due to the rules regarding virtual member use in constructors,
construction and initialization of a CConfigurableObject
is usually a two step process. The object is constructed, and then
either an empty or initialized configuration is attached to the
object via call to Attach
.
When Attach
is called it, in turn calls
the virtual method (pure/abstract in this base class), to
allow the actual concrete class to initialize the configuration
by defining an appropriate set of parameters and their constraints.
Once initialized, a partial facade is supplied that allows external clients to configure and dump the configuration of an object.
CConfigurableObject();
Construts a configurable object. Note that this does not
create a configuration unless explicitly done by the constructor.
Normally due to restrictions in when virtual functions are actually
virtual (they are not in constructors), The client that constructs
this will also construct and attach a
CItemConfiguration
via
Attach
soon after constructing the object.
CConfigurableObject(const CConfigurableObject& rhs);
Copy construction. This results in a snapshot of the configuration being dynamically made, and marked for deletion on destruction of the object. No attempt is made to synchronize the copy with the original.
virtual ~CConfigurableObject();
Destroys the configurable object. If the configuration attached
was marked as dynamcially allocated it is deleted in order
to prevent memory leaks. Dynamic marking is done either
by copy construction or by specifying at
Attach
time that the object is responsible
for deleting ItemConfiguration
object being attached.
CConfigurableObject& operator=(const CConfigurableObject& rsh);
Object assignment. If the target object's configuration exists and was dynamically attached, it is deleted. Regardless, the source's configuration cloned and assigned to the current configuration with dynamic marking.
const int operator==(const CConfigurableObject& rhs);
Returns nonzero if rhs
's configuration is
identical to that of this
.
const int operator!=(const CConfigurableObject& rhs);
Returns nonzero if operator==
returns 0.
void Attach(CItemConfiguration* pConfiguration, bool dynamic = true);
The client should call this to attach a configuration,
pConfiguration
, (either
pre-configured or empty) to the object.
dynamic
should be true
if pConfiguration
should be
deleted when the object is destroyed.
The virtual function onAttach
will be invoked
to allow the configuration to be set up for the object.
void configure(std::string name, std::string value);
Passes the name
and value
parameters on to the configuration object's
CItemConfiguration
object. If no configuration
has been attached to the object, a string exception is
thrown describing this fact.
const std::string getName();
Returns the configuration's name. This will throw a string exception if no configuration has been attached to the object.
std::string cget(std::string name);
Returns the value of a configuration option name
.
The assumption is that clients only want to present configuration
option values to humans, or save them and therefore ther is only
a facade in front of the string value function.
In addition to the string exceptions that can be thrown by the configuration object, an exception will be thrown if the object has no attached configuration.
CItemConfiguration::ConfigurationArray cget();
Returns the entire configuration. Throws a string exception if no configuration object has been attached.
virtual =0 void onAttach();
Concrete base classes should supply and implement this. Normally
onAttache
is used to initialize the
configuration which is stored in the protected data member
m_pConfiguration
, a pointer to
CItemConfiguration
.