src/common.h

changeset 539
72ad83a67165
parent 525
4f6e46a3bfc3
child 540
0334789cb4d7
equal deleted inserted replaced
538:2f85d4d286e5 539:72ad83a67165
21 // Stuff defined and included here is universally included. 21 // Stuff defined and included here is universally included.
22 22
23 #ifndef LDFORGE_COMMON_H 23 #ifndef LDFORGE_COMMON_H
24 #define LDFORGE_COMMON_H 24 #define LDFORGE_COMMON_H
25 25
26 // Hack to make KDevelop parse QString properly. Q_REQUIRED_RESULT expands into
27 // an __attribute__((warn_unused_result)) KDevelop's lexer doesn't seem to
28 // understand, yielding an error and leaving some methods unlexed.
29 //
30 // The following first includes <QChar> to get Q_REQUIRED_RESULT defined first,
31 // then re-defining it as nothing. This causes Q_REQUIRED_RESULT to essentially
32 // "vanish" from QString's methods when KDevelop lexes them.
33 //
34 // Similar reasoning for Q_DECL_HIDDEN, except with Q_OBJECT this time.
35 #ifdef IN_IDE_PARSER
36 # include <QChar>
37 # undef Q_REQUIRED_RESULT
38 # undef Q_DECL_HIDDEN
39 # define Q_REQUIRED_RESULT
40 # define Q_DECL_HIDDEN
41 #endif
42
26 #include <stdio.h> 43 #include <stdio.h>
27 #include <stdlib.h> 44 #include <stdlib.h>
28 #include <stdint.h> 45 #include <stdint.h>
29 #include <stdarg.h> 46 #include <stdarg.h>
30 #include <QString> 47 #include <QString>
69 #else // WIN32 86 #else // WIN32
70 # define DIRSLASH "/" 87 # define DIRSLASH "/"
71 # define DIRSLASH_CHAR '/' 88 # define DIRSLASH_CHAR '/'
72 #endif // WIN32 89 #endif // WIN32
73 90
74 #define PROP_NAME(GET) m_##GET 91 #define PROPERTY( ACCESS, TYPE, NAME, OPS, CALLBACK ) \
75 92 private: \
76 #define READ_ACCESSOR(T, GET) \ 93 TYPE m_##NAME; \
77 T const& GET() const 94 DEFINE_##CALLBACK( NAME ) \
78 95 \
79 #define SET_ACCESSOR(T, SET) \ 96 public: \
80 void SET (T val) 97 inline TYPE const& GET_READ_METHOD( NAME, OPS ) const \
81 98 { return m_##NAME; \
82 // Read-only, private property with a get accessor 99 } \
83 #define DECLARE_READ_PROPERTY(T, GET, SET) \ 100 \
84 private: \ 101 ACCESS: \
85 T PROP_NAME (GET); \ 102 inline void set##NAME( TYPE const& NAME##_ ) \
86 SET_ACCESSOR (T, SET); \ 103 { m_##NAME = NAME##_; \
87 public: \ 104 TRIGGER_CALLBACK( NAME, CALLBACK ) \
88 READ_ACCESSOR (T, GET); 105 } \
89 106 \
90 // Read/write private property with get and set accessors 107 DEFINE_PROPERTY_##OPS( TYPE, NAME, CALLBACK )
91 #define DECLARE_PROPERTY(T, GET, SET) \ 108
92 private: \ 109 #define GET_READ_METHOD( NAME, OPS ) \
93 T PROP_NAME (GET); \ 110 GET_READ_METHOD_##OPS( NAME )
94 public: \ 111
95 READ_ACCESSOR (T, GET); \ 112 #define GET_READ_METHOD_BOOL_OPS( NAME ) is##NAME()
96 SET_ACCESSOR (T, SET); 113 #define GET_READ_METHOD_NO_OPS( NAME ) get##NAME()
97 114 #define GET_READ_METHOD_STR_OPS( NAME ) get##NAME()
98 #define DEFINE_PROPERTY(T, CLASS, GET, SET) \ 115 #define GET_READ_METHOD_NUM_OPS( NAME ) get##NAME()
99 READ_ACCESSOR (T, CLASS::GET) { return PROP_NAME (GET); } \ 116
100 SET_ACCESSOR (T, CLASS::SET) { PROP_NAME (GET) = val; } 117 #define DEFINE_WITH_CB( NAME ) void NAME##Changed();
101 118 #define DEFINE_NO_CB( NAME )
102 // Shortcuts 119
103 #define PROPERTY(T, GET, SET) \ 120 #define DEFINE_PROPERTY_NO_OPS( TYPE, NAME, CALLBACK )
104 private: \ 121
105 T PROP_NAME (GET); \ 122 #define DEFINE_PROPERTY_STR_OPS( TYPE, NAME, CALLBACK ) \
106 public: \ 123 void append##NAME( TYPE a ) \
107 READ_ACCESSOR (T, GET) { return PROP_NAME (GET); } \ 124 { m_##NAME.append( a ); \
108 SET_ACCESSOR (T, SET) { PROP_NAME (GET) = val; } 125 TRIGGER_CALLBACK( NAME, CALLBACK ) \
109 126 } \
110 #define READ_PROPERTY(T, GET, SET) \ 127 \
111 private: \ 128 void prepend##NAME( TYPE a ) \
112 T PROP_NAME (GET); \ 129 { m_##NAME.prepend( a ); \
113 SET_ACCESSOR (T, SET) { PROP_NAME (GET) = val; } \ 130 TRIGGER_CALLBACK( NAME, CALLBACK ) \
114 public: \ 131 } \
115 READ_ACCESSOR (T, GET) { return PROP_NAME (GET); } 132 \
116 133 void replaceIn##NAME( TYPE a, TYPE b ) \
117 // Property whose set accessor is a public slot 134 { m_##NAME.replace( a, b ); \
118 // TODO: make this replace PROPERTY 135 TRIGGER_CALLBACK( NAME, CALLBACK ) \
119 #define SLOT_PROPERTY (T, GET, SET) \ 136 }
120 private: \ 137
121 T PROP_NAME (GET); \ 138 #define DEFINE_PROPERTY_NUM_OPS( TYPE, NAME, CALLBACK ) \
122 public: \ 139 inline void increase##NAME( TYPE a = 1 ) \
123 READ_ACCESSOR (T, GET) { return PROP_NAME (GET); } \ 140 { m_##NAME += a; \
124 public slots: \ 141 TRIGGER_CALLBACK( NAME, CALLBACK ) \
125 SET_ACCESSOR (T, SET) { PROP_NAME (GET) = val; } 142 } \
143 \
144 inline void decrease##NAME( TYPE a = 1 ) \
145 { m_##NAME -= a; \
146 TRIGGER_CALLBACK( NAME, CALLBACK ) \
147 }
148
149 #define DEFINE_PROPERTY_BOOL_OPS( TYPE, NAME, CALLBACK ) \
150 inline void toggle##NAME() \
151 { m_##NAME = !m_##NAME; \
152 TRIGGER_CALLBACK( NAME, CALLBACK ) \
153 }
154
155 #define TRIGGER_CALLBACK( NAME, CALLBACK ) \
156 TRIGGER_CALLBACK_##CALLBACK( NAME );
157
158 #define TRIGGER_CALLBACK_NO_CB( NAME )
159 #define TRIGGER_CALLBACK_WITH_CB( NAME ) \
160 NAME##Changed();
126 161
127 #ifdef null 162 #ifdef null
128 #undef null 163 #undef null
129 #endif // null 164 #endif // null
130 165

mercurial