Drizzled Public API Documentation

instance.h
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2010 Brian Aker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #pragma once
22 
23 #include <boost/thread/mutex.hpp>
24 #include <boost/make_shared.hpp>
25 
26 #include <drizzled/message/catalog.h>
27 
28 namespace drizzled {
29 namespace catalog {
30 
31 class Instance
32 {
33  Instance() :
34  _locked(false),
35  _lock_id(0)
36  { };
37 
38  bool _locked;
39  drizzled::session_id_t _lock_id;
40  message::catalog::shared_ptr _message;
41  mutable boost::mutex _schema_lock;
42  mutable boost::mutex _system_variable_lock;
43 
44 
45 public:
46  typedef boost::shared_ptr<Instance> shared_ptr;
47  typedef std::vector<shared_ptr> vector;
48  typedef const Instance* const_pointer;
49  typedef const Instance& const_reference;
50  typedef Instance& reference;
51 
52  Instance(message::catalog::shared_ptr &message_arg)
53  {
54  _message= message_arg;
55  };
56 
57  Instance(const message::catalog::shared_ptr &message_arg)
58  {
59  _message= message_arg;
60  };
61 
62  static shared_ptr make_shared(message::catalog::shared_ptr &message_arg)
63  {
64  assert(not message_arg->name().empty());
65  return boost::make_shared<Instance>(message_arg);
66  };
67 
68  static shared_ptr make_shared(const identifier::Catalog &identifier)
69  {
70  drizzled::message::catalog::shared_ptr new_message= drizzled::message::catalog::make_shared(identifier);
71  assert(not new_message->name().empty());
72  return boost::make_shared<Instance>(new_message);
73  }
74 
75  const std::string &getName() const
76  {
77  assert(_message);
78  return _message->name();
79  }
80 
81  const std::string &name() const
82  {
83  assert(_message);
84  return _message->name();
85  }
86 
87  message::catalog::shared_ptr message() const
88  {
89  return _message;
90  }
91 
92  bool locked() const
93  {
94  return _locked;
95  }
96 
97  bool lock(drizzled::session_id_t id)
98  {
99  if (_locked and _lock_id == id)
100  {
101  assert(0); // We shouldn't support recursion
102  return true;
103  }
104  else if (_locked)
105  {
106  return false;
107  }
108 
109  _locked= true;
110  _lock_id= id;
111 
112  return true;
113  }
114 
115  bool unlock(drizzled::session_id_t id)
116  {
117  if (_locked and _lock_id == id)
118  {
119  _locked= false;
120  _lock_id= 0;
121 
122  return true;
123  }
124 
125  return false;
126  }
127 
128  boost::mutex &schemaLock()
129  {
130  return _schema_lock;
131  }
132 
133  boost::mutex &systemVariableLock()
134  {
135  return _system_variable_lock;
136  }
137 };
138 
139 } /* namespace catalog */
140 } /* namespace drizzled */
141