Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. #include <type_traits>
  2. #include <cstdint>
  3. #include <cstring>
  4. #include <cstdio>
  5.  
  6. template<class SrcType, class TargetType>
  7. struct copy_constness_t
  8. {
  9.   typedef typename std::conditional<std::is_const<SrcType>::value,const TargetType,TargetType>::type type;
  10. };
  11.  
  12. template<class ValueType>
  13. class slice_T
  14. {
  15. public:
  16.   typedef ValueType value_type;
  17.  
  18.   explicit slice_T(ValueType* const p_begin, const std::size_t& p_size):
  19.     m_begin(p_begin),
  20.     m_size(p_size)
  21.   {
  22.   }
  23.  
  24.   explicit slice_T(typename copy_constness_t<ValueType, void>::type* const p_begin, const std::size_t& p_size):
  25.     m_begin(reinterpret_cast<ValueType*>(p_begin)),
  26.     m_size(p_size)
  27.   {
  28.   }
  29.  
  30.   template<class Other>
  31.   explicit slice_T(const Other& p_other):
  32.     m_begin(p_other.begin()),
  33.     m_size(p_other.size())
  34.   {
  35.   }
  36.  
  37.   std::size_t size() const
  38.   {
  39.     return m_size;
  40.   }
  41.  
  42.   //const or non const based on ValueType
  43.   ValueType* const begin() const
  44.   {
  45.     return m_begin;
  46.   }
  47.  
  48.   //const or non const based on ValueType
  49.   ValueType* const end() const
  50.   {
  51.     return m_begin+m_size;
  52.   }
  53.  
  54.   /*
  55.   //const in any way
  56.   const ValueType* const const_begin() const
  57.   {
  58.     return begin();
  59.   }
  60.  
  61.   //const in any way
  62.   const ValueType* const const_end() const
  63.   {
  64.     return end();
  65.   }
  66.  
  67.   std::size_t byte_size() const
  68.   {
  69.     return m_size * sizeof(ValueType);
  70.   }  
  71.  
  72.   //same slice?
  73.   template<class Other>
  74.   bool operator ==(const Other& p_other) const
  75.   {
  76.     static_assert(sizeof(typename Other::value_type) == sizeof(value_type),"not equal size type");
  77.     //Other muss passende size() und begin() definieren
  78.     return
  79.       m_begin == p_other.begin()
  80.       && size() == p_other.size();
  81.   }
  82.  
  83.   template<class Other>
  84.   bool operator !=(const Other& p_other) const
  85.   {
  86.     return !operator==(p_other);
  87.   }
  88.  
  89.   //same content?
  90.   template<class Other>
  91.   bool equal(const Other& p_other) const
  92.   {
  93.     static_assert(sizeof(typename Other::value_type) == sizeof(value_type),"not equal size type");
  94.     //Other muss passende size() und begin() definieren
  95.     return
  96.       size() == p_other.size()
  97.       && ( m_begin == p_other.begin() || ::memcmp(begin(), p_other.begin(), byte_size()) == 0 );
  98.   }
  99.  
  100.   void enough_space_left_at(const size_t& p_offset, const size_t& p_count) const
  101.   {
  102.     if( p_count > left_at(p_offset) ) throw 1;
  103.   }
  104.  
  105.   template<class SliceValueType>
  106.   slice_T<SliceValueType> get_slice(const size_t& p_offset, const size_t& p_size) const
  107.   {
  108.     enough_space_left_at(p_offset, p_size);
  109.     return slice_T<SliceValueType>(begin()+p_offset, p_size);
  110.   }
  111.  
  112.   slice_T<ValueType> slice(const size_t& p_offset, const size_t& p_size) const
  113.   {
  114.     return get_slice<ValueType>(p_offset, p_size);
  115.   }
  116.  
  117.   slice_T<const ValueType> const_slice(const size_t& p_offset, const size_t& p_size) const
  118.   {
  119.     return get_slice<const ValueType>(p_offset, p_size);
  120.   }
  121.  
  122.   size_t left_at(const size_t& p_offset) const
  123.   {
  124.     return size() - p_offset;
  125.   }
  126.  
  127.   //assigne operator
  128.   slice_T<ValueType>& operator=(const slice_T<ValueType>& p_other)
  129.   {
  130.     if (this != &p_other)
  131.     {
  132.       m_size = p_other.size();
  133.       m_begin = p_other.begin();
  134.     }
  135.     return *this;
  136.   }
  137.  
  138.   //copy ctor
  139.   slice_T(const slice_T<ValueType>& p_other):
  140.   m_begin(p_other.begin()),
  141.     m_size(p_other.size())
  142.   {
  143.   }
  144.   */
  145.  
  146. private:
  147.   ValueType* const m_begin;
  148.   size_t m_size;
  149. };
  150.  
  151. template<class SliceType>
  152. class binary_reader_t
  153. {
  154. public:
  155.   static_assert(sizeof(typename SliceType::value_type) == sizeof(std::uint8_t), "only uint8_t like slices allowed");
  156.  
  157.   explicit binary_reader_t(const SliceType& p_slice):
  158.     m_slice(p_slice),
  159.     m_offset(0)
  160.   {
  161.   }
  162.  
  163.   std::size_t left() const
  164.   {
  165.     return m_slice.size() - m_offset;
  166.   }
  167.  
  168.   //copy constness of slice
  169.   template<class TargetType>
  170.   struct ref_type_t
  171.   {
  172.     typedef typename copy_constness_t<typename SliceType::value_type,TargetType>::type type;
  173.   };
  174.  
  175.   void enough_space_left(const size_t& p_size) const
  176.   {
  177.     if( p_size > left() ) throw 1;
  178.   }
  179.  
  180.   //ref
  181.   template<class ValueType>
  182.   typename ref_type_t<ValueType>::type& read_ref()
  183.   {
  184.     enough_space_left(sizeof(ValueType));
  185.     typename ref_type_t<ValueType>::type& ref = *reinterpret_cast<typename ref_type_t<ValueType>::type*>(m_slice.begin()+m_offset);
  186.     m_offset += sizeof(ValueType);
  187.     return ref;
  188.   }
  189.  
  190.   //copy
  191.   template<class ValueType>
  192.   typename ref_type_t<ValueType>::type read_value()
  193.   {
  194.     return read_ref<ValueType>();
  195.   }
  196.  
  197. private:
  198.   SliceType m_slice;
  199.   size_t m_offset;
  200. };
  201.  
  202. int main()
  203. {
  204.   try
  205.   {
  206.     const uint8_t TEST_DATA[] = "THIS IS BINARY TEST DATA"; // immutable data
  207.     const slice_T<const std::uint8_t> test_slice(TEST_DATA, sizeof(TEST_DATA));
  208.     typedef binary_reader_t<slice_T<const std::uint8_t>> stream_t;
  209.     static_assert(std::is_const<stream_t::ref_type_t<std::uint32_t>::type>::value, "not const");
  210.     stream_t stream(test_slice);
  211.     const std::uint32_t& ref = stream.read_ref<std::uint32_t>();
  212.     const std::uint32_t value = stream.read_value<std::uint32_t>();
  213.   }
  214.   catch(const int& p_error)
  215.   {
  216.     printf("exception error: %i\n", p_error);
  217.   }
  218.   catch(...)
  219.   {
  220.     printf("exception unknown\n");
  221.   }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement