Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/network/FunctionCall.cc @ 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: 5.5 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 <scheusso [at] orxonox.net>, (C) 2010
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "FunctionCall.h"
30
31#include <cassert>
32#include "util/MultiType.h"
33#include "NetworkFunction.h"
34#include "NetworkFunctionManager.h"
35
36namespace orxonox {
37
38FunctionCall::FunctionCall()
39 : nrOfArguments_(-1), objectID_(OBJECTID_UNKNOWN), size_(0)
40{
41}
42
43FunctionCall::~FunctionCall()
44{
45}
46
47
48bool FunctionCall::execute(){
49  NetworkFunctionBase* fct = static_cast<NetworkFunctionStatic*>(NetworkFunctionManager::getFunction( this->functionID_ ));
50  assert( this->nrOfArguments_==this->arguments_.size() );
51  switch(this->nrOfArguments_)
52  {
53    case 0:
54      return !fct->call(this->objectID_);
55    case 1:
56      return !fct->call(this->objectID_, this->arguments_[0]);
57    case 2:
58      return !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1]);
59    case 3:
60      return !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2]);
61    case 4:
62      return !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3]);
63    case 5:
64      return !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3], this->arguments_[4]);
65    default:
66      assert(0);
67      return true; // return true to avoid that this functions gets called over and over again
68  }
69}
70
71void FunctionCall::setCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
72
73  // first determine the size that has to be reserved for this call
74  uint32_t callsize = 2*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and for bool isStatic
75  uint32_t nrOfArguments = 0;
76  if(mt1)
77  {
78    nrOfArguments++;
79    callsize += mt1->getNetworkSize();
80    this->arguments_.push_back(*mt1);
81    if(mt2)
82    {
83      nrOfArguments++;
84      callsize += mt2->getNetworkSize();
85      this->arguments_.push_back(*mt2);
86      if(mt3)
87      {
88        nrOfArguments++;
89        callsize += mt3->getNetworkSize();
90        this->arguments_.push_back(*mt3);
91        if(mt4)
92        {
93          nrOfArguments++;
94          callsize += mt4->getNetworkSize();
95          this->arguments_.push_back(*mt4);
96          if(mt5)
97          {
98            nrOfArguments++;
99            callsize += mt5->getNetworkSize();
100            this->arguments_.push_back(*mt5);
101          }
102        }
103      }
104    }
105  }
106  this->nrOfArguments_ = nrOfArguments;
107  this->size_ = callsize;
108  this->functionID_ = networkID;
109}
110
111void FunctionCall::setCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){
112
113  // first determine the size that has to be reserved for this call
114  uint32_t callsize = 3*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and the objectID and bIsStatic
115  uint32_t nrOfArguments = 0;
116  if(mt1)
117  {
118    nrOfArguments++;
119    callsize += mt1->getNetworkSize();
120    this->arguments_.push_back(*mt1);
121    if(mt2)
122    {
123      nrOfArguments++;
124      callsize += mt2->getNetworkSize();
125      this->arguments_.push_back(*mt2);
126      if(mt3)
127      {
128        nrOfArguments++;
129        callsize += mt3->getNetworkSize();
130        this->arguments_.push_back(*mt3);
131        if(mt4)
132        {
133          nrOfArguments++;
134          callsize += mt4->getNetworkSize();
135          this->arguments_.push_back(*mt4);
136          if(mt5)
137          {
138            nrOfArguments++;
139            callsize += mt5->getNetworkSize();
140            this->arguments_.push_back(*mt5);
141          }
142        }
143      }
144    }
145  }
146  this->nrOfArguments_ = nrOfArguments;
147  this->functionID_ = networkID;
148  this->size_ = callsize;
149  this->objectID_ = objectID;
150}
151
152void FunctionCall::loadData(uint8_t*& mem)
153{
154  this->functionID_ = *(uint32_t*)mem;
155  this->nrOfArguments_ = *(uint32_t*)(mem+sizeof(uint32_t));
156  this->objectID_ = *(uint32_t*)(mem+2*sizeof(uint32_t));
157  mem += 3*sizeof(uint32_t);
158  for( unsigned int i=0; i<this->nrOfArguments_; ++i )
159  {
160    this->arguments_.push_back(MultiType());
161    this->arguments_.back().importData(mem);
162  }
163}
164
165void FunctionCall::saveData(uint8_t*& mem)
166{
167  // now serialise the mt values and copy the function id and isStatic
168  *(uint32_t*)mem = this->functionID_;
169  *(uint32_t*)(mem+sizeof(uint32_t)) = this->nrOfArguments_;
170  *(uint32_t*)(mem+2*sizeof(uint32_t)) = this->objectID_;
171  mem += 3*sizeof(uint32_t);
172  for( std::vector<MultiType>::iterator it = this->arguments_.begin(); it!=this->arguments_.end(); ++it )
173  {
174    it->exportData( mem );
175  }
176}
177
178
179
180} //namespace orxonox
Note: See TracBrowser for help on using the repository browser.