Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
functions.h
Go to the documentation of this file.
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library
2  Copyright (C) 2013 Jonathan Pelletier, New Paradigm Software
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef __cplusplus
20  #pragma message "C++ support is required for extras module"
21  #include "mcr/err.h"
22 #endif
23 
28 #ifndef MCR_EXTRAS_FUNCTIONS_H_
29 #define MCR_EXTRAS_FUNCTIONS_H_
30 
31 #include "mcr/base/base.h"
32 
33 namespace mcr
34 {
35 template<typename T>
36 T &reference(void *pointer)
37 {
38  mcr_dassert(pointer);
39  return *reinterpret_cast<T *>(pointer);
40 }
41 
43 template<typename rT, typename vT>
44 rT *offset(vT *valuePt)
45 {
46  rT *r = nullptr;
47  size_t diff = ((char *)&r->self) - ((char *)r);
48  return reinterpret_cast<rT *>(((char *)valuePt) - diff);
49 }
50 
52 template<typename rT, typename vT>
53 const rT *offset(const vT *valuePt)
54 {
55  rT *r = nullptr;
56  size_t diff = ((const char *)&r->self) - ((const char *)r);
57  return reinterpret_cast<const rT *>(((const char *)valuePt) - diff);
58 }
59 
61 template<typename T>
63 {
64  return new T();
65 }
66 
68 template<typename T>
69 void deleteDataMember(void *dataPt)
70 {
71  if (dataPt)
72  delete reinterpret_cast<T *>(dataPt);
73 }
74 
76 template<typename T>
77 void resetDataMember(void *dataPt)
78 {
79  if (dataPt)
80  reference<T>(dataPt) = T();
81 }
82 
84 template<typename T>
86 {
87  return (mcr_DataMember) {
88  .data = new T(), .deallocate = &deleteDataMember<T>
89  };
90 }
91 
92 extern MCR_API void throwError(const char *lineString, int errorNumber);
93 }
94 
95 #endif
mcr_DataMember doufu()
Definition: functions.h:85
void deleteDataMember(void *dataPt)
Definition: functions.h:69
Raise a compiler error. Usage: #include "mcr/err.h"
void * newDataMember()
Definition: functions.h:62
Libmacro, by Jonathan Pelletier, New Paradigm Software. Alpha version.
Definition: classes.h:31
Base module implements mcr_base - Libmacro first module.
void resetDataMember(void *dataPt)
Definition: functions.h:77
rT * offset(vT *valuePt)
Definition: functions.h:44
#define mcr_dassert(expression)
Definition: defines.h:298