context.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2006 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_DISPLAY_CONTEXT_H
00021 #define ARCHON_DISPLAY_CONTEXT_H
00022 
00023 #include <stdexcept>
00024 #include <string>
00025 
00026 #include <archon/util/exception.H>
00027 #include <archon/util/ref.H>
00028 #include <archon/display/visual.H>
00029 #include <archon/display/drawable.H>
00030 
00031 namespace Archon
00032 {
00033   namespace Display
00034   {
00035     using namespace std;
00036     using namespace Utilities;
00037 
00038     // Fallback declarations. A fallback declaration for class Foo is
00039     // needed when the header that defines Foo includes this
00040     // header. In this case this header will not include "foo.H" due
00041     // to include guards, and Foo would be unknown.
00042     struct Visual;
00043     struct Drawable;
00044 
00045 
00046     struct ContextAlreadyBoundException: Exception
00047     {
00048       ContextAlreadyBoundException(string l): Exception(l) {}
00049     };
00050 
00051     struct NestedBindingException: Exception
00052     {
00053       NestedBindingException(string l): Exception(l) {}
00054     };
00055 
00056 
00057     struct Bind;
00058 
00078     struct Context: virtual RefObjectBase
00079     {
00086       virtual Ref<Visual> getVisual() = 0;
00087 
00093       virtual bool isDirect() = 0;
00094 
00095     private:
00096       friend struct Bind;
00097       virtual void bind(const Ref<Drawable> &, bool block)
00098         throw(ContextAlreadyBoundException, NestedBindingException,
00099               invalid_argument) = 0;
00100       virtual void unbind() = 0;
00101     };
00102 
00103 
00129     struct Bind
00130     {
00142       Bind(const Ref<Context> &c, const Ref<Drawable> &d, bool block = true)
00143         throw(ContextAlreadyBoundException, NestedBindingException, invalid_argument)
00144       {
00145         aquire(c, d, block);
00146       }
00147 
00148       ~Bind() { release(); }
00149 
00181       void aquire(const Ref<Context> &c, const Ref<Drawable> &d, bool block = true)
00182         throw(ContextAlreadyBoundException, NestedBindingException, invalid_argument)
00183       {
00184         if(context) release();
00185         c->bind(d, block);
00186         context = c;
00187       }
00188 
00192       void release()
00193       {
00194         if(!context) return;
00195         context->unbind();
00196         context.reset();
00197       }
00198 
00199     private:
00200       Ref<Context> context;
00201     };
00202   }
00203 }
00204 
00205 #endif // ARCHON_DISPLAY_CONTEXT_H

Generated on Sun Jul 30 22:55:41 2006 for Archon by  doxygen 1.4.4