-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathStruct.h
83 lines (67 loc) · 2.32 KB
/
Struct.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2014-12-22
* Update : 2018-1-8
* Author : scott.cgi
*/
#ifndef STRUCT_H
#define STRUCT_H
#include <stddef.h>
/*
Examples:
struct Parent
{
int arr[1];
int value;
int* ptr;
}
Parent parent[1];
output true:
AStruct_GetParentWithName(parent->arr, Parent, arr) == parent
AStruct_GetParentWithName(&parent->arr, Parent, arr) == parent
AStruct_GetParentWithName(&parent->value, Parent, value) == parent
AStruct_GetParentWithName(&parent->ptr, Parent, ptr) == parent
arr = parent->arr
value = parent->value
ptr = parent->ptr
valuePtr = &parent->value
ptrPtr = &parent->ptr
output true:
AStruct_GetParent(arr, Parent) == parent
AStruct_GetParent(&arr, Parent) == parent
AStruct_GetParent(valuePtr, Parent) == parent
AStruct_GetParent(ptrPtr, Parent) == parent
output false:
AStruct_GetParent(value, Parent) == parent
AStruct_GetParent(ptr, Parent) == parent
AStruct_GetParent(&value, Parent) == parent
AStruct_GetParent(&ptr, Parent) == parent
*/
/**
* Get struct pointer from member pointer.
* this for memberPtr same as memberName.
*
* memberPtr: the pointer that point struct's member.
* it's the member address offset from struct address.
*
*/
#define AStruct_GetParent(memberPtr, ParentType) \
((ParentType*) ((char*) (memberPtr) - offsetof(ParentType, memberPtr)))
/**
* Get struct pointer from member pointer with memberName.
* this for memberPtr not same as memberName.
*
* memberPtr: the pointer that point struct's member.
* it's the member address offset from struct address.
*/
#define AStruct_GetParentWithName(memberPtr, ParentType, memberName) \
((ParentType*) ((char*) (memberPtr) - offsetof(ParentType, memberName)))
#endif