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 #include <drizzled/identifier/session.h>
00028 
00029 namespace drizzled {
00030 namespace catalog {
00031 
00032 class Instance
00033 {
00034   Instance() :
00035     _locked(false),
00036     _lock_id(0)
00037   { };
00038 
00039   bool _locked;
00040   drizzled::session_id_t _lock_id;
00041   message::catalog::shared_ptr _message;
00042   mutable boost::mutex _schema_lock;
00043   mutable boost::mutex _system_variable_lock;
00044 
00045 
00046 public:
00047   typedef boost::shared_ptr<Instance> shared_ptr;
00048   typedef std::vector<shared_ptr> vector;
00049   typedef const Instance* const_pointer;
00050   typedef const Instance& const_reference;
00051   typedef Instance& reference;
00052 
00053   Instance(message::catalog::shared_ptr &message_arg)
00054   {
00055     _message= message_arg;
00056   };
00057 
00058   Instance(const message::catalog::shared_ptr &message_arg)
00059   {
00060     _message= message_arg;
00061   };
00062 
00063   static shared_ptr make_shared(message::catalog::shared_ptr &message_arg)
00064   {
00065     assert(not message_arg->name().empty());
00066     return boost::make_shared<Instance>(message_arg);
00067   };
00068 
00069   static shared_ptr make_shared(const identifier::Catalog &identifier)
00070   {
00071     drizzled::message::catalog::shared_ptr new_message= drizzled::message::catalog::make_shared(identifier);
00072     assert(not new_message->name().empty());
00073     return boost::make_shared<Instance>(new_message);
00074   }
00075 
00076   const std::string &getName() const
00077   {
00078     assert(_message);
00079     return _message->name();
00080   }
00081 
00082   const std::string &name() const
00083   {
00084     assert(_message);
00085     return _message->name();
00086   }
00087 
00088   message::catalog::shared_ptr message() const
00089   {
00090     return _message;
00091   }
00092 
00093   bool locked() const
00094   {
00095     return _locked;
00096   }
00097 
00098   bool lock(drizzled::session_id_t id)
00099   {
00100     if (_locked and _lock_id == id)
00101     {
00102       assert(0); // We shouldn't support recursion
00103       return true;
00104     }
00105     else if (_locked)
00106     {
00107       return false;
00108     }
00109 
00110     _locked= true;
00111     _lock_id= id;
00112 
00113     return true;
00114   }
00115 
00116   bool unlock(drizzled::session_id_t id)
00117   {
00118     if (_locked and _lock_id == id)
00119     {
00120       _locked= false;
00121       _lock_id= 0;
00122 
00123       return true;
00124     }
00125 
00126     return false;
00127   }
00128 
00129   boost::mutex &schemaLock()
00130   {
00131     return _schema_lock;
00132   }
00133 
00134   boost::mutex &systemVariableLock()
00135   {
00136     return _system_variable_lock;
00137   }
00138 };
00139 
00140 } /* namespace catalog */
00141 } /* namespace drizzled */
00142