sourcehook/sourcehook_impl_cvfnptr.h
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 07 22:45:22 2007 +0000 (2007-10-07)
changeset 384 264cab8ff2cc
parent 381 8eded451c936
child 418 109309ff6350
permissions -rw-r--r--
correct version now... i guess i'm just tired :)
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2007 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko
     8 * ============================
     9 */
    10 
    11 #ifndef __SOURCEHOOK_IMPL_CVFNPTR_H__
    12 #define __SOURCEHOOK_IMPL_CVFNPTR_H__
    13 
    14 #include "sh_list.h"
    15 
    16 namespace SourceHook
    17 {
    18 	namespace Impl
    19 	{
    20 		class CVfnPtr
    21 		{
    22 			// *** Data ***
    23 			void *m_Ptr;
    24 			void *m_OrigEntry;
    25 
    26 			List<CIface> m_IfaceList;
    27 		public:
    28 			// *** Descriptor ***
    29 			typedef void* Descriptor;
    30 
    31 			// *** Interface ***
    32 			inline CVfnPtr(void *ptr);
    33 			inline bool operator==(const Descriptor &other);
    34 			inline void *GetPtr() const;
    35 			inline void *GetOrigEntry() const;
    36 			inline List<CIface> &GetIfaceList();
    37 			inline const List<CIface> &GetIfaceList() const;
    38 			inline CIface *FindIface(void *iface);
    39 			CIface &GetIface(void *iface);
    40 			bool Patch(void *newValue);
    41 			inline bool Revert();
    42 		};
    43 
    44 		// *** Implementation ***
    45 		inline CVfnPtr::CVfnPtr(void *ptr)
    46 			: m_Ptr(ptr), m_OrigEntry(*reinterpret_cast<void**>(m_Ptr))
    47 		{
    48 		}
    49 
    50 		inline bool CVfnPtr::operator==(const Descriptor &other)
    51 		{
    52 			return m_Ptr == other;
    53 		}
    54 
    55 		inline void *CVfnPtr::GetPtr() const
    56 		{
    57 			return m_Ptr;
    58 		}
    59 
    60 		inline void *CVfnPtr::GetOrigEntry() const
    61 		{
    62 			return m_OrigEntry;
    63 		}
    64 
    65 		inline List<CIface> &CVfnPtr::GetIfaceList()
    66 		{
    67 			return m_IfaceList;
    68 		}
    69 
    70 		inline const List<CIface> &CVfnPtr::GetIfaceList() const
    71 		{
    72 			return m_IfaceList;
    73 		}
    74 
    75 		inline bool CVfnPtr::Revert()
    76 		{
    77 			return Patch(m_OrigEntry);
    78 		}
    79 
    80 		inline CIface *CVfnPtr::FindIface(void *iface)
    81 		{
    82 			List<CIface>::iterator iter = m_IfaceList.find(iface);
    83 			if (iter == m_IfaceList.end())
    84 				return NULL;
    85 			else
    86 				return &(*iter);
    87 		}
    88 	}
    89 }
    90 
    91 #endif
    92