Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

moved static maps from NetworkFunctionStatic and NetworkMemberFunctionBase to NetworkFunctionManager

  • Property svn:eol-style set to native
File size: 6.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
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, const NetworkFunctionPointer& p);
72    virtual ~NetworkFunctionBase() {}
73
74    void setNetworkID(uint32_t 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        : NetworkFunctionBase(name, p)
89        , functor_(functor)
90    { }
91
92    inline void call(){ (*this->functor_)(); }
93    inline void call(const MultiType& mt1){ (*this->functor_)(mt1); }
94    inline void call(const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); }
95    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); }
96    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); }
97    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); }
98
99  private:
100    FunctorStaticPtr functor_;
101
102};
103
104
105class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase {
106  public:
107    NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p)
108        : NetworkFunctionBase(name, p)
109    { }
110
111    //
112    virtual bool call(uint32_t objectID)=0;
113    virtual bool call(uint32_t objectID, const MultiType& mt1)=0;
114    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0;
115    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0;
116    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0;
117    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0;
118};
119
120
121template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase {
122  public:
123    NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p)
124        : NetworkMemberFunctionBase(name, p)
125        , functor_(functor)
126    { }
127
128    inline bool call(uint32_t objectID)
129    {
130      if ( Synchronisable::getSynchronisable(objectID)!=0 )
131      {
132        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)));
133        return true;
134      }
135      else
136        return false;
137    }
138    inline bool call(uint32_t objectID, const MultiType& mt1)
139    {
140      if ( Synchronisable::getSynchronisable(objectID)!=0 )
141      {
142        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1);
143        return true;
144      }
145      else
146        return false;
147    }
148    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)
149    {
150      if ( Synchronisable::getSynchronisable(objectID)!=0 )
151      {
152        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2);
153        return true;
154      }
155      else
156        return false;
157    }
158    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
159    {
160      if ( Synchronisable::getSynchronisable(objectID)!=0 )
161      {
162        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3);
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, const MultiType& mt4)
169    {
170      if ( Synchronisable::getSynchronisable(objectID)!=0 )
171      {
172        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4);
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, const MultiType& mt5)
179    {
180      if ( Synchronisable::getSynchronisable(objectID)!=0 )
181      {
182        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5);
183        return true;
184      }
185      else
186        return false;
187    }
188
189  private:
190    FunctorMemberPtr<T> functor_;
191};
192
193template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)
194{
195  if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0)
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
203}
204
205#endif /* _NetworkFunction_H__ */
Note: See TracBrowser for help on using the repository browser.