translate_int_type.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_DETAIL_TRANSLATE_INT_TYPE_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_TRANSLATE_INT_TYPE_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/type_traits/is_same.hpp>
  12. namespace boost { namespace iostreams { namespace detail {
  13. template<bool IsSame>
  14. struct translate_int_type_impl;
  15. //
  16. // Template name: translate_char.
  17. // Description: Translates a character or an end-of-file indicator from the
  18. // int_type of one character traits type to the int_type of another.
  19. //
  20. template<typename SourceTr, typename TargetTr>
  21. typename TargetTr::int_type
  22. translate_int_type(typename SourceTr::int_type c)
  23. {
  24. typedef translate_int_type_impl<is_same<SourceTr, TargetTr>::value> impl;
  25. return impl::template inner<SourceTr, TargetTr>::translate(c);
  26. }
  27. //----------------------------------------------------------------------------//
  28. template<>
  29. struct translate_int_type_impl<true> {
  30. template<typename SourceTr, typename TargetTr>
  31. struct inner {
  32. static typename TargetTr::int_type
  33. translate(typename SourceTr::int_type c) { return c; }
  34. };
  35. };
  36. template<>
  37. struct translate_int_type_impl<false> {
  38. template<typename SourceTr, typename TargetTr>
  39. struct inner {
  40. static typename TargetTr::int_type
  41. translate(typename SourceTr::int_type c)
  42. {
  43. return SourceTr::eq_int_type(SourceTr::eof()) ?
  44. TargetTr::eof() :
  45. TargetTr::to_int_type(SourceTr::to_char_type(c));
  46. }
  47. };
  48. };
  49. } } } // End namespaces detail, iostreams, boost.
  50. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_TRANSLATE_INT_TYPE_HPP_INCLUDED