tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
session.h
1 /*
2  Copyright (c) 2012 Vinícius dos Santos Oliveira
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all
12  copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  SOFTWARE.
21  */
22 
23 #ifndef TUFAO_SESSION_H
24 #define TUFAO_SESSION_H
25 
26 #include "sessionstore.h"
27 #include <QtCore/QVariant>
28 
29 namespace Tufao {
30 
48 class TUFAO_EXPORT Session
49 {
50 public:
61  {
62  public:
71  PropertyWrapper(Session &session, const QByteArray &key) :
72  session(session),
73  key(key)
74  {}
75 
79  operator bool() const
80  {
81  return session.hasValue(key);
82  }
83 
87  PropertyWrapper &operator =(const QVariant &value)
88  {
89  session.setValue(key, value);
90  return *this;
91  }
92 
96  QVariant operator ()()
97  {
98  return session.value(key);
99  }
100 
101  private:
102  Session &session;
103  const QByteArray key;
104  };
105 
113  Session(SessionStore &store, const HttpServerRequest &request,
114  HttpServerResponse &response) :
115  store(store),
116  request(request),
117  response(response)
118  {}
119 
126  bool hasValue(const QByteArray &key) const
127  {
128  return store.hasProperty(request, response, key);
129  }
130 
138  QVariant value(const QByteArray &key) const
139  {
140  return store.property(request, response, key);
141  }
142 
149  void setValue(const QByteArray &key, const QVariant &value)
150  {
151  store.setProperty(request, response, key, value);
152  }
153 
158  PropertyWrapper operator [](const QByteArray &key)
159  {
160  return PropertyWrapper(*this, key);
161  }
162 
176  template<class F>
177  static void apply(SessionStore &store, const QByteArray &property,
178  const HttpServerRequest &request,
179  HttpServerResponse &response, F f)
180  {
181  QVariant v = store.property(request, response, property);
182  f(v);
183  store.setProperty(request, response, property, v);
184  }
185 
203  template<class F>
204  static void apply(SessionStore &store, const HttpServerRequest &request,
205  HttpServerResponse &response, F f)
206  {
207  QMap<QByteArray, QVariant> properties;
208 
209  for (const auto &property: store.properties(request, response))
210  properties[property] = store.property(request, response, property);
211 
212  f(properties);
213 
214  for (auto i = properties.begin();i != properties.end();++i)
215  store.setProperty(request, response, i.key(), i.value());
216  }
217 
218 private:
219  SessionStore &store;
220  const HttpServerRequest &request;
221  HttpServerResponse &response;
222 };
223 
224 } // namespace Tufao
225 
226 #endif // TUFAO_SESSION_H
virtual QVariant property(const HttpServerRequest &request, HttpServerResponse &response, const QByteArray &key) const =0
Returns the property referenced by key in the session, or a null QVariant if the property isn't found...
PropertyWrapper(Session &session, const QByteArray &key)
Constructs a new PropertyWrappe object.
Definition: session.h:71
The Tufao::HttpServerResponse is used to respond to a Tufao::HttpServerRequest.
Definition: httpserverresponse.h:142
The Tufao::HttpServer represents a HTTP request received by Tufao::HttpServer.
Definition: httpserverrequest.h:54
This class provides easier access to the session's properties.
Definition: session.h:48
virtual QList< QByteArray > properties(const HttpServerRequest &request, const HttpServerResponse &response) const =0
Returns a list of set properties to this session.
Provides a object that give less verbose access to a session property.
Definition: session.h:60
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...
Definition: session.h:138
static void apply(SessionStore &store, const HttpServerRequest &request, HttpServerResponse &response, F f)
Takes a functor to access the session's properties.
Definition: session.h:204
virtual void setProperty(const HttpServerRequest &request, HttpServerResponse &response, const QByteArray &key, const QVariant &value)=0
Sets the property's value referenced by key to value.
static void apply(SessionStore &store, const QByteArray &property, const HttpServerRequest &request, HttpServerResponse &response, F f)
Takes a functor to access a session's property.
Definition: session.h:177
bool hasValue(const QByteArray &key) const
Returns true if the session has a property accessible through key.
Definition: session.h:126
This is the namespace where all Tufão facilities are grouped.
Definition: abstracthttpserverrequesthandler.h:30
Session(SessionStore &store, const HttpServerRequest &request, HttpServerResponse &response)
Constructs a new Session object.
Definition: session.h:113
void setValue(const QByteArray &key, const QVariant &value)
Sets the property's value referenced by key to value.
Definition: session.h:149
SessionStore class can be used to store data that must persist among different requests.
Definition: sessionstore.h:220