tufao  0.8.1
An asynchronous web framework for C++ built on top of Qt
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
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 
163 private:
164  SessionStore &store;
165  const HttpServerRequest &request;
166  HttpServerResponse &response;
167 };
168 
169 } // namespace Tufao
170 
171 #endif // TUFAO_SESSION_H
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:57
The Tufao::HttpServer represents a HTTP request received by Tufao::HttpServer.
Definition: httpserverrequest.h:47
This class provides easier access to the session's properties.
Definition: session.h:48
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
bool hasValue(const QByteArray &key) const
Returns true if the session has a property accessible through key.
Definition: session.h:126
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