[Orxonox-commit 5816] r10476 - code/branches/core7/src/libraries/network

landauf at orxonox.net landauf at orxonox.net
Mon May 25 15:23:55 CEST 2015


Author: landauf
Date: 2015-05-25 15:23:55 +0200 (Mon, 25 May 2015)
New Revision: 10476

Modified:
   code/branches/core7/src/libraries/network/NetworkFunction.cc
   code/branches/core7/src/libraries/network/NetworkFunctionIncludes.h
Log:
wrap NetworkFunction in a StaticallyInitializedInstance

Modified: code/branches/core7/src/libraries/network/NetworkFunction.cc
===================================================================
--- code/branches/core7/src/libraries/network/NetworkFunction.cc	2015-05-25 12:41:57 UTC (rev 10475)
+++ code/branches/core7/src/libraries/network/NetworkFunction.cc	2015-05-25 13:23:55 UTC (rev 10476)
@@ -37,7 +37,6 @@
         this->networkID_ = networkID++;
         this->name_ = name;
         this->pointer_ = pointer;
-        NetworkFunctionManager::getInstance().registerFunction(this);
     }
 
     void NetworkFunctionBase::setNetworkID(uint32_t id)

Modified: code/branches/core7/src/libraries/network/NetworkFunctionIncludes.h
===================================================================
--- code/branches/core7/src/libraries/network/NetworkFunctionIncludes.h	2015-05-25 12:41:57 UTC (rev 10475)
+++ code/branches/core7/src/libraries/network/NetworkFunctionIncludes.h	2015-05-25 13:23:55 UTC (rev 10476)
@@ -36,49 +36,68 @@
 
 #include "NetworkFunction.h"
 #include "NetworkFunctionManager.h"
+#include "core/module/StaticallyInitializedInstance.h"
 
+#define registerStaticNetworkFunction( functionPointer ) \
+    static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) \
+        = (new orxonox::SI_NF(orxonox::registerStaticNetworkFunctionFct( functionPointer, #functionPointer )))->getFunction()
+
+#define registerMemberNetworkFunction( class, function ) \
+    static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) \
+        = (new orxonox::SI_NF(orxonox::registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function)))->getFunction()
+
+// call it with functionPointer, clientID, args
+#define callStaticNetworkFunction( functionPointer, ...) \
+    { \
+        NetworkFunctionPointer p1; \
+        copyPtr( functionPointer, p1 ); \
+        FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), OBJECTID_UNKNOWN, __VA_ARGS__); \
+    }
+
+// call it with class, function, objectID, clientID, args
+#define callMemberNetworkFunction( class, function, objectID, ...) \
+    { \
+        NetworkFunctionPointer p1; \
+        copyPtr( &class::function, p1 ); \
+        FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), objectID, __VA_ARGS__); \
+    }
+
 namespace orxonox
 {
-    #define registerStaticNetworkFunction( functionPointer ) \
-        static void* BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) = registerStaticNetworkFunctionFct( functionPointer, #functionPointer );
+    class _CoreExport StaticallyInitializedNetworkFunction : public StaticallyInitializedInstance
+    {
+        public:
+            StaticallyInitializedNetworkFunction(NetworkFunctionBase* function) : function_(function) {}
 
-    #define registerMemberNetworkFunction( class, function ) \
-        static void* BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) = registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function);
+            virtual void load()
+                { NetworkFunctionManager::getInstance().registerFunction(this->function_); }
+            virtual void unload()
+                { NetworkFunctionManager::getInstance().unregisterFunction(this->function_); }
 
-    // call it with functionPointer, clientID, args
-    #define callStaticNetworkFunction( functionPointer, ...) \
-        { \
-            NetworkFunctionPointer p1; \
-            copyPtr( functionPointer, p1 ); \
-            FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), OBJECTID_UNKNOWN, __VA_ARGS__); \
-        }
+            inline NetworkFunctionBase& getFunction()
+                { return *this->function_; }
 
-    // call it with class, function, objectID, clientID, args
-    #define callMemberNetworkFunction( class, function, objectID, ...) \
-        { \
-            NetworkFunctionPointer p1; \
-            copyPtr( &class::function, p1 ); \
-            FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), objectID, __VA_ARGS__); \
-        }
+        private:
+            NetworkFunctionBase* function_;
+    };
 
-    template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, const std::string& name )
+    typedef StaticallyInitializedNetworkFunction SI_NF;
+
+    template<class T> inline NetworkFunctionBase* registerStaticNetworkFunctionFct( T ptr, const std::string& name )
     {
         BOOST_STATIC_ASSERT( sizeof(T)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for static functions than defined above
         NetworkFunctionPointer destptr;
         copyPtr( ptr, destptr );
-        new NetworkFunctionStatic( createFunctor(ptr), name, destptr );
-        return 0;
+        return new NetworkFunctionStatic( createFunctor(ptr), name, destptr );
     }
 
-    template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, const std::string& name )
+    template<class T, class PT> inline NetworkFunctionBase* registerMemberNetworkFunctionFct( PT ptr, const std::string& name )
     {
         BOOST_STATIC_ASSERT( sizeof(PT)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for a specific kind of member functions than defined above
         NetworkFunctionPointer destptr;
         copyPtr( ptr, destptr );
-        new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr );
-        return 0;
+        return new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr );
     }
-
 }
 
 #endif /* _NetworkFunctionIncludes_H__ */




More information about the Orxonox-commit mailing list