Added operator == for Session
[metaproxy-moved-to-github.git] / src / session.hpp
1 /* $Id: session.hpp,v 1.8 2005-10-16 16:05:18 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #ifndef SESSION_HPP
8 #define SESSION_HPP
9
10 //#include <stdexcept>
11
12 #include <boost/thread/mutex.hpp>
13
14 namespace yp2 {
15     
16     class Session
17     {
18         //typedef unsigned long type;
19     public:
20         
21         /// create new session with new unique id
22         Session() {
23             boost::mutex::scoped_lock scoped_lock(m_mutex);
24             ++m_global_id;
25             m_id =  m_global_id;
26             m_close = false;
27         };
28         
29         /// copy session including old id
30         Session(const Session &s) : m_id(s.m_id), m_close(s.m_close) {};
31         
32         Session& operator=(const Session &s) {
33             if (this != &s)
34             {
35                 m_id = s.m_id;
36                 m_close = s.m_close;
37             }
38             return *this;
39         }
40         
41         unsigned long id() const {
42             return m_id;
43         };
44         
45         bool is_closed() const {
46             return m_close;
47         };
48         
49         /// mark session closed, can not be unset
50         void close() {
51             m_close = true;
52         };
53
54         bool operator == (Session &ses) {
55             return ses.m_id == m_id;
56         }
57     private:
58         
59         unsigned long int m_id;
60         bool m_close;
61         
62         /// static mutex to lock static m_id
63         static boost::mutex m_mutex;
64         
65         /// static m_id to make sure that there is only one id counter
66         static unsigned long int m_global_id;
67         
68     };
69     
70 }
71
72 #endif
73 /*
74  * Local variables:
75  * c-basic-offset: 4
76  * indent-tabs-mode: nil
77  * c-file-style: "stroustrup"
78  * End:
79  * vim: shiftwidth=4 tabstop=8 expandtab
80  */