Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/network/NetworkFunctionIncludes.h @ 10535

Last change on this file since 10535 was 10535, checked in by landauf, 9 years ago

statically initialized instances are now registered with a type. CoreStaticInitializationHandler initializes all instances in core, NetworkStaticInitializationHandler initializes all instances in network.

File size: 5.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss
24 *   Co-authors:
25 *      Fabian 'x3n' Landau
26 *
27 */
28
29#ifndef _NetworkFunctionIncludes_H__
30#define _NetworkFunctionIncludes_H__
31
32#include "NetworkPrereqs.h"
33
34#include <boost/preprocessor/cat.hpp>
35#include <boost/static_assert.hpp>
36
37#include "NetworkFunction.h"
38#include "core/module/StaticallyInitializedInstance.h"
39
40#define registerStaticNetworkFunction( functionPointer ) \
41    static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) \
42        = (new orxonox::SI_NF(orxonox::registerStaticNetworkFunctionFct( functionPointer, #functionPointer )))->getFunction()
43
44#define registerMemberNetworkFunction( class, function ) \
45    static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) \
46        = (new orxonox::SI_NF(orxonox::registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function)))->getFunction()
47
48namespace orxonox
49{
50    class _NetworkExport StaticallyInitializedNetworkFunction : public StaticallyInitializedInstance
51    {
52        public:
53            StaticallyInitializedNetworkFunction(NetworkFunctionBase* function)
54                : StaticallyInitializedInstance(StaticInitialization::NETWORK_FUNCTION)
55                , function_(function)
56            {}
57            ~StaticallyInitializedNetworkFunction() { delete function_; }
58
59            virtual void load();
60            virtual void unload();
61
62            inline NetworkFunctionBase& getFunction()
63                { return *this->function_; }
64
65        private:
66            NetworkFunctionBase* function_;
67    };
68
69    typedef StaticallyInitializedNetworkFunction SI_NF;
70
71    template<class PT>
72    inline NetworkFunctionBase* registerStaticNetworkFunctionFct(PT ptr, const std::string& name)
73    {
74        BOOST_STATIC_ASSERT(sizeof(PT) <= sizeof(NetworkFunctionPointer)); // if this fails your compiler uses bigger pointers for static functions than defined above
75        NetworkFunctionPointer destptr;
76        copyPtr(ptr, destptr);
77        return new NetworkFunctionStatic(createFunctor(ptr), name, destptr);
78    }
79
80    template<class T, class PT>
81    inline NetworkFunctionBase* registerMemberNetworkFunctionFct(PT ptr, const std::string& name)
82    {
83        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
84        NetworkFunctionPointer destptr;
85        copyPtr(ptr, destptr);
86        return new NetworkMemberFunction<T>(createFunctor(ptr), name, destptr);
87    }
88
89    _NetworkExport uint32_t getNetworkIdForPointer(const NetworkFunctionPointer& pointer);
90
91    // call it with functionPointer, clientID, args
92    template<class PT>
93    void callStaticNetworkFunction(PT ptr, uint32_t clientID, const MultiType& mt1 = MultiType::Null, const MultiType& mt2 = MultiType::Null, const MultiType& mt3 = MultiType::Null, const MultiType& mt4 = MultiType::Null, const MultiType& mt5 = MultiType::Null)
94    {
95        NetworkFunctionPointer destptr;
96        copyPtr(ptr, destptr);
97        FunctionCallManager::addCall(getNetworkIdForPointer(destptr), OBJECTID_UNKNOWN, clientID, mt1, mt2, mt3, mt4, mt5);
98    }
99
100    // call it with class::function, objectID, clientID, args
101    template<class PT>
102    void callMemberNetworkFunction(PT ptr, uint32_t objectID, uint32_t clientID, const MultiType& mt1 = MultiType::Null, const MultiType& mt2 = MultiType::Null, const MultiType& mt3 = MultiType::Null, const MultiType& mt4 = MultiType::Null, const MultiType& mt5 = MultiType::Null)
103    {
104        NetworkFunctionPointer destptr;
105        copyPtr(ptr, destptr);
106        FunctionCallManager::addCall(getNetworkIdForPointer(destptr), objectID, clientID, mt1, mt2, mt3, mt4, mt5);
107    }
108
109    template<class PT>
110    inline void copyPtr(PT ptr, NetworkFunctionPointer& destptr)
111    {
112        if (sizeof(NetworkFunctionPointer) - sizeof(PT) > 0)
113            memset((uint8_t*)&destptr + sizeof(PT), 0, sizeof(NetworkFunctionPointer) - sizeof(PT));
114        PT p2 = ptr;
115        memcpy(&destptr, &p2, sizeof(PT));
116    }
117}
118
119#endif /* _NetworkFunctionIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.