~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/catalog/instance.h

Merge in trunk to catalogs.

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/make_shared.hpp>
 
25
#include "drizzled/message/catalog.h"
 
26
#include "drizzled/identifier/session.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
 
 
42
public:
 
43
  typedef boost::shared_ptr<Instance> shared_ptr;
 
44
  typedef std::vector<shared_ptr> vector;
 
45
  typedef const Instance* const_pointer;
 
46
 
 
47
  Instance(message::catalog::shared_ptr &message_arg)
 
48
  {
 
49
    _message= message_arg;
 
50
  };
 
51
 
 
52
  Instance(const message::catalog::shared_ptr &message_arg)
 
53
  {
 
54
    _message= message_arg;
 
55
  };
 
56
 
 
57
  static shared_ptr create(message::catalog::shared_ptr &message_arg)
 
58
  {
 
59
    assert(not message_arg->name().empty());
 
60
    return boost::make_shared<Instance>(message_arg);
 
61
  };
 
62
 
 
63
  static shared_ptr create(const identifier::Catalog &identifier)
 
64
  {
 
65
    drizzled::message::catalog::shared_ptr new_message= drizzled::message::catalog::make_shared(identifier);
 
66
    assert(not new_message->name().empty());
 
67
    return boost::make_shared<Instance>(new_message);
 
68
  }
 
69
 
 
70
  const std::string &getName() const
 
71
  {
 
72
    assert(_message);
 
73
    return _message->name();
 
74
  }
 
75
 
 
76
  const std::string &name() const
 
77
  {
 
78
    assert(_message);
 
79
    return _message->name();
 
80
  }
 
81
 
 
82
  message::catalog::shared_ptr message() const
 
83
  {
 
84
    return _message;
 
85
  }
 
86
 
 
87
  bool locked() const
 
88
  {
 
89
    return _locked;
 
90
  }
 
91
 
 
92
  bool lock(drizzled::session_id_t id)
 
93
  {
 
94
    if (_locked and _lock_id == id)
 
95
    {
 
96
      assert(0); // We shouldn't support recursion
 
97
      return true;
 
98
    }
 
99
    else if (_locked)
 
100
    {
 
101
      return false;
 
102
    }
 
103
 
 
104
    _locked= true;
 
105
    _lock_id= id;
 
106
 
 
107
    return true;
 
108
  }
 
109
 
 
110
  bool unlock(drizzled::session_id_t id)
 
111
  {
 
112
    if (_locked and _lock_id == id)
 
113
    {
 
114
      _locked= false;
 
115
      _lock_id= 0;
 
116
 
 
117
      return true;
 
118
    }
 
119
 
 
120
    return false;
 
121
  }
 
122
};
 
123
 
 
124
} /* namespace catalog */
 
125
} /* namespace drizzled */
 
126
 
 
127
#endif /* DRIZZLED_CATALOG_INSTANCE_H */