上一篇中大概描述了c++ java中關於protobuf反序列化對象實體和實體處理(函數)關係,並貼出了java的實現方案,針對c++版本也只是簡單的描述了一下 採用std::bind().這裏採用 id+ 指針,經過ID索引到指針,利用多態性來處理。java
因爲c++ 不像java能夠有反射,因此這裏實例化引用直接用靜態的指針。交給編譯器處理 在靜態的區來開闢儲存。大概實現以下:ios
基類,全部的處理函數controller均實現該類:
c++
#ifndef __BaseController__ #define __BaseController__ /** * @Author 石頭哥哥, 15-02-08 17:02:32 * * @brief creat a single fot controller * * @param _CLASS_ _CLASS_ controller who had extend base controller * * @return controller */ #ifndef SINGLE_INSTANCE_FUNC #define SINGLE_INSTANCE_FUNC(_CLASS_)\ static _CLASS_* getInstance( ) \ { \ static _CLASS_* _p = NULL; \ if(!_p) _p = new _CLASS_( ); \ return _p; \ } #endif /*********pb頭文件分類***************/ //pb header file #include "robotServer.pb.h" /************************/ #include <stdio.h> #include <iostream> #include "PlayerInstance.h" #include <google/protobuf/message.h> using namespace std; using namespace google_private::protobuf; using namespace google_private::protobuf::io; #include <cocos2d.h> USING_NS_CC; #include <map> /** * @Author 石頭哥哥, 15-02-08 20:02:48 * * @brief this is abstract class */ class BaseController{ public: BaseController(); virtual~BaseController(); /** * @Author 石頭哥哥, 15-02-07 22:02:00 * * @brief 遊戲邏輯處理函數 * * @param player 封裝的遊戲客戶端session對象 player * @param message 對應遊戲pb實體數據 */ virtual void dispatcherMessage(PlayerInstance&player,const string &message)const=0; public: static map<int,BaseController*> CONTROLLER_MAPPERS; /** * @Author 石頭哥哥, 15-02-08 20:02:05 * * @brief <#Description#> */ static void releaseController(); }; #endif /* defined(__BaseController__) */
// // BaseController.cpp // mygame5 // // Created by chenlei on 15/2/7. // // #include "BaseController.h" map<int,BaseController*> BaseController::CONTROLLER_MAPPERS; BaseController::BaseController(){ } BaseController::~BaseController(){} void BaseController::releaseController(){ // for (auto iter=begin(CONTROLLER_MAPPERS); iter!=end(CONTROLLER_MAPPERS); ++iter) { // cout<<"獲取值:"<<iter->second<<" key: "<<iter->first<<endl; // } // for_each(begin(CONTROLLER_MAPPERS), end(CONTROLLER_MAPPERS), [](map<int,BaseController*>::reference entry){// value_type is reference CC_SAFE_DELETE(entry.second); }); CONTROLLER_MAPPERS.clear(); }
子類處理 cpp:session
// // LoginController.cpp // mygame5 // // Created by chenlei on 15/2/8. // // #include "LoginController.h" //init pointer and register function LoginController *LoginController::_login_controller=new LoginController; LoginController::LoginController(){ CONTROLLER_MAPPERS[LoginRequest::ID]=this; } /** * @Author 石頭哥哥, 15-02-08 18:02:35 * * @brief <#Description#> * * @param player#> <#player#> description#> * @param message#> <#message#> description#> */ void LoginController::dispatcherMessage(PlayerInstance &player,const string &message)const{ cout<<message<<endl; /* LoginRequest* LoginRequest::New() const { return new LoginRequest; } careful obj must be delete */ LoginRequest * obj=LoginRequest::default_instance().New(); if (obj->ParsePartialFromString(message)){ //do something } //can use cocos2d macros delete pointer CC_SAFE_DELETE(obj); }
.h:函數
// // LoginController.h // mygame5 // // Created by chenlei on 15/2/8. // // #ifndef _____LoginController__ #define _____LoginController__ #include <stdio.h> #include "BaseController.h" class LoginController : public BaseController { public: LoginController(); public: /** * @Author 石頭哥哥, 15-02-08 18:02:21 * * @brief <#Description#> * * @param player player description * @param message message description */ virtual void dispatcherMessage(PlayerInstance &player,const string &message)const; private: //pointer for hand controller function static LoginController *_login_controller; }; #endif /* defined(_____LoginController__) */
經過一個靜態的map來存儲對應的引用和消息處理ID。前文也提到在客戶端採用map。ui
以上是解析多個pb實體的方案,有更好的 歡迎討論。this
附 示例pb描述文件:google
message LoginRequest { enum msgID { ID = 2999; } //消息編號 required string username = 1; //用戶名 required string password = 2; //密碼 optional uint64 sendTime=3;//發送消息時間戳 }