Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9523 was 8418, checked in by rgrieder, 14 years ago

Added preprocessor macro UNIQUE_NUMBER which will return a new integer number every time is invoked if such a mechanism is available.
On GCC < 4.3, it will be defined as LINE. Newer GCC and MSVC support COUNTER, which is then used.
Also replaced uses of LINE with UNIQUE_NUMBER if appropriate.

  • Property svn:eol-style set to native
File size: 11.1 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
[3325]41#include "core/Identifier.h"
[7284]42#include "core/command/Functor.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();
[6417]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]; }
[6417]81
82    static inline void setNetworkID(const std::string& name, uint32_t id)
83    {
84        std::map<std::string, NetworkFunctionBase*>& map = NetworkFunctionBase::getNameMap();
85        assert( map.find(name)!=map.end() );
86        map[name]->setNetworkID(id);
87    }
88
[3304]89    static void destroyAllNetworkFunctions();
[6417]90
[2944]91  protected:
92    static std::map<uint32_t, bool> isStaticMap_;
[6417]93
[2937]94  private:
[6417]95    static std::map<std::string, NetworkFunctionBase*>& getNameMap();
[2937]96    uint32_t networkID_;
97    std::string name_;
[6417]98
[2937]99};
100
101
102class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase {
103  public:
[7284]104    NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p);
[6417]105
[2937]106    inline void call(){ (*this->functor_)(); }
107    inline void call(const MultiType& mt1){ (*this->functor_)(mt1); }
108    inline void call(const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); }
109    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); }
110    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); }
111    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); }
[6417]112
113    virtual void setNetworkID( uint32_t id )
114        { NetworkFunctionBase::setNetworkID( id ); NetworkFunctionStatic::getIdMap()[id] = this; }
115    static inline NetworkFunctionStatic* getNetworkFunction( uint32_t id)
116        { assert( NetworkFunctionStatic::getIdMap().find(id)!=NetworkFunctionStatic::getIdMap().end() ); return NetworkFunctionStatic::getIdMap()[id]; }
117    static NetworkFunctionStatic* getFunction( uint32_t id )
118        { assert( NetworkFunctionStatic::getIdMap().find(id) != NetworkFunctionStatic::getIdMap().end() ); return NetworkFunctionStatic::getIdMap()[id]; }
119    static NetworkFunctionStatic* getFunction( const NetworkFunctionPointer& p )
120        { assert( NetworkFunctionStatic::getFunctorMap().find(p) != NetworkFunctionStatic::getFunctorMap().end() ); return NetworkFunctionStatic::getFunctorMap()[p]; }
121
[2937]122  private:
[6417]123    static std::map<NetworkFunctionPointer, NetworkFunctionStatic*>& getFunctorMap();
124    static std::map<uint32_t, NetworkFunctionStatic*>& getIdMap();
[7284]125    FunctorStaticPtr functor_;
[6417]126
[2937]127};
128
129
[2941]130class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase {
[2937]131  public:
[3214]132    NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p);
[2937]133    ~NetworkMemberFunctionBase();
[6417]134
[3214]135    virtual void setNetworkID( uint32_t id ){ NetworkFunctionBase::setNetworkID( id ); idMap_[id] = this; }
[2937]136    static inline NetworkMemberFunctionBase* getNetworkFunction( uint32_t id){ assert( idMap_.find(id)!=idMap_.end() ); return idMap_[id]; }
137    static NetworkMemberFunctionBase* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; }
138    static NetworkMemberFunctionBase* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; }
[6417]139
140    //
[7495]141    virtual bool call(uint32_t objectID)=0;
142    virtual bool call(uint32_t objectID, const MultiType& mt1)=0;
143    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0;
144    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0;
145    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0;
146    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0;
[6417]147
[2937]148  private:
149    static std::map<NetworkFunctionPointer, NetworkMemberFunctionBase*> functorMap_;
150    static std::map<uint32_t, NetworkMemberFunctionBase*> idMap_;
151};
152
153
[2995]154template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase {
[2937]155  public:
[7284]156    NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p);
[6417]157
[7495]158    inline bool call(uint32_t objectID)
[6417]159    {
[2951]160      if ( Synchronisable::getSynchronisable(objectID)!=0 )
[7495]161      {
[3325]162        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)));
[7495]163        return true;
164      }
165      else
166        return false;
[2951]167    }
[7495]168    inline bool call(uint32_t objectID, const MultiType& mt1)
[6417]169    {
[2951]170      if ( Synchronisable::getSynchronisable(objectID)!=0 )
[7495]171      {
[3325]172        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1);
[7495]173        return true;
174      }
175      else
176        return false;
[2951]177    }
[7495]178    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)
[6417]179    {
[2951]180      if ( Synchronisable::getSynchronisable(objectID)!=0 )
[7495]181      {
[3325]182        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2);
[7495]183        return true;
184      }
185      else
186        return false;
[2951]187    }
[7495]188    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
[6417]189    {
[2951]190      if ( Synchronisable::getSynchronisable(objectID)!=0 )
[7495]191      {
[3325]192        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3);
[7495]193        return true;
194      }
195      else
196        return false;
[2951]197    }
[7495]198    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
[6417]199    {
[2951]200      if ( Synchronisable::getSynchronisable(objectID)!=0 )
[7495]201      {
[3325]202        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4);
[7495]203        return true;
204      }
205      else
206        return false;
[2951]207    }
[7495]208    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
[6417]209    {
[2951]210      if ( Synchronisable::getSynchronisable(objectID)!=0 )
[7495]211      {
[3325]212        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5);
[7495]213        return true;
214      }
215      else
216        return false;
[2951]217    }
[6417]218
[2937]219  private:
[7284]220    FunctorMemberPtr<T> functor_;
[2937]221};
222
[7284]223template <class T> NetworkMemberFunction<T>::NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p):
[2944]224    NetworkMemberFunctionBase(name, p), functor_(functor)
225{
226}
[2937]227
228template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)
229{
[3304]230  if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0)
231    memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));
[2937]232  T p2 = ptr;
233  memcpy( &destptr, &p2, sizeof(T) );
234//   for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++)
235//     *((uint32_t*)destptr+i) = p2>>32*i;
236}
237
[3214]238template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, const std::string& name )
[2937]239{
[3304]240  BOOST_STATIC_ASSERT( sizeof(T)<=sizeof(NetworkFunctionPointer) ); // if this fails your compiler uses bigger pointers for static functions than defined above
[2937]241  NetworkFunctionPointer destptr;
242  copyPtr( ptr, destptr );
243  new NetworkFunctionStatic( createFunctor(ptr), name, destptr );
244  return 0;
245}
246
[3214]247template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, const std::string& name )
[2937]248{
[3304]249  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]250  NetworkFunctionPointer destptr;
251  copyPtr( ptr, destptr );
[2948]252  new NetworkMemberFunction<T>( createFunctor(ptr), name, destptr );
[2937]253  return 0;
254}
255
[2948]256#define registerStaticNetworkFunction( functionPointer ) \
[8418]257  static void* BOOST_PP_CAT( NETWORK_FUNCTION_, __UNIQUE_NUMBER__ ) = registerStaticNetworkFunctionFct( functionPointer, #functionPointer );
[2948]258#define registerMemberNetworkFunction( class, function ) \
[8418]259  static void* BOOST_PP_CAT( NETWORK_FUNCTION_##class, __UNIQUE_NUMBER__ ) = registerMemberNetworkFunctionFct<class>( &class::function, #class "_" #function);
[2948]260  // call it with functionPointer, clientID, args
[2937]261#define callStaticNetworkFunction( functionPointer, ...) \
262  { \
263    NetworkFunctionPointer p1; \
264    copyPtr( functionPointer, p1 ); \
[2943]265    FunctionCallManager::addCallStatic(NetworkFunctionStatic::getFunction(p1)->getNetworkID(), __VA_ARGS__); \
[2937]266  }
[2948]267  // call it with class, function, objectID, clientID, args
268#define callMemberNetworkFunction( class, function, objectID, ...) \
[2937]269  { \
270    NetworkFunctionPointer p1; \
[2948]271    copyPtr( &class::function, p1 ); \
272    FunctionCallManager::addCallMember(NetworkMemberFunctionBase::getFunction(p1)->getNetworkID(), objectID, __VA_ARGS__); \
[2937]273  }
274
275
276}
277
[3214]278#endif /* _NetworkFunction_H__ */
Note: See TracBrowser for help on using the repository browser.