Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10475 for code


Ignore:
Timestamp:
May 25, 2015, 2:41:57 PM (9 years ago)
Author:
landauf
Message:

refactored the interface of NetworkFunctionManager: maps are better encapsulated now

Location:
code/branches/core7/src/libraries/network
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/network/FunctionCall.cc

    r10474 r10475  
    4747
    4848bool FunctionCall::execute(){
    49   NetworkFunctionBase* fct = static_cast<NetworkFunctionStatic*>(NetworkFunctionManager::getInstance().getFunction( this->functionID_ ));
     49  NetworkFunctionBase* fct = static_cast<NetworkFunctionStatic*>(NetworkFunctionManager::getInstance().getFunctionByNetworkId( this->functionID_ ));
    5050  assert( this->nrOfArguments_==this->arguments_.size() );
    5151  switch(this->nrOfArguments_)
  • code/branches/core7/src/libraries/network/NetworkFunction.cc

    r10474 r10475  
    3232namespace orxonox
    3333{
    34     NetworkFunctionBase::NetworkFunctionBase(const std::string& name, const NetworkFunctionPointer& p)
     34    NetworkFunctionBase::NetworkFunctionBase(const std::string& name, const NetworkFunctionPointer& pointer)
    3535    {
    3636        static uint32_t networkID = 0;
    3737        this->networkID_ = networkID++;
    38 
    3938        this->name_ = name;
    40         NetworkFunctionManager::getInstance().getNameMap()[name] = this;
    41         NetworkFunctionManager::getInstance().getFunctorMap()[p] = this;
    42         NetworkFunctionManager::getInstance().getIdMap()[this->getNetworkID()] = this;
     39        this->pointer_ = pointer;
     40        NetworkFunctionManager::getInstance().registerFunction(this);
    4341    }
    4442
    4543    void NetworkFunctionBase::setNetworkID(uint32_t id)
    4644    {
    47         NetworkFunctionManager::getInstance().getIdMap().erase(this->networkID_);  // remove old id
     45        NetworkFunctionManager::getInstance().unregisterFunction(this); // unregister with old id
    4846        this->networkID_ = id;
    49         NetworkFunctionManager::getInstance().getIdMap()[this->networkID_] = this; // add new id
     47        NetworkFunctionManager::getInstance().registerFunction(this);   // register with new id
    5048    }
    5149}
  • code/branches/core7/src/libraries/network/NetworkFunction.h

    r10472 r10475  
    6969class _NetworkExport NetworkFunctionBase {
    7070  public:
    71     NetworkFunctionBase(const std::string& name, const NetworkFunctionPointer& p);
     71    NetworkFunctionBase(const std::string& name, const NetworkFunctionPointer& pointer);
    7272    virtual ~NetworkFunctionBase() {}
    7373
    7474    void setNetworkID(uint32_t id);
    75     inline uint32_t     getNetworkID() const            { return this->networkID_; }
    76     inline const std::string& getName() const           { return name_; }
     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_; }
    7778
    7879    virtual bool call(uint32_t objectID)=0;
     
    8687    uint32_t networkID_;
    8788    std::string name_;
     89    NetworkFunctionPointer pointer_;
    8890
    8991};
  • code/branches/core7/src/libraries/network/NetworkFunctionIncludes.h

    r10474 r10475  
    5151            NetworkFunctionPointer p1; \
    5252            copyPtr( functionPointer, p1 ); \
    53             FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunction(p1)->getNetworkID(), OBJECTID_UNKNOWN, __VA_ARGS__); \
     53            FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), OBJECTID_UNKNOWN, __VA_ARGS__); \
    5454        }
    5555
     
    5959            NetworkFunctionPointer p1; \
    6060            copyPtr( &class::function, p1 ); \
    61             FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunction(p1)->getNetworkID(), objectID, __VA_ARGS__); \
     61            FunctionCallManager::addCall(NetworkFunctionManager::getInstance().getFunctionByFunctionPointer(p1)->getNetworkID(), objectID, __VA_ARGS__); \
    6262        }
    6363
  • code/branches/core7/src/libraries/network/NetworkFunctionManager.cc

    r10474 r10475  
    3838    }
    3939
    40     void NetworkFunctionManager::setNetworkID(const std::string& name, uint32_t id)
     40    void NetworkFunctionManager::registerFunction(NetworkFunctionBase* function)
    4141    {
    42         std::map<std::string, NetworkFunctionBase*>& map = this->nameMap_;
    43         assert( map.find(name)!=map.end() );
    44         map[name]->setNetworkID(id);
     42        this->functions_.insert(function);
     43        this->nameMap_[function->getName()] = function;
     44        this->idMap_[function->getNetworkID()] = function;
     45        this->functorMap_[function->getPointer()] = function;
     46    }
     47
     48    void NetworkFunctionManager::unregisterFunction(NetworkFunctionBase* function)
     49    {
     50        this->functions_.erase(function);
     51        this->nameMap_.erase(function->getName());
     52        this->idMap_.erase(function->getNetworkID());
     53        this->functorMap_.erase(function->getPointer());
    4554    }
    4655
    4756    void NetworkFunctionManager::destroyAllNetworkFunctions()
    4857    {
    49         std::map<std::string, NetworkFunctionBase*>& map = this->nameMap_;
    50         std::map<std::string, NetworkFunctionBase*>::iterator it;
    51         for (it = map.begin(); it != map.end(); ++it)
    52             delete it->second;
     58        std::set<NetworkFunctionBase*>::iterator it;
     59        for (it = this->functions_.begin(); it != this->functions_.end(); ++it)
     60            delete (*it);
    5361    }
    5462
    55     NetworkFunctionBase* NetworkFunctionManager::getFunction(const NetworkFunctionPointer& p)
     63    NetworkFunctionBase* NetworkFunctionManager::getFunctionByName(const std::string& name)
     64    {
     65        std::map<std::string, NetworkFunctionBase*>::iterator it = nameMap_.find(name);
     66        assert(it != nameMap_.end());
     67        return it->second;
     68    }
     69
     70    NetworkFunctionBase* NetworkFunctionManager::getFunctionByFunctionPointer(const NetworkFunctionPointer& p)
    5671    {
    5772        std::map<NetworkFunctionPointer, NetworkFunctionBase*>::iterator it = functorMap_.find(p);
     
    6075    }
    6176
    62     NetworkFunctionBase* NetworkFunctionManager::getFunction(uint32_t id)
     77    NetworkFunctionBase* NetworkFunctionManager::getFunctionByNetworkId(uint32_t id)
    6378    {
    6479        std::map<uint32_t, NetworkFunctionBase*>::iterator it = idMap_.find(id);
  • code/branches/core7/src/libraries/network/NetworkFunctionManager.h

    r10474 r10475  
    3434#include <cassert>
    3535#include <map>
     36#include <set>
    3637
    3738namespace orxonox
     
    4243            static NetworkFunctionManager& getInstance();
    4344
    44             void setNetworkID(const std::string& name, uint32_t id);
     45            void registerFunction(NetworkFunctionBase* function);
     46            void unregisterFunction(NetworkFunctionBase* function);
     47
    4548            void destroyAllNetworkFunctions();
    4649
    47             inline std::map<std::string, NetworkFunctionBase*>& getNameMap()
    48                 { return nameMap_; }
    49             inline std::map<NetworkFunctionPointer, NetworkFunctionBase*>& getFunctorMap()
    50                 { return functorMap_; }
    51             inline std::map<uint32_t, NetworkFunctionBase*>& getIdMap()
    52                 { return idMap_; }
     50            inline const std::set<NetworkFunctionBase*>& getAllFunctions()
     51                { return functions_; }
    5352
    54             NetworkFunctionBase* getFunction(const NetworkFunctionPointer& p);
    55             NetworkFunctionBase* getFunction(uint32_t id);
     53            NetworkFunctionBase* getFunctionByName(const std::string& name);
     54            NetworkFunctionBase* getFunctionByFunctionPointer(const NetworkFunctionPointer& p);
     55            NetworkFunctionBase* getFunctionByNetworkId(uint32_t id);
    5656
    5757        private:
     58            std::set<NetworkFunctionBase*> functions_;
    5859            std::map<std::string, NetworkFunctionBase*> nameMap_;
    5960            std::map<NetworkFunctionPointer, NetworkFunctionBase*> functorMap_;
  • code/branches/core7/src/libraries/network/packet/FunctionIDs.cc

    r10474 r10475  
    5656
    5757  //calculate total needed size (for all strings and integers)
    58   std::map<std::string, NetworkFunctionBase*>& map = NetworkFunctionManager::getInstance().getNameMap();
    59   std::map<std::string, NetworkFunctionBase*>::iterator it;
    60   for (it = map.begin(); it != map.end(); ++it)
     58  const std::set<NetworkFunctionBase*>& set = NetworkFunctionManager::getInstance().getAllFunctions();
     59  std::set<NetworkFunctionBase*>::const_iterator it;
     60  for (it = set.begin(); it != set.end(); ++it)
    6161  {
    62     const std::string& functionname = it->second->getName();
    63     networkID = it->second->getNetworkID();
     62    const std::string& functionname = (*it)->getName();
     63    networkID = (*it)->getNetworkID();
    6464    // now push the network id and the classname to the stack
    6565    tempQueue.push( std::pair<unsigned int, std::string>(networkID, functionname) );
     
    140140    functionname = temp+2*sizeof(uint32_t);
    141141    orxout(internal_info, context::packets) << "processing functionid: " << networkID << " name: " << functionname << endl;
    142     NetworkFunctionManager::getInstance().setNetworkID((const char*)functionname, networkID);
     142    NetworkFunctionManager::getInstance().getFunctionByName((const char*)functionname)->setNetworkID(networkID);
    143143    temp += 2*sizeof(uint32_t) + stringsize;
    144144  }
Note: See TracChangeset for help on using the changeset viewer.