Same header and footer for all files. Header includes copyright +
[metaproxy-moved-to-github.git] / src / session.hpp
1 /* $Id: session.hpp,v 1.7 2005-10-15 14:09:09 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     private:
55         
56         unsigned long int m_id;
57         bool m_close;
58         
59         /// static mutex to lock static m_id
60         static boost::mutex m_mutex;
61         
62         /// static m_id to make sure that there is only one id counter
63         static unsigned long int m_global_id;
64         
65     };
66     
67 }
68
69 #endif
70 /*
71  * Local variables:
72  * c-basic-offset: 4
73  * indent-tabs-mode: nil
74  * c-file-style: "stroustrup"
75  * End:
76  * vim: shiftwidth=4 tabstop=8 expandtab
77  */