call.h

Go to the documentation of this file.
00001 //    sampalib.org ESL library and tools
00002 //    Copyright (C) 2007  Thierry Grellier
00003 //
00004 //    This program is free software; you can redistribute it and/or modify
00005 //    it under the terms of the GNU General Public License version 2 as
00006 //    published by the Free Software Foundation.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //    You should have received a copy of the GNU General Public License along
00014 //    with this program; if not, write to the Free Software Foundation, Inc.,
00015 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 //
00017 //    contact: www.sampalib.org
00018 
00019 #ifndef SAMPA_CALL_H
00020 #define SAMPA_CALL_H
00021 
00022 #include "sampa/core/object.h"
00023 
00024 #if SAMPA_CONFIG_TRACE_CALLS == SAMPA_DISABLED
00025 
00026 #  define SAMPA_IMPLEMENT_INTERFACE /* */
00027 
00028 namespace Sampa {    
00029   
00036   template<class Interface>
00037   class CallObserver : public Object {
00038   public:
00039     CallObserver(const Name& name)
00040     : Object(name), m_interface(0) {}
00041     void bind_target(Interface* interface) { m_interface = interface; }
00043     Interface* operator->() { return m_interface; }
00044   private:  
00045     Interface* m_interface;
00046     SAMPA_PERSISTENT(CallObserver);
00048   };
00049 }
00050 
00051 #else
00052 
00053 namespace Sampa {
00054 
00056   class Object;
00057   
00058   class CallBase {
00059   public:
00060     CallBase(Object* from, Object* to);
00061     CallBase();
00062     ~CallBase();
00063     inline static void implement_interface(const char* function_name) 
00064     {
00065       if (M_current_call) {
00066         M_current_call->m_function_name = function_name;
00067         M_current_call = 0;
00068       }
00069     }
00070   private:
00071     bool             m_trace;
00072     const char*      m_function_name;
00073     unsigned         m_call_id;
00074     static CallBase* M_current_call;
00075     static unsigned  M_num_call;
00076   };
00077   
00078   template<class Interface>
00079   class Call : public CallBase {
00080   public:
00081     Call(Interface* interface, Object* from, Object* to)
00082     : CallBase(from, to), m_interface(interface) { }
00083     Call(Interface* interface)
00084     : m_interface(interface) { }
00085     Interface* operator->() { return m_interface; }
00086   private:
00087     Interface* m_interface;
00088   };
00090   
00097   template<class Interface>
00098   class CallObserver : public Object {
00099   public:
00100     CallObserver(const Name& name)
00101     : Object(name), m_interface(0), m_target(0) {}
00102     void bind_target(Interface* interface) { m_interface = interface; m_target = dynamic_cast<Interface>(interface); }
00104     Call<Interface> operator->() { return Call<Interface>(m_interface, this, m_target); }
00105   private:  
00106     Interface* m_interface;
00107     Object*    m_target;
00108     SAMPA_PERSISTENT(CallObserver);
00110   };
00111 }
00112 
00113 #ifdef __GNUC__
00114 #  define SAMPA_IMPLEMENT_INTERFACE Sampa::CallBase::implement_interface(__PRETTY_FUNCTION__)
00115 #else
00116 #  define SAMPA_IMPLEMENT_INTERFACE Sampa::CallBase::implement_interface(__func__)
00117 #endif
00118 
00119 #endif
00120 
00121 #endif

Generated on Sat Feb 16 16:23:15 2008 for Sampa by  doxygen 1.5.3