1 /* $Id: session.hpp,v 1.11 2005-10-26 18:53:49 adam Exp $
2 Copyright (c) 2005, Index Data.
10 //#include <stdexcept>
12 #include <boost/thread/mutex.hpp>
18 //typedef unsigned long type;
21 /// create new session with new unique id
23 boost::mutex::scoped_lock scoped_lock(m_mutex);
29 /// copy session including old id
30 Session(const Session &s) : m_id(s.m_id), m_close(s.m_close) {};
32 Session& operator=(const Session &s) {
41 bool operator<(const Session &s) const {
42 return m_id < s.m_id ? true : false;
45 unsigned long id() const {
49 bool is_closed() const {
53 /// mark session closed, can not be unset
58 bool operator == (Session &ses) const {
59 return ses.m_id == m_id;
63 unsigned long int m_id;
66 /// static mutex to lock static m_id
67 static boost::mutex m_mutex;
69 /// static m_id to make sure that there is only one id counter
70 static unsigned long int m_global_id;
74 template <class T> class session_map {
76 void create(T &t, const yp2::Session &s) {
77 boost::mutex::scoped_lock lock(m_map_mutex);
78 m_map[s] = SessionItem(t);
80 void release(const yp2::Session &s) {
81 boost::mutex::scoped_lock lock(m_map_mutex);
86 T &get_session_data(const yp2::Session &s) {
87 boost::mutex::scoped_lock lock(m_map_mutex);
89 typename std::map<yp2::Session,SessionItem>::const_iterator it;
91 if (it == m_map.end())
93 boost::mutx::scoped_lock *scoped_ptr =
94 new boost::mutex::scoped_lock(it->second->m_item_mutex);
97 bool exist(const yp2::Session &s) {
98 typename std::map<yp2::Session,SessionItem>::const_iterator it;
100 return it == m_map.end() ? false : true;
106 SessionItem(T &t) : m_t(t) {};
107 SessionItem &operator =(const SessionItem &s) {
113 SessionItem(const SessionItem &s) {
117 boost::mutex m_item_mutex;
120 boost::mutex m_map_mutex;
121 std::map<yp2::Session,SessionItem>m_map;
130 * indent-tabs-mode: nil
131 * c-file-style: "stroustrup"
133 * vim: shiftwidth=4 tabstop=8 expandtab