Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/network/NetworkFunction.h @ 10470

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

NetworkFunctionBase doesn't have to inherit from Listable: there's a list of all instances stored in NetworkFunctionManager stored anyway

  • Property svn:eol-style set to native
File size: 8.3 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
34#include <cassert>
35#include <cstring>
36#include <map>
37#include <string>
38
39#include "core/command/Functor.h"
40#include "FunctionCallManager.h"
41#include "synchronisable/Synchronisable.h"
42
43namespace orxonox
44{
45
46#if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32)
47static const unsigned int MAX_FUNCTION_POINTER_SIZE = 8;
48#else
49static const unsigned int MAX_FUNCTION_POINTER_SIZE = 16;
50#endif //ORXONOX_COMPILER_GCC
51static const unsigned int MAX_FUNCTION_POINTER_INTS = (MAX_FUNCTION_POINTER_SIZE-1)/4+1;
52
53struct _NetworkExport NetworkFunctionPointer {
54  uint32_t pointer[MAX_FUNCTION_POINTER_INTS];
55  bool operator<(const NetworkFunctionPointer& b) const
56  {
57#if defined(ORXONOX_COMPILER_GCC) && defined(ORXONOX_ARCH_32)
58    return pointer[0]<b.pointer[0] ? true : pointer[1]<b.pointer[1];
59#else //ORXONOX_COMPILER_GCC
60    return pointer[0]<b.pointer[0] ? true : ( pointer[1]<b.pointer[1] ? true : ( pointer[2]<b.pointer[2] ? true : pointer[3]<b.pointer[3] ) );
61#endif //ORXONOX_COMPILER_GCC
62  }
63};
64
65
66
67
68
69class _NetworkExport NetworkFunctionBase {
70  public:
71    NetworkFunctionBase(const std::string& name);
72    virtual ~NetworkFunctionBase();
73
74    virtual void        setNetworkID(uint32_t id)       { this->networkID_ = id; }
75    inline uint32_t     getNetworkID() const            { return this->networkID_; }
76    inline const std::string& getName() const           { return name_; }
77
78  private:
79    uint32_t networkID_;
80    std::string name_;
81
82};
83
84
85class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase {
86  public:
87    NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p);
88
89    inline void call(){ (*this->functor_)(); }
90    inline void call(const MultiType& mt1){ (*this->functor_)(mt1); }
91    inline void call(const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); }
92    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); }
93    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); }
94    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); }
95
96    virtual void setNetworkID( uint32_t id )
97        { NetworkFunctionBase::setNetworkID( id ); NetworkFunctionStatic::getIdMap()[id] = this; }
98    static NetworkFunctionStatic* getFunction( uint32_t id )
99        { assert( NetworkFunctionStatic::getIdMap().find(id) != NetworkFunctionStatic::getIdMap().end() ); return NetworkFunctionStatic::getIdMap()[id]; }
100    static NetworkFunctionStatic* getFunction( const NetworkFunctionPointer& p )
101        { assert( NetworkFunctionStatic::getFunctorMap().find(p) != NetworkFunctionStatic::getFunctorMap().end() ); return NetworkFunctionStatic::getFunctorMap()[p]; }
102
103  private:
104    static std::map<NetworkFunctionPointer, NetworkFunctionStatic*>& getFunctorMap();
105    static std::map<uint32_t, NetworkFunctionStatic*>& getIdMap();
106    FunctorStaticPtr functor_;
107
108};
109
110
111class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase {
112  public:
113    NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p);
114    ~NetworkMemberFunctionBase();
115
116    virtual void setNetworkID( uint32_t id ){ NetworkFunctionBase::setNetworkID( id ); idMap_[id] = this; }
117    static NetworkMemberFunctionBase* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; }
118    static NetworkMemberFunctionBase* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; }
119
120    //
121    virtual bool call(uint32_t objectID)=0;
122    virtual bool call(uint32_t objectID, const MultiType& mt1)=0;
123    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0;
124    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0;
125    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0;
126    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0;
127
128  private:
129    static std::map<NetworkFunctionPointer, NetworkMemberFunctionBase*> functorMap_;
130    static std::map<uint32_t, NetworkMemberFunctionBase*> idMap_;
131};
132
133
134template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase {
135  public:
136    NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p);
137
138    inline bool call(uint32_t objectID)
139    {
140      if ( Synchronisable::getSynchronisable(objectID)!=0 )
141      {
142        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)));
143        return true;
144      }
145      else
146        return false;
147    }
148    inline bool call(uint32_t objectID, const MultiType& mt1)
149    {
150      if ( Synchronisable::getSynchronisable(objectID)!=0 )
151      {
152        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1);
153        return true;
154      }
155      else
156        return false;
157    }
158    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)
159    {
160      if ( Synchronisable::getSynchronisable(objectID)!=0 )
161      {
162        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2);
163        return true;
164      }
165      else
166        return false;
167    }
168    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
169    {
170      if ( Synchronisable::getSynchronisable(objectID)!=0 )
171      {
172        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3);
173        return true;
174      }
175      else
176        return false;
177    }
178    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
179    {
180      if ( Synchronisable::getSynchronisable(objectID)!=0 )
181      {
182        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4);
183        return true;
184      }
185      else
186        return false;
187    }
188    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
189    {
190      if ( Synchronisable::getSynchronisable(objectID)!=0 )
191      {
192        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5);
193        return true;
194      }
195      else
196        return false;
197    }
198
199  private:
200    FunctorMemberPtr<T> functor_;
201};
202
203template <class T> NetworkMemberFunction<T>::NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p):
204    NetworkMemberFunctionBase(name, p), functor_(functor)
205{
206}
207
208template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)
209{
210  if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0)
211    memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));
212  T p2 = ptr;
213  memcpy( &destptr, &p2, sizeof(T) );
214//   for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++)
215//     *((uint32_t*)destptr+i) = p2>>32*i;
216}
217
218}
219
220#endif /* _NetworkFunction_H__ */
Note: See TracBrowser for help on using the repository browser.