我正在寻找一种标准化和简化编码模式的方法,我经常对不依赖于旧的C样式宏的复制对象的共享对象实例重复使用该代码。
下面的示例表示一般的编码模式-例如,我希望能够getInstance()
,reset()
,声明sObjectInstance
,定义std::shared_ptr
等。重用和标准化。它还包含特定于示例ShaderOptions
类的内容(例如,采样质量支持),因为我需要一个支持类特定成员,枚举,函数等的解决方案。
我知道我可以使用C宏,例如DECLARE_SHARED_POINTER( ShaderOptions )
,但我希望找到一种更简洁,更C ++的方式。
#pragma once
#include "cinder/CinderAssert.h"
#include <memory>
typedef std::shared_ptr<class ShaderOptions> ShaderOptionsPtr;
class ShaderOptions
{
public:
// Sampling quality.
enum SamplingQuality
{
NORMAL,
HIGH
};
// Total sampling qualities.
static const size_t TOTAL_SAMPLING_QUALITIES = (size_t)SamplingQuality::HIGH+1;
//! Get the object instance.
static ShaderOptionsPtr getInstance()
{
if ( !sObjectInstance ) {
sObjectInstance = ShaderOptionsPtr( new ShaderOptions );
}
CI_ASSERT( sObjectInstance );
return sObjectInstance;
}
//! Reset the object instance.
static void reset() { sObjectInstance.reset(); }
//! Set the sampling quality.
void setSamplingQuality( const SamplingQuality samplingQuality ) { mSamplingQuality = samplingQuality; }
SamplingQuality getSamplingQuality() const { return mSamplingQuality; }
private:
//! Construct the shader options.
ShaderOptions() {}
//! Sampling quality.
SamplingQuality mSamplingQuality = SamplingQuality::NORMAL;
//! Shared object instance.
inline static ShaderOptionsPtr sObjectInstance;
};