Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:27

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   dump.hpp
0009  * \author Andrey Semashev
0010  * \date   03.05.2013
0011  *
0012  * This header contains the \c dump output manipulator.
0013  */
0014 
0015 #ifndef BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_
0016 #define BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_
0017 
0018 #include <cstddef>
0019 #include <iosfwd>
0020 #include <boost/log/detail/config.hpp>
0021 #include <boost/log/detail/header.hpp>
0022 
0023 #ifdef BOOST_HAS_PRAGMA_ONCE
0024 #pragma once
0025 #endif
0026 
0027 namespace boost {
0028 
0029 BOOST_LOG_OPEN_NAMESPACE
0030 
0031 namespace aux {
0032 
0033 typedef void dump_data_char_t(const void* data, std::size_t size, std::basic_ostream< char >& strm);
0034 extern BOOST_LOG_API dump_data_char_t* dump_data_char;
0035 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char >& strm)
0036 {
0037     (dump_data_char)(data, size, strm);
0038 }
0039 
0040 typedef void dump_data_wchar_t(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm);
0041 extern BOOST_LOG_API dump_data_wchar_t* dump_data_wchar;
0042 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm)
0043 {
0044     (dump_data_wchar)(data, size, strm);
0045 }
0046 
0047 #if !defined(BOOST_NO_CXX11_CHAR16_T)
0048 typedef void dump_data_char16_t(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm);
0049 extern BOOST_LOG_API dump_data_char16_t* dump_data_char16;
0050 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm)
0051 {
0052     (dump_data_char16)(data, size, strm);
0053 }
0054 #endif
0055 
0056 #if !defined(BOOST_NO_CXX11_CHAR32_T)
0057 typedef void dump_data_char32_t(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm);
0058 extern BOOST_LOG_API dump_data_char32_t* dump_data_char32;
0059 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm)
0060 {
0061     (dump_data_char32)(data, size, strm);
0062 }
0063 #endif
0064 
0065 template< std::size_t SizeV, typename R >
0066 struct enable_dump_size_based
0067 {
0068 };
0069 
0070 template< typename R >
0071 struct enable_dump_size_based< 1u, R >
0072 {
0073     typedef R type;
0074 };
0075 
0076 template< typename T, typename R >
0077 struct enable_dump :
0078     public enable_dump_size_based< sizeof(T), R >
0079 {
0080 };
0081 
0082 template< typename R >
0083 struct enable_dump< void, R >
0084 {
0085     typedef R type;
0086 };
0087 
0088 template< typename R >
0089 struct enable_dump< const void, R >
0090 {
0091     typedef R type;
0092 };
0093 
0094 template< typename R >
0095 struct enable_dump< volatile void, R >
0096 {
0097     typedef R type;
0098 };
0099 
0100 template< typename R >
0101 struct enable_dump< const volatile void, R >
0102 {
0103     typedef R type;
0104 };
0105 
0106 } // namespace aux
0107 
0108 /*!
0109  * \brief Manipulator for printing binary representation of the data
0110  */
0111 class dump_manip
0112 {
0113 private:
0114     //! Beginning of the data
0115     const void* m_data;
0116     //! Size of the data, in bytes
0117     std::size_t m_size;
0118 
0119 public:
0120     dump_manip(const void* data, std::size_t size) BOOST_NOEXCEPT : m_data(data), m_size(size) {}
0121     dump_manip(dump_manip const& that) BOOST_NOEXCEPT : m_data(that.m_data), m_size(that.m_size) {}
0122 
0123     const void* get_data() const BOOST_NOEXCEPT { return m_data; }
0124     std::size_t get_size() const BOOST_NOEXCEPT { return m_size; }
0125 };
0126 
0127 //! The operator outputs binary data to a stream
0128 template< typename CharT, typename TraitsT >
0129 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, dump_manip const& manip)
0130 {
0131     if (BOOST_LIKELY(strm.good()))
0132         aux::dump_data(manip.get_data(), manip.get_size(), strm);
0133 
0134     return strm;
0135 }
0136 
0137 /*!
0138  * \brief Manipulator for printing binary representation of the data with a size limit
0139  */
0140 class bounded_dump_manip :
0141     public dump_manip
0142 {
0143 private:
0144     //! Maximum size to output, in bytes
0145     std::size_t m_max_size;
0146 
0147 public:
0148     bounded_dump_manip(const void* data, std::size_t size, std::size_t max_size) BOOST_NOEXCEPT : dump_manip(data, size), m_max_size(max_size) {}
0149     bounded_dump_manip(bounded_dump_manip const& that) BOOST_NOEXCEPT : dump_manip(static_cast< dump_manip const& >(that)), m_max_size(that.m_max_size) {}
0150 
0151     std::size_t get_max_size() const BOOST_NOEXCEPT { return m_max_size; }
0152 };
0153 
0154 //! The operator outputs binary data to a stream
0155 template< typename CharT, typename TraitsT >
0156 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, bounded_dump_manip const& manip)
0157 {
0158     if (BOOST_LIKELY(strm.good()))
0159     {
0160         const std::size_t size = manip.get_size(), max_size = manip.get_max_size();
0161         if (max_size >= size)
0162         {
0163             aux::dump_data(manip.get_data(), size, strm);
0164         }
0165         else
0166         {
0167             aux::dump_data(manip.get_data(), max_size, strm);
0168             strm << " and " << (size - max_size) << " bytes more";
0169         }
0170     }
0171 
0172     return strm;
0173 }
0174 
0175 /*!
0176  * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form
0177  * \param data The pointer to the beginning of the region
0178  * \param size The size of the region, in bytes
0179  * \return The manipulator that is to be put to a stream
0180  */
0181 template< typename T >
0182 inline typename aux::enable_dump< T, dump_manip >::type dump(T* data, std::size_t size) BOOST_NOEXCEPT
0183 {
0184     return dump_manip((const void*)data, size);
0185 }
0186 
0187 /*!
0188  * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form
0189  * \param data The pointer to the beginning of the array
0190  * \param count The size of the region, in number of \c T elements
0191  * \return The manipulator that is to be put to a stream
0192  */
0193 template< typename T >
0194 inline dump_manip dump_elements(T* data, std::size_t count) BOOST_NOEXCEPT
0195 {
0196     return dump_manip((const void*)data, count * sizeof(T));
0197 }
0198 
0199 /*!
0200  * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form
0201  * \param data The pointer to the beginning of the region
0202  * \param size The size of the region, in bytes
0203  * \param max_size The maximum number of bytes of the region to output
0204  * \return The manipulator that is to be put to a stream
0205  */
0206 template< typename T >
0207 inline typename aux::enable_dump< T, bounded_dump_manip >::type dump(T* data, std::size_t size, std::size_t max_size) BOOST_NOEXCEPT
0208 {
0209     return bounded_dump_manip((const void*)data, size, max_size);
0210 }
0211 
0212 /*!
0213  * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form
0214  * \param data The pointer to the beginning of the array
0215  * \param count The size of the region, in number of \c T elements
0216  * \param max_count The maximum number of elements to output
0217  * \return The manipulator that is to be put to a stream
0218  */
0219 template< typename T >
0220 inline bounded_dump_manip dump_elements(T* data, std::size_t count, std::size_t max_count) BOOST_NOEXCEPT
0221 {
0222     return bounded_dump_manip((const void*)data, count * sizeof(T), max_count * sizeof(T));
0223 }
0224 
0225 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0226 
0227 } // namespace boost
0228 
0229 #include <boost/log/detail/footer.hpp>
0230 
0231 #endif // BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_