Silicium
memory_range.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_MEMORY_RANGE_HPP
2 #define SILICIUM_MEMORY_RANGE_HPP
3 
5 #include <boost/mpl/if.hpp>
6 #include <boost/type_traits/add_const.hpp>
7 #include <boost/type_traits/remove_const.hpp>
8 #include <boost/type_traits/is_const.hpp>
9 #include <boost/static_assert.hpp>
10 #include <boost/utility/addressof.hpp>
11 
12 namespace Si
13 {
16 
19 
20  template <class To, class From>
21  struct copy_const
22  {
23  typedef typename boost::mpl::if_<boost::is_const<From>, boost::add_const<To>, boost::remove_const<To>>::type::type type;
24  };
25 
26  template <class Byte, class DestType = typename copy_const<char, Byte>::type>
27  auto make_memory_range(Byte *begin, Byte *end)
28 #if !SILICIUM_COMPILER_HAS_AUTO_RETURN_TYPE
30 #endif
31  {
32  BOOST_STATIC_ASSERT(sizeof(Byte) == 1);
33  BOOST_STATIC_ASSERT(std::is_pod<Byte>::value);
34  return make_iterator_range(
35  reinterpret_cast<DestType *>(begin),
36  reinterpret_cast<DestType *>(end)
37  );
38  }
39 
40  template <class Byte>
41  auto make_memory_range(Byte *data, std::size_t size)
43  {
44  return make_memory_range(data, data + size);
45  }
46 
47  template <class ContiguousRange>
48  auto make_memory_range(ContiguousRange &&range)
49 #if !SILICIUM_COMPILER_HAS_AUTO_RETURN_TYPE
50  -> decltype(make_memory_range(boost::addressof(*std::begin(range)), boost::addressof(*std::end(range))))
51 #endif
52  {
53  BOOST_STATIC_ASSERT(std::is_lvalue_reference<ContiguousRange>::value);
54  using std::begin;
55  using std::end;
56  auto begin_ = begin(range);
57  auto end_ = end(range);
58  BOOST_STATIC_ASSERT((std::is_same<std::random_access_iterator_tag, typename std::iterator_traits<decltype(begin_)>::iterator_category>::value));
59  auto data = boost::addressof(*begin_);
60  return make_memory_range(data, data + std::distance(begin_, end_));
61  }
62 
63  template <class C>
64  auto make_c_str_range(C const *str)
65 #if !SILICIUM_COMPILER_HAS_AUTO_RETURN_TYPE
67 #endif
68  {
69  return make_iterator_range(str, str + std::char_traits<C>::length(str));
70  }
71 }
72 
73 #endif
BOOST_STATIC_ASSERT(Si::is_handle< absolute_path >::value)
Definition: absolute_path.hpp:19
BOOST_CONSTEXPR Iterator const & end(iterator_range< Iterator > const &range)
Definition: iterator_range.hpp:136
auto make_c_str_range(C const *str) -> iterator_range< C const * >
Definition: memory_range.hpp:64
iterator_range< char * > mutable_memory_range
Definition: memory_range.hpp:15
BOOST_CONSTEXPR Iterator const & begin(iterator_range< Iterator > const &range)
Definition: iterator_range.hpp:123
Definition: iterator_range.hpp:26
Definition: memory_range.hpp:21
BOOST_CONSTEXPR auto make_iterator_range(Iterator1 &&begin, Iterator2 &&end) -> iterator_range< typename std::decay< Iterator1 >::type >
Definition: iterator_range.hpp:142
Definition: is_handle.hpp:21
boost::mpl::if_< boost::is_const< From >, boost::add_const< To >, boost::remove_const< To > >::type::type type
Definition: memory_range.hpp:23
iterator_range< char const * > memory_range
Definition: memory_range.hpp:14
auto make_memory_range(Byte *begin, Byte *end) -> iterator_range< DestType * >
Definition: memory_range.hpp:27