Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10476 for code/branches


Ignore:
Timestamp:
May 25, 2015, 3:23:55 PM (9 years ago)
Author:
landauf
Message:

wrap NetworkFunction in a StaticallyInitializedInstance

Location:
code/branches/core7/src/libraries/network
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/network/NetworkFunction.cc

    r10475 r10476  
    3838        this->name_ = name;
    3939        this->pointer_ = pointer;
    40         NetworkFunctionManager::getInstance().registerFunction(this);
    4140    }
    4241
  • code/branches/core7/src/libraries/network/NetworkFunctionIncludes.h

    r10475 r10476  
    3737#include "NetworkFunction.h"
    3838#include "NetworkFunctionManager.h"
     39#include "core/module/StaticallyInitializedInstance.h"
     40
     41#define registerStaticNetworkFunction( functionPointer ) \
     42    static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) \
     43        = (new orxonox::SI_NF(orxonox::registerStaticNetworkFunctionFct( functionPointer, #functionPointer )))->getFunction()
     44
     45#define registerMemberNetworkFunction( class, function ) \
     46    static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) \
     47        = (new orxonox::SI_NF(orxonox::registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function)))->getFunction()
     48
     49// call it with functionPointer, clientID, args
     50#define callStaticNetworkFunction( functionPointer, ...) \
     51    { \
     52        NetworkFunctionPointer p1; \
     53        copyPtr( functionPointer, p1 ); \
     54        FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), OBJECTID_UNKNOWN, __VA_ARGS__); \
     55    }
     56
     57// call it with class, function, objectID, clientID, args
     58#define callMemberNetworkFunction( class, function, objectID, ...) \
     59    { \
     60        NetworkFunctionPointer p1; \
     61        copyPtr( &class::function, p1 ); \
     62        FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), objectID, __VA_ARGS__); \
     63    }
    3964
    4065namespace orxonox
    4166{
    42     #define registerStaticNetworkFunction( functionPointer ) \
    43         static void* BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) = registerStaticNetworkFunctionFct( functionPointer, #functionPointer );
     67    class _CoreExport StaticallyInitializedNetworkFunction : public StaticallyInitializedInstance
     68    {
     69        public:
     70            StaticallyInitializedNetworkFunction(NetworkFunctionBase* function) : function_(function) {}
    4471
    45     #define registerMemberNetworkFunction( class, function ) \
    46         static void* BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) = registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function);
     72            virtual void load()
     73                { NetworkFunctionManager::getInstance().registerFunction(this->function_); }
     74            virtual void unload()
     75                { NetworkFunctionManager::getInstance().unregisterFunction(this->function_); }
    4776
    48     // call it with functionPointer, clientID, args
    49     #define callStaticNetworkFunction( functionPointer, ...) \
    50         { \
    51             NetworkFunctionPointer p1; \
    52             copyPtr( functionPointer, p1 ); \
    53             FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), OBJECTID_UNKNOWN, __VA_ARGS__); \
    54         }
     77            inline NetworkFunctionBase& getFunction()
     78                { return *this->function_; }
    5579
    56     // call it with class, function, objectID, clientID, args
    57     #define callMemberNetworkFunction( class, function, objectID, ...) \
    58         { \
    59             NetworkFunctionPointer p1; \
    60             copyPtr( &class::function, p1 ); \
    61             FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), objectID, __VA_ARGS__); \
    62         }
     80        private:
     81            NetworkFunctionBase* function_;
     82    };
    6383
    64     template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, const std::string& name )
     84    typedef StaticallyInitializedNetworkFunction SI_NF;
     85
     86    template<class T> inline NetworkFunctionBase* registerStaticNetworkFunctionFct( T ptr, const std::string& name )
    6587    {
    6688        BOOST_STATIC_ASSERT( sizeof(T)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for static functions than defined above
    6789        NetworkFunctionPointer destptr;
    6890        copyPtr( ptr, destptr );
    69         new NetworkFunctionStatic( createFunctor(ptr), name, destptr );
    70         return 0;
     91        return new NetworkFunctionStatic( createFunctor(ptr), name, destptr );
    7192    }
    7293
    73     template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, const std::string& name )
     94    template<class T, class PT> inline NetworkFunctionBase* registerMemberNetworkFunctionFct( PT ptr, const std::string& name )
    7495    {
    7596        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
    7697        NetworkFunctionPointer destptr;
    7798        copyPtr( ptr, destptr );
    78         new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr );
    79         return 0;
     99        return new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr );
    80100    }
    81 
    82101}
    83102
Note: See TracChangeset for help on using the changeset viewer.