Add dstream

This commit is contained in:
Vinnie Falco
2016-05-09 20:13:20 -04:00
parent 82f1661a47
commit 908794bab2
3 changed files with 144 additions and 96 deletions

View File

@@ -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 <boost/utility/base_from_member.hpp>
#include <iostream>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#ifdef _MSC_VER
# ifndef NOMINMAX
# define NOMINMAX 1
# endif
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# undef WIN32_LEAN_AND_MEAN
# undef NOMINMAX
#endif
namespace beast {
namespace unit_test {
namespace detail {
#ifdef _MSC_VER
template<class CharT, class Traits, class Allocator>
class dstream_buf
: public std::basic_stringbuf<CharT, Traits, Allocator>
{
bool dbg_;
template<class T>
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 CharT, class Traits, class Allocator>
class dstream_buf
: public std::basic_stringbuf<CharT, Traits, Allocator>
{
template<class T>
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<CharT>,
class Allocator = std::allocator<CharT>
>
class basic_dstream
: private boost::base_from_member<
detail::dstream_buf<CharT, Traits, Allocator>>
, public std::basic_ostream<CharT, Traits>
{
public:
basic_dstream()
: std::basic_ostream<CharT, Traits>(&member)
{
}
};
using dstream = basic_dstream<char>;
using dwstream = basic_dstream<wchar_t>;
} // test
} // beast
#endif

View File

@@ -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 <boost/filesystem.hpp>
#include <string>
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

View File

@@ -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 <beast/detail/unit_test/amount.h>
#include <beast/detail/unit_test/print.h>
#include <beast/detail/unit_test/global_suites.h>
#include <beast/detail/unit_test/match.h>
#include <beast/detail/unit_test/recorder.h>
#include <beast/detail/unit_test/reporter.h>
#include <beast/detail/unit_test/results.h>
#include <beast/detail/unit_test/runner.h>
#include <beast/detail/unit_test/suite.h>
#include <beast/detail/unit_test/suite_info.h>
#include <beast/detail/unit_test/suite_list.h>
#endif