工具类库系列(十)-Object
来源:互联网 发布:21周胎儿发育标准数据 编辑:程序博客网 时间:2024/06/06 12:36
第十个工具类:Object
Object是作为很多类的基类来使用的
用来实现提供给的每个类对象一个唯一的内存guid的功能,方便做map
Object封装一个无符号长整型的m_id,
定义一个全局的object_guid,从1开始自增,在Object的构造函数中自增,保留0用来表示对象不存在
在多线程环境下,为了保证object_guid自增的线程安全,用到了原子操作atomic,linux下面就是__sync_fetch_and_add
上代码:
Object.h
#ifndef __Object_h__#define __Object_h__#include "ToolDefine.h"namespace common{namespace tool{class Object{public:Object();Object(const Object& other);virtual ~Object();Object& operator=(const Object& other);inline objectid64 id() const{return m_id;}private:objectid64 m_id;};}}#endif
Object.cpp
#include "Object.h"#ifdef WIN32#include <atomic>#else#endifnamespace common{namespace tool{#ifdef WIN32std::atomic<objectid64> g_object_guid = 1;#elseobjectid64 g_object_guid = 1;#endifObject::Object(){#ifdef WIN32m_id = g_object_guid++;#elsem_id = __sync_fetch_and_add(&g_object_guid, 1);#endif}Object::Object(const Object& other){#ifdef WIN32m_id = g_object_guid++;#elsem_id = __sync_fetch_and_add(&g_object_guid, 1);#endif}Object::~Object(){}Object& Object::operator=(const Object& other){return *this;}}}
其中ToolDefine.h中定义了
// obj idtypedef unsigned long long objectid64;// Object 对象的无效id,可以表示对象不存在const objectid64 NULL_ID = 0;
0 0
- 工具类库系列(十)-Object
- C#高级编程十七天----Object类
- 《软件世界:使用工具vim十例》系列讲座
- Ember旅程系列(十) -- 服务与工具
- 工具类库系列(一)-StringTool
- 工具类库系列(二)-ExePath
- 工具类库系列(三)-IniReader
- 工具类库系列(四)-CsvReader
- 工具类库系列(五)-Timer
- 工具类库系列(六)-TimeTool
- 工具类库系列(七)-Logger
- 工具类库系列(八)-WinService
- 工具类库系列(九)-ReflectEnum
- 工具类库系列(十一)-ObjectMap
- 工具类库系列(十二)-ObjectSharedPtrMap
- 工具类库系列(十三)-ObjectSharedPtrMap2
- 工具类库系列(十四)-FileData
- 工具类系列-JavaBean2Map
- Java中HashMap遍历的两种方式
- ubuntu常用命令
- Android界面布局
- jQuery-Ajax
- 散列-PAT.A1078 Hashing
- 工具类库系列(十)-Object
- Spark:Yarn-cluster和Yarn-client区别与联系
- Java枚举的七种常见用法
- linux中vim的配置
- Makefile通用版
- vertica数据库备份与恢复
- iOS10从app跳转到WiFi设置界面
- Callable和Runnable
- 模板缓冲区