Orxonox  0.0.5 Codename: Arcturus
NetworkFunctionIncludes.h
Go to the documentation of this file.
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 
36 #include "NetworkFunction.h"
38 
39 #define registerStaticNetworkFunction( functionPointer ) \
40  static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) \
41  = (new orxonox::SI_NF(orxonox::registerStaticNetworkFunctionFct( functionPointer, #functionPointer )))->getFunction()
42 
43 #define registerMemberNetworkFunction( class, function ) \
44  static orxonox::NetworkFunctionBase& BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) \
45  = (new orxonox::SI_NF(orxonox::registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function)))->getFunction()
46 
47 namespace orxonox
48 {
50  {
51  public:
53  : StaticallyInitializedInstance(StaticInitialization::NETWORK_FUNCTION)
54  , function_(function)
55  {}
56  ~StaticallyInitializedNetworkFunction() { delete function_; }
57 
58  virtual void load() override;
59  virtual void unload() override;
60 
62  { return *this->function_; }
63 
64  private:
66  };
67 
69 
70  template<class PT>
72  {
73  static_assert(sizeof(PT) <= sizeof(NetworkFunctionPointer), "check pointer size"); // if this fails your compiler uses bigger pointers for static functions than defined above
74  NetworkFunctionPointer destptr;
75  copyPtr(ptr, destptr);
76  return new NetworkFunctionStatic(createFunctor(ptr), name, destptr);
77  }
78 
79  template<class T, class PT>
81  {
82  static_assert(sizeof(PT) <= sizeof(NetworkFunctionPointer), "check pointer size"); // if this fails your compiler uses bigger pointers for a specific kind of member functions than defined above
83  NetworkFunctionPointer destptr;
84  copyPtr(ptr, destptr);
85  return new NetworkMemberFunction<T>(createFunctor(ptr), name, destptr);
86  }
87 
89 
90  // call it with functionPointer, clientID, args
91  template<class PT>
92  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)
93  {
94  NetworkFunctionPointer destptr;
95  copyPtr(ptr, destptr);
96  FunctionCallManager::addCall(getNetworkIdForPointer(destptr), OBJECTID_UNKNOWN, clientID, mt1, mt2, mt3, mt4, mt5);
97  }
98 
99  // call it with class::function, objectID, clientID, args
100  template<class PT>
101  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)
102  {
103  NetworkFunctionPointer destptr;
104  copyPtr(ptr, destptr);
105  FunctionCallManager::addCall(getNetworkIdForPointer(destptr), objectID, clientID, mt1, mt2, mt3, mt4, mt5);
106  }
107 
108  template<class PT>
109  inline void copyPtr(PT ptr, NetworkFunctionPointer& destptr)
110  {
111  if (sizeof(NetworkFunctionPointer) - sizeof(PT) > 0)
112  memset((uint8_t*)&destptr + sizeof(PT), 0, sizeof(NetworkFunctionPointer) - sizeof(PT));
113  PT p2 = ptr;
114  memcpy(&destptr, &p2, sizeof(PT));
115  }
116 }
117 
118 #endif /* _NetworkFunctionIncludes_H__ */
Definition: NetworkFunction.h:83
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)
Definition: NetworkFunctionIncludes.h:101
Shared library macros, enums, constants and forward declarations for the network library ...
Definition: NetworkFunctionIncludes.h:49
Definition: NetworkFunction.h:53
static constexpr Type NETWORK_FUNCTION
Definition: NetworkPrereqs.h:100
::std::string string
Definition: gtest-port.h:756
StaticallyInitializedNetworkFunction SI_NF
Definition: NetworkFunctionIncludes.h:68
NetworkFunctionBase * registerMemberNetworkFunctionFct(PT ptr, const std::string &name)
Definition: NetworkFunctionIncludes.h:80
uint32_t getNetworkIdForPointer(const NetworkFunctionPointer &pointer)
Definition: NetworkFunctionIncludes.cc:49
FunctorMemberPtr< O > createFunctor(R(O::*functionPointer)(Params...), OO *object)
Creates a new FunctorMember with the given function-pointer and an assigned object.
Definition: Functor.h:583
static void addCall(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType &mt1, const MultiType &mt2, const MultiType &mt3, const MultiType &mt4, const MultiType &mt5)
Definition: FunctionCallManager.cc:42
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)
Definition: NetworkFunctionIncludes.h:92
void copyPtr(PT ptr, NetworkFunctionPointer &destptr)
Definition: NetworkFunctionIncludes.h:109
Definition: NetworkFunction.h:108
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
#define _NetworkExport
Definition: NetworkPrereqs.h:59
Definition: StaticallyInitializedInstance.h:36
NetworkFunctionBase & getFunction()
Definition: NetworkFunctionIncludes.h:61
The MultiType can hold a value of many possible types and convert them to other types.
Definition: MultiType.h:130
static const MultiType Null
Definition: MultiType.h:304
StaticallyInitializedNetworkFunction(NetworkFunctionBase *function)
Definition: NetworkFunctionIncludes.h:52
NetworkFunctionBase * registerStaticNetworkFunctionFct(PT ptr, const std::string &name)
Definition: NetworkFunctionIncludes.h:71
NetworkFunctionBase * function_
Definition: NetworkFunctionIncludes.h:65
~StaticallyInitializedNetworkFunction()
Definition: NetworkFunctionIncludes.h:56
Definition: NetworkFunction.h:137
static constexpr uint32_t OBJECTID_UNKNOWN
Definition: CorePrereqs.h:71