Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/NetworkFunction.h @ 3207

Last change on this file since 3207 was 3207, checked in by scheusso, 15 years ago

fix in synchronisation of function ids

  • Property svn:eol-style set to native
File size: 9.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 *      ...
26 *
27 */
28
29#ifndef NETWORKFUNCTION_H
30#define NETWORKFUNCTION_H
31
32#include "NetworkPrereqs.h"
33#include "core/OrxonoxClass.h"
34
35#include <string>
36#include <map>
37#include <cassert>
38#include <boost/preprocessor/cat.hpp>
39#include "util/MultiType.h"
40#include "core/Functor.h"
41#include "synchronisable/Synchronisable.h"
42#include "OrxonoxConfig.h"
43#include "FunctionCallManager.h"
44
45
46namespace orxonox
47{
48
49#ifdef ORXONOX_COMPILER_GCC
50static const unsigned int MAX_FUNCTION_POINTER_SIZE = 8;
51#else //ORXONOX_COMPILER_GCC
52static const unsigned int MAX_FUNCTION_POINTER_SIZE = 16;
53#endif //ORXONOX_COMPILER_GCC
54static const unsigned int MAX_FUNCTION_POINTER_INTS = (MAX_FUNCTION_POINTER_SIZE-1)/4+1;
55
56struct _NetworkExport NetworkFunctionPointer {
57  uint32_t pointer[MAX_FUNCTION_POINTER_INTS];
58  bool operator<(const NetworkFunctionPointer& b) const
59  {
60#ifdef ORXONOX_COMPILER_GCC
61    return pointer[0]<b.pointer[0] ? true : pointer[1]<b.pointer[1];
62#else //ORXONOX_COMPILER_GCC
63    return pointer[0]<b.pointer[0] ? true : ( pointer[1]<b.pointer[1] ? true : ( pointer[2]<b.pointer[2] ? true : pointer[3]<b.pointer[3] ) );
64#endif //ORXONOX_COMPILER_GCC
65  }
66};
67
68
69
70
71
72class _NetworkExport NetworkFunctionBase: virtual public OrxonoxClass {
73  public:
74    NetworkFunctionBase(std::string name);
75    ~NetworkFunctionBase();
76   
77    virtual void         setNetworkID(uint32_t id)       { this->networkID_ = id; }
78    inline uint32_t     getNetworkID() const            { return this->networkID_; }
79    inline std::string  getName() const                 { return name_; }
80    static inline bool  isStatic( uint32_t networkID )  { return isStaticMap_[networkID]; }
81   
82    static inline void setNetworkID(std::string name, uint32_t id){ assert( nameMap_.find(name)!=nameMap_.end() ); nameMap_[name]->setNetworkID(id); }
83   
84  protected:
85    static std::map<uint32_t, bool> isStaticMap_;
86   
87  private:
88    static std::map<std::string, NetworkFunctionBase*> nameMap_;
89    uint32_t networkID_;
90    std::string name_;
91     
92};
93
94
95class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase {
96  public:
97    NetworkFunctionStatic(FunctorStatic* functor, std::string name, const NetworkFunctionPointer& p);
98    ~NetworkFunctionStatic();
99   
100    inline void call(){ (*this->functor_)(); }
101    inline void call(const MultiType& mt1){ (*this->functor_)(mt1); }
102    inline void call(const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); }
103    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); }
104    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); }
105    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5){ (*this->functor_)(mt1, mt2, mt3, mt4, mt5); }
106   
107    virtual void setNetworkID( uint32_t id ){ NetworkFunctionBase::setNetworkID( id ); idMap_[id] = this; }
108    static inline NetworkFunctionStatic* getNetworkFunction( uint32_t id){ assert( idMap_.find(id)!=idMap_.end() ); return idMap_[id]; }
109    static NetworkFunctionStatic* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; }
110    static NetworkFunctionStatic* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; }
111   
112  private:
113    static std::map<NetworkFunctionPointer, NetworkFunctionStatic*> functorMap_;
114    static std::map<uint32_t, NetworkFunctionStatic*> idMap_;
115   
116    FunctorStatic* functor_;
117   
118};
119
120
121class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase {
122  public:
123    NetworkMemberFunctionBase(std::string name, const NetworkFunctionPointer& p);
124    ~NetworkMemberFunctionBase();
125   
126    virtual void setNetworkID( uint32_t id ){ NetworkFunctionBase::setNetworkID( id ); idMap_[id] = this; }
127    static inline NetworkMemberFunctionBase* getNetworkFunction( uint32_t id){ assert( idMap_.find(id)!=idMap_.end() ); return idMap_[id]; }
128    static NetworkMemberFunctionBase* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; }
129    static NetworkMemberFunctionBase* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; }
130   
131    //
132    virtual void call(uint32_t objectID)=0;
133    virtual void call(uint32_t objectID, const MultiType& mt1)=0;
134    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0;
135    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0;
136    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0;
137    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0;
138   
139  private:
140    static std::map<NetworkFunctionPointer, NetworkMemberFunctionBase*> functorMap_;
141    static std::map<uint32_t, NetworkMemberFunctionBase*> idMap_;
142};
143
144
145template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase {
146  public:
147    NetworkMemberFunction(FunctorMember<T>* functor, std::string name, const NetworkFunctionPointer& p);
148    ~NetworkMemberFunction();
149   
150    inline void call(uint32_t objectID)
151    { 
152      if ( Synchronisable::getSynchronisable(objectID)!=0 )
153        (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)));
154    }
155    inline void call(uint32_t objectID, const MultiType& mt1)
156    { 
157      if ( Synchronisable::getSynchronisable(objectID)!=0 )
158        (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1);
159    }
160    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)
161    { 
162      if ( Synchronisable::getSynchronisable(objectID)!=0 )
163        (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2);
164    }
165    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
166    { 
167      if ( Synchronisable::getSynchronisable(objectID)!=0 )
168        (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3);
169    }
170    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
171    { 
172      if ( Synchronisable::getSynchronisable(objectID)!=0 )
173        (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4);
174    }
175    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
176    { 
177      if ( Synchronisable::getSynchronisable(objectID)!=0 )
178        (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5);
179    }
180   
181  private:
182    FunctorMember<T>* functor_;
183};
184
185template <class T> NetworkMemberFunction<T>::NetworkMemberFunction(FunctorMember<T>* functor, std::string name, const NetworkFunctionPointer& p):
186    NetworkMemberFunctionBase(name, p), functor_(functor)
187{
188}
189template <class T> NetworkMemberFunction<T>::~NetworkMemberFunction()
190{
191  delete this->functor_;
192}
193
194template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)
195{
196  memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));
197  T p2 = ptr;
198  memcpy( &destptr, &p2, sizeof(T) );
199//   for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++)
200//     *((uint32_t*)destptr+i) = p2>>32*i;
201}
202
203template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, std::string name )
204{
205  NetworkFunctionPointer destptr;
206  copyPtr( ptr, destptr );
207  new NetworkFunctionStatic( createFunctor(ptr), name, destptr );
208  return 0;
209}
210
211template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, std::string name )
212{
213  NetworkFunctionPointer destptr;
214  copyPtr( ptr, destptr );
215  new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr );
216  return 0;
217}
218
219#define registerStaticNetworkFunction( functionPointer ) \
220  static void* BOOST_PP_CAT( NETWORK_FUNCTION_, __LINE__ ) = registerStaticNetworkFunctionFct( functionPointer, #functionPointer );
221#define registerMemberNetworkFunction( class, function ) \
222  static void* BOOST_PP_CAT( NETWORK_FUNCTION_##class, __LINE__ ) = registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function);
223  // call it with functionPointer, clientID, args
224#define callStaticNetworkFunction( functionPointer, ...) \
225  { \
226    NetworkFunctionPointer p1; \
227    copyPtr( functionPointer, p1 ); \
228    FunctionCallManager::addCallStatic(NetworkFunctionStatic::getFunction(p1)->getNetworkID(), __VA_ARGS__); \
229  }
230  // call it with class, function, objectID, clientID, args
231#define callMemberNetworkFunction( class, function, objectID, ...) \
232  { \
233    NetworkFunctionPointer p1; \
234    copyPtr( &class::function, p1 ); \
235    FunctionCallManager::addCallMember(NetworkMemberFunctionBase::getFunction(p1)->getNetworkID(), objectID, __VA_ARGS__); \
236  }
237
238
239}
240
241#endif
Note: See TracBrowser for help on using the repository browser.