Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/NetworkFunction.h @ 5874

Last change on this file since 5874 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

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