Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

moved macro ans helperfunction from NetworkFunction.h to NetworkFunctionIncludes.h

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