IoTivity C++ SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OIC::Service::RCSResourceObject Class Reference

This class represents a resource and handles any requests from clients automatically with attributes. More...

Classes

class  Builder
 This is a builder to create resource with properties and attributes. More...
 
class  LockGuard
 The class provides a convenient RAII-style mechanism for the attributes of a RCSResourceObject. More...
 

Public Types

enum  AutoNotifyPolicy { AutoNotifyPolicy::NEVER, AutoNotifyPolicy::ALWAYS, AutoNotifyPolicy::UPDATED }
 Represents the policy of auto-notify function. More...
 
enum  SetRequestHandlerPolicy { SetRequestHandlerPolicy::NEVER, SetRequestHandlerPolicy::ACCEPTANCE }
 Represents the policy of set-request handler. More...
 
typedef std::shared_ptr
< RCSResourceObject
Ptr
 
typedef std::shared_ptr< const
RCSResourceObject
ConstPtr
 
typedef std::function
< RCSGetResponse(const
RCSRequest &request,
RCSResourceAttributes
&attributes) > 
GetRequestHandler
 Callback definition for a handler to be invoked when a get request is received. More...
 
typedef std::function
< RCSSetResponse(const
RCSRequest &request,
RCSResourceAttributes
&attributes) > 
SetRequestHandler
 Callback definition for a handler to be invoked when a set request is received. More...
 
typedef std::function< void(const
RCSResourceAttributes::Value
&oldValue, const
RCSResourceAttributes::Value
&newValue) > 
AttributeUpdatedListener
 Callback definition to be invoked when an attribute is updated. More...
 

Public Member Functions

 RCSResourceObject (RCSResourceObject &&)=delete
 
 RCSResourceObject (const RCSResourceObject &)=delete
 
RCSResourceObjectoperator= (RCSResourceObject &&)=delete
 
RCSResourceObjectoperator= (const RCSResourceObject &)=delete
 
virtual ~RCSResourceObject ()
 
void setAttribute (const std::string &key, const RCSResourceAttributes::Value &value)
 Sets a particular attribute value. More...
 
void setAttribute (const std::string &key, RCSResourceAttributes::Value &&value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void setAttribute (std::string &&key, const RCSResourceAttributes::Value &value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void setAttribute (std::string &&key, RCSResourceAttributes::Value &&value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
RCSResourceAttributes::Value getAttributeValue (const std::string &key) const
 Returns an attribute value corresponding to a key. More...
 
template<typename T >
getAttribute (const std::string &key) const
 Returns the attribute value as T. More...
 
bool removeAttribute (const std::string &key)
 Removes a particular attribute of the resource. More...
 
bool containsAttribute (const std::string &key) const
 Checks whether a particular attribute exists or not. More...
 
RCSResourceAttributesgetAttributes ()
 Returns reference to the attributes of the RCSResourceObject. More...
 
const RCSResourceAttributesgetAttributes () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
virtual bool isObservable () const
 Checks whether the resource is observable or not. More...
 
virtual bool isDiscoverable () const
 Checks whether the resource is discoverable or not. More...
 
virtual void setGetRequestHandler (GetRequestHandler handler)
 Sets the get request handler. More...
 
virtual void setSetRequestHandler (SetRequestHandler handler)
 Sets the set request handler. More...
 
virtual void addAttributeUpdatedListener (const std::string &key, AttributeUpdatedListener listener)
 Adds a listener for a particular attribute updated. More...
 
virtual void addAttributeUpdatedListener (std::string &&key, AttributeUpdatedListener listener)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
virtual bool removeAttributeUpdatedListener (const std::string &key)
 Removes a listener for a particular attribute updated. More...
 
virtual void notify () const
 Notifies all observers of the current attributes. More...
 
void setAutoNotifyPolicy (AutoNotifyPolicy policy)
 Sets auto notify policy. More...
 
AutoNotifyPolicy getAutoNotifyPolicy () const
 Returns the current policy. More...
 
void setSetRequestHandlerPolicy (SetRequestHandlerPolicy policy)
 Sets the policy for handling a set request. More...
 
SetRequestHandlerPolicy getSetRequestHandlerPolicy () const
 Returns the current policy. More...
 
void bindResource (const RCSResourceObject::Ptr &resource)
 Bind a resource to this resource. More...
 
void unbindResource (const RCSResourceObject::Ptr &resource)
 Unbind a resource from this resource. More...
 
std::vector
< RCSResourceObject::Ptr
getBoundResources () const
 Returns all bound resources to this resource. More...
 
std::string getUri () const
 Returns the uri of the resource. More...
 
std::string getDefaultInterface () const
 Returns the default interface of the resource. More...
 
std::vector< std::string > getInterfaces () const
 Returns all interfaces added for the resource. More...
 
std::vector< std::string > getTypes () const
 Returns all types added for the resource. More...
 
OCResourceHandle getResourceHandle () const
 Returns the resource handle of the resource. More...
 

Friends

class RCSSeparateResponse
 

Detailed Description

This class represents a resource and handles any requests from clients automatically with attributes.

It also provides an auto notification mechanism that notifies to the observers.

Requests are handled automatically by defaultAction of RCSGetResponse and RCSSetResponse. You can override them and send your own response.

For simple resources, they are simply required to notify whenever attributes are changed by a set request. In this case, add an AttributeUpdatedListener with a key interested in instead of overriding SetRequestHandler.

Member Typedef Documentation

Callback definition to be invoked when an attribute is updated.

Parameters
oldValuethe value before being changed
newValuechanged value

Callback definition for a handler to be invoked when a get request is received.

The handler will be called first when a get request is received, before the RCSResourceObject handles.

Parameters
requestthe request information
attributesattributes of the request
Returns
response to be sent and that indicates how the request to be handled by the RCSResourceObject.
See Also
setGetRequestHandler

Callback definition for a handler to be invoked when a set request is received.

The handler will be called first when a get request is received, before the RCSResourceObject handles. If the attributes are modified in the callback, the modified attributes will be set in the RCSResourceObject if the request is not ignored.

Parameters
requestthe request information
attributesattributes of the request
Returns
response to be sent and that indicates how the request to be handled by the RCSResourceObject.
See Also
setGetRequestHandler

Member Enumeration Documentation

Represents the policy of auto-notify function.

In accord with this policy, observers are notified of attributes when the attributes are set.

Note
Attributes are set according to the execution of some functions which modify attributes or receipt of set requests.
See Also
RCSResourceObject::setAttribute
RCSResourceObject::removeAttribute
RCSResourceObject::getAttributes
RCSResourceObject::LockGuard
Enumerator
NEVER 

Never.

ALWAYS 

Always.

UPDATED 

Only when attributes are changed.

Represents the policy of set-request handler.

In accord with this, the RCSResourceObject decides whether a set-request is acceptable or not.

Enumerator
NEVER 

Requests will be ignored if attributes of the request contain a new key or a value that has different type from the current value of the key.

ACCEPTANCE 

The attributes of the request will be applied unconditionally even if there are new name or type conflicts.

Constructor & Destructor Documentation

OIC::Service::RCSResourceObject::RCSResourceObject ( RCSResourceObject &&  )
delete
OIC::Service::RCSResourceObject::RCSResourceObject ( const RCSResourceObject )
delete
virtual OIC::Service::RCSResourceObject::~RCSResourceObject ( )
virtual

Member Function Documentation

virtual void OIC::Service::RCSResourceObject::addAttributeUpdatedListener ( const std::string &  key,
AttributeUpdatedListener  listener 
)
virtual

Adds a listener for a particular attribute updated.

Parameters
keythe interested attribute's key
listenerlistener to be invoked
virtual void OIC::Service::RCSResourceObject::addAttributeUpdatedListener ( std::string &&  key,
AttributeUpdatedListener  listener 
)
virtual

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void OIC::Service::RCSResourceObject::bindResource ( const RCSResourceObject::Ptr resource)

Bind a resource to this resource.

Binding another resource makes this resource work as a collection resource, by default.

Parameters
resourcea resource to be bound to this resource.
Exceptions
RCSInvalidParameterExceptionIf resource is nullptr or itself.
RCSPlatformExceptionIf the operation failed.
See Also
unbindResource
bool OIC::Service::RCSResourceObject::containsAttribute ( const std::string &  key) const

Checks whether a particular attribute exists or not.

Parameters
keykey of the attribute
Returns
True if the key exists, otherwise false.
Note
Thread-safety is guaranteed for the attributes.
template<typename T >
T OIC::Service::RCSResourceObject::getAttribute ( const std::string &  key) const
inline

Returns the attribute value as T.

Parameters
keykey of the attribute
Exceptions
RCSBadGetExceptionIf type of the underlying value is not T.
RCSInvalidKeyExceptionIf key doesn't match the key of any value.
Note
Thread-safety is guaranteed for the attributes.
RCSResourceAttributes& OIC::Service::RCSResourceObject::getAttributes ( )

Returns reference to the attributes of the RCSResourceObject.

Precondition
The call must be guarded by LockGuard.
Returns
Reference to the attributes
Exceptions
NoLockExceptionIf the call is not guarded by LockGuard.
Note
Here is the standard idiom for LockGuard:
{
RCSResourceObject::LockGuard lock(rcsResourceObject);
auto &attributes = server->getAttributes();
...
}
const RCSResourceAttributes& OIC::Service::RCSResourceObject::getAttributes ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

RCSResourceAttributes::Value OIC::Service::RCSResourceObject::getAttributeValue ( const std::string &  key) const

Returns an attribute value corresponding to a key.

Parameters
keykey of the attribute
Exceptions
RCSInvalidKeyExceptionIf key is invalid.
Note
Thread-safety is guaranteed for the attributes.
AutoNotifyPolicy OIC::Service::RCSResourceObject::getAutoNotifyPolicy ( ) const

Returns the current policy.

std::vector< RCSResourceObject::Ptr > OIC::Service::RCSResourceObject::getBoundResources ( ) const

Returns all bound resources to this resource.

std::string OIC::Service::RCSResourceObject::getDefaultInterface ( ) const

Returns the default interface of the resource.

See Also
Builder::setDefaultInterface
std::vector< std::string > OIC::Service::RCSResourceObject::getInterfaces ( ) const

Returns all interfaces added for the resource.

See Also
Builder::addInterface
OCResourceHandle OIC::Service::RCSResourceObject::getResourceHandle ( ) const

Returns the resource handle of the resource.

SetRequestHandlerPolicy OIC::Service::RCSResourceObject::getSetRequestHandlerPolicy ( ) const

Returns the current policy.

std::vector< std::string > OIC::Service::RCSResourceObject::getTypes ( ) const

Returns all types added for the resource.

See Also
Builder::addType
std::string OIC::Service::RCSResourceObject::getUri ( ) const

Returns the uri of the resource.

virtual bool OIC::Service::RCSResourceObject::isDiscoverable ( ) const
virtual

Checks whether the resource is discoverable or not.

virtual bool OIC::Service::RCSResourceObject::isObservable ( ) const
virtual

Checks whether the resource is observable or not.

virtual void OIC::Service::RCSResourceObject::notify ( ) const
virtual

Notifies all observers of the current attributes.

Exceptions
RCSPlatformExceptionIf the operation failed.
RCSResourceObject& OIC::Service::RCSResourceObject::operator= ( RCSResourceObject &&  )
delete
RCSResourceObject& OIC::Service::RCSResourceObject::operator= ( const RCSResourceObject )
delete
bool OIC::Service::RCSResourceObject::removeAttribute ( const std::string &  key)

Removes a particular attribute of the resource.

Parameters
keykey of the attribute.
Returns
True if the key exists and matched attribute is removed, otherwise false.
Note
Thread-safety is guaranteed for the attributes.
virtual bool OIC::Service::RCSResourceObject::removeAttributeUpdatedListener ( const std::string &  key)
virtual

Removes a listener for a particular attribute updated.

Parameters
keythe key associated with the listener to be removed
Returns
True if the listener added with same key exists and is removed.
void OIC::Service::RCSResourceObject::setAttribute ( const std::string &  key,
const RCSResourceAttributes::Value value 
)

Sets a particular attribute value.

Parameters
keykey of attribute
valuevalue to be mapped against the key
Note
Thread-safety is guaranteed for the attributes.
void OIC::Service::RCSResourceObject::setAttribute ( const std::string &  key,
RCSResourceAttributes::Value &&  value 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void OIC::Service::RCSResourceObject::setAttribute ( std::string &&  key,
const RCSResourceAttributes::Value value 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void OIC::Service::RCSResourceObject::setAttribute ( std::string &&  key,
RCSResourceAttributes::Value &&  value 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void OIC::Service::RCSResourceObject::setAutoNotifyPolicy ( AutoNotifyPolicy  policy)

Sets auto notify policy.

Parameters
policypolicy to be set
virtual void OIC::Service::RCSResourceObject::setGetRequestHandler ( GetRequestHandler  handler)
virtual

Sets the get request handler.

To remove handler, pass empty handler or nullptr.

Default behavior is RCSGetResponse::defaultAction().

Parameters
handlera get request handler
See Also
RCSGetResponse
virtual void OIC::Service::RCSResourceObject::setSetRequestHandler ( SetRequestHandler  handler)
virtual

Sets the set request handler.

To remove handler, pass empty handler or nullptr.

Default behavior is RCSSetResponse::defaultAction().

Parameters
handlera set request handler
See Also
RCSSetResponse
void OIC::Service::RCSResourceObject::setSetRequestHandlerPolicy ( SetRequestHandlerPolicy  policy)

Sets the policy for handling a set request.

Parameters
policypolicy to be set
void OIC::Service::RCSResourceObject::unbindResource ( const RCSResourceObject::Ptr resource)

Unbind a resource from this resource.

If there is no bound resource left, the resource will run as a normal resource.

Parameters
resourcea resource to be unbound from this resource.
Exceptions
RCSInvalidParameterExceptionIf resource is nullptr or itself.
RCSPlatformExceptionIf the operation failed.
See Also
bindResource

Friends And Related Function Documentation

friend class RCSSeparateResponse
friend

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