~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/catalog/instance.h

  • Committer: Brian Aker
  • Date: 2011-01-25 05:20:15 UTC
  • mto: (2109.1.6 drizzle-build)
  • mto: This revision was merged to the branch mainline in revision 2112.
  • Revision ID: brian@tangent.org-20110125052015-nw5jhmiq0at1qt9u
Merge in reference from pointer.

Show diffs side-by-side

added added

removed removed

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