浅谈C++物理设计:最佳实践

发布时间:2019-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了浅谈C++物理设计:最佳实践脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

如何保证自满足

为了验证头文件设计的自满足原则,实现文件的第一条语句必然是包含其对应的头文件。

反例:

// cppunIT/testCase.cpp
#include "cppunit/core/TestResult.h"
#include "cppunit/core/Functor.h"
// 错误:没有放在第一行,无法校验其自满足性
#include "cppunit/core/TestCase.h"

namespace
{
    struct TestCaSEMethoDFunctor : Functor
    {
        tyPEdef void (TestCase::*Method)();
    
        TestCaseMethodFunctor(TestCase &target, Method method)
           : target(target), method(method)
        {}
    
        bool operator()() const
        {
            target.*method();
            return true;
        }
    
    PRivate:
        TestCase ⌖
        Method method;
    };
}

void TestCase::run(TestResult &result)
{
    result.startTest(*this);
  
    if (result.protect(TestCaseMethodFunctor(*this, &TestCase::SETUP)))
    {
        result.protect(TestCaseMethodFunctor(*this, &TestCase::runTest)); 
    }

    result.protect(TestCaseMethodFunctor(*this, &TestCase::tearDown));

    result.endTest(*this);
}

...

正例:

// cppunit/TestCase.cpp
#include "cppunit/core/TestCase.h"
#include "cppunit/core/TestResult.h"
#include "cppunit/core/Functor.h"

namespace
{
    struct TestCaseMethodFunctor : Functor
    {
        typedef void (TestCase::*Method)();
    
        TestCaseMethodFunctor(TestCase &target, Method method)
           : target(target), method(method)
        {}
    
        bool operator()() const
        {
            target.*method();
            return true;
        }
    
    private:
        TestCase ⌖
        Method method;
    };
}

void TestCase::run(TestResult &result)
{
    result.startTest(*this);
  
    if (result.protect(TestCaseMethodFunctor(*this, &TestCase::setUp))
    {
        result.protect(TestCaseMethodFunctor(*this, &TestCase::runTest)); 
    }

    result.protect(TestCaseMethodFunctor(*this, &TestCase::tearDown));

    result.endTest(*this);
}

...

overrideprivate

所有override的函数(除overridevirtual析构函数之外)都应该private的,以保证按接口编程的良好设计原则。

反例:

// html-parser/filter/AndFilter.h
#ifndef EOIPWORPIO_06123124_NMVBNSDHJF_497392
#define EOIPWORPIO_06123124_NMVBNSDHJF_497392

#include "html-parser/filter/NodeFilter.h"
#include <list>

struct AndFilter : NodeFilter
{
     void add(NodeFilter*);

     // 设计缺陷:本应该private
     OVERRIDE(bool accept(const Node&) const);

private:
     std::list<NodeFilter*> filters;
};

#endif

正例:

// html-parser/filter/AndFilter.h
#ifndef EOIPWORPIO_06123124_NMVBNSDHJF_497392
#define EOIPWORPIO_06123124_NMVBNSDHJF_497392

#include "html-parser/filter/NodeFilter.h"
#include <list>

struct AndFilter : NodeFilter
{
     void add(NodeFilter*);

private:
     OVERRIDE(bool accept(const Node&) const);

private:
     std::list<NodeFilter*> filters;
};

#endif

inline

避免头文件中inline

头文件中避免定义inline函数,除非性能报告指出此函数是性能的关键瓶颈。

C++语言将声明和实现进行分离,程序员为此不得不在头文件和实现文件中重复地对函数进行声明。这是C/C++天生给我们的设计带来的重复。这是一件痛苦的事情,驱使部分程序员直接将函数实现为inline

inline函数的代码作为一种不稳定的内部实现细节,被放置在头文件里,其变更所导致的大面积的重新编译是个大概率事件,为改善微乎其微的函数调用性能与其相比将得不偿失。

除非有相关profiling性能测试报告,表明这部分关键的热点代码需要被放回头文件中。

但需要注意在特殊的情况,可以将实现inline在头文件中,因为为它们创建实现文件过于累赘和麻烦。

  • virtual析构函数

  • 空的virtual函数实现

  • C++11default函数

鼓励实现文件中inline

对于在编译单元内部定义的类而言,因为它的客户数量是确定的,就是它本身。另外,由于它本来就定义在代码文件中,因此并没有增加任何“物理耦合”。所以,对于这样的类,我们大可以将其所有函数都实现为inline的,就像写Java代码那样,Once & Only Once

以单态类的一种实现技为例讲解编译时依赖的解耦与匿名命名空间的使用。(首先,应该抵制单态设计的诱惑,单态其本质是面向对象技术中全局变量的替代品。滥用单态模式,犹如滥用全局变量,是一种典型的设计坏味道。只有确定在系统中唯一存在的概念,才能使用单态模式)。

实现单态,需要对系统中唯一存在的概念进行封装;但这个概念往往具有巨大的数据结构,如果将其声明在头文件中,无疑造成很大的编译时依赖。

反例:

// ne/NetworkElementRepository.h
#ifndef UIJVASDF_8945873_YUQWTYRDF_85643
#define UIJVASDF_8945873_YUQWTYRDF_85643    

#include "base/Status.h"
#include "base/BaseTypes.h"
#include "transport/ne/NetworkElement.h"
#include <vector>

struct NetworkElementRepository
{
    static NetworkElement& getInstance();

    Status add(const U16 id);
    Status release(const U16 id);
    Status modify(const U16 id);
    
private:
    typedef std::vector<NetworkElement> NetworkElements;
    NetworkElements elements;
};

#endif

文章篇幅的所限,NetworkElement.h未列出所有代码实现,但我们知道NetworkElement拥有巨大的数据结构,上述设计导致所有包含NetworkElementRepository的头文件都被NetworkElement所间接污染。

此时,其中可以将依赖置入到实现文件中,解除揭开其严重的编译时依赖。更重要的是,它更好地遵守了按接口编程的原则,改善了软件的扩展性。

正例:

// ne/NetworkElementRepository.h
#ifndef UIJVASDF_8945873_YUQWTYRDF_85643
#define UIJVASDF_8945873_YUQWTYRDF_85643    

#include "base/Status.h"
#include "base/BaseTypes.h"
#include "base/Role.h"

DEFINE_ROLE(NetworkElementRepository)
{
    static NetworkElementRepository& getInstance();

    ABSTRACT(Status add(const U16 id));
    ABSTRACT(Status release(const U16 id));
    ABSTRACT(Status modify(const U16 id));
};

#endif

其实现文件包含NetworkElement.h,将对其的依赖控制在本编译单元内部。

// ne/NetworkElementRepository.cpp}]
#include "transport/ne/NetworkElementRepository.h"
#include "transport/ne/NetworkElement.h"
#include <vector>

namespace
{
    struct NetworkElementRepositoryImpl : NetworkElementRepository
    {
        OVERRIDE(Status add(const U16 id))
        {
            // inline implements
        }

        OVERRIDE(Status release(const U16 id))
        {
            // inline implements
        }

        OVERRIDE(Status modify(const U16 id))
        {
            // inline implements
        }
    
    private:
        typedef std::vector<NetworkElement> NetworkElements;
        NetworkElements elements;
    };
}

NetworkElementRepository& NetworkElementRepository::getInstance()
{
    static NetworkElementRepositoryImpl inst;
    return inst;
}

此处,对NetworkElementRepositoryImpl类的依赖是非常明确的,仅本编译单元内,所有可以直接进行inline,从而简化了很多实现。

匿名namespace

匿名namespace的存在常常被人遗忘,但它的确是一个利器。匿名namespace的存在,使得所有受限于编译单元内的实体拥有了明确的处所。

自此之后,所有C风格并局限于编译单元内的static函数和变量;以及类似Java中常见的private static的提取函数将常常被匿名namespace替代。

请记住匿名命名空间也是一种重要的信息隐藏技术。在实现文件中提倡使用匿名namespace, 以避免潜在的命名冲突。

如上例,NetworkElementRepository.cpp通过匿名namespace,极大地减低了其头文件的编译时依赖。

struct VS. class

除了名字不同之外,classstruct唯一的差别是:默认可见性。这体现在定义和继承时。struct在定义一个成员,或者继承时,如果不指明,则默认为public,而class则默认为private

但这些都不是重点,重点在于定义接口和继承时,冗余public修饰符总让人不舒服。简单设计四原则告诉告诉我们,所有冗余的代码都应该被剔除。

但很多人会认为structC遗留问题,应该避免使用。但这不是问题,我们不应该否认在写C++程序时,依然在使用着很多C语言遗留的特性。关键在于,我们使用的是C语言中能给设计带来好处的特性,何乐而不为呢?

正例:

// hamcrest/SelfDescribing.h
#ifndef OIWER_NMVCHJKSD_TYT_48457_GSDFUIE
#define OIWER_NMVCHJKSD_TYT_48457_GSDFUIE

struct Description;

struct SelfDescribing
{
    virtual void describeTo(Description& description) const = 0;
    virtual ~SelfDescribing() {}
};

#endif

反例:

// hamcrest/SelfDescribing.h
#ifndef OIWER_NMVCHJKSD_TYT_48457_GSDFUIE
#define OIWER_NMVCHJKSD_TYT_48457_GSDFUIE

class Description;

class SelfDescribing
{
public:
    virtual void describeTo(Description& description) const = 0;
    virtual ~SelfDescribing() {}
};

#endif

更重要的是,我们确信“抽象”和“信息隐藏”对于软件的重要性,这促使我public接口总置于类的最前面成为我们的首选,class的特性正好与我们的期望背道而驰(class的特性正好适合于将数据结构捧为神物的程序员,它们常常将数据结构置于类声明的最前面。)

不管你信仰那一个流派,切忌不能混合使用classstruct。在大量使用前导声明的情况下,一旦一个使用struct的类改为class,所有的前置声明都需要修改。

万恶的struct tag

定义C风格的结构体时,struct tag彻底抑制了结构体前置声明的可能性,从而阻碍了编译优化的空间。

反例:

// radio/domain/Cell.h
#ifndef AQTYER_023874_NMHSFHKE_7432378293
#define AQTYER_023874_NMHSFHKE_7432378293

typedef struct tag_Cell
{
    WORD16 wCellId;
    WORD32 dwDlArfcn;
} T_Cell;

#endif
// radio/domain/Cell.h
#ifndef AQTYER_023874_NMHSFHKE_7432378293
#define AQTYER_023874_NMHSFHKE_7432378293

typedef struct
{
    WORD16 wCellId;
    WORD32 dwDlArfcn;
} T_Cell;

#endif

为了兼容C并为结构体前置声明提供便利,如下解法是最合适的。

正例:

// radio/domain/Cell.h
#ifndef AQTYER_023874_NMHSFHKE_7432378293
#define AQTYER_023874_NMHSFHKE_7432378293

typedef struct T_Cell
{
    WORD16 wCellId;
    WORD32 dwDlArfcn;
} T_Cell;

#endif

需要注意的是,在C语言中,如果没有使用typedef,则定义一个结构体的指针,必须显式地加上struct关键字:struct T_Cell *pcell,而C++没有这方面的要求。

PIMPL

如果性能不是关键问题,考虑使用PIMPL降低编译时依赖。

反例:

// mockcpp/ApiHook.h
#ifndef OIWTQNVHD_10945_HDFIUE_23975_HFGA
#define OIWTQNVHD_10945_HDFIUE_23975_HFGA

#include "mockcpp/JmpOnlyApiHook.h"

struct ApiHook
{
    ApiHook(const void* api, const void* stub)
      : stubHook(api, stub)
    {}

private:
    JmpOnlyApiHook stubHook;
};

#endif

正例:

// mockcpp/ApiHook.h
#ifndef OIWTQNVHD_10945_HDFIUE_23975_HFGA
#define OIWTQNVHD_10945_HDFIUE_23975_HFGA

struct ApiHookImpl;

struct ApiHook
{
    ApiHook(const void* api, const void* stub);
    ~ApiHook();

private:
    ApiHookImpl* This;
};

#endif
// mockcpp/ApiHook.cpp
#include "mockcpp/ApiHook.h"
#include "mockcpp/JmpOnlyApiHook.h"

struct ApiHookImpl
{
   ApiHookImpl(const void* api, const void* stub)
     : stubHook(api, stub)
   {
   }

   JmpOnlyApiHook stubHook;
};

ApiHook::ApiHook( const void* api, const void* stub)
  : This(new ApiHookImpl(api, stub))
{
}

ApiHook::~ApiHook()
{
    delete This;
}

通过ApiHookImpl* This的桥接,在头文件中解除了对JmpOnlyApiHook的依赖,将其依赖控制在本编译单元内部。

template

编译时依赖

当选择模板时,不得不将其实现定义在头文件中。当编译时依赖开销非常大时,编译模板将成为一种负担。设法降低编译时依赖,不仅仅为了缩短编译时间,更重要的是为了得到一个低耦合的实现。

反例:

// oss/OssSender.h
#ifndef HGGAOO_4611330_NMSDFHW_86794303_HJHASI
#define HGGAOO_4611330_NMSDFHW_86794303_HJHASI

#include "pub_typedef.h"
#include "pub_oss.h"
#include "oss_comm.h"
#include "pub_commdef.h"
#include "base/Assertions.h"
#include "base/Status.h"

struct OssSender
{
    OssSender(const PID& pid, const U8 commType)
      : pid(pid), commType(commType)
    {
    }
    
    template <typename MSG>
    Status send(const U16 eventId, const MSG& msg)
    {
        DCM_ASSERT_TRUE(OSS_SendAsynMsg(eventId, &msg, sizeof(msg), commType,(PID*)&pid) == OSS_SUCCESS); 
        return DCM_SUCCESS;
    }

private:
    PID pid;
    U8 commType;
};

#endif

为了实现模板函数send,将OSS的一些实现细节暴露到了头文件中,包含OssSender.h所有文件将无意识地产生了对OSS头文件的依赖。

提取一个私有的send函数,并将对OSS的依赖移入到OssSender.cpp中,对PID依赖通过前置声明解除,最终实现如代码所示。

正例:

// oss/OssSender.h
#ifndef HGGAOO_4611330_NMSDFHW_86794303_HJHASI
#define HGGAOO_4611330_NMSDFHW_86794303_HJHASI

#include "base/Status.h"
#include "base/BaseTypes.h"

struct PID;

struct OssSender
{
    OssSender(const PID& pid, const U16 commType)
      : pid(pid), commType(commType) 
    {
    }
    
    template <typename MSG>
    Status send(const U16 eventId, const MSG& msg)
    {
        return send(eventId, (const void*)&msg, sizeof(MSG));    
    }

private:
    Status send(const U16 eventId, const void* msg, size_t size);

private:
    const PID& pid;
    U8 commType;
};

#endif

识别哪些与泛型相关,哪些与泛型无关的知识,并解开此类编译时依赖是C++程序员的必备之技。

显式模板实例化

模板的编译时依赖存在两个基本模型:包含模型,export模型。export模型受编译技术实现的挑战,最终被C++11标准放弃

此时,似乎我们只能选择包含模型。其实,存在一种特殊的场景,适时选择显式模板实例化(Explicit Template Instantiated),降低模板的编译时依赖。是能做到降低模板编译时依赖的。

反例:

// quantity/Quantity.h
#ifndef HGGQMVJK_892302_NGFSLEU_796YJ_GF5284
#define HGGQMVJK_892302_NGFSLEU_796YJ_GF5284

#include <quantity/Amount.h>

template <typename Unit>
struct Quantity
{
    Quantity(const Amount amount, const Unit& unit)      
      : amountInBaseUnit(unit.toAmountInBaseUnit(amount))
    {}
    
    bool operator==(const Quantity& rhs) const
    {
        return amountInBaseUnit == rhs.amountInBaseUnit;
    }
    
    bool operator!=(const Quantity& rhs) const
    {
        return !(*this == rhs);
    }
    
private:
    const Amount amountInBaseUnit;
};

#endif
// quantity/Length.h
#ifndef TYIW7364_JG6389457_BVGD7562_VNW12_JFH
#define TYIW7364_JG6389457_BVGD7562_VNW12_JFH
 
#include "quantity/Quantity.h"
#include "quantity/LengthUnit.h"

typedef Quantity<LengthUnit> Length;

#endif
// quantity/Volume.h
#ifndef HG764MD_NKGJKDSJLD_RY64930_NVHF977E
#define HG764MD_NKGJKDSJLD_RY64930_NVHF977E
 
#include "quantity/Quantity.h"
#include "quantity/VolumeUnit.h"

typedef Quantity<VolumeUnit> Volume;

#endif

如上的设计,泛型类Quantity的实现都放在了头文件,不稳定的实现细节,例如计算amountInBaseUnit的算法变化等因素,将导致包含LengthVolume的所有源文件都需要重新编译。

更重要的是,因为LengthUnit, VolumeUnit头文件的包含,如果因需求变化需要增加支持的单位,将间接导致了包含LengthVolume的所有源文件也需要重新编译。

如何控制和隔离Quantity, LengthUnit, VolumeUnit变化的蔓延,而避免大部分的客户代码重新编译,从而与客户彻底解偶呢?可以通过显式模板实例化将模板实现从头文件中剥离出去,从而避免了不必要的依赖。

正例:

// quantity/Quantity.h
#ifndef HGGQMVJK_892302_NGFSLEU_796YJ_GF5284
#define HGGQMVJK_892302_NGFSLEU_796YJ_GF5284

#include <quantity/Amount.h>

template <typename Unit>
struct Quantity
{
    Quantity(const Amount amount, const Unit& unit);
        
    bool operator==(const Quantity& rhs) const;
    bool operator!=(const Quantity& rhs) const;
        
private:
    const Amount amountInBaseUnit;
};

#endif
// quantity/Quantity.tcc
#ifndef FKJHJT68302_NVGKS97474_YET122_HEIW8565
#define FKJHJT68302_NVGKS97474_YET122_HEIW8565

#include <quantity/Quantity.h>

template <typename Unit>
Quantity<Unit>::Quantity(const Amount amount, const Unit& unit)      
  : amountInBaseUnit(unit.toAmountInBaseUnit(amount))
{}
    
template <typename Unit>
bool Quantity<Unit>::operator==(const Quantity& rhs) const
{
    return amountInBaseUnit == rhs.amountInBaseUnit;
}
    
template <typename Unit>
bool Quantity<Unit>::operator!=(const Quantity& rhs) const
{
    return !(*this == rhs);
}

#endif
// quantity/Length.h
#ifndef TYIW7364_JG6389457_BVGD7562_VNW12_JFH
#define TYIW7364_JG6389457_BVGD7562_VNW12_JFH

#include "quantity/Quantity.h"

struct LengthUnit;
struct Length : Quantity<LengthUnit> {};

#endif
// quantity/Length.cpp
#include "quantity/Quantity.tcc"
#include "quantity/LengthUnit.h"

template struct Quantity<LengthUnit>;
// quantity/Volume.h
#ifndef HG764MD_NKGJKDSJLD_RY64930_NVHF977E
#define HG764MD_NKGJKDSJLD_RY64930_NVHF977E

#include "quantity/Quantity.h"

struct VolumeUnit;
struct Volume : Quantity<VolumeUnit> {};

#endif
// quantity/Volume.cpp
#include "quantity/Quantity.tcc"
#include "quantity/VolumeUnit.h"

template struct Quantity<VolumeUnit>;

Length.h仅仅对Quantity.h产生依赖; 特殊地,Length.cpp没有产生对Length.h的依赖,相反对Quantity.tcc产生了依赖。

另外,Length.hLengthUnit的依赖关系也简化为声明依赖,而对其真正的编译时依赖,也控制在模板实例化的时刻,即在Length.cpp内部。

LenghtUnit, VolumeUnit的变化,及其Quantity.tcc实现细节的变化,被完全地控制在Length.cpp, Volume.cpp内部。

子类化优于typedef/using

如果使用typedef,如果存在对Length的依赖,即使是名字的声明依赖,除了包含头文件之外,别无选择。

另外,如果Quantity存在virtual函数时,Length还有进一步扩展Quantity的可能性,从而使设计提供了更大的灵活性。

反例:

// quantity/Length.h
#ifndef TYIW7364_JG6389457_BVGD7562_VNW12_JFH
#define TYIW7364_JG6389457_BVGD7562_VNW12_JFH

#include "quantity/Quantity.h"

struct LengthUnit;
typedef Quantity<LengthUnit> Length;

#endif

正例:

// quantity/Length.h
#ifndef TYIW7364_JG6389457_BVGD7562_VNW12_JFH
#define TYIW7364_JG6389457_BVGD7562_VNW12_JFH

#include "quantity/Quantity.h"

struct LengthUnit;
struct Length : Quantity<LengthUnit> {};

#endif

脚本宝典总结

以上是脚本宝典为你收集整理的浅谈C++物理设计:最佳实践全部内容,希望文章能够帮你解决浅谈C++物理设计:最佳实践所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。