tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
Tufao::Session Class Reference

This class provides easier access to the session's properties. More...

+ Collaboration diagram for Tufao::Session:

Classes

class  PropertyWrapper
 Provides a object that give less verbose access to a session property. More...
 

Public Member Functions

 Session (SessionStore &store, const HttpServerRequest &request, HttpServerResponse &response)
 Constructs a new Session object. More...
 
bool hasValue (const QByteArray &key) const
 Returns true if the session has a property accessible through key. More...
 
QVariant value (const QByteArray &key) const
 Returns the value of the property referenced by key, or a null QVariant if the property isn't found. More...
 
void setValue (const QByteArray &key, const QVariant &value)
 Sets the property's value referenced by key to value. More...
 
PropertyWrapper operator[] (const QByteArray &key)
 Returns a PropertyWrapper that will remember the key used to manipulate the session property.
 

Static Public Member Functions

template<class F >
static void apply (SessionStore &store, const QByteArray &property, const HttpServerRequest &request, HttpServerResponse &response, F f)
 Takes a functor to access a session's property. More...
 
template<class F >
static void apply (SessionStore &store, const HttpServerRequest &request, HttpServerResponse &response, F f)
 Takes a functor to access the session's properties. More...
 

Detailed Description

This class provides easier access to the session's properties.

It uses C++ features used in containers to provide a familiar interface, such as overloading the operator [].

bool RequestHandler::handleRequest(Tufao::HttpServerRequest &request,
{
Tufao::Session s(store, request, response);
s["access"] = s["access"]().toInt() + 1;
response.writeHead(200, "OK");
response << "You have "
<< QByteArray::number(s["access"]().toInt())
<< " access";
response.end();
return true;
}
Note
All member functions of this class are inlined and should add the minimum (if any) of overhead.
See also
SessionStore
Since
0.4

Constructor & Destructor Documentation

Tufao::Session::Session ( SessionStore store,
const HttpServerRequest request,
HttpServerResponse response 
)
inline

Constructs a new Session object.

The object will use store, request and response in every operation and you should ensure these objects aren't destructed before this Session object.

Member Function Documentation

template<class F >
static void Tufao::Session::apply ( SessionStore store,
const QByteArray &  property,
const HttpServerRequest request,
HttpServerResponse response,
f 
)
inlinestatic

Takes a functor to access a session's property.

Parameters
fa functor that receives a QVariant object reference as an argument.
Note
After the functor returns, the property is updated.
bool RequestHandler::handleRequest(Tufao::HttpServerRequest &request,
{
response.writeHead(200, "OK");
Tufao::Session::apply(store, "access", request, response,
[&response](QVariant &access) {
access = access.toInt() + 1;
response << "You visited this page "
<< QByteArray::number(access.toInt())
<< " times";
});
response.end();
return true;
}
Since
1.0
template<class F >
static void Tufao::Session::apply ( SessionStore store,
const HttpServerRequest request,
HttpServerResponse response,
f 
)
inlinestatic

Takes a functor to access the session's properties.

Parameters
fa functor that receives a QMap<QByteArray, QVariant> object reference as an argument.
Note
After the functor returns, the property is updated.
bool RequestHandler::handleRequest(Tufao::HttpServerRequest &request,
{
response.writeHead(200, "OK");
Tufao::Session::apply(store, request, response,
[&response](QMap<QByteArray, QVariant> &properties) {
properties["access"] = properties["access"].toInt() + 1;
response << "You visited this page "
<< QByteArray::number(properties["access"].toInt())
<< " times";
});
response.end();
return true;
}
Note
If the session may contain a lot of properties and you aren't going to access most of them, this helper function might be inefficient.
Since
1.0
bool Tufao::Session::hasValue ( const QByteArray &  key) const
inline

Returns true if the session has a property accessible through key.

See also
SessionStore::hasProperty
void Tufao::Session::setValue ( const QByteArray &  key,
const QVariant &  value 
)
inline

Sets the property's value referenced by key to value.

See also
SessionStore::setProperty
QVariant Tufao::Session::value ( const QByteArray &  key) const
inline

Returns the value of the property referenced by key, or a null QVariant if the property isn't found.

See also
SessionStore::property

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