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/identifier.h>
27 #include <drizzled/identifier/catalog.h>
28 #include <drizzled/message/catalog.h>
29 
30 namespace drizzled {
31 namespace catalog {
32 
33 class Instance
34 {
35  Instance() :
36  _locked(false),
37  _lock_id(0),
38  _identifier(str_ref(""))
39  { };
40 
41  bool _locked;
42  drizzled::session_id_t _lock_id;
43  message::catalog::shared_ptr _message;
44  mutable boost::mutex _schema_lock;
45  mutable boost::mutex _system_variable_lock;
47 
48 
49 public:
50  typedef boost::shared_ptr<Instance> shared_ptr;
51  typedef std::vector<shared_ptr> vector;
52  typedef const Instance* const_pointer;
53  typedef const Instance& const_reference;
54  typedef Instance& reference;
55 
56  Instance(message::catalog::shared_ptr &message_arg) :
57  _identifier(message_arg->name())
58  {
59  _message= message_arg;
60  };
61 
62  Instance(const message::catalog::shared_ptr &message_arg) :
63  _identifier(message_arg->name())
64  {
65  _message= message_arg;
66  };
67 
68  static shared_ptr make_shared(message::catalog::shared_ptr &message_arg)
69  {
70  assert(not message_arg->name().empty());
71  return boost::make_shared<Instance>(message_arg);
72  };
73 
74  static shared_ptr make_shared(const identifier::Catalog &identifier)
75  {
76  drizzled::message::catalog::shared_ptr new_message= drizzled::message::catalog::make_shared(identifier);
77  assert(not new_message->name().empty());
78  return boost::make_shared<Instance>(new_message);
79  }
80 
81  const drizzled::identifier::Catalog &identifier() const
82  {
83  return _identifier;
84  }
85 
86  const std::string &getName() const
87  {
88  assert(_message);
89  return _message->name();
90  }
91 
92  const std::string &name() const
93  {
94  assert(_message);
95  return _message->name();
96  }
97 
98  message::catalog::shared_ptr message() const
99  {
100  return _message;
101  }
102 
103  bool locked() const
104  {
105  return _locked;
106  }
107 
108  bool lock(drizzled::session_id_t id)
109  {
110  if (_locked and _lock_id == id)
111  {
112  assert(0); // We shouldn't support recursion
113  return true;
114  }
115  else if (_locked)
116  {
117  return false;
118  }
119 
120  _locked= true;
121  _lock_id= id;
122 
123  return true;
124  }
125 
126  bool unlock(drizzled::session_id_t id)
127  {
128  if (_locked and _lock_id == id)
129  {
130  _locked= false;
131  _lock_id= 0;
132 
133  return true;
134  }
135 
136  return false;
137  }
138 
139  boost::mutex &schemaLock()
140  {
141  return _schema_lock;
142  }
143 
144  boost::mutex &systemVariableLock()
145  {
146  return _system_variable_lock;
147  }
148 };
149 
150 } /* namespace catalog */
151 } /* namespace drizzled */
152