fork download
  1. #include <vector>
  2. using namespace std;
  3.  
  4.  
  5. template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
  6. class FrontBackBuffer {
  7.  
  8. public:
  9.  
  10. template <typename T>
  11. struct MyRefTypes {
  12. typedef const T & Con;
  13. typedef T& Ref;
  14. typedef const T& CRef;
  15. static Ref getRef(T& v) {
  16. return v;
  17. }
  18. };
  19.  
  20. //Specialization for Reference
  21. template <typename T>
  22. struct MyRefTypes<T&> {
  23. typedef T & Con;
  24. typedef T& Ref;
  25. typedef const T& CRef;
  26. static inline Ref getRef(T& v) {
  27. return v;
  28. }
  29. };
  30.  
  31. //Specialization for const Reference
  32. template <typename T>
  33. struct MyRefTypes<const T&> {
  34. typedef const T & Con;
  35. typedef const T& Ref;
  36. typedef const T& CRef;
  37. static inline Ref getRef(const T& v) {
  38. return v;
  39. }
  40. };
  41.  
  42. //Specialization for const
  43. template <typename T>
  44. struct MyRefTypes<const T> {
  45. typedef const T & Con;
  46. typedef const T& Ref;
  47. typedef const T& CRef;
  48. static inline Ref getRef(const T& v) {
  49. return v;
  50. }
  51. };
  52.  
  53. //Specialization for pointers
  54. template <typename T>
  55. struct MyRefTypes<T*> {
  56. typedef T* Con;
  57. typedef T& Ref;
  58. typedef T* const CRef; //! note this is a pointer....
  59. static inline Ref getRef(T* v) {
  60. return *v;
  61. }
  62. };
  63.  
  64. //Specialization for const pointers
  65. template <typename T>
  66. struct MyRefTypes<const T*> {
  67. typedef const T* Con;
  68. typedef const T& Ref;
  69. typedef const T* const CRef; //! note this is a pointer....
  70. static inline Ref getRef(const T* v) {
  71. return *v;
  72. }
  73. };
  74.  
  75.  
  76. typedef typename MyRefTypes<TBufferTypeFront>::Ref TBufferTypeFrontRef;
  77. typedef typename MyRefTypes<TBufferTypeFront>::CRef TBufferTypeFrontCRef;
  78. typedef typename MyRefTypes<TBufferTypeFront>::Con TBufferTypeFrontCon;
  79.  
  80. typedef typename MyRefTypes<TBufferTypeBack >::Ref TBufferTypeBackRef;
  81. typedef typename MyRefTypes<TBufferTypeBack >::CRef TBufferTypeBackCRef;
  82. typedef typename MyRefTypes<TBufferTypeBack >::Con TBufferTypeBackCon;
  83.  
  84. // Remove reference because const T & yields with T = int& -> int&
  85. explicit FrontBackBuffer(
  86. TBufferTypeFrontCon m_front,
  87. TBufferTypeBackCon m_back):
  88. m_Front(m_front),
  89. m_Back(m_back)
  90. {
  91. };
  92.  
  93.  
  94. ~FrontBackBuffer()
  95. {};
  96.  
  97. TBufferTypeFrontRef getFront() {
  98. return MyRefTypes<TBufferTypeFront>::getRef(m_Front);
  99. }
  100. TBufferTypeBackRef getBack() {
  101. return MyRefTypes<TBufferTypeBack>::getRef(m_Back);
  102. }
  103. private:
  104.  
  105. TBufferTypeFront m_Front; ///< The front buffer
  106. TBufferTypeBack m_Back; ///< The back buffer
  107.  
  108. };
  109.  
  110.  
  111. typedef std::vector<float> GAGAType ;
  112.  
  113. int main() {
  114. int aa = 7;
  115. FrontBackBuffer<int&, double> a(aa,7);
  116. //a.getFront() = 4 //COmpile error! OK!;
  117. a.getBack() = 4;
  118.  
  119. const int r = 7;
  120. FrontBackBuffer<const int&, std::vector<int> > a1(r, std::vector<int>(7));
  121. //a.getFront() = 4 //COmpile error! OK!;
  122. a1.getBack()[0] = 4;
  123.  
  124. int bb;
  125. GAGAType bbb;
  126. FrontBackBuffer< int*, GAGAType * > b(&bb, &bbb);
  127. b.getBack().push_back(0);
  128. b.getFront() = int (4);
  129.  
  130. }
Success #stdin #stdout 0s 3056KB
stdin
Standard input is empty
stdout
Standard output is empty