Class Attributes
Synopsis
#include <lib/inc/drogon/Attribute.h>
class Attributes
Description
This class represents the attributes stored in the HTTP request. One can add/get any type of data to/from an Attributes object.
Methods
Attributes | Constructor, usually called by the framework. | |
clear | Clear all attributes. | |
erase | Erase the data identified by the given key. | |
find | Retrun true if the data identified by the key exists. | |
get | Get the data identified by the key parameter. | |
insert overload | Insert a key-value pair. | |
operator[] | Get the 'any' object identified by the given key. |
Source
Lines 28-126 in lib/inc/drogon/Attribute.h.
class Attributes
{
public:
/**
* @brief Get the data identified by the key parameter.
* @note if the data is not found, a default value is returned.
* For example:
* @code
auto &userName = attributesPtr->get<std::string>("user name");
@endcode
*/
template <typename T>
const T &get(const std::string &key) const
{
const static T nullVal = T();
auto it = attributesMap_.find(key);
if (it != attributesMap_.end())
{
if (typeid(T) == it->second.type())
{
return *(any_cast<T>(&(it->second)));
}
else
{
LOG_ERROR << "Bad type";
}
}
return nullVal;
}
/**
* @brief Get the 'any' object identified by the given key
*/
any &operator[](const std::string &key)
{
return attributesMap_[key];
}
/**
* @brief Insert a key-value pair
* @note here the any object can be created implicitly. for example
* @code
attributesPtr->insert("user name", userNameString);
@endcode
*/
void insert(const std::string &key, const any &obj)
{
attributesMap_[key] = obj;
}
/**
* @brief Insert a key-value pair
* @note here the any object can be created implicitly. for example
* @code
attributesPtr->insert("user name", userNameString);
@endcode
*/
void insert(const std::string &key, any &&obj)
{
attributesMap_[key] = std::move(obj);
}
/**
* @brief Erase the data identified by the given key.
*/
void erase(const std::string &key)
{
attributesMap_.erase(key);
}
/**
* @brief Retrun true if the data identified by the key exists.
*/
bool find(const std::string &key)
{
if (attributesMap_.find(key) == attributesMap_.end())
{
return false;
}
return true;
}
/**
* @brief Clear all attributes.
*/
void clear()
{
attributesMap_.clear();
}
/**
* @brief Constructor, usually called by the framework
*/
Attributes() = default;
private:
using AttributesMap = std::map<std::string, any>;
AttributesMap attributesMap_;
};