core/NativeOwner.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 06:51:21 2008 +0000 (2008-07-11)
branchrefac-jit
changeset 2192 7602d7fb60d9
parent 2168 c24a6fa957db
child 2226 d10b38524be3
permissions -rw-r--r--
fixed a compat regression, forgot msvc does funky things with overloaded virtual calls
     1 #include "NativeOwner.h"
     2 #include "ShareSys.h"
     3 #include "PluginSys.h"
     4 
     5 void CNativeOwner::SetMarkSerial(unsigned int serial)
     6 {
     7 	m_nMarkSerial = serial;
     8 }
     9 
    10 unsigned int CNativeOwner::GetMarkSerial()
    11 {
    12 	return m_nMarkSerial;
    13 }
    14 
    15 void CNativeOwner::AddDependent(CPlugin *pPlugin)
    16 {
    17 	m_Dependents.push_back(pPlugin);
    18 }
    19 
    20 void CNativeOwner::AddWeakRef(const WeakNative & ref)
    21 {
    22 	m_WeakRefs.push_back(ref);
    23 }
    24 
    25 void CNativeOwner::AddNatives(const sp_nativeinfo_t *natives)
    26 {
    27 	NativeEntry *pEntry;
    28 
    29 	for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
    30 	{
    31 		if ((pEntry = g_ShareSys.AddNativeToCache(this, &natives[i])) == NULL)
    32 		{
    33 			continue;
    34 		}
    35 
    36 		m_Natives.push_back(pEntry);
    37 	}
    38 }
    39 
    40 void CNativeOwner::PropogateMarkSerial(unsigned int serial)
    41 {
    42 	CNativeOwner *pOwner;
    43 	List<CPlugin *>::iterator iter;
    44 
    45 	for (iter = m_Dependents.begin();
    46 		 iter != m_Dependents.end();
    47 		 iter++)
    48 	{
    49 		pOwner = (*iter);
    50 		pOwner->SetMarkSerial(serial);
    51 	}
    52 }
    53 
    54 void CNativeOwner::UnbindWeakRef(const WeakNative & ref)
    55 {
    56 	sp_native_t *native;
    57 	IPluginContext *pContext;
    58 
    59 	pContext = ref.pl->GetBaseContext();
    60 	if ((pContext->GetNativeByIndex(ref.idx, &native)) == SP_ERROR_NONE)
    61 	{
    62 		/* If there is no reference, the native must be unbound */
    63 		if (ref.entry == NULL)
    64 		{
    65 			native->status = SP_NATIVE_UNBOUND;
    66 			native->pfn = NULL;
    67 		}
    68 		/* If we've cached a reference, it's a core native we can 
    69 		 * rebind back to its original (this was a replacement).
    70 		 */
    71 		else
    72 		{
    73 			native->pfn = ref.entry->func;
    74 		}
    75 	}
    76 }
    77 
    78 void CNativeOwner::DropEverything()
    79 {
    80 	NativeEntry *pEntry;
    81 	List<WeakNative>::iterator iter;
    82 	List<NativeEntry *>::iterator ntv_iter;
    83 
    84 	/* Unbind and remove all weak references to us */
    85 	iter = m_WeakRefs.begin();
    86 	while (iter != m_WeakRefs.end())
    87 	{
    88 		UnbindWeakRef((*iter));
    89 		iter = m_WeakRefs.erase(iter);
    90 	}
    91 
    92 	/* Unmark our replacement natives */
    93 	ntv_iter = m_ReplacedNatives.begin();
    94 	while (ntv_iter != m_ReplacedNatives.end())
    95 	{
    96 		pEntry = (*ntv_iter);
    97 		pEntry->replacement.func = NULL;
    98 		pEntry->replacement.owner = NULL;
    99 		ntv_iter = m_ReplacedNatives.erase(ntv_iter);
   100 	}
   101 
   102 	/* Strip all of our natives from the cache */
   103 	ntv_iter = m_Natives.begin();
   104 	while (ntv_iter != m_Natives.end())
   105 	{
   106 		g_ShareSys.ClearNativeFromCache(this, (*ntv_iter)->name);
   107 		ntv_iter = m_Natives.erase(ntv_iter);
   108 	}
   109 }
   110 
   111 void CNativeOwner::DropWeakRefsTo(CPlugin *pPlugin)
   112 {
   113 	List<WeakNative>::iterator iter;
   114 
   115 	iter = m_WeakRefs.begin();
   116 	while (iter != m_WeakRefs.end())
   117 	{
   118 		WeakNative & ref = (*iter);
   119 
   120 		if (ref.pl == pPlugin)
   121 		{
   122 			iter = m_WeakRefs.erase(iter);
   123 		}
   124 		else
   125 		{
   126 			iter++;
   127 		}
   128 	}
   129 }
   130 
   131 void CNativeOwner::DropRefsTo(CPlugin *pPlugin)
   132 {
   133 	m_Dependents.remove(pPlugin);
   134 	DropWeakRefsTo(pPlugin);
   135 }
   136 
   137 void CNativeOwner::AddReplacedNative(NativeEntry *pEntry)
   138 {
   139 	m_ReplacedNatives.push_back(pEntry);
   140 }