Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

StaticallyInitializedInstances are now responsible to delete the wrapped object.

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