piranha  0.10
prepare_for_print.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_DETAIL_PREPARE_FOR_PRINT_HPP
30 #define PIRANHA_DETAIL_PREPARE_FOR_PRINT_HPP
31 
32 #include <type_traits>
33 
34 namespace piranha
35 {
36 
37 namespace detail
38 {
39 
40 // Helper to print char types without displaying garbage.
41 template <typename T>
42 struct pfp_special {
43  static const bool value
44  = std::is_same<T, char>::value || std::is_same<T, unsigned char>::value || std::is_same<T, signed char>::value;
45 };
46 
47 template <typename T>
48 inline const T &prepare_for_print(const T &x, typename std::enable_if<!pfp_special<T>::value>::type * = nullptr)
49 {
50  return x;
51 }
52 
53 template <typename T>
54 inline int
55 prepare_for_print(const T &n,
56  typename std::enable_if<pfp_special<T>::value && std::is_signed<T>::value>::type * = nullptr)
57 {
58  return static_cast<int>(n);
59 }
60 
61 template <typename T>
62 inline unsigned
63 prepare_for_print(const T &n,
64  typename std::enable_if<pfp_special<T>::value && std::is_unsigned<T>::value>::type * = nullptr)
65 {
66  return static_cast<unsigned>(n);
67 }
68 }
69 }
70 
71 #endif
Root piranha namespace.
Definition: array_key.hpp:52