beast.streams improvements:

* New basic_std_ostream wraps any std::stream.
* New debug_ostream redirects output to debugger if attached.
This commit is contained in:
Vinnie Falco
2014-03-20 16:50:25 -07:00
parent e92dd73169
commit 0bb6171a85
5 changed files with 166 additions and 20 deletions

View File

@@ -193,6 +193,8 @@
<ClInclude Include="..\..\beast\streams\abstract_ostream.h" />
<ClInclude Include="..\..\beast\streams\basic_abstract_ostream.h" />
<ClInclude Include="..\..\beast\streams\basic_scoped_ostream.h" />
<ClInclude Include="..\..\beast\streams\debug_ostream.h" />
<ClInclude Include="..\..\beast\streams\basic_std_ostream.h" />
<ClInclude Include="..\..\beast\Strings.h" />
<ClInclude Include="..\..\beast\strings\CharacterFunctions.h" />
<ClInclude Include="..\..\beast\strings\CharPointer_ASCII.h" />

View File

@@ -1164,6 +1164,12 @@
<ClInclude Include="..\..\beast\container\const_container.h">
<Filter>beast\container</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\streams\basic_std_ostream.h">
<Filter>beast\streams</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\streams\debug_ostream.h">
<Filter>beast\streams</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\files\DirectoryIterator.cpp">

View File

@@ -66,26 +66,6 @@ public:
}
/** @} */
/** Returns a basic_scoped_ostream without appending anything.
This can be used if the caller wants to put the scoped stream
on the stack to build up output in a thread-safe fashion.
*/
#if 0
scoped_stream_type
scoped_stream ()
{
#if 0
return scoped_stream_type (
[this](string_type const& s)
{
this->write (s);
});
#else
return scoped_stream_type (*this);
#endif
}
#endif
/** Called to output each string. */
virtual
void

View File

@@ -0,0 +1,73 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_UTILITY_STD_OSTREAM_H_INCLUDED
#define BEAST_UTILITY_STD_OSTREAM_H_INCLUDED
#include "basic_abstract_ostream.h"
#include <ostream>
namespace beast {
/** Wraps an existing std::basic_ostream as an abstract_ostream. */
template <
class CharT,
class Traits = std::char_traits <CharT>
>
class basic_std_ostream
: public basic_abstract_ostream <CharT, Traits>
{
private:
using typename basic_abstract_ostream <CharT, Traits>::string_type;
std::reference_wrapper <std::ostream> m_stream;
public:
explicit basic_std_ostream (
std::basic_ostream <CharT, Traits>& stream)
: m_stream (stream)
{
}
void
write (string_type const& s) override
{
m_stream.get() << s << std::endl;
}
};
typedef basic_std_ostream <char> std_ostream;
//------------------------------------------------------------------------------
/** Returns a basic_std_ostream using template argument deduction. */
template <
class CharT,
class Traits = std::char_traits <CharT>
>
basic_std_ostream <CharT, Traits>
make_std_ostream (std::basic_ostream <CharT, Traits>& stream)
{
return basic_std_ostream <CharT, Traits> (stream);
}
}
#endif

View File

@@ -0,0 +1,85 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_UTILITY_DEBUG_OSTREAM_H_INCLUDED
#define BEAST_UTILITY_DEBUG_OSTREAM_H_INCLUDED
#include "abstract_ostream.h"
#include <iostream>
#ifdef _MSC_VER
# ifndef WIN32_LEAN_AND_MEAN // VC_EXTRALEAN
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# undef WIN32_LEAN_AND_MEAN
# else
# include <windows.h>
# endif
#endif
namespace beast {
#ifdef _MSC_VER
/** A basic_abstract_ostream that redirects output to an attached debugger. */
class debug_ostream
: public abstract_ostream
{
private:
bool m_debugger;
public:
debug_ostream()
: m_debugger (IsDebuggerPresent() != FALSE)
{
// Note that the check for an attached debugger is made only
// during construction time, for efficiency. A stream created before
// the debugger is attached will not have output redirected.
}
void
write (string_type const& s) override
{
if (m_debugger)
{
OutputDebugStringA ((s + "\n").c_str());
return;
}
std::cout << s << std::endl;
}
};
#else
class debug_ostream
: public abstract_ostream
{
public:
void
write (string_type const& s) override
{
std::cout << s << std::endl;
}
};
#endif
}
#endif