2009-07-21 09:40:48 +00:00
|
|
|
/* GSInternal
|
|
|
|
Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02111 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* This file defines macros for managing internal (hidden) instance variables
|
|
|
|
* of a public class so that users of the public class don't need to recompile
|
|
|
|
* their code when the class implementation is changed in new versions of the
|
|
|
|
* library.
|
|
|
|
*
|
2011-02-16 05:49:45 +00:00
|
|
|
* The public class MUST declare its instance variables (after any public
|
|
|
|
* instance variables which are part of the unchanging public API) using
|
|
|
|
* code of the form:
|
2010-02-26 11:04:14 +00:00
|
|
|
* #if GS_NONFRAGILE
|
|
|
|
* # if defined(GS_X_IVARS)
|
2011-02-14 10:57:57 +00:00
|
|
|
* @public GS_X_IVARS;
|
2010-02-26 11:04:14 +00:00
|
|
|
* # endif
|
|
|
|
* #else
|
2011-02-16 05:49:45 +00:00
|
|
|
* @private void *_internal GS_UNUSED_IVAR;
|
2010-02-26 11:04:14 +00:00
|
|
|
* #endif
|
2010-02-26 10:25:35 +00:00
|
|
|
*
|
2011-02-16 05:49:45 +00:00
|
|
|
* In the non fragile case, this means that the public header has nothing
|
|
|
|
* visible, but the ivars defined in GS_X_IVARS are visible within the
|
|
|
|
* implementation.
|
|
|
|
*
|
|
|
|
* In the fragile case, the '_internal' pinter is visible in the public
|
|
|
|
* header, but as an opaque private instance variable, while macros from
|
|
|
|
* this file allow the actual memory to be accessed either as a malloc'ed
|
|
|
|
* structure or as a private class.
|
|
|
|
*
|
2010-02-26 10:25:35 +00:00
|
|
|
* Before including the header file containing the public class declaration,
|
|
|
|
* you must define GS_X_IVARS (where X is the class name) to be the
|
|
|
|
* list of actual instance variable declarations for the class.
|
2009-07-21 09:40:48 +00:00
|
|
|
*
|
|
|
|
* Before including this file, you must define 'GSInternal' to be the name
|
|
|
|
* of your public class with * 'Internal' appended.
|
|
|
|
* eg. if your class is called 'MyClass' then use the following define:
|
|
|
|
* #define GSInternal MyClassInternal
|
|
|
|
*
|
2010-02-26 10:25:35 +00:00
|
|
|
* After including this file you can use the GS_PRIVATE_INTERNAL() macro
|
|
|
|
* to declare the private subclass used to hold real instance variables.
|
|
|
|
* The argument to this macro is the public class name (the GS_X_IVARS
|
|
|
|
* list must also be defined).
|
2009-07-21 09:40:48 +00:00
|
|
|
*
|
|
|
|
* You use GS_CREATE_INTERNAL() in your intialiser to create the object
|
|
|
|
* holding the internal instance variables, and GS_DESTROY_INTERNAL() to
|
2010-02-26 10:25:35 +00:00
|
|
|
* get rid of that object in your -dealloc method.
|
2009-07-21 09:40:48 +00:00
|
|
|
*
|
|
|
|
* Instance variables are referenced using the 'internal->ivar' suntax or
|
|
|
|
* the GSIV(classname,object,ivar) macro.
|
|
|
|
*
|
|
|
|
*/
|
2010-02-26 11:04:14 +00:00
|
|
|
#if !GS_NONFRAGILE
|
2009-07-21 09:40:48 +00:00
|
|
|
|
2010-02-26 10:25:35 +00:00
|
|
|
/* Code for when we don't have non-fragile instance variables
|
2011-02-16 05:49:45 +00:00
|
|
|
* The presence of GS_INTERNAL_STRUCT means that the instance variables are
|
|
|
|
* accessible as a struct rather than as a class.
|
2009-07-21 09:40:48 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-16 05:49:45 +00:00
|
|
|
#if defined(GS_INTERNAL_STRUCT)
|
|
|
|
|
|
|
|
/* Start declaration of internal ivars.
|
|
|
|
*/
|
|
|
|
#define GS_PRIVATE_INTERNAL(name) \
|
|
|
|
typedef struct name ## InternalStruct \
|
|
|
|
{ \
|
|
|
|
GS_##name##_IVARS; \
|
|
|
|
} name ## Internal;
|
|
|
|
|
|
|
|
/* Create holder for internal ivars.
|
|
|
|
*/
|
|
|
|
#define GS_CREATE_INTERNAL(name) \
|
|
|
|
_internal = (void*)NSZoneCalloc([self zone], 1, sizeof(name ## Internal));
|
|
|
|
|
|
|
|
/* Destroy holder for internal ivars.
|
|
|
|
*/
|
|
|
|
#define GS_DESTROY_INTERNAL(name) \
|
|
|
|
if (_internal != 0) { NSZoneFree([self zone], _internal); _internal = 0; }
|
|
|
|
|
|
|
|
#else /* GS_INTERNAL_STRUCT */
|
|
|
|
|
2009-07-21 09:40:48 +00:00
|
|
|
/* Start declaration of internal ivars.
|
|
|
|
*/
|
2010-02-26 10:25:35 +00:00
|
|
|
#define GS_PRIVATE_INTERNAL(name) \
|
2009-07-21 09:40:48 +00:00
|
|
|
@interface name ## Internal : NSObject \
|
|
|
|
{ \
|
2010-02-26 10:25:35 +00:00
|
|
|
@public \
|
2011-02-14 10:57:57 +00:00
|
|
|
GS_##name##_IVARS; \
|
2009-07-21 09:40:48 +00:00
|
|
|
} \
|
|
|
|
@end \
|
|
|
|
@implementation name ## Internal \
|
|
|
|
@end
|
|
|
|
|
|
|
|
/* Create holder for internal ivars.
|
|
|
|
*/
|
|
|
|
#define GS_CREATE_INTERNAL(name) \
|
2011-02-16 05:49:45 +00:00
|
|
|
_internal = (void*)[name ## Internal new];
|
2009-07-21 09:40:48 +00:00
|
|
|
|
2010-02-26 10:25:35 +00:00
|
|
|
/* Destroy holder for internal ivars.
|
2009-07-21 09:40:48 +00:00
|
|
|
*/
|
|
|
|
#define GS_DESTROY_INTERNAL(name) \
|
2011-02-16 05:49:45 +00:00
|
|
|
if (_internal != 0) {[(id)_internal release]; _internal = 0; }
|
|
|
|
|
|
|
|
#endif /* GS_INTERNAL_STRUCT */
|
2009-07-21 09:40:48 +00:00
|
|
|
|
|
|
|
#undef internal
|
|
|
|
#define internal ((GSInternal*)_internal)
|
2009-09-07 16:25:04 +00:00
|
|
|
#undef GSIVar
|
|
|
|
#define GSIVar(X,Y) (((GSInternal*)(X->_internal))->Y)
|
2009-07-21 09:40:48 +00:00
|
|
|
|
2010-02-26 11:04:14 +00:00
|
|
|
#else /* GS_NONFRAGILE */
|
2009-07-21 09:40:48 +00:00
|
|
|
|
2010-02-14 10:48:10 +00:00
|
|
|
/* We have support for non-fragile ivars
|
2009-07-21 09:40:48 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-26 10:25:35 +00:00
|
|
|
#define GS_PRIVATE_INTERNAL(name)
|
2009-07-21 09:40:48 +00:00
|
|
|
|
|
|
|
#define GS_CREATE_INTERNAL(name)
|
|
|
|
|
|
|
|
#define GS_DESTROY_INTERNAL(name)
|
|
|
|
|
|
|
|
/* Define constant to reference internal ivars.
|
|
|
|
*/
|
|
|
|
#undef internal
|
|
|
|
#define internal self
|
|
|
|
#undef GSIVar
|
2009-09-07 16:25:04 +00:00
|
|
|
#define GSIVar(X,Y) ((X)->Y)
|
2009-07-21 09:40:48 +00:00
|
|
|
|
2010-02-26 11:04:14 +00:00
|
|
|
#endif /* GS_NONFRAGILE */
|
2009-07-21 09:40:48 +00:00
|
|
|
|
|
|
|
|