Debian Codegraph @

RSPiX/Src/ORANGE/DynaLink/dynalink.h

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 RWS Inc, All Rights Reserved
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of version 2 of the GNU General Public License as published by
7 // the Free Software Foundation
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 //
18 //////////////////////////////////////////////////////////////////////////////
19 //
20 // dynalink.h
21 //
22 // History:
23 // 01/10/96 JMI Started.
24 //
25 //////////////////////////////////////////////////////////////////////////////
26 //
27 // This object implements linking at run time. We do this by keeping an array
28 // of function pointers in a class whose constructor fills an array element
29 // with a function pointer. We are guaranteed to be able to access the
30 // function safely (i.e., we know it's already allocated) b/c it is a static
31 // member of the constructing class.
32 //
33 // Use the macros below to instantiate your class and to provide easy user
34 // implementation.
35 //
36 //////////////////////////////////////////////////////////////////////////////
37 #ifndef DYNALINK_H
38 #define DYNALINK_H
39
40 //////////////////////////////////////////////////////////////////////////////
41 // Includes.
42 //////////////////////////////////////////////////////////////////////////////
43 #include "common/bdebug.h"
44
45 //////////////////////////////////////////////////////////////////////////////
46 // Macros.
47 //////////////////////////////////////////////////////////////////////////////
48 // Use this macro to instantiate your class and its array of function ptrs.
49 // FunctionTypedef is a typedef of a pointer to your function type.
50 // FriendClass is a class to befriend this one (allowing it, and only it,
51 // to access the function array.
52 // MaxElements is the maximum number of functions in your array list. It must
53 // be 1 or greater.
54 #define LINKINSTANTIATE(FunctionTypedef, FriendClass, MaxElements) \
55 FunctionTypedef \
56 CDynaLink<FunctionTypedef, FriendClass, MaxElements>::ms_afp[MaxElements] \
57 = { NULL, }
58
59 // Use this macro to create a macro for your users to use to link.
60 // You can use this function so you don't have to remember any template
61 // declaration crap. Create a macro in your header for your users that calls
62 // LINKLATE similar to the following:
63 // #define LINKME(pUserFunc, lUserFuncIndex) \
64 // LINKLATE([Your Function Typedef], [Your Friend Class], [Max functions], \
65 // pUserFunc, lUserFuncIndex)
66 // FunctionTypedef is a typedef of a pointer to your function type.
67 // FriendClass is a class to befriend this one (allowing it, and only it,
68 // to access the function array.
69 // MaxElements is the maximum number of functions in your array list. It must
70 // be 1 or greater.
71 // pUserFunc is a pointer to the user's function.
72 // lUserFuncIndex is the index in the function array to be filled with
73 // pUserFunc.
74 // Note that dl##FunctionTypedef##lUserFuncIndex creates a name for the
75 // CDynaLink that is specific for each element of the array which means
76 // one module can link more than one function and allows easy identification
77 // of the CDynaLink during compile, link, and debug steps. This means,
78 // however, you cannot put anything for lUserFuncIndex that would make an
79 // invalid variable name (i.e., a + 1, MACRO(ARG), etc. are sux).
80 #define LINKLATE(FunctionTypedef, FriendClass, MaxElements, pUserFunc, lUserFuncIndex) \
81 static CDynaLink<FunctionTypedef, FriendClass, MaxElements> \
82 dl##FunctionTypedef##lUserFuncIndex(pUserFunc, lUserFuncIndex)
83
84 // To access a function from your array.
85 // FunctionTypedef is a typedef of a pointer to your function type.
86 // FriendClass is a class to befriend this one (allowing it, and only it,
87 // to access the function array.
88 // MaxElements is the maximum number of functions in your array list. It must
89 // be 1 or greater.
90 // lUserFuncIndex is the index in the function array to be filled with
91 // pUserFunc.
92 // Returns a pointer to the user function.
93 #define GETLINKFUNC(FunctionTypedef, FriendClass, MaxElements, lUserFuncIndex) \
94 (CDynaLink<FunctionTypedef, FriendClass, MaxElements>::ms_afp[lUserFuncIndex])
95
96 //////////////////////////////////////////////////////////////////////////////
97 // Typedefs.
98 //////////////////////////////////////////////////////////////////////////////
99 template <class FUNCTYPEPTR, class FRIEND, int32_t lMax> class CDynaLink
100 {
101 public:
102 // Constructor Especial.
103 CDynaLink(FUNCTYPEPTR pfn, int32_t lIndex)
104 {
105 ASSERT(lIndex < lMax);
106 ASSERT(ms_afp[lIndex] == NULL);
107 ms_afp[lIndex] = pfn;
108 }
109
110 friend FRIEND;
111
112 protected:
113 static FUNCTYPEPTR ms_afp[lMax];
114 };
115
116 #endif // DYNALINK_H