如何复现私有库API的类+typedef实现方式?(扩展私有库场景)
理解并实现类似
xrchandle的智能指针与扩展类 我来帮你拆解这个问题,一步步搞懂怎么实现和原私有库一致的扩展结构,先从核心原理说起,再补全代码。
一、先搞懂现有代码的核心逻辑
你给出的xrchandle是一个自定义引用计数智能指针模板类,作用和std::shared_ptr类似,但专门适配xobject派生类。背后的核心原理是:
- 它通过
attach、release方法管理对象的引用计数(xobject基类肯定包含AddRef()和Release()这类方法,虽然你没给出,但从智能指针的行为能推断出来) - 当把原始指针赋值给
xrchandle时,自动调用对象的AddRef()增加引用计数 - 当
xrchandle被销毁或重新赋值时,调用release(),内部触发对象的Release(),如果引用计数降到0,自动销毁对象 uifcColor_ptr只是xrchandle<uifcColor>的别名(typedef),用来简化书写,和using uifcColor_ptr = std::shared_ptr<uifcColor>是一个道理
二、补全xrchandle的核心实现(基于声明推断)
你只有xrchandle的头文件声明,我来补全关键方法的实现,帮你看清它的工作方式:
// 先补全xobject基类的核心接口(原私有库肯定有这个) class xobject { public: virtual void AddRef() = 0; virtual void Release() = 0; protected: virtual ~xobject() = default; // 虚析构确保派生类能正确销毁 }; // 假设xrcbasehandle是存储原始指针的基础类,包含xobject_child* m_obj成员 template <class T> class xrcbasehandle { protected: T* m_obj = nullptr; public: xrcbasehandle(T* obj) : m_obj(obj) {} ~xrcbasehandle() = default; }; // 补全xrchandle的实现 template <class xobject_child> class xrchandle : public xrcbasehandle <xobject_child> { public: inline xrchandle() : xrcbasehandle<xobject_child>(nullptr) {} #ifdef OSAPI_WIN32 inline xrchandle(int in_obj) : xrcbasehandle<xobject_child>(reinterpret_cast<xobject_child*>(in_obj)) { if (this->m_obj) this->m_obj->AddRef(); } #endif inline xrchandle(xobject_child *in_obj) : xrcbasehandle<xobject_child>(in_obj) { if (this->m_obj) this->m_obj->AddRef(); } inline xrchandle(const xrchandle<xobject_child> ©) : xrcbasehandle<xobject_child>(copy.m_obj) { if (this->m_obj) this->m_obj->AddRef(); } inline xrchandle(const xrcunihandle<xobject_child> ©) : xrcbasehandle<xobject_child>(copy.get()) { if (this->m_obj) this->m_obj->AddRef(); } inline ~xrchandle() { release(); } inline operator xobject_child * () const { return this->m_obj; } inline xrchandle<xobject_child> &operator= (xobject_child *in_obj) { set(in_obj); return *this; } inline xrchandle<xobject_child> &operator= (const xrchandle<xobject_child> ©) { set(copy.m_obj); return *this; } #ifdef PTC_CXX11_RVALUE_REFERENCES inline xrchandle(xrchandle<xobject_child> &©) : xrcbasehandle<xobject_child>(copy.m_obj) { copy.m_obj = nullptr; // 转移所有权,原指针不再管理对象 } inline xrchandle<xobject_child> &operator=(xrchandle<xobject_child> &©) { if (this != ©) { release(); this->m_obj = copy.m_obj; copy.m_obj = nullptr; } return *this; } #endif private: inline void attach(xobject_child *in_obj) { if (in_obj) in_obj->AddRef(); this->m_obj = in_obj; } inline void release() { if (this->m_obj) { this->m_obj->Release(); this->m_obj = nullptr; } } inline void set(xobject_child *in_obj) { if (this->m_obj != in_obj) { release(); attach(in_obj); } } };
三、创建你的扩展类并定义对应智能指针
现在你可以模仿uifcColor的模式,创建自己的扩展类,保持和原库一致的使用方式:
1. 扩展类的头文件定义
// MyExtendedColor.h #include "uifcColor.h" // 引入私有库的头文件 // 扩展uifcColor,添加自定义功能 class MyExtendedColor : virtual public uifcColor { xdeclare(MyExtendedColor) // 这个宏是原库的类元信息声明,直接复用即可 protected: // 构造函数设为保护,只能通过Create静态方法创建对象(工厂模式) MyExtendedColor(xint red, xint green, xint blue, xint alpha) : uifcColor(red, green, blue), mAlpha(alpha) {} virtual ~MyExtendedColor() {} public: // 新增的自定义方法 xint GetAlpha(); void SetAlpha(xint value); // 模仿原库的静态Create方法,返回智能指针 static MyExtendedColor_ptr Create(xint red, xint green, xint blue, xint alpha); private: xint mAlpha; // 新增的成员变量 }; // 定义对应的智能指针typedef,和原库的uifcColor_ptr格式一致 typedef xrchandle<MyExtendedColor> MyExtendedColor_ptr;
2. 扩展类的源文件实现
// MyExtendedColor.cpp #include "MyExtendedColor.h" // 实现自定义方法 xint MyExtendedColor::GetAlpha() { return mAlpha; } void MyExtendedColor::SetAlpha(xint value) { mAlpha = value; } // 实现工厂方法,创建对象并返回智能指针 MyExtendedColor_ptr MyExtendedColor::Create(xint red, xint green, xint blue, xint alpha) { MyExtendedColor* obj = new MyExtendedColor(red, green, blue, alpha); // 返回智能指针,它会自动接管对象的生命周期 return MyExtendedColor_ptr(obj); }
四、使用你的扩展类
现在你可以像使用原库的uifcColor一样使用扩展类:
// 创建带透明度的扩展颜色对象 MyExtendedColor_ptr myColor = MyExtendedColor::Create(255, 255, 255, 128); // 调用原库的方法 int red = myColor->GetRed(); // 调用自定义方法 int alpha = myColor->GetAlpha(); // 修改自定义属性 myColor->SetAlpha(255);
五、关键要点总结
- 智能指针的意义:
xrchandle是为了避免手动管理内存,防止泄漏,通过引用计数自动在最后一个指针销毁时释放对象。 - 类的设计模式:
uifcColor用了工厂模式(静态Create方法),构造函数保护,确保对象只能通过智能指针创建和管理,避免用户直接new/delete。 - 扩展逻辑:因为
uifcColor虚继承xobject,你可以安全继承它添加功能,再用xrchandle管理扩展类对象,完全保持原库的使用体验。
内容的提问来源于stack exchange,提问作者joaogoncalv_es
相关产品推荐
相关产品推荐

