From 908794bab26dc0da68d9242f663ab7dfcf7f116b Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 9 May 2016 20:13:20 -0400 Subject: [PATCH] Add dstream --- extras/beast/unit_test/dstream.hpp | 144 +++++++++++++++++++++++++ include/beast/core/detail/temp_dir.hpp | 73 ------------- include/beast/core/detail/unit_test.h | 23 ---- 3 files changed, 144 insertions(+), 96 deletions(-) create mode 100644 extras/beast/unit_test/dstream.hpp delete mode 100644 include/beast/core/detail/temp_dir.hpp delete mode 100644 include/beast/core/detail/unit_test.h diff --git a/extras/beast/unit_test/dstream.hpp b/extras/beast/unit_test/dstream.hpp new file mode 100644 index 00000000..a42fd6f4 --- /dev/null +++ b/extras/beast/unit_test/dstream.hpp @@ -0,0 +1,144 @@ +// +// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BEAST_UNIT_TEST_DSTREAM_HPP +#define BEAST_UNIT_TEST_DSTREAM_HPP + +#include +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +# ifndef NOMINMAX +# define NOMINMAX 1 +# endif +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include +# undef WIN32_LEAN_AND_MEAN +# undef NOMINMAX +#endif + +namespace beast { +namespace unit_test { + +namespace detail { + +#ifdef _MSC_VER + +template +class dstream_buf + : public std::basic_stringbuf +{ + bool dbg_; + + template + void write(T const*) = delete; + + void write(char const* s) + { + if(dbg_) + OutputDebugStringA(s); + else + std::cout << s; + } + + void write(wchar_t const* s) + { + if(dbg_) + OutputDebugStringW(s); + else + std::wcout << s; + } + +public: + dstream_buf() + : dbg_(IsDebuggerPresent() != FALSE) + { + } + + ~dstream_buf() + { + sync(); + } + + int + sync() override + { + write(str().c_str()); + str(""); + return 0; + } +}; + +#else + +template +class dstream_buf + : public std::basic_stringbuf +{ + template + void write(T const*) = delete; + + void write(char const* s) + { + std::cout << s; + } + + void write(wchar_t const* s) + { + std::wcout << s; + } + +public: + ~dstream_buf() + { + sync(); + } + + int + sync() override + { + write(str().c_str()); + str(""); + return 0; + } +}; + +#endif + +} // detail + +/// A std::ostream that redirects output to the debugger if attached. +template< + class CharT, + class Traits = std::char_traits, + class Allocator = std::allocator +> +class basic_dstream + : private boost::base_from_member< + detail::dstream_buf> + , public std::basic_ostream +{ +public: + basic_dstream() + : std::basic_ostream(&member) + { + } +}; + +using dstream = basic_dstream; +using dwstream = basic_dstream; + +} // test +} // beast + +#endif diff --git a/include/beast/core/detail/temp_dir.hpp b/include/beast/core/detail/temp_dir.hpp deleted file mode 100644 index 54a0d905..00000000 --- a/include/beast/core/detail/temp_dir.hpp +++ /dev/null @@ -1,73 +0,0 @@ -// -// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -#ifndef BEAST_DETAIL_TEMP_DIR_H_INCLUDED -#define BEAST_DETAIL_TEMP_DIR_H_INCLUDED - -#include -#include - -namespace beast { -namespace detail { - -/** RAII temporary directory. - - The directory and all its contents are deleted when - the instance of `temp_dir` is destroyed. -*/ -class temp_dir -{ - boost::filesystem::path path_; - -public: -#if ! GENERATING_DOCS - temp_dir(const temp_dir&) = delete; - temp_dir& operator=(const temp_dir&) = delete; -#endif - - /// Construct a temporary directory. - temp_dir() - { - auto const dir = - boost::filesystem::temp_directory_path(); - do - { - path_ = - dir / boost::filesystem::unique_path(); - } - while(boost::filesystem::exists(path_)); - boost::filesystem::create_directory (path_); - } - - /// Destroy a temporary directory. - ~temp_dir() - { - boost::filesystem::remove_all (path_); - } - - /// Get the native path for the temporary directory - std::string - path() const - { - return path_.string(); - } - - /** Get the native path for the a file. - - The file does not need to exist. - */ - std::string - file(std::string const& name) const - { - return (path_ / name).string(); - } -}; - -} // detail -} // beast - -#endif diff --git a/include/beast/core/detail/unit_test.h b/include/beast/core/detail/unit_test.h deleted file mode 100644 index a53379e0..00000000 --- a/include/beast/core/detail/unit_test.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -#ifndef BEAST_DETAIL_UNIT_TEST_H_INCLUDED -#define BEAST_DETAIL_UNIT_TEST_H_INCLUDED - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#endif