librnr  1.14.5
RoadNarrows Robotics Common Library 1
assoc.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*! \file
3  *
4  * \brief Simple associative map data and operator declarations.
5  *
6  * These associative maps are composed of tables (vectors) of discrete (x, y)
7  * points where x and y may be of any data types.
8  *
9  * The specific Nvp_T Name-Value Pair associative map point provides a
10  * char* <--> int mapping.
11  *
12  * The more general associative mapping is provided by the
13  * AssocMap<<em>x</em>> declarations.
14  *
15  * The general associative maps are configured to support a "poor man's" C
16  * version of class inheritance and templating.
17  *
18  * The "base" set of void* data types and functions are
19  * AssocMapVoid<<em>x</em>> named definitions with data types returning
20  * void*. These definitions are defined in librnr.
21  *
22  * A "derived" associative map can be defined using the following three
23  * \#define's (usually in a header file). Follow these \#defines with the
24  * inclusion of this header file (assoc.h):\n
25  * <dl>
26  * <dt> \ref AMAP_NAME </dt>
27  * <dd>
28  * The associative map derived namespace name. All data types and
29  * function definitions are named:
30  * AssocMap<\ref AMAP_NAME><<em>x</em>>
31  * </dd>
32  * <dt> \ref AMAP_XTYPE </dt>
33  * <dd>
34  * The derived x data type.
35  * </dd>
36  * <dt> \ref AMAP_YTYPE </dt>
37  * <dd>
38  * The the derived y data type.
39  * </dd>
40  * </dl>
41  *
42  * Multiple assoc. maps derived types and the base type may be used in the
43  * same C source.
44  *
45  * \sa
46  * \ref example_assoc under "Related Pages" for an example.
47  *
48  * \pkgsynopsis
49  * RoadNarrows Robotics Common Library 1
50  *
51  * \pkgcomponent{Library}
52  * librnr
53  *
54  * \pkgfile{rnr/assoc.h}
55  *
56  * \author Robin Knight (robin.knight@roadnarrows.com)
57  *
58  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
59  *
60  * \license{MIT}
61  *
62  * \EulaBegin
63  * See the README and EULA files for any copyright and licensing information.
64  * \EulaEnd
65  */
66 ////////////////////////////////////////////////////////////////////////////////
67 
68 #ifndef _RNR_ASSOC_H
69 #include <sys/types.h>
70 #endif // _RNR_ASSOC_H
71 
72 //-----------------------------------------------------------------------------
73 // Base and Derived Associative Controls
74 //-----------------------------------------------------------------------------
75 /*!
76  * Define the associative namespace (must be literal).
77  *
78  * If \ref AMAP_NAME is defined then a new set of assoc. map data types and
79  * functions will be defined, prefaced by AssocMap<\ref AMAP_NAME>.
80  *
81  * If \ref AMAP_NAME is not defined, then the base set prefaced by
82  * AssocMapVoid data types and functions will be used (already defined in
83  * librnr).
84  */
85 #ifndef AMAP_NAME
86 #define AMAP_NAME Void ///< base assoc. map name
87 #undef _AMAP_DERIVED ///< not a "derived" assoc. map
88 #else
89 #define _AMAP_DERIVED ///< derived assoc. map
90 #endif
91 #define _AMAP_NAME AMAP_NAME ///< assoc. map name
92 
93 /*!
94  * Define the derived associative map x and y data types (must be literal).
95  *
96  * \ref AMAP_XTYPE defines the x data type. If not defined, then void* is used.
97  *
98  * \ref AMAP_YTYPE defines the y data type. If not defined, then void* is used.
99  */
100 #ifndef AMAP_XTYPE
101 #define AMAP_XTYPE void * ///< default assoc. map x data type
102 #endif
103 #ifndef AMAP_YTYPE
104 #define AMAP_YTYPE void * ///< default assoc. map y data type
105 #endif
106 #define _AMAP_XTYPE AMAP_XTYPE ///< assoc. map x data type
107 #define _AMAP_YTYPE AMAP_YTYPE ///< assoc. map y data type
108 
109 /*!
110  * Useful string concatenation macros.
111  */
112 #undef _CONCAT
113 #undef _CONCAT_
114 #define _CONCAT_(x, y) x ## y ///< build concatenation operator
115 #define _CONCAT(x, y) _CONCAT_(x, y) ///< now concatenate
116 
117 /*!
118  * \brief AssocMap (base or derived) definition preface
119  */
120 #undef _AMAP_DEFPREFACE
121 #define _AMAP_DEFPREFACE _CONCAT(AssocMap, _AMAP_NAME)
122 
123 /*!
124  * \brief AssocMap (base or derived) definition name
125  */
126 #undef _AMAP_DEF
127 #define _AMAP_DEF(name) _CONCAT(_AMAP_DEFPREFACE, name)
128 
129 
130 //-----------------------------------------------------------------------------
131 // Base Data Types
132 //-----------------------------------------------------------------------------
133 
134 #ifndef _RNR_ASSOC_H
135 
136 #include "rnr/rnrconfig.h"
137 
139 
140 /*!
141  * Simple Name-Value Pair (Nvp) associative map entry structure.
142  *
143  * \par Association:
144  * name <--> value where name is a string and value is an integer.
145  *
146  * \note
147  * Although there are no restrictions to either the names or the values,
148  * be careful about shadowing. The library searches always return the
149  * first match, hiding any potential subsequent matches.
150  */
151 typedef struct
152 {
153  const char *m_sName; ///< null-terminated string name
154  int m_iVal; ///< associated integer value
155 } Nvp_T;
156 
157 
158 /*!
159  * Base associative map data point structure type.
160  *
161  * \par Association:
162  * x <--> y where x,y are of types void*.
163  *
164  * \note
165  * Although there are no restrictions to either the names or the values,
166  * be careful about shadowing. The library searches always return the
167  * first match, hiding any potential subsequent matches.
168  */
169 typedef struct
170 {
171  void *x; ///< the x value associates
172  void *y; ///< with this y value (and vice versa).
174 
175 /*!
176  * \brief base associative map comparator function type
177  */
178 typedef int (*AssocMapVoidCmp_T)(void *t1, void *t2);
179 
180 /*!
181  * Base associative mapper structure type.
182  *
183  * The structure holds an associative map plus operators on that map.
184  */
185 typedef struct
186 {
187  AssocMapVoidPoint_T *m_tblAssocMap; ///< associative map table
188  size_t m_tblSize; ///< number of entries in table
189  AssocMapVoidCmp_T m_opXCmp; ///< return 0 if x1 == x2
190  AssocMapVoidCmp_T m_opYCmp; ///< return 0 if y1 == y2
191  AssocMapVoidPoint_T *m_pMapDft; ///< default x, y point
193 
195 
196 #endif // _RNR_ASSOC_H
197 
198 
199 //-----------------------------------------------------------------------------
200 // Derived Data Types
201 //-----------------------------------------------------------------------------
202 
203 #ifdef _AMAP_DERIVED
204 
206 
207 /*!
208  * Derived associative map data point structure type.
209  *
210  * \par Association:
211  * x <--> y where x is of type AMAP_XTYPE and y is of type AMAP_YTYPE.
212  *
213  * \par Name:
214  * AssocMap<\ref AMAP_NAME>Point_T
215  */
216 typedef struct
217 {
218  _AMAP_XTYPE x; ///< the x value associates
219  _AMAP_YTYPE y; ///< with this y value (and vice versa).
220 } _AMAP_DEF(Point_T);
221 
222 /*!
223  * \brief Derived x and y comparator callback function types.
224  *
225  * \par Name:
226  * AssocMap<\ref AMAP_NAME>XCmp_T()\n
227  * AssocMap<\ref AMAP_NAME>YCmp_T()
228  */
229 typedef int (*_AMAP_DEF(XCmp_T))(const _AMAP_XTYPE, const _AMAP_XTYPE);
230 typedef int (*_AMAP_DEF(YCmp_T))(const _AMAP_YTYPE, const _AMAP_YTYPE);
231 
232 /*!
233  * Derived associative mapper structure type.
234  *
235  * The structure holds an associative map plus operators on that map.
236  *
237  * \par Name:
238  * AssocMap<\ref AMAP_NAME>Mapper_T\n
239  */
240 typedef struct
241 {
242  _AMAP_DEF(Point_T) *m_tblAssocMap; ///< associative map table
243  size_t m_tblSize; ///< number of entries in table
244  _AMAP_DEF(XCmp_T) m_opXCmp; ///< return 0 if x1 == x2
245  _AMAP_DEF(YCmp_T) m_opYCmp; ///< return 0 if y1 == y2
246  _AMAP_DEF(Point_T) *m_pMapDft; ///< default x, y point
247 } _AMAP_DEF(Mapper_T);
248 
250 
251 #endif // _AMAP_DERIVED
252 
253 
254 //-----------------------------------------------------------------------------
255 // Base Prototypes
256 //-----------------------------------------------------------------------------
257 
258 #ifndef _RNR_ASSOC_H
259 
261 
262 //
263 // Prototypes
264 //
265 extern int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName);
266 
267 extern const char *NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal);
268 
269 extern void *AssocMapVoidXtoY(AssocMapVoidMapper_T *pMapper, void *px);
270 
271 extern void *AssocMapVoidYtoX(AssocMapVoidMapper_T *pMapper, void *py);
272 
274 
275 #endif // _RNR_ASSOC_H
276 
277 
278 //-----------------------------------------------------------------------------
279 // Derived Extensions
280 //-----------------------------------------------------------------------------
281 
282 #ifdef _AMAP_DERIVED
283 
285 
286 /*!
287  * \brief Derived: Get the y value associated with the given x value.
288  *
289  * The table search terminates at the first match or at the end of
290  * the table.
291  *
292  * \par Name:
293  * AssocMap<\ref AMAP_NAME>XtoY()
294  *
295  * \param pMapper Derived associative map mapper container.
296  * \param x Derived x value in table to find.
297  *
298  * \return
299  * The associated derived y value on success, or the default y value if no
300  * x value is found.
301  */
302 static inline _AMAP_YTYPE _AMAP_DEF(XtoY)(_AMAP_DEF(Mapper_T) *pMapper,
303  _AMAP_XTYPE x)
304 {
306  (void *)x);
307 }
308 
309 /*!
310  * \brief Derived: Get the x value associated with the given y value.
311  *
312  * The table search terminates at the first match or at the end of
313  * the table.
314  *
315  * \par Name:
316  * AssocMap<\ref AMAP_NAME>YtoX()
317  *
318  * \param pMapper Derived associative map mapper container.
319  * \param y Derived y value in table to find.
320  *
321  * \return
322  * The associated derived x value on success, or the default x value if no
323  * y value is found.
324  */
325 static inline _AMAP_XTYPE _AMAP_DEF(YtoX)(_AMAP_DEF(Mapper_T) *pMapper,
326  _AMAP_YTYPE y)
327 {
329  (void *)y);
330 }
331 
333 
334 #endif // _AMAP_DERIVED
335 
336 
337 //-----------------------------------------------------------------------------
338 // Fix Up Defines
339 //-----------------------------------------------------------------------------
340 
341 /*!
342  * Undefine macros to allow redefinition.
343  */
344 #undef AMAP_NAME
345 #undef AMAP_XTYPE
346 #undef AMAP_YTYPE
347 #undef _AMAP_DERIVED
348 #undef _AMAP_NAME
349 #undef _AMAP_XTYPE
350 #undef _AMAP_YTYPE
351 
352 #define _RNR_ASSOC_H ///< include base declaration only once
const char * NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal)
Get the name associated with the value.
Definition: assoc.c:94
AssocMapVoidPoint_T * m_pMapDft
default x, y point
Definition: assoc.h:191
#define _AMAP_XTYPE
assoc. map x data type
Definition: assoc.h:106
size_t m_tblSize
number of entries in table
Definition: assoc.h:188
const char * m_sName
null-terminated string name
Definition: assoc.h:153
void * AssocMapVoidXtoY(AssocMapVoidMapper_T *pMapper, void *px)
Get the y value associated with the given x value.
Definition: assoc.c:123
AssocMapVoidPoint_T * m_tblAssocMap
associative map table
Definition: assoc.h:187
Definition: assoc.h:151
int(* AssocMapVoidCmp_T)(void *t1, void *t2)
base associative map comparator function type
Definition: assoc.h:178
#define C_DECLS_BEGIN
C declaration block begin in C.
Definition: rnrconfig.h:71
int m_iVal
associated integer value
Definition: assoc.h:154
RoadNarrows Robotics common configuration file.
void * y
with this y value (and vice versa).
Definition: assoc.h:172
#define C_DECLS_END
C declaration block end in C.
Definition: rnrconfig.h:72
void * x
the x value associates
Definition: assoc.h:171
#define _AMAP_YTYPE
assoc. map y data type
Definition: assoc.h:107
void * AssocMapVoidYtoX(AssocMapVoidMapper_T *pMapper, void *py)
Get the x value associated with the given y value.
Definition: assoc.c:159
AssocMapVoidCmp_T m_opYCmp
return 0 if y1 == y2
Definition: assoc.h:190
AssocMapVoidCmp_T m_opXCmp
return 0 if x1 == x2
Definition: assoc.h:189
#define _AMAP_DEF(name)
AssocMap (base or derived) definition name.
Definition: assoc.h:127
int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName)
Get the value associated with the name.
Definition: assoc.c:64