mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-11 06:35:51 +00:00
Add abstract streams module:
* New basic_abstract_ostream template for generic output * New abstract_ostream, common type alias * basic_scoped_ostream, RAII output to abstract streams
This commit is contained in:
@@ -204,6 +204,9 @@
|
|||||||
<ClInclude Include="..\..\beast\smart_ptr\SharedObject.h" />
|
<ClInclude Include="..\..\beast\smart_ptr\SharedObject.h" />
|
||||||
<ClInclude Include="..\..\beast\smart_ptr\SharedPtr.h" />
|
<ClInclude Include="..\..\beast\smart_ptr\SharedPtr.h" />
|
||||||
<ClInclude Include="..\..\beast\StaticAssert.h" />
|
<ClInclude Include="..\..\beast\StaticAssert.h" />
|
||||||
|
<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\Strings.h" />
|
<ClInclude Include="..\..\beast\Strings.h" />
|
||||||
<ClInclude Include="..\..\beast\strings\CharacterFunctions.h" />
|
<ClInclude Include="..\..\beast\strings\CharacterFunctions.h" />
|
||||||
<ClInclude Include="..\..\beast\strings\CharPointer_ASCII.h" />
|
<ClInclude Include="..\..\beast\strings\CharPointer_ASCII.h" />
|
||||||
@@ -681,6 +684,7 @@
|
|||||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\..\beast\smart_ptr\SmartPtr.cpp" />
|
<ClCompile Include="..\..\beast\smart_ptr\SmartPtr.cpp" />
|
||||||
|
<ClCompile Include="..\..\beast\streams\streams.cpp" />
|
||||||
<ClCompile Include="..\..\beast\strings\impl\CharacterFunctions.cpp">
|
<ClCompile Include="..\..\beast\strings\impl\CharacterFunctions.cpp">
|
||||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||||
|
|||||||
@@ -318,6 +318,9 @@
|
|||||||
<Filter Include="beast\workaround">
|
<Filter Include="beast\workaround">
|
||||||
<UniqueIdentifier>{84acd0d5-5531-470e-b9a7-42af9003aa64}</UniqueIdentifier>
|
<UniqueIdentifier>{84acd0d5-5531-470e-b9a7-42af9003aa64}</UniqueIdentifier>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="beast\streams">
|
||||||
|
<UniqueIdentifier>{899ea9a6-1969-4cde-b26d-8ad60acebfa4}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\modules\beast_core\beast_core.h">
|
<ClInclude Include="..\..\modules\beast_core\beast_core.h">
|
||||||
@@ -1332,6 +1335,15 @@
|
|||||||
<ClInclude Include="..\..\beast\workaround\noexcept.h">
|
<ClInclude Include="..\..\beast\workaround\noexcept.h">
|
||||||
<Filter>beast\workaround</Filter>
|
<Filter>beast\workaround</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\beast\streams\abstract_ostream.h">
|
||||||
|
<Filter>beast\streams</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\beast\streams\basic_abstract_ostream.h">
|
||||||
|
<Filter>beast\streams</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\beast\streams\basic_scoped_ostream.h">
|
||||||
|
<Filter>beast\streams</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\modules\beast_core\containers\DynamicObject.cpp">
|
<ClCompile Include="..\..\modules\beast_core\containers\DynamicObject.cpp">
|
||||||
@@ -1925,6 +1937,9 @@
|
|||||||
<ClCompile Include="..\..\beast\http\impl\joyent_parser.cpp">
|
<ClCompile Include="..\..\beast\http\impl\joyent_parser.cpp">
|
||||||
<Filter>beast\http\impl</Filter>
|
<Filter>beast\http\impl</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\beast\streams\streams.cpp">
|
||||||
|
<Filter>beast\streams</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Text Include="..\..\TODO.txt">
|
<Text Include="..\..\TODO.txt">
|
||||||
|
|||||||
32
beast/streams/abstract_ostream.h
Normal file
32
beast/streams/abstract_ostream.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
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_ABSTRACT_OSTREAM_H_INCLUDED
|
||||||
|
#define BEAST_UTILITY_ABSTRACT_OSTREAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include "basic_abstract_ostream.h"
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
/** An abstract ostream for `char`. */
|
||||||
|
typedef basic_abstract_ostream <char> abstract_ostream;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
118
beast/streams/basic_abstract_ostream.h
Normal file
118
beast/streams/basic_abstract_ostream.h
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
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_STREAMS_BASIC_ABSTRACT_OSTREAM_H_INCLUDED
|
||||||
|
#define BEAST_STREAMS_BASIC_ABSTRACT_OSTREAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include "basic_scoped_ostream.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
/** Abstraction for an output stream similar to std::basic_ostream. */
|
||||||
|
template <
|
||||||
|
class CharT,
|
||||||
|
class Traits = std::char_traits <CharT>
|
||||||
|
>
|
||||||
|
class basic_abstract_ostream
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef std::basic_string <CharT, Traits> string_type;
|
||||||
|
typedef basic_scoped_ostream <CharT, Traits> scoped_stream_type;
|
||||||
|
|
||||||
|
basic_abstract_ostream() = default;
|
||||||
|
|
||||||
|
virtual
|
||||||
|
~basic_abstract_ostream() = default;
|
||||||
|
|
||||||
|
basic_abstract_ostream (basic_abstract_ostream const&) = default;
|
||||||
|
basic_abstract_ostream& operator= (
|
||||||
|
basic_abstract_ostream const&) = default;
|
||||||
|
|
||||||
|
/** Returns `true` if the stream is active.
|
||||||
|
Inactive streams do not produce output.
|
||||||
|
*/
|
||||||
|
/** @{ */
|
||||||
|
virtual
|
||||||
|
bool
|
||||||
|
active() const
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit
|
||||||
|
operator bool() const
|
||||||
|
{
|
||||||
|
return active();
|
||||||
|
}
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/** 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
|
||||||
|
write (string_type const& s) = 0;
|
||||||
|
|
||||||
|
scoped_stream_type
|
||||||
|
operator<< (std::ostream& manip (std::ostream&))
|
||||||
|
{
|
||||||
|
return scoped_stream_type (manip,
|
||||||
|
[this](string_type const& s)
|
||||||
|
{
|
||||||
|
this->write (s);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
scoped_stream_type
|
||||||
|
operator<< (T const& t)
|
||||||
|
{
|
||||||
|
return scoped_stream_type (t,
|
||||||
|
[this](string_type const& s)
|
||||||
|
{
|
||||||
|
this->write (s);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
149
beast/streams/basic_scoped_ostream.h
Normal file
149
beast/streams/basic_scoped_ostream.h
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
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_STREAMS_BASIC_SCOPED_OSTREAM_H_INCLUDED
|
||||||
|
#define BEAST_STREAMS_BASIC_SCOPED_OSTREAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include "../cxx14/memory.h" // <memory>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// gcc libstd++ doesn't have move constructors for basic_ostringstream
|
||||||
|
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316
|
||||||
|
//
|
||||||
|
#ifndef BEAST_NO_STDLIB_STREAM_MOVE
|
||||||
|
# ifdef __GLIBCXX__
|
||||||
|
# define BEAST_NO_STDLIB_STREAM_MOVE 1
|
||||||
|
# else
|
||||||
|
# define BEAST_NO_STDLIB_STREAM_MOVE 0
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
template <
|
||||||
|
class CharT,
|
||||||
|
class Traits
|
||||||
|
>
|
||||||
|
class basic_abstract_ostream;
|
||||||
|
|
||||||
|
/** Scoped output stream that forwards to a functor upon destruction. */
|
||||||
|
template <
|
||||||
|
class CharT,
|
||||||
|
class Traits = std::char_traits <CharT>,
|
||||||
|
class Allocator = std::allocator <CharT>
|
||||||
|
>
|
||||||
|
class basic_scoped_ostream
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
typedef std::function <void (
|
||||||
|
std::basic_string <CharT, Traits, Allocator> const&)> handler_t;
|
||||||
|
|
||||||
|
typedef std::basic_ostringstream <
|
||||||
|
CharT, Traits, Allocator> stream_type;
|
||||||
|
|
||||||
|
handler_t m_handler;
|
||||||
|
|
||||||
|
#if BEAST_NO_STDLIB_STREAM_MOVE
|
||||||
|
std::unique_ptr <stream_type> m_ss;
|
||||||
|
|
||||||
|
stream_type& stream()
|
||||||
|
{
|
||||||
|
return *m_ss;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
stream_type m_ss;
|
||||||
|
|
||||||
|
stream_type& stream()
|
||||||
|
{
|
||||||
|
return m_ss;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef std::basic_string <CharT, Traits> string_type;
|
||||||
|
|
||||||
|
// Disallow copy since that would duplicate the output
|
||||||
|
basic_scoped_ostream (basic_scoped_ostream const&) = delete;
|
||||||
|
basic_scoped_ostream& operator= (basic_scoped_ostream const) = delete;
|
||||||
|
|
||||||
|
template <class Handler>
|
||||||
|
explicit basic_scoped_ostream (Handler&& handler)
|
||||||
|
: m_handler (std::forward <Handler> (handler))
|
||||||
|
#if BEAST_NO_STDLIB_STREAM_MOVE
|
||||||
|
, m_ss (std::make_unique <stream_type>())
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, class Handler>
|
||||||
|
basic_scoped_ostream (T const& t, Handler&& handler)
|
||||||
|
: m_handler (std::forward <Handler> (handler))
|
||||||
|
#if BEAST_NO_STDLIB_STREAM_MOVE
|
||||||
|
, m_ss (std::make_unique <stream_type>())
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
stream() << t;
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_scoped_ostream (basic_abstract_ostream <
|
||||||
|
CharT, Traits>& ostream)
|
||||||
|
: m_handler (
|
||||||
|
[&](string_type const& s)
|
||||||
|
{
|
||||||
|
ostream.write (s);
|
||||||
|
})
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_scoped_ostream (basic_scoped_ostream&& other)
|
||||||
|
: m_handler (std::move (other.m_handler))
|
||||||
|
, m_ss (std::move (other.m_ss))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~basic_scoped_ostream()
|
||||||
|
{
|
||||||
|
auto const& s (stream().str());
|
||||||
|
if (! s.empty())
|
||||||
|
m_handler (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_scoped_ostream&
|
||||||
|
operator<< (std::ostream& manip (std::ostream&))
|
||||||
|
{
|
||||||
|
stream() << manip;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
basic_scoped_ostream&
|
||||||
|
operator<< (T const& t)
|
||||||
|
{
|
||||||
|
stream() << t;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
70
beast/streams/streams.cpp
Normal file
70
beast/streams/streams.cpp
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
//==============================================================================
|
||||||
|
|
||||||
|
#if BEAST_INCLUDE_BEASTCONFIG
|
||||||
|
#include "../../BeastConfig.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../../modules/beast_core/beast_core.h"
|
||||||
|
|
||||||
|
#include "basic_abstract_ostream.h"
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
class streams_Tests : public UnitTest
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class test_stream : public basic_abstract_ostream <char>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit test_stream (UnitTest& test)
|
||||||
|
: m_test (test)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void write (string_type const& s) override
|
||||||
|
{
|
||||||
|
m_test.logMessage (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
test_stream& operator= (test_stream const&) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
UnitTest& m_test;
|
||||||
|
};
|
||||||
|
|
||||||
|
void runTest()
|
||||||
|
{
|
||||||
|
beginTestCase ("stream");
|
||||||
|
|
||||||
|
test_stream ts (*this);
|
||||||
|
|
||||||
|
ts << "Hello";
|
||||||
|
|
||||||
|
pass();
|
||||||
|
}
|
||||||
|
|
||||||
|
streams_Tests() : UnitTest ("streams", "beast")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static streams_Tests streams_tests;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user