coan 4.2.4
|
00001 #ifndef STATE_UTILS_H 00002 #define STATE_UTILS_H 00003 /*************************************************************************** 00004 * Copyright (C) 2004, 2006 Symbian Software Ltd. * 00005 * All rights reserved. * 00006 * Copyright (C) 2007-2011 Mike Kinghan, imk@strudl.org * 00007 * All rights reserved. * 00008 * * 00009 * Contributed originally by Mike Kinghan, imk@strudl.org * 00010 * * 00011 * Redistribution and use in source and binary forms, with or without * 00012 * modification, are permitted provided that the following conditions * 00013 * are met: * 00014 * * 00015 * Redistributions of source code must retain the above copyright * 00016 * notice, this list of conditions and the following disclaimer. * 00017 * * 00018 * Redistributions in binary form must reproduce the above copyright * 00019 * notice, this list of conditions and the following disclaimer in the * 00020 * documentation and/or other materials provided with the distribution. * 00021 * * 00022 * Neither the name of Symbian Software Ltd. nor the names of its * 00023 * contributors may be used to endorse or promote products derived from * 00024 * this software without specific prior written permission. * 00025 * * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * 00027 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * 00028 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * 00029 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * 00030 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * 00031 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * 00032 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * 00033 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * 00034 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,* 00035 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * 00036 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 00037 * DAMAGE. * 00038 * * 00039 **************************************************************************/ 00040 #include "bool.h" 00041 #include <stdlib.h> 00042 #include <assert.h> 00043 #include <string.h> 00044 00056 00057 #ifndef STATE_ALLOCATOR 00058 00059 #define STATE_ALLOCATOR (malloc) 00060 #endif 00061 00063 #define STATE(component) component##_state 00064 00065 #define PUBLIC_STATE(component) component##_public_state 00066 00067 #define HANDLE(component) component##_h 00068 00069 #define PUBLIC_HANDLE(component) component##_public_h 00070 00071 #define USER_INIT(component) component##_init 00072 00073 #define USER_FINIS(component) component##_finis 00074 00075 #define USER_INIT_HANDLE(component) component##_init_h 00076 00077 #define USER_FINIS_HANDLE(component) component##_finis_h 00078 00079 #define INITOR(component) component##_initor 00080 00081 #define FINITOR(component) component##_finitor 00082 00083 #define STATIC_INITIALISER(component) component##_static_initialiser 00084 00085 #define STATIC_INITIALISER_HANDLE(component) component##_static_initialiser_h 00086 00087 #define STORAGE_TYPE_TAG(component) component##_storage_type 00088 00089 #define INITIALISATION_TYPE_TAG(component) component##_initialisation_type 00090 00091 #define DECL_USER_INIT(component)\ 00092 void USER_INIT(component)(STATE_T(component) *) 00093 00094 #define DECL_USER_FINIS(component)\ 00095 void USER_FINIS(component)(STATE_T(component) *) 00096 00097 #define DECL_USER_INIT_HANDLE(component)\ 00098 void (*USER_INIT_HANDLE(component))(STATE_T(component) *) 00099 00100 #define DECL_USER_FINIS_HANDLE(component)\ 00101 void (*USER_FINIS_HANDLE(component))(STATE_T(component) *) 00102 00103 #define DECL_INITOR(component) void INITOR(component)(void) 00104 00105 #define DECL_FINITOR(component) void FINITOR(component)(void) 00106 00107 #define DECL_STATIC_INITIALISER(component)\ 00108 static const STATE_T(component) STATIC_INITIALISER(component) 00109 00110 #define DECL_STATIC_INITIALISER_HANDLE(component)\ 00111 static const STATE_T(component) * const STATIC_INITIALISER_HANDLE(component) 00112 00113 #define SELECT_STATIC(component)\ 00114 enum component##_storage_type { STORAGE_TYPE_TAG(component) = STATE_STATIC } 00115 00116 #define SELECT_DYNAMIC(component)\ 00117 enum component##_storage_type { STORAGE_TYPE_TAG(component) = STATE_DYNAMIC } 00118 00119 #define SELECT_INITIALISATION_TYPE(component,init_type)\ 00120 enum component##_init_type { INITIALISATION_TYPE_TAG(component) = init_type } 00121 00122 #define SELECT_ZERO_INITABLE(component)\ 00123 SELECT_INITIALISATION_TYPE(component,ZERO_INITABLE) 00124 00125 #define SELECT_STATIC_INITABLE(component)\ 00126 SELECT_INITIALISATION_TYPE(component,STATIC_INITABLE) 00127 00128 #define SELECT_USER_INITABLE(component)\ 00129 SELECT_INITIALISATION_TYPE(component,USER_INITABLE) 00130 00131 #define IS_STATIC(component) (STORAGE_TYPE_TAG(component) == (int)STATE_STATIC) 00132 00133 #define IS_ZERO_INITABLE(component)\ 00134 (INITIALISATION_TYPE_TAG(component) == (int)ZERO_INITABLE) 00135 00136 #define HAS_STATIC_INITIALISER(component)\ 00137 (INITIALISATION_TYPE_TAG(component) == STATIC_INITABLE) 00138 00139 #define HAS_USER_INIT(component) (INITIALISATION_TYPE_TAG(component) == (int)USER_INITABLE) 00140 00143 #define DECL_DYNAMIC_STATE(component)\ 00144 STATE_T(component) * HANDLE(component) = NULL;\ 00145 PUBLIC_STATE_T(component) * PUBLIC_HANDLE(component) = NULL 00146 00150 #define DECL_STATIC_STATE(component)\ 00151 static STATE_T(component) STATE(component);\ 00152 STATE_T(component) * HANDLE(component) = &STATE(component);\ 00153 PUBLIC_STATE_T(component) * PUBLIC_HANDLE(component) =\ 00154 (PUBLIC_STATE_T(component) *)&STATE(component) 00155 00157 #define IMPORT_INITOR(component) extern DECL_INITOR(component) 00158 00159 #define IMPORT_FINITOR(component) extern DECL_FINITOR(component) 00160 00162 #define IMPORT_STATE(component)\ 00163 extern PUBLIC_STATE_T(component) * PUBLIC_HANDLE(component) 00164 00168 #define GET_STATE_BY_HANDLE(component,field)\ 00169 (assert(true),HANDLE(component)->field) 00170 00174 #define SET_STATE_BY_HANDLE(component,field) (HANDLE(component)->field) 00175 00179 #define GET_STATIC_STATE(component,field) (assert(true),STATE(component).field) 00180 00184 #define SET_STATIC_STATE(component,field) (STATE(component).field) 00185 00187 #define DEFINE_INITOR(component)\ 00188 DECL_INITOR(component) {\ 00189 component_initor(\ 00190 IS_STATIC(component),\ 00191 IS_ZERO_INITABLE(component),\ 00192 (void **)(char *)&HANDLE(component),\ 00193 (void **)(char *)&PUBLIC_HANDLE(component),\ 00194 STATIC_INITIALISER_HANDLE(component),\ 00195 sizeof(STATE_T(component)),\ 00196 USER_INIT_HANDLE(component));\ 00197 } 00198 00200 #define DEFINE_FINITOR(component)\ 00201 DECL_FINITOR(component) {\ 00202 component_finitor(\ 00203 IS_STATIC(component),\ 00204 (void **)(char *)&HANDLE(component),\ 00205 (void **)(char *)&PUBLIC_HANDLE(component),\ 00206 USER_INIT_HANDLE(component));\ 00207 } 00208 00235 extern void 00236 component_initor( 00237 bool is_static, 00238 bool is_zero_initable, 00239 void **state, 00240 void **public_state, 00241 void const *initialiser, 00242 size_t size, 00243 void (*user_init)()); 00244 00264 extern void 00265 component_finitor( 00266 bool is_static, 00267 void **state, 00268 void **public_state, 00269 void (*user_finis)()); 00270 00606 00608 enum component_storage_type { 00609 STATE_STATIC, 00611 STATE_DYNAMIC 00613 }; 00614 00616 enum component_initialisation_type { 00617 ZERO_INITABLE, 00619 STATIC_INITABLE, 00621 USER_INITABLE 00624 }; 00625 00626 00634 #define IMPLEMENT_STATIC(component,init_type)\ 00635 DECL_STATIC_STATE(component);\ 00636 SELECT_STATIC(component);\ 00637 SELECT_INITIALISATION_TYPE(component,init_type);\ 00638 DECL_STATIC_INITIALISER_HANDLE(component);\ 00639 DECL_USER_INIT_HANDLE(component);\ 00640 DECL_USER_FINIS_HANDLE(component);\ 00641 DEFINE_INITOR(component)\ 00642 DEFINE_FINITOR(component) 00643 00651 #define IMPLEMENT_DYNAMIC(component,init_type)\ 00652 DECL_DYNAMIC_STATE(component);\ 00653 SELECT_DYNAMIC(component);\ 00654 SELECT_INITIALISATION_TYPE(component,init_type);\ 00655 DECL_STATIC_INITIALISER_HANDLE(component);\ 00656 DECL_USER_INIT_HANDLE(component);\ 00657 DECL_USER_FINIS_HANDLE(component);\ 00658 DEFINE_INITOR(component)\ 00659 DEFINE_FINITOR(component) 00660 /* Implement the component's state dynamically*/ 00661 00662 #ifdef DEFAULT_STATIC_STATE 00663 00666 #define IMPLEMENT(component,init_type) IMPLEMENT_STATIC(component,init_type) 00667 #else 00668 00671 #define IMPLEMENT(component,init_type) IMPLEMENT_DYNAMIC(component,init_type) 00672 #endif 00673 00684 #define USE_STATIC_INITIALISER(component)\ 00685 DECL_STATIC_INITIALISER(component);\ 00686 DECL_STATIC_INITIALISER_HANDLE(component) = &STATIC_INITIALISER(component);\ 00687 DECL_STATIC_INITIALISER(component) 00688 00708 #define DEFINE_USER_INIT(component)\ 00709 DECL_USER_INIT(component);\ 00710 DECL_USER_INIT_HANDLE(component) = &USER_INIT(component);\ 00711 void USER_INIT(component) /* ...user_init function body here*/ 00712 00732 #define DEFINE_USER_FINIS(component)\ 00733 DECL_USER_FINIS(component);\ 00734 DECL_USER_FINIS_HANDLE(component) = &USER_FINIS(component);\ 00735 void USER_FINIS(component) /* ... user_finis function body here*/ 00736 00743 #define INCLUDE_PUBLIC(component) PUBLIC_STATE_T(component) PUBLIC_STATE(component) 00744 00752 #define STATE_DEF(component) typedef struct component##_state_s 00753 00761 #define STATE_T(component) component##_state_t 00762 00768 #define NO_PRIVATE_STATE(component)\ 00769 typedef PUBLIC_STATE_T(component) STATE_T(component) 00770 00771 #ifdef DEFAULT_STATIC_STATE 00772 00780 #define GET_STATE(component,field) GET_STATIC_STATE(component,field) 00781 00795 #define SET_STATE(component,field) SET_STATIC_STATE(component,field) 00796 #else 00797 00809 #define GET_STATE(component,field) GET_STATE_BY_HANDLE(component,field) 00810 00824 #define SET_STATE(component,field) SET_STATE_BY_HANDLE(component,field) 00825 #endif 00826 00827 00832 00840 #define PUBLIC_STATE_DEF(component) typedef struct component##_public_state_s 00841 00849 #define PUBLIC_STATE_T(component) component##_public_state_t 00850 00857 #define NO_PUBLIC_STATE(component)\ 00858 typedef STATE_T(component) PUBLIC_STATE_T(component) 00859 00865 #define IMPORT(component)\ 00866 IMPORT_STATE(component);\ 00867 IMPORT_INITOR(component);\ 00868 IMPORT_FINITOR(component) 00869 00876 00881 #define INITIALISE(component) INITOR(component)() 00882 00887 #define FINALISE(component) INITOR(component)() 00888 00892 #define REINITIALISE(component)\ 00893 (void)(FINALISE(component),INITIALISE(component)) 00894 00900 #define GET_PUBLIC(component,field)\ 00901 (assert(true),(PUBLIC_HANDLE(component)->field)) 00902 00911 #define SET_PUBLIC(component,field) (PUBLIC_HANDLE(component)->field) 00912 00913 00914 #endif 00915 00918 /* EOF*/