MercuryCallback.h

Go to the documentation of this file.
00001 #ifndef MERCURYCALLBACK_H
00002 #define MERCURYCALLBACK_H
00003 
00004 #include <map>
00005 #include "global.h"
00006 #include "PStack.h"
00007 
00008 static const MString BLANKSTR;
00009 
00011 
00014 typedef void (*MercuryCallbackFunction)( const MString &, void *, PStack & );
00015 
00017 struct MercuryCallback
00018 {
00019     MercuryCallback( ): funct( NULL ), luser(NULL) {}
00020     MercuryCallback( MercuryCallbackFunction f, void * userdata ) :
00021         funct( f ), luser( userdata ) { }
00022 
00023     MercuryCallbackFunction funct;  //Function name
00024     void * luser;                   //user data
00025 
00026     inline bool operator ==( const MercuryCallback & c )
00027     {   return ( ( c.funct == funct ) && (c.luser == luser )); }
00028 
00029     inline bool operator !=( void * c )
00030     {   return ( ( funct != c ) || ( luser != c )); }
00031 
00032 };
00033 
00034 //Thread safe
00035 class ClbkManager
00036 {
00037 public:
00038     void Register(const void* addr)
00039     {
00040         MLockPtr< std::map< MVPtr, bool> > clbkMap(m_clbkMap, m_mutex);
00041         (*clbkMap)[(MVPtr)addr] = true;
00042     };
00043 
00044     bool IsValid(const void* addr) const
00045     {
00046         MLockPtr< std::map< MVPtr, bool> > clbkMap(m_clbkMap, m_mutex);
00047         std::map< MVPtr, bool>::const_iterator i = clbkMap->find((MVPtr)addr);
00048         if (i != clbkMap->end())
00049             return true;
00050         return false;
00051     };
00052 
00053     void Remove(const void* addr)
00054     {
00055         MLockPtr< std::map< MVPtr, bool> > clbkMap(m_clbkMap, m_mutex);
00056         std::map< MVPtr, bool>::iterator i = clbkMap->find((MVPtr)addr);
00057         if (i != clbkMap->end())
00058             clbkMap->erase(i);
00059     }
00060 private:
00061     volatile std::map< MVPtr, bool > m_clbkMap;
00062     MercuryMutex m_mutex;
00063 };
00064 
00065 //extern ClbkManager CLBKMAN;
00066 
00067 #endif
00068 
00069 /* 
00070  * Copyright (c) 2005-2006 Charles Lohr
00071  * All rights reserved.
00072  *
00073  * Redistribution and use in source and binary forms, with or
00074  * without modification, are permitted provided that the following
00075  * conditions are met:
00076  *  -   Redistributions of source code must retain the above
00077  *      copyright notice, this list of conditions and the following disclaimer.
00078  *  -   Redistributions in binary form must reproduce the above copyright
00079  *      notice, this list of conditions and the following disclaimer in
00080  *      the documentation and/or other materials provided with the distribution.
00081  *  -   Neither the name of the Mercury Engine nor the names of its
00082  *      contributors may be used to endorse or promote products derived from
00083  *      this software without specific prior written permission.
00084  *
00085  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00086  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00087  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00088  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00089  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00090  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00091  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00092  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00093  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00094  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00095  */

Hosted by SourceForge.net Logo