Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

use the same interface for static an member functions

  • Property svn:eol-style set to native
File size: 7.2 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    virtual bool call(uint32_t objectID)=0;
79    virtual bool call(uint32_t objectID, const MultiType& mt1)=0;
80    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0;
81    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0;
82    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0;
83    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0;
84
85  private:
86    uint32_t networkID_;
87    std::string name_;
88
89};
90
91
92class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase {
93  public:
94    NetworkFunctionStatic(const FunctorStaticPtr& functor, const std::string& name, const NetworkFunctionPointer& p)
95        : NetworkFunctionBase(name, p)
96        , functor_(functor)
97    { }
98
99    // ignore the objectID because its a static function
100    virtual bool call(uint32_t objectID){ (*this->functor_)(); return true; }
101    virtual bool call(uint32_t objectID, const MultiType& mt1){ (*this->functor_)(mt1); return true; }
102    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); return true; }
103    virtual bool call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); return true; }
104    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; }
105    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; }
106
107  private:
108    FunctorStaticPtr functor_;
109
110};
111
112
113class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase {
114  public:
115    NetworkMemberFunctionBase(const std::string& name, const NetworkFunctionPointer& p)
116        : NetworkFunctionBase(name, p)
117    { }
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.