Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10479 was 10478, checked in by landauf, 10 years ago

callStaticNetworkFunction() and callMemberNetworkFunction() are now template functions instead of macros.
simplified function call interface: always pass five MultiType-references. Check if MultiType.null() evaluates to true to see if the argument was defined.
moved references to NetworkFunctionManager to NetworkFunctionIncludes.cc.
this also fixed a linker error in MSVC by including NetworkFunctionIncludes.h in a build-unit inside the network library.

File size: 4.9 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) : function_(function) {}
54
55            virtual void load();
56            virtual void unload();
57
58            inline NetworkFunctionBase& getFunction()
59                { return *this->function_; }
60
61        private:
62            NetworkFunctionBase* function_;
63    };
64
65    typedef StaticallyInitializedNetworkFunction SI_NF;
66
67    template<class PT>
68    inline NetworkFunctionBase* registerStaticNetworkFunctionFct(PT ptr, const std::string& name)
69    {
70        BOOST_STATIC_ASSERT(sizeof(PT) <= sizeof(NetworkFunctionPointer)); // if this fails your compiler uses bigger pointers for static functions than defined above
71        NetworkFunctionPointer destptr;
72        copyPtr(ptr, destptr);
73        return new NetworkFunctionStatic(createFunctor(ptr), name, destptr);
74    }
75
76    template<class T, class PT>
77    inline NetworkFunctionBase* registerMemberNetworkFunctionFct(PT ptr, const std::string& name)
78    {
79        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
80        NetworkFunctionPointer destptr;
81        copyPtr(ptr, destptr);
82        return new NetworkMemberFunction<T>(createFunctor(ptr), name, destptr);
83    }
84
85    _NetworkExport uint32_t getNetworkIdForPointer(const NetworkFunctionPointer& pointer);
86
87    // call it with functionPointer, clientID, args
88    template<class PT>
89    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)
90    {
91        NetworkFunctionPointer destptr;
92        copyPtr(ptr, destptr);
93        FunctionCallManager::addCall(getNetworkIdForPointer(destptr), OBJECTID_UNKNOWN, clientID, mt1, mt2, mt3, mt4, mt5);
94    }
95
96    // call it with class::function, objectID, clientID, args
97    template<class PT>
98    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)
99    {
100        NetworkFunctionPointer destptr;
101        copyPtr(ptr, destptr);
102        FunctionCallManager::addCall(getNetworkIdForPointer(destptr), objectID, clientID, mt1, mt2, mt3, mt4, mt5);
103    }
104
105    template<class PT>
106    inline void copyPtr(PT ptr, NetworkFunctionPointer& destptr)
107    {
108        if (sizeof(NetworkFunctionPointer) - sizeof(PT) > 0)
109            memset((uint8_t*)&destptr + sizeof(PT), 0, sizeof(NetworkFunctionPointer) - sizeof(PT));
110        PT p2 = ptr;
111        memcpy(&destptr, &p2, sizeof(PT));
112    }
113}
114
115#endif /* _NetworkFunctionIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.