fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
  7. class FrontBackBuffer {
  8.  
  9. // If <const * int , int&> --> this result in is_same< int , int >
  10. // STATIC_ASSERT( std::is_same< RemoveModifiers<TBufferTypeFront>::type, typename RemoveModifiers<TBufferTypeFront>::type>::result )
  11.  
  12. public:
  13.  
  14. template <typename T>
  15. struct MyRefTypes {
  16. typedef T Org;
  17. typedef T* Ptr;
  18. typedef const T & Con;
  19. typedef T& Ref;
  20. typedef const T& CRef;
  21. static inline Ptr getUnderlyingPtr(T& v) {
  22. return &v;
  23. }
  24. static Ref getRef(T& v) {
  25. return v;
  26. }
  27. };
  28.  
  29. //Specialization for Reference
  30. template <typename T>
  31. struct MyRefTypes<T&> {
  32. typedef T Org;
  33. typedef T* Ptr;
  34. typedef T & Con;
  35. typedef T& Ref;
  36. typedef const T& CRef;
  37. static inline Ptr getUnderlyingPtr(T& v) {
  38. return &v;
  39. }
  40. static inline Ref getRef(T& v) {
  41. return v;
  42. }
  43. };
  44.  
  45. //Specialization for const Reference
  46. template <typename T>
  47. struct MyRefTypes<const T&> {
  48. typedef T Org;
  49. typedef T* Ptr;
  50. typedef const T & Con;
  51. typedef const T& Ref;
  52. typedef const T& CRef;
  53. static inline Ptr getUnderlyingPtr(const T& v) {
  54. return &const_cast<T&>(v);
  55. }
  56. static inline Ref getRef(const T& v) {
  57. return v;
  58. }
  59. };
  60.  
  61. //Specialization for const
  62. template <typename T>
  63. struct MyRefTypes<const T> {
  64. typedef T Org;
  65. typedef T* Ptr;
  66. typedef const T & Con;
  67. typedef const T& Ref;
  68. typedef const T& CRef;
  69. static inline Ptr getUnderlyingPtr(const T& v) {
  70. return &const_cast<T&>(v);
  71. }
  72. static inline Ref getRef(const T& v) {
  73. return v;
  74. }
  75. };
  76.  
  77. //Specialization for pointers
  78. template <typename T>
  79. struct MyRefTypes<T*> {
  80. typedef T* Ptr;
  81. typedef T Org;
  82. typedef T* Con;
  83. typedef T& Ref;
  84. typedef T* const CRef; //! note this is a pointer....
  85. static inline Ptr getUnderlyingPtr(T* v) {
  86. return v;
  87. }
  88. static inline Ref getRef(T* v) {
  89. return *v;
  90. }
  91. };
  92.  
  93. //Specialization for const pointers
  94. template <typename T>
  95. struct MyRefTypes<const T*> {
  96. typedef T Org;
  97. typedef T* Ptr;
  98. typedef const T* Con;
  99. typedef const T& Ref;
  100. typedef const T* const CRef; //! note this is a pointer....
  101. static inline Ptr getUnderlyingPtr(const T* v) {
  102. return const_cast<T*>(v);
  103. }
  104. static inline Ref getRef(const T* v) {
  105. return *v;
  106. }
  107. };
  108.  
  109.  
  110. typedef typename MyRefTypes<TBufferTypeFront>::Ref TBufferTypeFrontRef;
  111. typedef typename MyRefTypes<TBufferTypeFront>::CRef TBufferTypeFrontCRef;
  112. typedef typename MyRefTypes<TBufferTypeFront>::Con TBufferTypeFrontCon;
  113. typedef typename MyRefTypes<TBufferTypeFront>::Org TBufferTypeFrontOrg;
  114. typedef typename MyRefTypes<TBufferTypeFront>::Ptr TBufferTypeFrontPtr;
  115.  
  116. typedef typename MyRefTypes<TBufferTypeBack >::Ref TBufferTypeBackRef;
  117. typedef typename MyRefTypes<TBufferTypeBack >::CRef TBufferTypeBackCRef;
  118. typedef typename MyRefTypes<TBufferTypeBack >::Con TBufferTypeBackCon;
  119. typedef typename MyRefTypes<TBufferTypeBack >::Org TBufferTypeBackOrg;
  120. typedef typename MyRefTypes<TBufferTypeBack >::Ptr TBufferTypeBackPtr;
  121.  
  122. explicit FrontBackBuffer(
  123. TBufferTypeFrontCon front,
  124. TBufferTypeBackCon back):
  125. m_Front(front),
  126. m_Back(back)
  127. {
  128. m_pBack = MyRefTypes<TBufferTypeBack>::getUnderlyingPtr(m_Back);
  129. m_pFront = MyRefTypes<TBufferTypeFront>::getUnderlyingPtr(m_Front);
  130.  
  131. };
  132.  
  133.  
  134. ~FrontBackBuffer()
  135. {};
  136.  
  137. TBufferTypeFrontRef getFront() {
  138. return *m_pFront;
  139. }
  140. TBufferTypeBackRef getBack() {
  141. return *m_pBack;
  142. }
  143.  
  144. void swap(){
  145. TBufferTypeFrontPtr temp = m_pFront;
  146. m_pFront = m_pBack;
  147. m_pBack = temp;
  148. }
  149.  
  150. private:
  151.  
  152.  
  153.  
  154. TBufferTypeFrontPtr m_pFront; ///< The pointer to front buffer
  155. TBufferTypeBackPtr m_pBack; ///< The pointer to back buffer
  156.  
  157. TBufferTypeFront m_Front; ///< The front buffer
  158. TBufferTypeBack m_Back; ///< The back buffer
  159.  
  160. };
  161.  
  162.  
  163. typedef std::vector<float> GAGAType ;
  164.  
  165. int main() {
  166. int front=10;
  167. int back=3;
  168. FrontBackBuffer< const int*, int & > buf1(&front, back);
  169. buf1.getBack() = 4; // change from 3 to 4
  170. // buf.getFront() = 5; NO! is const!
  171. buf1.swap();
  172. std::cout << buf1.getFront() << buf1.getBack() << std::endl;
  173. //NOW getBack() and getFront() should return 4 and 10!
  174.  
  175. front = 1;
  176. back= -1;
  177. FrontBackBuffer<int &, int > buf2(front, back);
  178. buf2.getBack() = 2;
  179. buf2.getFront() = 3;
  180.  
  181. buf2.swap();
  182. //NOW getBack() and getFront() should return 2 and 3!
  183. std::cout << buf2.getFront() << buf2.getBack() << std::endl;
  184.  
  185. front = 1;
  186. back= -1;
  187. FrontBackBuffer<int, const int &> buf3(front, back);
  188. //buf3.getBack() = 2; // IS CONST!!
  189. buf3.getFront() = 3;
  190.  
  191. buf3.swap();
  192. //NOW getBack() and getFront() should return -1 and 3!
  193. std::cout << buf3.getFront() << buf3.getBack() << std::endl;
  194.  
  195. }
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
410
23
-13