/**
* @file
*
- * This file contains the "RCSResourceAttributes" class & its helper classes
+ * This file contains the declaration of classes and its members related to RCSResourceAttributes
*/
#ifndef RES_ENCAPSULATION_RESOURCEATTRIBUTES_H
#define RES_ENCAPSULATION_RESOURCEATTRIBUTES_H
#include <functional>
#include <unordered_map>
+#include <vector>
-#include <boost/variant.hpp>
-#include <boost/mpl/contains.hpp>
-#include <boost/mpl/find.hpp>
-#include <boost/mpl/distance.hpp>
-#include <boost/mpl/begin_end.hpp>
-#include <boost/scoped_ptr.hpp>
+#include "boost/variant.hpp"
+#include "boost/mpl/contains.hpp"
+#include "boost/mpl/find.hpp"
+#include "boost/mpl/distance.hpp"
+#include "boost/mpl/begin_end.hpp"
+#include "boost/scoped_ptr.hpp"
-#include <RCSException.h>
+#include "RCSException.h"
namespace OIC
{
{
/**
- * RCSResourceAttributes represents the attributes for a resource.
+ * This represents the attributes for a resource.
*
* It provides similar usage to c++ standard containers. (iterator,
* operators and accessors)<br/>
* An attribute value can be one of various types. <br/>
*
- * @note If client developer wants to get the RCSResourceAttributes for the resource of
- * interest following are the steps:
- * - first call the discover API of DiscoveryManager class.
- * - After getting the RemoteResourceObject, call getRemoteAttributes() API
- * of RemoteResourceObject class
*
* @see Value
* @see Type
double,
bool,
std::string,
- RCSResourceAttributes
+ RCSResourceAttributes,
+
+ std::vector< int >,
+ std::vector< double >,
+ std::vector< bool >,
+ std::vector< std::string >,
+ std::vector< RCSResourceAttributes >,
+
+ std::vector< std::vector< int > >,
+ std::vector< std::vector< std::vector< int > > >,
+
+ std::vector< std::vector< double > >,
+ std::vector< std::vector< std::vector< double > > >,
+
+ std::vector< std::vector< bool > >,
+ std::vector< std::vector< std::vector< bool > > >,
+
+ std::vector< std::vector< std::string > >,
+ std::vector< std::vector< std::vector< std::string > > >,
+
+ std::vector< std::vector< RCSResourceAttributes > >,
+ std::vector< std::vector< std::vector< RCSResourceAttributes > > >
> ValueVariant;
template< typename T, typename V = void,
typedef V type;
};
- template< typename VISITOR >
+ template< typename VISITOR, typename MOVE = std::false_type >
class KeyValueVisitorHelper: public boost::static_visitor< >
{
public:
- KeyValueVisitorHelper(VISITOR& visitor) :
+ KeyValueVisitorHelper(VISITOR& visitor) noexcept :
m_visitor( visitor )
{
}
- template< typename T >
- void operator()(const std::string& key, const T& value) const
+ template< typename T, typename M = MOVE >
+ typename std::enable_if< std::is_same< M, std::false_type >::value >::type
+ operator()(const std::string& key, const T& value) const
{
m_visitor(key, value);
}
+ template< typename T, typename M = MOVE >
+ typename std::enable_if< std::is_same< M, std::true_type >::value >::type
+ operator()(const std::string& key, T& value)
+ {
+ m_visitor(key, std::move(value));
+ }
+
private:
VISITOR& m_visitor;
};
IsSupportedTypeHelper< T >::type::value, std::true_type, std::false_type>::type { };
/**
- * Identifier for types of Value.
+ * Identifiers for types of Value.
*
* @see Type
*/
* Returns type identifier.
*
* @return Identifier of type.
+ *
+ * @see getBaseTypeId
+ */
+ TypeId getId() const noexcept;
+
+ /**
+ * Returns the type identifier of a base type of sequence.
+ *
+ * For non sequence types, it is equivalent to calling getId.
+ *
+ * @return Identifier of type.
+ *
+ * @see getDepth
+ * @see getId
+ */
+ static TypeId getBaseTypeId(const Type& t) noexcept;
+
+ /**
+ * Returns the depth of a type.
+ *
+ * The return will be zero for non sequence types.
+ *
+ * @see getBaseTypeId
+ */
+ static size_t getDepth(const Type& t) noexcept;
+
+ /**
+ * Factory method to create Type instance from T.
+ *
+ * @return An instance that has TypeId for T.
+ *
+ * @note T must be supported by Value. Otherwise, it won't compile.
+ *
+ * @see is_supported_type
*/
- TypeId getId() const;
+ template < typename T >
+ constexpr static Type typeOf(const T&) noexcept
+ {
+ return Type{ IndexOfType< T >::value };
+ }
/**
* Factory method to create Type instance from T.
*
* @return An instance that has TypeId for T.
*
- * @note T must be supported by Value. Otherwise, it won't be compiled.
+ * @note T must be supported by Value. Otherwise, it won't compile.
*
* @see is_supported_type
*/
template < typename T >
- static Type typeOf(const T& value)
+ constexpr static Type typeOf() noexcept
{
- return Type(value);
+ return Type{ IndexOfType< T >::value };
}
//! @cond
- friend bool operator==(const Type&, const Type&);
+ friend bool operator==(const Type&, const Type&) noexcept;
//! @endcond
private:
- template < typename T >
- explicit Type(const T&) :
- m_which{ IndexOfType< T >::value }
+ constexpr explicit Type(int which) noexcept :
+ m_which{ which }
{
}
*
* Type helps identify type information of Value.
*
+ * Supported types are below
+ * @code
+ int
+ double
+ bool
+ std::string
+ RCSResourceAttributes
+
+ std::vector< int >
+ std::vector< double >
+ std::vector< bool >
+ std::vector< std::string >
+ std::vector< RCSResourceAttributes >
+
+ std::vector< std::vector< int > >
+ std::vector< std::vector< std::vector< int > > >
+
+ std::vector< std::vector< double > >
+ std::vector< std::vector< std::vector< double > > >
+
+ std::vector< std::vector< bool > >
+ std::vector< std::vector< std::vector< bool > > >
+
+ std::vector< std::vector< std::string > >
+ std::vector< std::vector< std::vector< std::string > > >
+
+ std::vector< std::vector< RCSResourceAttributes > >
+ std::vector< std::vector< std::vector< RCSResourceAttributes > > >
+ * @endcode
+ *
* @see RCSResourceAttributes
* @see Type
* @see is_supported_type
Value();
Value(const Value&);
- Value(Value&&);
+ Value(Value&&) noexcept;
/**
* Constructs a Value if T is a supported type.<br/>
- * Otherwise it won't be compiled.
+ * Otherwise it won't compile.
*/
template< typename T, typename = typename enable_if_supported< T >::type >
Value(T&& value) :
{
}
- Value(const char* value);
+ Value(const char*);
Value& operator=(const Value&);
Value& operator=(Value&&);
/**
* Exchanges the content of the object by the content of the parameter.
*/
- void swap(Value&);
+ void swap(Value&) noexcept;
//! @cond
friend class RCSResourceAttributes;
}
catch (const boost::bad_get&)
{
- throw BadGetException{ "Wrong type" };
+ throw RCSBadGetException{ "Wrong type" };
}
}
{
return get< T >() == rhs;
}
- catch (const BadGetException&)
+ catch (const RCSBadGetException&)
{
return false;
}
/**
* Returns an {@link iterator} referring to the first element.
*/
- iterator begin();
+ iterator begin() noexcept;
/**
* Returns an {@link iterator} referring to the <i>past-the-end element</i>.
*/
- iterator end();
+ iterator end() noexcept;
/**
* @copydoc cbegin()
*/
- const_iterator begin() const;
+ const_iterator begin() const noexcept;
/**
* @copydoc cend()
*/
- const_iterator end() const;
+ const_iterator end() const noexcept;
/**
* Returns a const_iterator referring to the first element.
*/
- const_iterator cbegin() const;
+ const_iterator cbegin() const noexcept;
/**
* Returns a const_iterator referring to the <i>past-the-end element</i>.
*/
- const_iterator cend() const;
+ const_iterator cend() const noexcept;
/**
* Accesses a value.
/**
* Removes all elements.
*/
- void clear();
+ void clear() noexcept;
/**
* Removes a single element.
bool erase(const std::string& key);
/**
- * Checks the container has an element with a Key equivalent to key.
+ * Removes a single element.
+ *
+ * @param pos Iterator to the element to remove.
+ *
+ * @return Iterator following the last removed element.
+ */
+ iterator erase(const_iterator pos);
+
+ /**
+ * Checks this contains an element for the specified key.
*
* @param key Key to check.
*
*
* @see size
*/
- bool empty() const;
+ bool empty() const noexcept;
/**
* Returns the number of elements.
*
* @see empty
*/
- size_t size() const;
+ size_t size() const noexcept;
private:
template< typename VISITOR >
}
}
+ template< typename VISITOR >
+ void visitToMove(VISITOR& visitor)
+ {
+ KeyValueVisitorHelper< VISITOR, std::true_type > helper{ visitor };
+
+ for (auto& i : m_values)
+ {
+ boost::variant< const std::string& > key{ i.first };
+ boost::apply_visitor(helper, key, *i.second.m_data);
+ }
+ }
+
private:
std::unordered_map< std::string, Value > m_values;
public:
ComparisonHelper(const Value&);
+ ComparisonHelper(const ComparisonHelper&) = delete;
+ ComparisonHelper& operator=(const ComparisonHelper&) = delete;
+
template< typename T >
typename std::enable_if< is_supported_type< T >::value, bool >::type equals(
const T& v) const
const Value& m_valueRef;
};
+ //! @cond
template< typename T >
struct RCSResourceAttributes::IsSupportedTypeHelper
{
- typedef boost::mpl::contains<ValueVariant::types, typename std::decay< T >::type> type;
+ typedef boost::mpl::contains< ValueVariant::types, typename std::decay< T >::type > type;
};
- template <typename T>
+ template < typename T >
struct RCSResourceAttributes::IndexOfType
{
+ static_assert(RCSResourceAttributes::is_supported_type< T >::value,
+ "The type is not supported!");
+
typedef typename boost::mpl::find< ValueVariant::types, T >::type iter;
typedef typename boost::mpl::begin< ValueVariant::types >::type mpl_begin;
static constexpr int value = boost::mpl::distance< mpl_begin, iter >::value;
};
+ template < typename T > constexpr int RCSResourceAttributes::IndexOfType< T >::value;
+ //! @endcond
+
/**
* @relates RCSResourceAttributes::Type
*
*
* @return true if the objects are equal, false otherwise.
*/
- bool operator==(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&);
+ bool operator==(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&)
+ noexcept;
/**
* @relates RCSResourceAttributes::Type
*
* @return true if the objects are not equal, false otherwise.
*/
- bool operator!=(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&);
+ bool operator!=(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&)
+ noexcept;
/**
* @relates RCSResourceAttributes::Value
class KeyVisitor: public boost::static_visitor< const std::string& >
{
public:
- result_type operator()(iterator*) const;
- result_type operator()(const_iterator*) const;
+ result_type operator()(iterator*) const noexcept;
+ result_type operator()(const_iterator*) const noexcept;
};
class ValueVisitor: public boost::static_visitor< Value& >
{
public:
- result_type operator()(iterator*);
+ result_type operator()(iterator*) noexcept;
result_type operator()(const_iterator*);
};
class ConstValueVisitor: public boost::static_visitor< const Value& >
{
public:
- result_type operator()(iterator*) const;
- result_type operator()(const_iterator*) const;
+ result_type operator()(iterator*) const noexcept;
+ result_type operator()(const_iterator*) const noexcept;
};
public:
- const std::string& key() const;
- const RCSResourceAttributes::Value& value() const;
+ const std::string& key() const noexcept;
+ const RCSResourceAttributes::Value& value() const noexcept;
RCSResourceAttributes::Value& value();
private:
KeyValuePair(const KeyValuePair&) = default;
- KeyValuePair(boost::variant< iterator*, const_iterator* >&&);
+ KeyValuePair(boost::variant< iterator*, const_iterator* >&&) noexcept;
KeyValuePair& operator=(const KeyValuePair&) = default;
public:
iterator();
- iterator(const iterator&) = default;
+ iterator(const iterator&);
- iterator& operator=(const iterator&) = default;
+ iterator& operator=(const iterator&);
reference operator*();
pointer operator->();
* @see iterator
*/
class RCSResourceAttributes::const_iterator:
- public std::iterator < std::forward_iterator_tag,
+ public std::iterator< std::forward_iterator_tag,
const RCSResourceAttributes::KeyValuePair >
{
private:
public:
const_iterator();
- const_iterator(const const_iterator&) = default;
+ const_iterator(const const_iterator&);
const_iterator(const RCSResourceAttributes::iterator&);
- const_iterator& operator=(const const_iterator&) = default;
+ const_iterator& operator=(const const_iterator&);
const_iterator& operator=(const RCSResourceAttributes::iterator&);
reference operator*() const;