Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/network/NetworkFunction.h @ 2945

Last change on this file since 2945 was 2944, checked in by scheusso, 16 years ago

this is another commit for testing purpose
still trying to get network function calls to work (first success)

File size: 9.0 KB
RevLine 
[2937]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#include "core/OrxonoxClass.h"
34
35#include <string>
36#include <map>
37#include <cassert>
38#include "util/MultiType.h"
39#include "synchronisable/Synchronisable.h"
40#include "FunctionCallManager.h"
41
42
43namespace orxonox
44{
45
46#ifdef ORXONOX_COMPILER_GCC
47static const unsigned int MAX_FUNCTION_POINTER_SIZE = 8;
48#else //ORXONOX_COMPILER_GCC
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#ifdef ORXONOX_COMPILER_GCC
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
[2943]69class _NetworkExport NetworkFunctionBase: virtual public OrxonoxClass {
[2937]70  public:
71    NetworkFunctionBase(std::string name);
72    ~NetworkFunctionBase();
73   
74    inline void         setNetworkID(uint32_t id)       { this->networkID_ = id; }
75    inline uint32_t     getNetworkID() const            { return this->networkID_; }
76    inline std::string  getName() const                 { return name_; }
77    static inline bool  isStatic( uint32_t networkID )  { return isStaticMap_[networkID]; }
78   
79    static inline void setNetworkID(std::string name, uint32_t id){ assert( nameMap_.find(name)!=nameMap_.end() ); nameMap_[name]->setNetworkID(id); }
80   
[2944]81  protected:
82    static std::map<uint32_t, bool> isStaticMap_;
83   
[2937]84  private:
85    static std::map<std::string, NetworkFunctionBase*> nameMap_;
86    uint32_t networkID_;
87    std::string name_;
88     
89};
90
91
92class _NetworkExport NetworkFunctionStatic: public NetworkFunctionBase {
93  public:
[2944]94    NetworkFunctionStatic(FunctorStatic* functor, std::string name, const NetworkFunctionPointer& p);
[2937]95    ~NetworkFunctionStatic();
96   
97    inline void call(){ (*this->functor_)(); }
98    inline void call(const MultiType& mt1){ (*this->functor_)(mt1); }
99    inline void call(const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(mt1, mt2); }
100    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(mt1, mt2, mt3); }
101    inline void call(const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(mt1, mt2, mt3, mt4); }
102    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); }
103   
104    static inline NetworkFunctionStatic* getNetworkFunction( uint32_t id){ assert( idMap_.find(id)!=idMap_.end() ); return idMap_[id]; }
105    static NetworkFunctionStatic* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; }
106    static NetworkFunctionStatic* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; }
107   
108  private:
109    static std::map<NetworkFunctionPointer, NetworkFunctionStatic*> functorMap_;
110    static std::map<uint32_t, NetworkFunctionStatic*> idMap_;
111   
112    FunctorStatic* functor_;
113   
114};
115
116
[2941]117class _NetworkExport NetworkMemberFunctionBase: public NetworkFunctionBase {
[2937]118  public:
119    NetworkMemberFunctionBase(std::string name, const NetworkFunctionPointer& p);
120    ~NetworkMemberFunctionBase();
121   
122    static inline NetworkMemberFunctionBase* getNetworkFunction( uint32_t id){ assert( idMap_.find(id)!=idMap_.end() ); return idMap_[id]; }
123    static NetworkMemberFunctionBase* getFunction( uint32_t id ){ assert( idMap_.find(id) != idMap_.end() ); return idMap_[id]; }
124    static NetworkMemberFunctionBase* getFunction( const NetworkFunctionPointer& p ){ assert( functorMap_.find(p) != functorMap_.end() ); return functorMap_[p]; }
125   
126    //
127    virtual void call(uint32_t objectID)=0;
128    virtual void call(uint32_t objectID, const MultiType& mt1)=0;
129    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2)=0;
130    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)=0;
131    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)=0;
132    virtual void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)=0;
133   
134  private:
135    static std::map<NetworkFunctionPointer, NetworkMemberFunctionBase*> functorMap_;
136    static std::map<uint32_t, NetworkMemberFunctionBase*> idMap_;
137};
138
139
140template <class T> class _NetworkExport NetworkMemberFunction: public NetworkMemberFunctionBase {
141  public:
142    NetworkMemberFunction(FunctorMember<T>* functor, std::string name, const NetworkFunctionPointer& p);
143    ~NetworkMemberFunction();
144   
145    inline void call(uint32_t objectID){ (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID))); }
146    inline void call(uint32_t objectID, const MultiType& mt1){ (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1); }
147    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2){ (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2); }
148    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3){ (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3); }
149    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4){ (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4); }
150    inline void call(uint32_t objectID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5){ (*this->functor_)(dynamic_cast<T*>(Synchronisable::getSynchronisable(objectID)), mt1, mt2, mt3, mt4, mt5); }
151   
152  private:
153    FunctorMember<T>* functor_;
154};
155
[2944]156template <class T> NetworkMemberFunction<T>::NetworkMemberFunction(FunctorMember<T>* functor, std::string name, const NetworkFunctionPointer& p):
157    NetworkMemberFunctionBase(name, p), functor_(functor)
158{
159}
[2937]160
[2944]161
[2937]162template<class T> inline void copyPtr( T ptr, NetworkFunctionPointer& destptr)
163{
[2943]164  memset((uint8_t*)&destptr + sizeof(T), 0, sizeof(NetworkFunctionPointer)-sizeof(T));
[2937]165  T p2 = ptr;
166  memcpy( &destptr, &p2, sizeof(T) );
167//   for(unsigned int i=0; i<(sizeof(T)-1/4)+1; i++)
168//     *((uint32_t*)destptr+i) = p2>>32*i;
169}
170
171template<class T> inline void* registerStaticNetworkFunctionFct( T ptr, std::string name )
172{
173  NetworkFunctionPointer destptr;
174  copyPtr( ptr, destptr );
[2943]175  COUT(0) << "-==================== destptr: " << destptr.pointer[0] << ", " << destptr.pointer[1] << endl;
[2937]176  new NetworkFunctionStatic( createFunctor(ptr), name, destptr );
177  return 0;
178}
179
180template<class T, class PT> inline void* registerMemberNetworkFunctionFct( PT ptr, std::string name )
181{
182  NetworkFunctionPointer destptr;
183  copyPtr( ptr, destptr );
184  new NetworkMemberFunction<T>( new FunctorMember<T>(ptr), name, destptr );
185  return 0;
186}
187
188#define registerStaticNetworkFunction( functionPointer, name ) \
189  void* NETWORK_FUNCTION_name##a = registerStaticNetworkFunctionFct( functionPointer, name );
190#define registerMemberNetworkFunction( functionPointer, class, name ) \
191  void* NETWORK_FUNCTION_class##name##a = registerMemberNetworkFunction<class>( functionPointer, name );
192#define callStaticNetworkFunction( functionPointer, ...) \
193  { \
194    NetworkFunctionPointer p1; \
195    copyPtr( functionPointer, p1 ); \
[2943]196    FunctionCallManager::addCallStatic(NetworkFunctionStatic::getFunction(p1)->getNetworkID(), __VA_ARGS__); \
[2937]197  }
198#define callMemberNetworkFunction( functionPointer, objectID, ...) \
199  { \
200    NetworkFunctionPointer p1; \
201    copyPtr( functionPointer, p1 ); \
202    FunctionCallManager::addCallMember(NetworkMemberFunctionBase::getFunction(p1)->getNetworkID(), objectID, __VA_ARGS__) \
203  }
204
205
206}
207
208#endif
Note: See TracBrowser for help on using the repository browser.