Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

refactored the interface of NetworkFunctionManager: maps are better encapsulated now

  • Property svn:eol-style set to native
File size: 7.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, const NetworkFunctionPointer& pointer);
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 this->name_; }
77    inline const NetworkFunctionPointer& getPointer() const { return this->pointer_; }
78
79    virtual bool call(uint32_t objectID)=0;
80    virtual bool call(uint32_t objectID, const MultiType& mt1)=0;
81    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0;
82    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0;
83    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0;
84    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0;
85
86  private:
87    uint32_t networkID_;
88    std::string name_;
89    NetworkFunctionPointer pointer_;
90
91};
92
93
94class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase {
95  public:
96    NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p)
97        : NetworkFunctionBase(name, p)
98        , functor_(functor)
99    { }
100
101    // ignore the objectID because its a static function
102    virtual bool call(uint32_t objectID){ (*this->functor_)(); return true; }
103    virtual bool call(uint32_t objectID, const MultiType& mt1){ (*this->functor_)(mt1); return true; }
104    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); return true; }
105    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); return true; }
106    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); return true; }
107    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5){ (*this->functor_)(mt1, mt2, mt3, mt4, mt5); return true; }
108
109  private:
110    FunctorStaticPtr functor_;
111
112};
113
114
115class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase {
116  public:
117    NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p)
118        : NetworkFunctionBase(name, p)
119    { }
120};
121
122
123template <class T> class NetworkMemberFunction: public NetworkMemberFunctionBase {
124  public:
125    NetworkMemberFunction(const FunctorMemberPtr<T>& functor, const std::string& name, const NetworkFunctionPointer& p)
126        : NetworkMemberFunctionBase(name, p)
127        , functor_(functor)
128    { }
129
130    inline bool call(uint32_t objectID)
131    {
132      if ( Synchronisable::getSynchronisable(objectID)!=0 )
133      {
134        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)));
135        return true;
136      }
137      else
138        return false;
139    }
140    inline bool call(uint32_t objectID, const MultiType& mt1)
141    {
142      if ( Synchronisable::getSynchronisable(objectID)!=0 )
143      {
144        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1);
145        return true;
146      }
147      else
148        return false;
149    }
150    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)
151    {
152      if ( Synchronisable::getSynchronisable(objectID)!=0 )
153      {
154        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2);
155        return true;
156      }
157      else
158        return false;
159    }
160    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
161    {
162      if ( Synchronisable::getSynchronisable(objectID)!=0 )
163      {
164        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3);
165        return true;
166      }
167      else
168        return false;
169    }
170    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
171    {
172      if ( Synchronisable::getSynchronisable(objectID)!=0 )
173      {
174        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4);
175        return true;
176      }
177      else
178        return false;
179    }
180    inline bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
181    {
182      if ( Synchronisable::getSynchronisable(objectID)!=0 )
183      {
184        (*this->functor_)(orxonox_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5);
185        return true;
186      }
187      else
188        return false;
189    }
190
191  private:
192    FunctorMemberPtr<T> functor_;
193};
194
195template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)
196{
197  if( sizeof(NetworkFunctionPointer)-sizeof(T) > 0)
198    memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));
199  T p2 = ptr;
200  memcpy( &destptr, &p2, sizeof(T) );
201//   for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++)
202//     *((uint32_t*)destptr+i) = p2>>32*i;
203}
204
205}
206
207#endif /* _NetworkFunction_H__ */
Note: See TracBrowser for help on using the repository browser.