OS-IoT
Special Hanlding for _optional, _sequence and _base types

Introduction

Classes that contain parameters that are optional or sequences according ot the oneM2M XML protocol definition will generate special templated types with names ending "_optional" or "_sequence" respectively. For example, the onem2m::resource class contain a labels element which is of type "labels_optional".

These types provide a set of standard methods to manipulate the collection of data in the element which must be used. These methods allow the _optional or _sequence element to be translated in to it's underlying type. For example, if an object r is of type onem2m::resource then r.labels().get() will return an object of type onem2m::labels. Note that attempting to perform get() on an optional element that is not present will result in a memory violation. The present() method should be used to check if an optional element is present before accessing.

Classes that represent lists of information may be built on top of a type with a name ending "_base". For example onem2m::labels is built on onem2m::labels_base. In this case an interface of a standard C++ sequence (e.g., std::vector) will be provided.

_optional type interface

See section 2.8.2 of the Code Synthesis C++/Tree Mapping User Manual.

Types named "_optional" have the following interface:

template <typename X>
class optional
{
public:
optional ();
// Makes a deep copy.
//
explicit
optional (const X&);
// Assumes ownership.
//
explicit
optional (std::[auto|unique]_ptr<X>);
optional (const optional&);
public:
optional&
operator= (const X&);
optional&
operator= (const optional&);
// Pointer-like interface.
//
public:
const X*
operator-> () const;
X*
operator-> ();
const X&
operator* () const;
X&
operator* ();
typedef void (optional::*bool_convertible) ();
operator bool_convertible () const;
// Get/set interface.
//
public:
// Check if an element is present or not
bool
present () const;
// Get an element if it is present - an error otherwise
const X&
get () const;
// Get an element if it is present - an error otherwise
X&
get ();
// Makes a deep copy.
//
void
set (const X&);
// Assumes ownership.
//
void
set (std::[auto|unique]_ptr<X>);
// Detach and return the contained value.
//
std::[auto|unique]_ptr<X>
detach ();
// Delete an element
void
reset ();
};
template <typename X>
bool
operator== (const optional<X>&, const optional<X>&);
template <typename X>
bool
operator!= (const optional<X>&, const optional<X>&);
template <typename X>
bool
operator< (const optional<X>&, const optional<X>&);
template <typename X>
bool
operator> (const optional<X>&, const optional<X>&);
template <typename X>
bool
operator<= (const optional<X>&, const optional<X>&);
template <typename X>
bool
operator>= (const optional<X>&, const optional<X>&);

_sequence and _base type interface

See section 2.8.3 of the Code Synthesis C++/Tree Mapping User Manual.

Types named "_sequence" or derived from an "_base" class provide an interface of a standard C++ sequence (e.g., std::vector). Familiar functions like push_back() etc. may be used.

Example

The type onem2m::AE contains an optional element of type onem2m::labels. The onem2m::labels type is a sequence type derived from onem2m::labels_base.

The following code shows how the labels parameter in the AE may be populated

auto ae = AE();
auto lb = labels ();
lb.push_back("Type/updateSensor");
ae.labels(lb);