今天早上我遇到了这个问题:
我想构建一个通用类FrontBackBuffer,我可以将其用作以下(一些示例)。
编辑删除了一些令人困惑的部分!
int bb=10;
int fb=3;
FrontBackBuffer< const int*, int & > buf(&fb, &bb);
buf.getBack() = 4; // change from 10 to 4
// buf.getFront() = 5; NO! is const!
buf.swap();
//NOW getFront() and getBack() should return 4 and 3!
FrontBackBuffer< int, const & int > buf(14, bb);
buf.getBack() = 5; // change from 4 to 5
// buf.getFront() = 5; NO! is const!
buf.swap();
//NOW getFront() and getBack() should return 5 and 14!
它存储两个缓冲区,应该是相同的底层类型(参见 static_assert)。这些缓冲区可以是指针类型或引用类型,也可以是 const 或非 const。它有两个功能getFront()
和getBack()
。这些函数总是返回对底层缓冲区的引用,无论是常量还是非常量。这就是为什么会有各种MyRefTypes
特征的特殊化。
到目前为止工作的课程如下:
template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
class FrontBackBuffer {
// If <const * int , int&> --> this result in is_same< int , int >
// STATIC_ASSERT( std::is_same< RemoveModifiers<TBufferTypeFront>::type, typename RemoveModifiers<TBufferTypeFront>::type>::result )
public:
template <typename T>
struct MyRefTypes {
typedef const T & Con;
typedef T& Ref;
typedef const T& CRef;
static Ref getRef(T& v) {
return v;
}
};
//Specialization for Reference
template <typename T>
struct MyRefTypes<T&> {
typedef T & Con;
typedef T& Ref;
typedef const T& CRef;
static inline Ref getRef(T& v) {
return v;
}
};
//Specialization for const Reference
template <typename T>
struct MyRefTypes<const T&> {
typedef const T & Con;
typedef const T& Ref;
typedef const T& CRef;
static inline Ref getRef(const T& v) {
return v;
}
};
//Specialization for const
template <typename T>
struct MyRefTypes<const T> {
typedef const T & Con;
typedef const T& Ref;
typedef const T& CRef;
static inline Ref getRef(const T& v) {
return v;
}
};
//Specialization for pointers
template <typename T>
struct MyRefTypes<T*> {
typedef T* Con;
typedef T& Ref;
typedef T* const CRef; //! note this is a pointer....
static inline Ref getRef(T* v) {
return *v;
}
};
//Specialization for const pointers
template <typename T>
struct MyRefTypes<const T*> {
typedef const T* Con;
typedef const T& Ref;
typedef const T* const CRef; //! note this is a pointer....
static inline Ref getRef(const T* v) {
return *v;
}
};
typedef typename MyRefTypes<TBufferTypeFront>::Ref TBufferTypeFrontRef;
typedef typename MyRefTypes<TBufferTypeFront>::CRef TBufferTypeFrontCRef;
typedef typename MyRefTypes<TBufferTypeFront>::Con TBufferTypeFrontCon;
typedef typename MyRefTypes<TBufferTypeBack >::Ref TBufferTypeBackRef;
typedef typename MyRefTypes<TBufferTypeBack >::CRef TBufferTypeBackCRef;
typedef typename MyRefTypes<TBufferTypeBack >::Con TBufferTypeBackCon;
explicit FrontBackBuffer(
TBufferTypeFrontCon front,
TBufferTypeBackCon back):
m_Front(front),
m_Back(back)
{
m_pBack = (void*)&m_Back;
m_pFront = (void*)&m_Front;
};
~FrontBackBuffer()
{};
TBufferTypeFrontRef getFront() {
return MyRefTypes<TBufferTypeFront>::getRef(m_Front);
}
TBufferTypeBackRef getBack() {
return MyRefTypes<TBufferTypeBack>::getRef(m_Back);
}
private:
void swap(){
void * temp = m_pFront;
m_pFront = m_pBack;
m_pBack = temp;
}
TBufferTypeFront * m_pFront; ///< The pointer to front buffer
TBufferTypeBack * m_pBack; ///< The pointer to back buffer
TBufferTypeFront m_Front; ///< The front buffer
TBufferTypeBack m_Back; ///< The back buffer
};
现在的问题是(我不能完全正确地解决):如何添加一个通用函数swap()
来交换缓冲区,但不应该复制。我考虑了两个指针void * m_pFront
,void *m_pBack
我应该用它们来完成这项工作,并正确分配(参见构造函数)。
但是我现在如何编写这些 getter 函数对我来说是个谜:
TBufferTypeFrontRef getFront() {
return MyRefTypes<TBufferTypeFront>::getRef(m_Front);
}
TBufferTypeBackRef getBack() {
return MyRefTypes<TBufferTypeBack>::getRef(m_Back);
}
归根结底,它应该可以正常工作:-)感谢您的帮助并尝试解决这个难题:-)!