1 /* $Id: session.hpp,v 1.15 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 //#include <stdexcept>
27 #include <boost/thread/mutex.hpp>
29 namespace metaproxy_1 {
33 //typedef unsigned long type;
36 /// create new session with new unique id
38 boost::mutex::scoped_lock scoped_lock(m_mutex);
44 /// copy session including old id
45 Session(const Session &s) : m_id(s.m_id), m_close(s.m_close) {};
47 Session& operator=(const Session &s) {
56 bool operator<(const Session &s) const {
57 return m_id < s.m_id ? true : false;
60 unsigned long id() const {
64 bool is_closed() const {
68 /// mark session closed, can not be unset
73 bool operator == (Session &ses) const {
74 return ses.m_id == m_id;
78 unsigned long int m_id;
81 /// static mutex to lock static m_id
82 static boost::mutex m_mutex;
84 /// static m_id to make sure that there is only one id counter
85 static unsigned long int m_global_id;
89 template <class T> class session_map {
91 void create(T &t, const metaproxy_1::Session &s) {
92 boost::mutex::scoped_lock lock(m_map_mutex);
93 m_map[s] = SessionItem(t);
95 void release(const metaproxy_1::Session &s) {
96 boost::mutex::scoped_lock lock(m_map_mutex);
101 T &get_session_data(const metaproxy_1::Session &s) {
102 boost::mutex::scoped_lock lock(m_map_mutex);
104 typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
106 if (it == m_map.end())
108 boost::mutx::scoped_lock *scoped_ptr =
109 new boost::mutex::scoped_lock(it->second->m_item_mutex);
112 bool exist(const metaproxy_1::Session &s) {
113 typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
115 return it == m_map.end() ? false : true;
121 SessionItem(T &t) : m_t(t) {};
122 SessionItem &operator =(const SessionItem &s) {
128 SessionItem(const SessionItem &s) {
132 boost::mutex m_item_mutex;
135 boost::mutex m_map_mutex;
136 std::map<metaproxy_1::Session,SessionItem>m_map;
145 * indent-tabs-mode: nil
146 * c-file-style: "stroustrup"
148 * vim: shiftwidth=4 tabstop=8 expandtab