BSON C++ API  2.5.1
uma::bson
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
uma::bson::ODMObject< Model > Class Template Reference

Abstract class that presents a more friendly ODM interface than uma::bson::Object. More...

#include <ODMObject.h>

+ Inheritance diagram for uma::bson::ODMObject< Model >:
+ Collaboration diagram for uma::bson::ODMObject< Model >:

Classes

class  MetaField
 Abstract base class that encapsulates a field in a model object. Primarily used to get around requirement that implementation needs exact type of data encapsulated in the field as template type. More...
 
class  MetaFieldImpl
 Encapsulates a field in a model object. Fields are represented as a triplet of the field in the class, and its accessor and mutator methods. More...
 

Public Member Functions

virtual ~ODMObject ()
 Virtual destructor for sub-classes.
 
const FieldNames getFieldNames () const
 Return the names of the fields registered. Note that the fields are stored in a std::map, hence the order of fields returned may not correspond to the order in which they were registered.
 
ValuegetValue (const std::string &name)
 Return the value for the element with the specified name.
 
void setValue (const std::string &name, const Value &value)
 Set the value for the element with the specified name.
 
- Public Member Functions inherited from uma::bson::Object
virtual ~Object ()
 Virtual destructor for sub-classes.
 
virtual const ValuegetValue (const std::string &name) const
 Constant version of getValue method.
 
virtual ObjectgetObjectForArray (const std::string &)
 Return an empty instance of an object implementation that represents the type of data stored in an array type field.
 
virtual void toBson (std::ostream &os) const
 Serialise the data in this object in BSON format to the specified output stream.
 
virtual void populate (std::istream &is)
 Populate the fields in this object from the BSON data stream. Complementary method to toBson.
 
int32_t getSize () const
 Return the size of the data as per BSON specifications.
 
Value::Type getType () const
 Returns the type for this instance as listed in the BSON specifications.
 
- Public Member Functions inherited from uma::bson::Value
virtual ~Value ()
 Virtual DTOR for sub-classes.
 
const std::string & getTypeName () const
 Returns a standard textual value for this type.
 

Protected Types

typedef Poco::SharedPtr
< MetaField
MetaFieldPtr
 Shared pointer for a meta field instance.
 

Protected Member Functions

void registerField (const MetaFieldPtr mo)
 Add (register) a field with the map of fields in the model object. Best practise is to check whether class has been registered (see registered, and if not, register all the fields for the class.
 
MetaFieldPtr getMetaField (const std::string &name)
 Return the meta field instance for the field with the specified name.
 

Static Protected Member Functions

static bool registered (Model *model)
 Check to see if the class for the model object has been registered. Registration is the process of defining the fields with their accessor and mutator methods.
 

Additional Inherited Members

- Public Types inherited from uma::bson::Object
typedef std::vector< std::string > FieldNames
 A container for the top-level fields in an object.
 
typedef FieldNames::const_iterator FieldsIterator
 Iterator for iterating over the top-level field names in an object.
 
- Static Public Member Functions inherited from uma::bson::Value
static const std::string & getTypeName (const Type type)
 Return a descriptive text about the specified type.
 

Detailed Description

template<typename Model>
class uma::bson::ODMObject< Model >

Abstract class that presents a more friendly ODM interface than uma::bson::Object.

This class uses a registration system for model objects to register their serialisable fields along with their accessor and mutator methods (see registerField. This removes the requirement to implement getValue using an ugly if-else statement block.

Warning
Note: Model classes will need to provide a non-const version of the accessor methods for the registration to work.
Template Parameters
ModelThe type of the sub-class (needed for the MetaField implementation).
Since
Version 2.5
Date
Created 2013/02/13 10:30
Author
Rakesh
Version
Id:
ODMObject.h 207 2013-02-28 16:03:52Z spt

Member Typedef Documentation

template<typename Model >
typedef Poco::SharedPtr<MetaField> uma::bson::ODMObject< Model >::MetaFieldPtr
protected

Shared pointer for a meta field instance.

Constructor & Destructor Documentation

template<typename Model >
virtual uma::bson::ODMObject< Model >::~ODMObject ( )
inlinevirtual

Virtual destructor for sub-classes.

Member Function Documentation

template<typename Model >
const FieldNames uma::bson::ODMObject< Model >::getFieldNames ( ) const
inlinevirtual

Return the names of the fields registered. Note that the fields are stored in a std::map, hence the order of fields returned may not correspond to the order in which they were registered.

Returns
const FieldNames The vector of field names.

Implements uma::bson::Object.

template<typename Model >
MetaFieldPtr uma::bson::ODMObject< Model >::getMetaField ( const std::string &  name)
inlineprotected

Return the meta field instance for the field with the specified name.

Parameters
nameThe name of the field whose meta instance is to be retrieved.
Returns
MetaFieldPtr The meta field representing the named field.
template<typename Model >
Value& uma::bson::ODMObject< Model >::getValue ( const std::string &  name)
inlinevirtual

Return the value for the element with the specified name.

This method is primarily intended to provide rudimentary ODM (object-document mapping) capabilities in the API. Model objects that extend this class and store fields as Value types may be serialised to BSON seamlessly. See unit test suite for a simple custom model object that is serialised to BSON.

Since
Version 2.2
Parameters
nameThe name of the BSON element
Returns
The value for the element.
Exceptions
Poco::NotFoundExceptionIf no element with specified name exists.

Implements uma::bson::Object.

template<typename Model >
static bool uma::bson::ODMObject< Model >::registered ( Model *  model)
inlinestaticprotected

Check to see if the class for the model object has been registered. Registration is the process of defining the fields with their accessor and mutator methods.

Parameters
modelThe model object to check to see if registered.
Returns
bool Returns true if already registered.
template<typename Model >
void uma::bson::ODMObject< Model >::registerField ( const MetaFieldPtr  mo)
inlineprotected

Add (register) a field with the map of fields in the model object. Best practise is to check whether class has been registered (see registered, and if not, register all the fields for the class.

Parameters
moThe shared pointer to the meta field instance.
template<typename Model >
void uma::bson::ODMObject< Model >::setValue ( const std::string &  name,
const Value value 
)
inlinevirtual

Set the value for the element with the specified name.

This method is also used to provide rudimentary ODM capabilities in the API. Model objects that extend this class and store fields as Value types may be deserialised from BSON through this method. See unit test suite for a simple custom model object that is de-serialised from BSON.

Note that from Version 2.3 onwards a default implementation is provided. The default implementation handles setting the value for all the simple types. In particular Value::Type::Object, Value::Type::Array and consequently Value::Type::CodeWScope are not handled and may throw an exception. Despite this clients may still not need to provide an implementation of this method even if they store other object and array fields. The BSON deserialiser implemented in this API retrieves a reference to the current object/array from the destination instance and populates those. Hence if the only code using this method is from the internal BSON deserialiser, there will be no need to provide additional implementation for setting object/array fields.

Since
Version 2.2
Parameters
name
value
Exceptions
Poco::InvalidArgumentExceptionWill be thrown if the specified value type does not match the existing value type. May also be throw since this method invokes getValue( const std::string& ) at the beginning to fetch the current instance that is to be modified.
Poco::NotImplementedExceptionIf the value specified is a complex type such as Value::Type::Object or Value::Type::Array.

Reimplemented from uma::bson::Object.


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