Drizzled Public API Documentation

instance.h
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #pragma once
00022 
00023 #include <boost/thread/mutex.hpp>
00024 #include <boost/make_shared.hpp>
00025 
00026 #include <drizzled/message/catalog.h>
00027 
00028 namespace drizzled {
00029 namespace catalog {
00030 
00031 class Instance
00032 {
00033   Instance() :
00034     _locked(false),
00035     _lock_id(0)
00036   { };
00037 
00038   bool _locked;
00039   drizzled::session_id_t _lock_id;
00040   message::catalog::shared_ptr _message;
00041   mutable boost::mutex _schema_lock;
00042   mutable boost::mutex _system_variable_lock;
00043 
00044 
00045 public:
00046   typedef boost::shared_ptr<Instance> shared_ptr;
00047   typedef std::vector<shared_ptr> vector;
00048   typedef const Instance* const_pointer;
00049   typedef const Instance& const_reference;
00050   typedef Instance& reference;
00051 
00052   Instance(message::catalog::shared_ptr &message_arg)
00053   {
00054     _message= message_arg;
00055   };
00056 
00057   Instance(const message::catalog::shared_ptr &message_arg)
00058   {
00059     _message= message_arg;
00060   };
00061 
00062   static shared_ptr make_shared(message::catalog::shared_ptr &message_arg)
00063   {
00064     assert(not message_arg->name().empty());
00065     return boost::make_shared<Instance>(message_arg);
00066   };
00067 
00068   static shared_ptr make_shared(const identifier::Catalog &identifier)
00069   {
00070     drizzled::message::catalog::shared_ptr new_message= drizzled::message::catalog::make_shared(identifier);
00071     assert(not new_message->name().empty());
00072     return boost::make_shared<Instance>(new_message);
00073   }
00074 
00075   const std::string &getName() const
00076   {
00077     assert(_message);
00078     return _message->name();
00079   }
00080 
00081   const std::string &name() const
00082   {
00083     assert(_message);
00084     return _message->name();
00085   }
00086 
00087   message::catalog::shared_ptr message() const
00088   {
00089     return _message;
00090   }
00091 
00092   bool locked() const
00093   {
00094     return _locked;
00095   }
00096 
00097   bool lock(drizzled::session_id_t id)
00098   {
00099     if (_locked and _lock_id == id)
00100     {
00101       assert(0); // We shouldn't support recursion
00102       return true;
00103     }
00104     else if (_locked)
00105     {
00106       return false;
00107     }
00108 
00109     _locked= true;
00110     _lock_id= id;
00111 
00112     return true;
00113   }
00114 
00115   bool unlock(drizzled::session_id_t id)
00116   {
00117     if (_locked and _lock_id == id)
00118     {
00119       _locked= false;
00120       _lock_id= 0;
00121 
00122       return true;
00123     }
00124 
00125     return false;
00126   }
00127 
00128   boost::mutex &schemaLock()
00129   {
00130     return _schema_lock;
00131   }
00132 
00133   boost::mutex &systemVariableLock()
00134   {
00135     return _system_variable_lock;
00136   }
00137 };
00138 
00139 } /* namespace catalog */
00140 } /* namespace drizzled */
00141