搜索
您的当前位置:首页正文

C++ 之PIMPL模式详解

来源:欧得旅游网

1 PIMPL解释

PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。
PIMPL(Pointer to Implementation)这个手法可以解決/改善C++编码时常碰到的2大问题。
1.class增加private/protected成员时,使用此class的相关 .cpp(s) 需要重新编译。
2.定义冲突与跨平台编译

2 PIMPL优点

举例:

//x.h
class X
{
public:
    void Fun();
private:
    int i; //add int i;
};

//c.h
#include <x.h>
class C
{
public:
    void Fun();
private:
    X x; //与X的强耦合
};

PIMPL做法:
//c.h
class X; //代替#include <x.h>
class C
{
public:
    void Fun();
private:
    X *pImpl; //pimpl
};

1)**降低模块的耦合。**因为隐藏了类的实现,被隐藏的类相当于原类不可见,对隐藏的类进行修改,不需要重新编译原类。

2)**降低编译依赖,提高编译速度。**指针的大小为(32位)或8(64位),X发生变化,指针大小却不会改变,文件c.h也不需要重编译。

3)接口与实现分离,提高接口的稳定性。

1、通过指针封装,当定义“new C”或"C c1"时 ,编译器生成的代码中不会掺杂X的任何信息。

2、当使用C时,使用的是C的接口(C接口里面操作的类其实是pImpl成员指向的X对象),与X无关,X被通过指针封装彻底的与实现分离。

//c.cpp
C::C()pImpl(new X())
{
}

C::~C()
{
     delete pImpl;
     pImpl = NULL;
}

void C::Fun()
{
    pImpl->Fun();
}

//main
#include <c.h>
int main()
{
    C c1;
    c1.Fun();
    return 0;
}

实例代码:

nestcalss.h

#ifndef __LINE_H__
#define __LINE_H__
 
 
//设计模式: PIMPL
//1. 实现信息隐藏
//2. 减小编译依赖, 可以用最小的代价平滑的升级库文件,
//3. 接口与实现进行解耦
 
class Line
{
public:
    Line(int,int,int,int);
    ~Line();
  
    void printLine() const;
private:
    class LineImpl;
private:
    LineImpl * _pimpl;
};
 
 
#endif

nestclass.cc

#include "nestClass.h"
#include <math.h>
 
#include <iostream>
using std::cout;
using std::endl;
 
class Line::LineImpl
{
    class Point
    {
    public:
        Point(int ix = 0, int iy = 0)
        : _ix(ix)
        , _iy(iy)
        {
            cout << "Point(int=0, int=0)" << endl;
        }
 
        void print() const
        {
            cout << "(" << _ix
                 << "," << _iy
                 << ")";
        }
 
    private:
        int _ix;
        int _iy;
    };
public:
    LineImpl(int x1, int y1, int x2, int y2)
    : _p1(x1, y1)
    , _p2(x2, y2)
    {
        cout << "LineImpl(int,int,int,int)" << endl;
    }
 
    ~LineImpl() {   cout << "~LineImpl()" << endl;  }
 
    void printLine() const;
private:
    Point _p1;
    Point _p2;
};
 
void Line::LineImpl::printLine() const
{
    _p1.print();
    cout << " --> ";
    _p2.print();
    cout << endl;
}
 
Line::Line(int x1, int y1, int x2, int y2)
: _pimpl(new LineImpl(x1, y1, x2, y2))
{
    cout << "Line(int,int,int,int)" << endl;
}
 
Line::~Line()
{
    delete _pimpl;
    cout << "~Line()" << endl;
}
 
void Line::printLine() const
{
    _pimpl->printLine();
}

main.cpp

#include "nestClass.h"
#include <iostream>
using std::cout;
using std::endl;
  
int main(void)
{
    Line line(1, 2, 3, 4);
    line.printLine();
 
    return 0;
}

Q1.class增加private/protected成员时,使用此class的相关 .cpp(s) 需要重新编译
假设我们有一个A.h(class A),并且有A/B/C/D 4个.cpp引用他,他们的关系如下图:

//a.h
#ifndef A_H
#define A_H
 
#include <memory>
 
class A
{
public:
    A();
    ~A();
     
    void doSomething();
     
private:    
      struct Impl;
      std::auto_ptr<impl> m_impl;
};
 
#endif

有一定C++基础的人都知道,使用前置声明(forward declaration)可以减少编译依赖,这个技巧告诉compile指向 class/struct的指针,而不用暴露struct/class的实现。在这里我们把原本的private成员封裝到struct A::Impl里,用一个不透明的指针(m_impl)指向他,auto_ptr是个smart pointer(from STL),会在A class object销毁时连带将资源销毁还给系统。
a.cpp 如下:

//a.cpp
#include <stdio.h>
#include "a.h"
 
struct A::Impl
{
    int m_count;
    Impl();
    ~Impl();
    void doPrivateThing();
};  
 
A::Impl::Impl():
    m_count(0)
{
}
 
A::Impl::~Impl()
{
}          
 
void A::Impl::doPrivateThing()
{
    printf("count = %d\n", ++m_count);
}    
 
A::A():m_impl(new Impl)
{
}      
 
A::~A()
{
} 
 
void A::doSomething()
{
    m_impl->doPrivateThing();    
}    

上面我们可以看到A private数据成员和成员函数全部被封裝到struct A::Impl里,如此一来无论private成员如何改变都只会重新编译A.cpp,而不会影响B/C/D.cpp,当然有时会有例外,不过大部分情况下还是能节约大量编译时间,项目越大越明显。

Q2.定义冲突与跨平台编译
如果你运气很好公司配給你8 cores CPU、SSD、32G DDRAM,会觉得PIMPL是多此一举。
但定定义冲突与跨平台编译问题不是电脑牛叉能够解決的,举个例子,你想在Windows上使用framework(例如 Qt)不具备的功能,你大概会这样做:

//foo.h
#ifndef FOO_H
#define FOO_H
 
#include <windows.h>
 
class Foo
{
 
public:
    Foo();
    ~Foo();
    void doSomething();
     
private:
    HANDLE m_handle;
     
};
 
#endif

Foo private数据成员: m_handle和系统相关,某天你想把Foo移植到Linux,应为Linux是用int来作为file descriptor,为了与Windows相区分,最直接的方法是用宏:

//foo.h
#ifndef FOO_H
#define FOO_H
 
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
 
class Foo
{
 
public:
    Foo();
    ~Foo();
    void doSomething();
     
private:
 
#ifdef _WIN32    
    HANDLE m_handle;
#else
    int m_handle;
#endif    
     
};
 
#endif

这样做会有什么问题?
1.windows.h是个巨大的header file,有可能会增加引用此header file的其他.cpp(s)编译时间,而实际上这些.cpp並不需要windows.h里面的内容。
2.windows.h会与framework冲突,虽然大部分的framework极力避免发生这种事情,但往往项目变得越来越大后常常出现这类编译错误,(Linux也可能发生)。
3.对于Linux用户,Windows那些header file是多余的,对于Windows用户Linux header files是多余的,沒必要也不该知道这些细节。

因篇幅问题不能全部显示,请点此查看更多更全内容

Top