用 boost::multi_index 管理玩家

(金庆的专栏)

网游服务器上的玩家集合需要多种索引:如用ID查找,角色名查找, 用登录时分配的会话ID查找。
用boost::multi_index进行玩家的管理,可在该容器上建立多种索引。

class Player
{
public:
    const PlayerId & GetId() const;
    const std::string & GetName() const;
    const SessionId & GetSessionId() const;
    ...
};

typedef boost::shared_ptr<Player> PlayerPtr;

struct tagName {};

typedef boost::multi_index::multi_index_container
<
    PlayerPtr,
    index_by
    <
        hashed_unique
        <
            tag<PlayerId>,
            member<Player, PlayerId, &Player::GetId>
        >,  // hashed_unique

        hashed_unique
        <
            tag<tagName>,
            member<Player, std::string, &Player::GetName>
        >,  // hashed_unique

        hashed_unique
        <
            tag<SessionId>,
            member<Player, SessionId, &Player::GetSessionId>
        >  // hashed_unique
    >  // index_by
> PlayerSet;

typedef PlayerSet::index<PlayerId>::type PlayerSetById;
typedef PlayerSet::index<tagName>::type PlayerSetByName;
typedef PlayerSet::index<SessionId>::type PlayerSetBySessionId;

使用如:

PlayerSet setPlayers;
PlayerSetById & rSet = setPlayers.get<PlayerId>();
PlayerSetById::const_iterator itr = rSet.find(id);

用 boost::multi_index 管理玩家的更多相关文章

  1. boost::multi_index 提供一种千人在线即时排行榜的设计思路

    原文地址: http://www.limerence2017.com/2019/06/23/cpp01/ 做游戏或金融后台开发,经常会遇到设计开发排行榜的需求.比如玩家的充值排行,战力排行等等.而这种 ...

  2. boost multi_index

    /** boost 多索引容器的一般使用 这里使用google 的gmock 库来验证对boost 多索引模板的使用,并验证. 这里是手敲的,可能会有一些字符敲错的情况,编译错误的放,修改一下,同时链 ...

  3. 使用boost::multi_index高速构建排行榜

    使用boost::multi_index高速构建排行榜 前几天在boost的maillist上看到boost::multi_index将要支持ranked_index(邮件内容见附件2),这实乃我等苦 ...

  4. boost::multi_index 多索引容器

    #include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...

  5. boost multi_index 插入返回值

    boost multi_index 对象插入函数emplace() 的返回值,是一个std::pair<iterator, bool>该pair 的first 是一个插入成功的位置,第二个 ...

  6. boost multi_index简单了解

    #include <string> #include <iostream> #include <boost/multi_index_container.hpp> # ...

  7. boost:进程管理

    概述 Boost.Process提供了一个灵活的C++ 进程管理框架.它允许C++ developer可以像Java和.Net程序developer那样管理进程.它还提供了管理当前执行进程上下文.创建 ...

  8. BOOST内存管理-intrusive_ptr

    参考链接https://blog.csdn.net/harbinzju/article/details/6754646 intrusive_ptr 是shared_ptr的插入式版本.与shared_ ...

  9. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

随机推荐

  1. MVC和MTV模式

    著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业务对象与数据库的对象(ORM),视图负责与用户的交 ...

  2. spring-boot配置静态资源映射的坑:properties文件不能添加注释

    如此博文所述,Spring Boot 对静态资源映射提供了默认配置 默认将 /** 所有访问映射到以下目录:classpath:/staticclasspath:/publicclasspath:/r ...

  3. MacOS下postgresql数据库密码的那些事

    如果你是第一次玩postgresql数据库,你会发现你给role或者user明明设置了密码,但在登录的时候毛都不用输入,直接就进去了,怎么那么爽快!? 虽然爽快,但貌似不该这样啊. 其实这些都和一个重 ...

  4. Java对象的创建 —— new之后JVM都做了什么?

    Java对象创建过程 1. 类加载检查 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没 ...

  5. 安卓高级7 vitamio 视频框架 从raw文件下获取文件uri

    vitamio免费的拥有多种解码器 而且容易操作 我们先来看看原生视频播放器的怎么使用 原生的: package qianfeng.com.videoviewdemo; import android. ...

  6. JS——2048(支持触屏及键盘操作)

    <html> <head> <title>2048</title> <style type="text/css"> ta ...

  7. 《Non-Negative Matrix Factorization for Polyphonic Music Transcription》译文

    NMF(非负矩阵分解),由于其分解出的矩阵是非负的,在一些实际问题中具有非常好的解释,因此用途很广.在此,我给大家介绍一下NMF在多声部音乐中的应用.要翻译的论文是利用NMF转录多声部音乐的开山之作, ...

  8. SOAP Binding: Difference between Document and RPC Style Web Services

    SOAP Binding: Difference between Document and RPC Style Web Services 20FLARES Twitter 1Facebook 9Goo ...

  9. Core Python Programming一书中关于深浅拷贝的错误

    该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...

  10. Dynamics CRM2013 6.1.1.1143版本插件注册器的一个bug

    最近在做的项目客户用的是CRM2013sp1版本,所以插件注册器使用的也是与之对应的6.1.1.1143,悲剧的事情也因此而开始. 在插件中注册step时,工具里有个run in user's con ...