为了提高效率,类中的一些帮助器函数经常使用 inline ,为了清晰的分别哪些是 inlie 的函数,哪些不是,则可以使用这样一种方法:用单独的文件存放 inline 函数。
在 cegui 的 silly 库的实现中,所有用到的 inlie 函数都被放在相应 .h 文件对应的 .icpp 文件中,比如:
SILLYDataSource.h
// 有这样的 inline 定义文件存在
SILLYDataSource.icpp
而在对应的 SILLYDataSource.cpp 中,这样处理:
#ifndef SILLY_OPT_INLINE
#define inline
#include "SILLYDataSource.icpp"
#undef inline
#endif
这个似乎有些多余,基本上在具体的实现文中,只要包含 SILLYDataSource.icpp 就好了,让我们来看看 SILLYDataSource.icpp 中写的是什么:
// Start of section namespace SILLY namespace SILLY { inline DataSource ::~DataSource () { }
inline byte DataSource ::operator [](size_t offset ) const { assert (offset < getSize () && "ASSERT: Trying to access pixel outside of the data" ); return getDataPtr ()[offset ]; }
} // End of section namespace SILLY
|
这是在另外的一个 SILLYFileDataSource.icpp 中的内容:
// Start of section namespace SILLY namespace SILLY { inline bool FileDataSource ::isValid () const { return d_error ; }
inline const byte * FileDataSource ::getDataPtr () const { return d_bytes ; }
inline size_t FileDataSource ::getSize () const { return d_size ; }
} // End of section namespace SILLY |
很清晰吧,哈哈,这应该就是大波哥所喜欢的类型 ...