From 5f7c58c28eacdc91cbdd470e6e58710506aca8cb Mon Sep 17 00:00:00 2001 From: Ivan Pelipenko Date: Thu, 17 Sep 2020 13:51:02 +0300 Subject: [PATCH] pip static initializer --- CMakeLists.txt | 4 +- libs/main/core/pibase.h | 38 ++++++++ libs/main/core/picollection.h | 17 +++- libs/main/core/piinit.h | 4 +- libs/main/core/piobject.h | 176 +++++++++++++++------------------- 5 files changed, 133 insertions(+), 106 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8de0d58a..d59cca2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(pip) set(pip_MAJOR 2) set(pip_MINOR 7) -set(pip_REVISION 0) -set(pip_SUFFIX _beta) +set(pip_REVISION 1) +set(pip_SUFFIX ) set(pip_COMPANY SHS) set(pip_DOMAIN org.SHS) diff --git a/libs/main/core/pibase.h b/libs/main/core/pibase.h index b5e7836d..73c79213 100644 --- a/libs/main/core/pibase.h +++ b/libs/main/core/pibase.h @@ -94,6 +94,25 @@ //! Macro is defined when PIP can use "rt" library for "PITimer::ThreadRT" timers implementation # define PIP_TIMER_RT +//! Macro to declare private section, export is optional +# define PRIVATE_DECLARATION(export) + +//! Macro to start definition of private section +# define PRIVATE_DEFINITION_START(Class) + +//! Macro to end definition of private section +# define PRIVATE_DEFINITION_END(Class) + +//! Macro to access private section by pointer +# define PRIVATE + +//! Macro to start static initializer +# define STATIC_INITIALIZER_BEGIN + +//! Macro to end static initializer +# define STATIC_INITIALIZER_END + + #endif #include @@ -217,10 +236,29 @@ #define PRIVATE (__privateinitializer__.p) #define PRIVATEWB __privateinitializer__.p + #define NO_COPY_CLASS(name) \ name(const name&) = delete; \ name& operator=(const name&) = delete; + +#define _PIP_ADD_COUNTER_WS(a, cnt) a##cnt +#define _PIP_ADD_COUNTER_WF(a, cnt) _PIP_ADD_COUNTER_WS(a, cnt) +#define _PIP_ADD_COUNTER(a) _PIP_ADD_COUNTER_WF(a, __COUNTER__) + + +#define STATIC_INITIALIZER_BEGIN \ + class { \ + class _Initializer_ { \ + public: \ + _Initializer_() { + +#define STATIC_INITIALIZER_END \ + } \ + } _initializer_; \ +} _PIP_ADD_COUNTER(_pip_initializer_); + + #ifdef FREERTOS # define PIP_MIN_MSLEEP 10. #else diff --git a/libs/main/core/picollection.h b/libs/main/core/picollection.h index 93e58a6c..7f2c36f1 100644 --- a/libs/main/core/picollection.h +++ b/libs/main/core/picollection.h @@ -44,10 +44,19 @@ # define ADD_NEW_TO_COLLECTION(group, class) #else -# define ADD_TO_COLLECTION(group, object) static PICollection::CollectionAdder __##group##_##__LINE__##_##adder##__(#group, object, false); -# define ADD_TO_COLLECTION_WITH_NAME(group, object, name) static PICollection::CollectionAdder __##group##_##__LINE__##_##adder##__(#group, object, #name, false); -# define ADD_NEW_TO_COLLECTION(group, class) static PICollection::CollectionAdder __##group##_##class##_##adder##__(#group, new class(), true); -# define ADD_NEW_TO_COLLECTION_WITH_NAME(group, class, name) static PICollection::CollectionAdder __##group##_##class##_##adder##__(#group, new class(), #name, true); + +# define ADD_TO_COLLECTION(group, object) \ + static PICollection::CollectionAdder _PIP_ADD_COUNTER(_collection_adder_)(#group, object, false); + +# define ADD_TO_COLLECTION_WITH_NAME(group, object, name) \ + static PICollection::CollectionAdder _PIP_ADD_COUNTER(_collection_adder_)(#group, object, #name, false); + +# define ADD_NEW_TO_COLLECTION(group, class) \ + static PICollection::CollectionAdder _PIP_ADD_COUNTER(_collection_adder_)(#group, new class(), true); + +# define ADD_NEW_TO_COLLECTION_WITH_NAME(group, class, name) \ + static PICollection::CollectionAdder _PIP_ADD_COUNTER(_collection_adder_)(#group, new class(), #name, true); + #endif class PIP_EXPORT PICollection diff --git a/libs/main/core/piinit.h b/libs/main/core/piinit.h index e0dad2c8..dedded84 100644 --- a/libs/main/core/piinit.h +++ b/libs/main/core/piinit.h @@ -38,6 +38,8 @@ public: static PIInit * __instance__; }; +static __PIInit_Initializer__ __piinit_initializer__; + class PIP_EXPORT PIInit { friend class __PIInit_Initializer__; @@ -67,7 +69,5 @@ private: char * file_charset; }; -static __PIInit_Initializer__ __piinit_initializer__; - #endif // PIINIT_H diff --git a/libs/main/core/piobject.h b/libs/main/core/piobject.h index 6bf52e8c..cbc309a3 100644 --- a/libs/main/core/piobject.h +++ b/libs/main/core/piobject.h @@ -257,112 +257,92 @@ #define EH_INIT0(ret, name) \ - class __##name##0_Initializer__ { \ - public: \ - __##name##0_Initializer__() { \ - PIMutexLocker ml(__meta_mutex()); \ - __MetaData & eh(__meta_data()[__classNameIDS()]); \ - void * fp = (void*)(ret(*)(void*))__stat_eh_##name##__; \ - void * fpV = fp; \ - if (eh.eh_set[fp]) return; \ - eh.eh_set << fp; \ - __MetaFunc & f(eh.eh_func[fp]); \ - f.scope = __classNameS(); \ - f.func_name = PIStringAscii(#name); \ - f.addr = fp; \ - f.addrV = fpV; \ - f.type_ret = PIStringAscii(#ret); \ - } \ - }; \ - __##name##0_Initializer__ __##name##0_init__; \ + STATIC_INITIALIZER_BEGIN \ + PIMutexLocker ml(__meta_mutex()); \ + __MetaData & eh(__meta_data()[__classNameIDS()]); \ + void * fp = (void*)(ret(*)(void*))__stat_eh_##name##__; \ + void * fpV = fp; \ + if (eh.eh_set[fp]) return; \ + eh.eh_set << fp; \ + __MetaFunc & f(eh.eh_func[fp]); \ + f.scope = __classNameS(); \ + f.func_name = PIStringAscii(#name); \ + f.addr = fp; \ + f.addrV = fpV; \ + f.type_ret = PIStringAscii(#ret); \ + STATIC_INITIALIZER_END #define EH_INIT1(ret, name, a0, n0) \ - class __##name##1##n0##_Initializer__ { \ - public: \ - __##name##1##n0##_Initializer__() { \ - PIMutexLocker ml(__meta_mutex()); \ - __MetaData & eh(__meta_data()[__classNameIDS()]); \ - void * fp = (void*)(ret(*)(void*, a0))__stat_eh_##name##__; \ - void * fpV = (void*)(ret(*)(void*, const PIVariant &))__stat_eh_v_##name##__; \ - if (eh.eh_set[fp]) return; \ - eh.eh_set << fp; \ - __MetaFunc & f(eh.eh_func[fp]); \ - f.scope = __classNameS(); \ - f.func_name = PIStringAscii(#name); \ - f.addr = fp; \ - f.addrV = fpV; \ - f.type_ret = PIStringAscii(#ret); \ - f.types << PIObject::simplifyType(#a0); \ - f.names << PIStringAscii(#n0); \ - } \ - }; \ - __##name##1##n0##_Initializer__ __##name##1##n0##_init__; \ + STATIC_INITIALIZER_BEGIN \ + PIMutexLocker ml(__meta_mutex()); \ + __MetaData & eh(__meta_data()[__classNameIDS()]); \ + void * fp = (void*)(ret(*)(void*, a0))__stat_eh_##name##__; \ + void * fpV = (void*)(ret(*)(void*, const PIVariant &))__stat_eh_v_##name##__; \ + if (eh.eh_set[fp]) return; \ + eh.eh_set << fp; \ + __MetaFunc & f(eh.eh_func[fp]); \ + f.scope = __classNameS(); \ + f.func_name = PIStringAscii(#name); \ + f.addr = fp; \ + f.addrV = fpV; \ + f.type_ret = PIStringAscii(#ret); \ + f.types << PIObject::simplifyType(#a0); \ + f.names << PIStringAscii(#n0); \ + STATIC_INITIALIZER_END #define EH_INIT2(ret, name, a0, n0, a1, n1) \ - class __##name##2##n0##n1##_Initializer__ { \ - public: \ - __##name##2##n0##n1##_Initializer__() { \ - PIMutexLocker ml(__meta_mutex()); \ - __MetaData & eh(__meta_data()[__classNameIDS()]); \ - void * fp = (void*)(ret(*)(void*, a0, a1))__stat_eh_##name##__; \ - void * fpV = (void*)(ret(*)(void*, const PIVariant &, const PIVariant &))__stat_eh_v_##name##__; \ - if (eh.eh_set[fp]) return; \ - eh.eh_set << fp; \ - __MetaFunc & f(eh.eh_func[fp]); \ - f.scope = __classNameS(); \ - f.func_name = PIStringAscii(#name); \ - f.addr = fp; \ - f.addrV = fpV; \ - f.type_ret = PIStringAscii(#ret); \ - f.types << PIObject::simplifyType(#a0) << PIObject::simplifyType(#a1); \ - f.names << PIStringAscii(#n0) << PIStringAscii(#n1); \ - } \ - }; \ - __##name##2##n0##n1##_Initializer__ __##name##2##n0##n1##_init__; \ + STATIC_INITIALIZER_BEGIN \ + PIMutexLocker ml(__meta_mutex()); \ + __MetaData & eh(__meta_data()[__classNameIDS()]); \ + void * fp = (void*)(ret(*)(void*, a0, a1))__stat_eh_##name##__; \ + void * fpV = (void*)(ret(*)(void*, const PIVariant &, const PIVariant &))__stat_eh_v_##name##__; \ + if (eh.eh_set[fp]) return; \ + eh.eh_set << fp; \ + __MetaFunc & f(eh.eh_func[fp]); \ + f.scope = __classNameS(); \ + f.func_name = PIStringAscii(#name); \ + f.addr = fp; \ + f.addrV = fpV; \ + f.type_ret = PIStringAscii(#ret); \ + f.types << PIObject::simplifyType(#a0) << PIObject::simplifyType(#a1); \ + f.names << PIStringAscii(#n0) << PIStringAscii(#n1); \ + STATIC_INITIALIZER_END #define EH_INIT3(ret, name, a0, n0, a1, n1, a2, n2) \ - class __##name##3##n0##n1##n2##_Initializer__ { \ - public: \ - __##name##3##n0##n1##n2##_Initializer__() { \ - PIMutexLocker ml(__meta_mutex()); \ - __MetaData & eh(__meta_data()[__classNameIDS()]); \ - void * fp = (void*)(ret(*)(void*, a0, a1, a2))__stat_eh_##name##__; \ - void * fpV = (void*)(ret(*)(void*, const PIVariant &, const PIVariant &, const PIVariant &))__stat_eh_v_##name##__; \ - if (eh.eh_set[fp]) return; \ - eh.eh_set << fp; \ - __MetaFunc & f(eh.eh_func[fp]); \ - f.scope = __classNameS(); \ - f.func_name = PIStringAscii(#name); \ - f.addr = fp; \ - f.addrV = fpV; \ - f.type_ret = PIStringAscii(#ret); \ - f.types << PIObject::simplifyType(#a0) << PIObject::simplifyType(#a1) << PIObject::simplifyType(#a2); \ - f.names << PIStringAscii(#n0) << PIStringAscii(#n1) << PIStringAscii(#n2); \ - } \ - }; \ - __##name##3##n0##n1##n2##_Initializer__ __##name##3##n0##n1##n2##_init__; \ + STATIC_INITIALIZER_BEGIN \ + PIMutexLocker ml(__meta_mutex()); \ + __MetaData & eh(__meta_data()[__classNameIDS()]); \ + void * fp = (void*)(ret(*)(void*, a0, a1, a2))__stat_eh_##name##__; \ + void * fpV = (void*)(ret(*)(void*, const PIVariant &, const PIVariant &, const PIVariant &))__stat_eh_v_##name##__; \ + if (eh.eh_set[fp]) return; \ + eh.eh_set << fp; \ + __MetaFunc & f(eh.eh_func[fp]); \ + f.scope = __classNameS(); \ + f.func_name = PIStringAscii(#name); \ + f.addr = fp; \ + f.addrV = fpV; \ + f.type_ret = PIStringAscii(#ret); \ + f.types << PIObject::simplifyType(#a0) << PIObject::simplifyType(#a1) << PIObject::simplifyType(#a2); \ + f.names << PIStringAscii(#n0) << PIStringAscii(#n1) << PIStringAscii(#n2); \ + STATIC_INITIALIZER_END #define EH_INIT4(ret, name, a0, n0, a1, n1, a2, n2, a3, n3) \ - class __##name##4##n0##n1##n2##n3##_Initializer__ { \ - public: \ - __##name##4##n0##n1##n2##n3##_Initializer__() { \ - PIMutexLocker ml(__meta_mutex()); \ - __MetaData & eh(__meta_data()[__classNameIDS()]); \ - void * fp = (void*)(ret(*)(void*, a0, a1, a2, a3))__stat_eh_##name##__; \ - void * fpV = (void*)(ret(*)(void*, const PIVariant &, const PIVariant &, const PIVariant &, const PIVariant &))__stat_eh_v_##name##__; \ - if (eh.eh_set[fp]) return; \ - eh.eh_set << fp; \ - __MetaFunc & f(eh.eh_func[fp]); \ - f.scope = __classNameS(); \ - f.func_name = PIStringAscii(#name); \ - f.addr = fp; \ - f.addrV = fpV; \ - f.type_ret = PIStringAscii(#ret); \ - f.types << PIObject::simplifyType(#a0) << PIObject::simplifyType(#a1) << PIObject::simplifyType(#a2) << PIObject::simplifyType(#a3); \ - f.names << PIStringAscii(#n0) << PIStringAscii(#n1) << PIStringAscii(#n2) << PIStringAscii(#n3); \ - } \ - }; \ - __##name##4##n0##n1##n2##n3##_Initializer__ __##name##4##n0##n1##n2##n3##_init__; \ + STATIC_INITIALIZER_BEGIN \ + PIMutexLocker ml(__meta_mutex()); \ + __MetaData & eh(__meta_data()[__classNameIDS()]); \ + void * fp = (void*)(ret(*)(void*, a0, a1, a2, a3))__stat_eh_##name##__; \ + void * fpV = (void*)(ret(*)(void*, const PIVariant &, const PIVariant &, const PIVariant &, const PIVariant &))__stat_eh_v_##name##__; \ + if (eh.eh_set[fp]) return; \ + eh.eh_set << fp; \ + __MetaFunc & f(eh.eh_func[fp]); \ + f.scope = __classNameS(); \ + f.func_name = PIStringAscii(#name); \ + f.addr = fp; \ + f.addrV = fpV; \ + f.type_ret = PIStringAscii(#ret); \ + f.types << PIObject::simplifyType(#a0) << PIObject::simplifyType(#a1) << PIObject::simplifyType(#a2) << PIObject::simplifyType(#a3); \ + f.names << PIStringAscii(#n0) << PIStringAscii(#n1) << PIStringAscii(#n2) << PIStringAscii(#n3); \ + STATIC_INITIALIZER_END #define EVENT_HANDLER0(ret, name) \