mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Rearrange sources (#4997)
This commit is contained in:
committed by
John Freeman
parent
2e902dee53
commit
e416ee72ca
449
include/xrpl/beast/utility/Journal.h
Normal file
449
include/xrpl/beast/utility/Journal.h
Normal file
@@ -0,0 +1,449 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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_JOURNAL_H_INCLUDED
|
||||
#define BEAST_UTILITY_JOURNAL_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** A namespace for easy access to logging severity values. */
|
||||
namespace severities {
|
||||
/** Severity level / threshold of a Journal message. */
|
||||
enum Severity {
|
||||
kAll = 0,
|
||||
|
||||
kTrace = kAll,
|
||||
kDebug,
|
||||
kInfo,
|
||||
kWarning,
|
||||
kError,
|
||||
kFatal,
|
||||
|
||||
kDisabled,
|
||||
kNone = kDisabled
|
||||
};
|
||||
} // namespace severities
|
||||
|
||||
/** A generic endpoint for log messages.
|
||||
|
||||
The Journal has a few simple goals:
|
||||
|
||||
* To be light-weight and copied by value.
|
||||
* To allow logging statements to be left in source code.
|
||||
* The logging is controlled at run-time based on a logging threshold.
|
||||
|
||||
It is advisable to check Journal::active(level) prior to formatting log
|
||||
text. Doing so sidesteps expensive text formatting when the results
|
||||
will not be sent to the log.
|
||||
*/
|
||||
class Journal
|
||||
{
|
||||
public:
|
||||
class Sink;
|
||||
|
||||
private:
|
||||
// Severity level / threshold of a Journal message.
|
||||
using Severity = severities::Severity;
|
||||
|
||||
// Invariant: m_sink always points to a valid Sink
|
||||
Sink* m_sink;
|
||||
|
||||
public:
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** Abstraction for the underlying message destination. */
|
||||
class Sink
|
||||
{
|
||||
protected:
|
||||
Sink() = delete;
|
||||
explicit Sink(Sink const& sink) = default;
|
||||
Sink(Severity thresh, bool console);
|
||||
Sink&
|
||||
operator=(Sink const& lhs) = delete;
|
||||
|
||||
public:
|
||||
virtual ~Sink() = 0;
|
||||
|
||||
/** Returns `true` if text at the passed severity produces output. */
|
||||
virtual bool
|
||||
active(Severity level) const;
|
||||
|
||||
/** Returns `true` if a message is also written to the Output Window
|
||||
* (MSVC). */
|
||||
virtual bool
|
||||
console() const;
|
||||
|
||||
/** Set whether messages are also written to the Output Window (MSVC).
|
||||
*/
|
||||
virtual void
|
||||
console(bool output);
|
||||
|
||||
/** Returns the minimum severity level this sink will report. */
|
||||
virtual Severity
|
||||
threshold() const;
|
||||
|
||||
/** Set the minimum severity this sink will report. */
|
||||
virtual void
|
||||
threshold(Severity thresh);
|
||||
|
||||
/** Write text to the sink at the specified severity.
|
||||
A conforming implementation will not write the text if the passed
|
||||
level is below the current threshold().
|
||||
*/
|
||||
virtual void
|
||||
write(Severity level, std::string const& text) = 0;
|
||||
|
||||
private:
|
||||
Severity thresh_;
|
||||
bool m_console;
|
||||
};
|
||||
|
||||
#ifndef __INTELLISENSE__
|
||||
static_assert(std::is_default_constructible<Sink>::value == false, "");
|
||||
static_assert(std::is_copy_constructible<Sink>::value == false, "");
|
||||
static_assert(std::is_move_constructible<Sink>::value == false, "");
|
||||
static_assert(std::is_copy_assignable<Sink>::value == false, "");
|
||||
static_assert(std::is_move_assignable<Sink>::value == false, "");
|
||||
static_assert(std::is_nothrow_destructible<Sink>::value == true, "");
|
||||
#endif
|
||||
|
||||
/** Returns a Sink which does nothing. */
|
||||
static Sink&
|
||||
getNullSink();
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
class Stream;
|
||||
|
||||
/* Scoped ostream-based container for writing messages to a Journal. */
|
||||
class ScopedStream
|
||||
{
|
||||
public:
|
||||
ScopedStream(ScopedStream const& other)
|
||||
: ScopedStream(other.m_sink, other.m_level)
|
||||
{
|
||||
}
|
||||
|
||||
ScopedStream(Sink& sink, Severity level);
|
||||
|
||||
template <typename T>
|
||||
ScopedStream(Stream const& stream, T const& t);
|
||||
|
||||
ScopedStream(Stream const& stream, std::ostream& manip(std::ostream&));
|
||||
|
||||
ScopedStream&
|
||||
operator=(ScopedStream const&) = delete;
|
||||
|
||||
~ScopedStream();
|
||||
|
||||
std::ostringstream&
|
||||
ostream() const
|
||||
{
|
||||
return m_ostream;
|
||||
}
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& manip(std::ostream&)) const;
|
||||
|
||||
template <typename T>
|
||||
std::ostream&
|
||||
operator<<(T const& t) const;
|
||||
|
||||
private:
|
||||
Sink& m_sink;
|
||||
Severity const m_level;
|
||||
std::ostringstream mutable m_ostream;
|
||||
};
|
||||
|
||||
#ifndef __INTELLISENSE__
|
||||
static_assert(
|
||||
std::is_default_constructible<ScopedStream>::value == false,
|
||||
"");
|
||||
static_assert(std::is_copy_constructible<ScopedStream>::value == true, "");
|
||||
static_assert(std::is_move_constructible<ScopedStream>::value == true, "");
|
||||
static_assert(std::is_copy_assignable<ScopedStream>::value == false, "");
|
||||
static_assert(std::is_move_assignable<ScopedStream>::value == false, "");
|
||||
static_assert(
|
||||
std::is_nothrow_destructible<ScopedStream>::value == true,
|
||||
"");
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
public:
|
||||
/** Provide a light-weight way to check active() before string formatting */
|
||||
class Stream
|
||||
{
|
||||
public:
|
||||
/** Create a stream which produces no output. */
|
||||
explicit Stream()
|
||||
: m_sink(getNullSink()), m_level(severities::kDisabled)
|
||||
{
|
||||
}
|
||||
|
||||
/** Create a stream that writes at the given level.
|
||||
|
||||
Constructor is inlined so checking active() very inexpensive.
|
||||
*/
|
||||
Stream(Sink& sink, Severity level) : m_sink(sink), m_level(level)
|
||||
{
|
||||
assert(m_level < severities::kDisabled);
|
||||
}
|
||||
|
||||
/** Construct or copy another Stream. */
|
||||
Stream(Stream const& other) : Stream(other.m_sink, other.m_level)
|
||||
{
|
||||
}
|
||||
|
||||
Stream&
|
||||
operator=(Stream const& other) = delete;
|
||||
|
||||
/** Returns the Sink that this Stream writes to. */
|
||||
Sink&
|
||||
sink() const
|
||||
{
|
||||
return m_sink;
|
||||
}
|
||||
|
||||
/** Returns the Severity level of messages this Stream reports. */
|
||||
Severity
|
||||
level() const
|
||||
{
|
||||
return m_level;
|
||||
}
|
||||
|
||||
/** Returns `true` if sink logs anything at this stream's level. */
|
||||
/** @{ */
|
||||
bool
|
||||
active() const
|
||||
{
|
||||
return m_sink.active(m_level);
|
||||
}
|
||||
|
||||
explicit operator bool() const
|
||||
{
|
||||
return active();
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** Output stream support. */
|
||||
/** @{ */
|
||||
ScopedStream
|
||||
operator<<(std::ostream& manip(std::ostream&)) const;
|
||||
|
||||
template <typename T>
|
||||
ScopedStream
|
||||
operator<<(T const& t) const;
|
||||
/** @} */
|
||||
|
||||
private:
|
||||
Sink& m_sink;
|
||||
Severity m_level;
|
||||
};
|
||||
|
||||
#ifndef __INTELLISENSE__
|
||||
static_assert(std::is_default_constructible<Stream>::value == true, "");
|
||||
static_assert(std::is_copy_constructible<Stream>::value == true, "");
|
||||
static_assert(std::is_move_constructible<Stream>::value == true, "");
|
||||
static_assert(std::is_copy_assignable<Stream>::value == false, "");
|
||||
static_assert(std::is_move_assignable<Stream>::value == false, "");
|
||||
static_assert(std::is_nothrow_destructible<Stream>::value == true, "");
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** Journal has no default constructor. */
|
||||
Journal() = delete;
|
||||
|
||||
/** Create a journal that writes to the specified sink. */
|
||||
explicit Journal(Sink& sink) : m_sink(&sink)
|
||||
{
|
||||
}
|
||||
|
||||
/** Returns the Sink associated with this Journal. */
|
||||
Sink&
|
||||
sink() const
|
||||
{
|
||||
return *m_sink;
|
||||
}
|
||||
|
||||
/** Returns a stream for this sink, with the specified severity level. */
|
||||
Stream
|
||||
stream(Severity level) const
|
||||
{
|
||||
return Stream(*m_sink, level);
|
||||
}
|
||||
|
||||
/** Returns `true` if any message would be logged at this severity level.
|
||||
For a message to be logged, the severity must be at or above the
|
||||
sink's severity threshold.
|
||||
*/
|
||||
bool
|
||||
active(Severity level) const
|
||||
{
|
||||
return m_sink->active(level);
|
||||
}
|
||||
|
||||
/** Severity stream access functions. */
|
||||
/** @{ */
|
||||
Stream
|
||||
trace() const
|
||||
{
|
||||
return {*m_sink, severities::kTrace};
|
||||
}
|
||||
|
||||
Stream
|
||||
debug() const
|
||||
{
|
||||
return {*m_sink, severities::kDebug};
|
||||
}
|
||||
|
||||
Stream
|
||||
info() const
|
||||
{
|
||||
return {*m_sink, severities::kInfo};
|
||||
}
|
||||
|
||||
Stream
|
||||
warn() const
|
||||
{
|
||||
return {*m_sink, severities::kWarning};
|
||||
}
|
||||
|
||||
Stream
|
||||
error() const
|
||||
{
|
||||
return {*m_sink, severities::kError};
|
||||
}
|
||||
|
||||
Stream
|
||||
fatal() const
|
||||
{
|
||||
return {*m_sink, severities::kFatal};
|
||||
}
|
||||
/** @} */
|
||||
};
|
||||
|
||||
#ifndef __INTELLISENSE__
|
||||
static_assert(std::is_default_constructible<Journal>::value == false, "");
|
||||
static_assert(std::is_copy_constructible<Journal>::value == true, "");
|
||||
static_assert(std::is_move_constructible<Journal>::value == true, "");
|
||||
static_assert(std::is_copy_assignable<Journal>::value == true, "");
|
||||
static_assert(std::is_move_assignable<Journal>::value == true, "");
|
||||
static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
Journal::ScopedStream::ScopedStream(Journal::Stream const& stream, T const& t)
|
||||
: ScopedStream(stream.sink(), stream.level())
|
||||
{
|
||||
m_ostream << t;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::ostream&
|
||||
Journal::ScopedStream::operator<<(T const& t) const
|
||||
{
|
||||
m_ostream << t;
|
||||
return m_ostream;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
Journal::ScopedStream
|
||||
Journal::Stream::operator<<(T const& t) const
|
||||
{
|
||||
return ScopedStream(*this, t);
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <class CharT, class Traits = std::char_traits<CharT>>
|
||||
class logstream_buf : public std::basic_stringbuf<CharT, Traits>
|
||||
{
|
||||
beast::Journal::Stream strm_;
|
||||
|
||||
template <class T>
|
||||
void
|
||||
write(T const*) = delete;
|
||||
|
||||
void
|
||||
write(char const* s)
|
||||
{
|
||||
if (strm_)
|
||||
strm_ << s;
|
||||
}
|
||||
|
||||
void
|
||||
write(wchar_t const* s)
|
||||
{
|
||||
if (strm_)
|
||||
strm_ << s;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm)
|
||||
{
|
||||
}
|
||||
|
||||
~logstream_buf()
|
||||
{
|
||||
sync();
|
||||
}
|
||||
|
||||
int
|
||||
sync() override
|
||||
{
|
||||
write(this->str().c_str());
|
||||
this->str("");
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <class CharT, class Traits = std::char_traits<CharT>>
|
||||
class basic_logstream : public std::basic_ostream<CharT, Traits>
|
||||
{
|
||||
typedef CharT char_type;
|
||||
typedef Traits traits_type;
|
||||
typedef typename traits_type::int_type int_type;
|
||||
typedef typename traits_type::pos_type pos_type;
|
||||
typedef typename traits_type::off_type off_type;
|
||||
|
||||
detail::logstream_buf<CharT, Traits> buf_;
|
||||
|
||||
public:
|
||||
explicit basic_logstream(beast::Journal::Stream const& strm)
|
||||
: std::basic_ostream<CharT, Traits>(&buf_), buf_(strm)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
using logstream = basic_logstream<char>;
|
||||
using logwstream = basic_logstream<wchar_t>;
|
||||
|
||||
} // namespace beast
|
||||
|
||||
#endif
|
||||
433
include/xrpl/beast/utility/PropertyStream.h
Normal file
433
include/xrpl/beast/utility/PropertyStream.h
Normal file
@@ -0,0 +1,433 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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_PROPERTYSTREAM_H_INCLUDED
|
||||
#define BEAST_UTILITY_PROPERTYSTREAM_H_INCLUDED
|
||||
|
||||
#include <ripple/beast/core/List.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace beast {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Abstract stream with RAII containers that produce a property tree. */
|
||||
class PropertyStream
|
||||
{
|
||||
public:
|
||||
class Map;
|
||||
class Set;
|
||||
class Source;
|
||||
|
||||
PropertyStream() = default;
|
||||
virtual ~PropertyStream() = default;
|
||||
|
||||
protected:
|
||||
virtual void
|
||||
map_begin() = 0;
|
||||
virtual void
|
||||
map_begin(std::string const& key) = 0;
|
||||
virtual void
|
||||
map_end() = 0;
|
||||
|
||||
virtual void
|
||||
add(std::string const& key, std::string const& value) = 0;
|
||||
|
||||
void
|
||||
add(std::string const& key, char const* value)
|
||||
{
|
||||
add(key, std::string(value));
|
||||
}
|
||||
|
||||
template <typename Value>
|
||||
void
|
||||
lexical_add(std::string const& key, Value value)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << value;
|
||||
add(key, ss.str());
|
||||
}
|
||||
|
||||
virtual void
|
||||
add(std::string const& key, bool value);
|
||||
virtual void
|
||||
add(std::string const& key, char value);
|
||||
virtual void
|
||||
add(std::string const& key, signed char value);
|
||||
virtual void
|
||||
add(std::string const& key, unsigned char value);
|
||||
virtual void
|
||||
add(std::string const& key, short value);
|
||||
virtual void
|
||||
add(std::string const& key, unsigned short value);
|
||||
virtual void
|
||||
add(std::string const& key, int value);
|
||||
virtual void
|
||||
add(std::string const& key, unsigned int value);
|
||||
virtual void
|
||||
add(std::string const& key, long value);
|
||||
virtual void
|
||||
add(std::string const& key, unsigned long value);
|
||||
virtual void
|
||||
add(std::string const& key, long long value);
|
||||
virtual void
|
||||
add(std::string const& key, unsigned long long value);
|
||||
virtual void
|
||||
add(std::string const& key, float value);
|
||||
virtual void
|
||||
add(std::string const& key, double value);
|
||||
virtual void
|
||||
add(std::string const& key, long double value);
|
||||
|
||||
virtual void
|
||||
array_begin() = 0;
|
||||
virtual void
|
||||
array_begin(std::string const& key) = 0;
|
||||
virtual void
|
||||
array_end() = 0;
|
||||
|
||||
virtual void
|
||||
add(std::string const& value) = 0;
|
||||
|
||||
void
|
||||
add(char const* value)
|
||||
{
|
||||
add(std::string(value));
|
||||
}
|
||||
|
||||
template <typename Value>
|
||||
void
|
||||
lexical_add(Value value)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << value;
|
||||
add(ss.str());
|
||||
}
|
||||
|
||||
virtual void
|
||||
add(bool value);
|
||||
virtual void
|
||||
add(char value);
|
||||
virtual void
|
||||
add(signed char value);
|
||||
virtual void
|
||||
add(unsigned char value);
|
||||
virtual void
|
||||
add(short value);
|
||||
virtual void
|
||||
add(unsigned short value);
|
||||
virtual void
|
||||
add(int value);
|
||||
virtual void
|
||||
add(unsigned int value);
|
||||
virtual void
|
||||
add(long value);
|
||||
virtual void
|
||||
add(unsigned long value);
|
||||
virtual void
|
||||
add(long long value);
|
||||
virtual void
|
||||
add(unsigned long long value);
|
||||
virtual void
|
||||
add(float value);
|
||||
virtual void
|
||||
add(double value);
|
||||
virtual void
|
||||
add(long double value);
|
||||
|
||||
private:
|
||||
class Item;
|
||||
class Proxy;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
// Item
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class PropertyStream::Item : public List<Item>::Node
|
||||
{
|
||||
public:
|
||||
explicit Item(Source* source);
|
||||
Source&
|
||||
source() const;
|
||||
Source*
|
||||
operator->() const;
|
||||
Source&
|
||||
operator*() const;
|
||||
|
||||
private:
|
||||
Source* m_source;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
// Proxy
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class PropertyStream::Proxy
|
||||
{
|
||||
private:
|
||||
Map const* m_map;
|
||||
std::string m_key;
|
||||
std::ostringstream mutable m_ostream;
|
||||
|
||||
public:
|
||||
Proxy(Map const& map, std::string const& key);
|
||||
Proxy(Proxy const& other);
|
||||
~Proxy();
|
||||
|
||||
template <typename Value>
|
||||
Proxy&
|
||||
operator=(Value value);
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& manip(std::ostream&)) const;
|
||||
|
||||
template <typename T>
|
||||
std::ostream&
|
||||
operator<<(T const& t) const
|
||||
{
|
||||
return m_ostream << t;
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
// Map
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class PropertyStream::Map
|
||||
{
|
||||
private:
|
||||
PropertyStream& m_stream;
|
||||
|
||||
public:
|
||||
explicit Map(PropertyStream& stream);
|
||||
explicit Map(Set& parent);
|
||||
Map(std::string const& key, Map& parent);
|
||||
Map(std::string const& key, PropertyStream& stream);
|
||||
~Map();
|
||||
|
||||
Map(Map const&) = delete;
|
||||
Map&
|
||||
operator=(Map const&) = delete;
|
||||
|
||||
PropertyStream&
|
||||
stream();
|
||||
PropertyStream const&
|
||||
stream() const;
|
||||
|
||||
template <typename Value>
|
||||
void
|
||||
add(std::string const& key, Value value) const
|
||||
{
|
||||
m_stream.add(key, value);
|
||||
}
|
||||
|
||||
template <typename Key, typename Value>
|
||||
void
|
||||
add(Key key, Value value) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << key;
|
||||
add(ss.str(), value);
|
||||
}
|
||||
|
||||
Proxy
|
||||
operator[](std::string const& key);
|
||||
|
||||
Proxy
|
||||
operator[](char const* key)
|
||||
{
|
||||
return Proxy(*this, key);
|
||||
}
|
||||
|
||||
template <typename Key>
|
||||
Proxy
|
||||
operator[](Key key) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << key;
|
||||
return Proxy(*this, ss.str());
|
||||
}
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
template <typename Value>
|
||||
PropertyStream::Proxy&
|
||||
PropertyStream::Proxy::operator=(Value value)
|
||||
{
|
||||
m_map->add(m_key, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
//
|
||||
// Set
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class PropertyStream::Set
|
||||
{
|
||||
private:
|
||||
PropertyStream& m_stream;
|
||||
|
||||
public:
|
||||
Set(std::string const& key, Map& map);
|
||||
Set(std::string const& key, PropertyStream& stream);
|
||||
~Set();
|
||||
|
||||
Set(Set const&) = delete;
|
||||
Set&
|
||||
operator=(Set const&) = delete;
|
||||
|
||||
PropertyStream&
|
||||
stream();
|
||||
PropertyStream const&
|
||||
stream() const;
|
||||
|
||||
template <typename Value>
|
||||
void
|
||||
add(Value value) const
|
||||
{
|
||||
m_stream.add(value);
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
// Source
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Subclasses can be called to write to a stream and have children. */
|
||||
class PropertyStream::Source
|
||||
{
|
||||
private:
|
||||
std::string const m_name;
|
||||
std::recursive_mutex lock_;
|
||||
Item item_;
|
||||
Source* parent_;
|
||||
List<Item> children_;
|
||||
|
||||
public:
|
||||
explicit Source(std::string const& name);
|
||||
virtual ~Source();
|
||||
|
||||
Source(Source const&) = delete;
|
||||
Source&
|
||||
operator=(Source const&) = delete;
|
||||
|
||||
/** Returns the name of this source. */
|
||||
std::string const&
|
||||
name() const;
|
||||
|
||||
/** Add a child source. */
|
||||
void
|
||||
add(Source& source);
|
||||
|
||||
/** Add a child source by pointer.
|
||||
The source pointer is returned so it can be used in ctor-initializers.
|
||||
*/
|
||||
template <class Derived>
|
||||
Derived*
|
||||
add(Derived* child)
|
||||
{
|
||||
add(*static_cast<Source*>(child));
|
||||
return child;
|
||||
}
|
||||
|
||||
/** Remove a child source from this Source. */
|
||||
void
|
||||
remove(Source& child);
|
||||
|
||||
/** Remove all child sources from this Source. */
|
||||
void
|
||||
removeAll();
|
||||
|
||||
/** Write only this Source to the stream. */
|
||||
void
|
||||
write_one(PropertyStream& stream);
|
||||
|
||||
/** write this source and all its children recursively to the stream. */
|
||||
void
|
||||
write(PropertyStream& stream);
|
||||
|
||||
/** Parse the path and write the corresponding Source and optional children.
|
||||
If the source is found, it is written. If the wildcard character '*'
|
||||
exists as the last character in the path, then all the children are
|
||||
written recursively.
|
||||
*/
|
||||
void
|
||||
write(PropertyStream& stream, std::string const& path);
|
||||
|
||||
/** Parse the dot-delimited Source path and return the result.
|
||||
The first value will be a pointer to the Source object corresponding
|
||||
to the given path. If no Source object exists, then the first value
|
||||
will be nullptr and the second value will be undefined.
|
||||
The second value is a boolean indicating whether or not the path string
|
||||
specifies the wildcard character '*' as the last character.
|
||||
|
||||
print statement examples
|
||||
"parent.child" prints child and all of its children
|
||||
"parent.child." start at the parent and print down to child
|
||||
"parent.grandchild" prints nothing- grandchild not direct discendent
|
||||
"parent.grandchild." starts at the parent and prints down to grandchild
|
||||
"parent.grandchild.*" starts at parent, print through grandchild
|
||||
children
|
||||
*/
|
||||
std::pair<Source*, bool>
|
||||
find(std::string path);
|
||||
|
||||
Source*
|
||||
find_one_deep(std::string const& name);
|
||||
PropertyStream::Source*
|
||||
find_path(std::string path);
|
||||
PropertyStream::Source*
|
||||
find_one(std::string const& name);
|
||||
|
||||
static bool
|
||||
peel_leading_slash(std::string* path);
|
||||
static bool
|
||||
peel_trailing_slashstar(std::string* path);
|
||||
static std::string
|
||||
peel_name(std::string* path);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** Subclass override.
|
||||
The default version does nothing.
|
||||
*/
|
||||
virtual void
|
||||
onWrite(Map&);
|
||||
};
|
||||
|
||||
} // namespace beast
|
||||
|
||||
#endif
|
||||
100
include/xrpl/beast/utility/WrappedSink.h
Normal file
100
include/xrpl/beast/utility/WrappedSink.h
Normal file
@@ -0,0 +1,100 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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_WRAPPEDSINK_H_INCLUDED
|
||||
#define BEAST_UTILITY_WRAPPEDSINK_H_INCLUDED
|
||||
|
||||
#include <ripple/beast/utility/Journal.h>
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** Wraps a Journal::Sink to prefix its output with a string. */
|
||||
|
||||
// A WrappedSink both is a Sink and has a Sink:
|
||||
// o It inherits from Sink so it has the correct interface.
|
||||
// o It has a sink (reference) so it preserves the passed write() behavior.
|
||||
// The data inherited from the base class is ignored.
|
||||
class WrappedSink : public beast::Journal::Sink
|
||||
{
|
||||
private:
|
||||
beast::Journal::Sink& sink_;
|
||||
std::string prefix_;
|
||||
|
||||
public:
|
||||
explicit WrappedSink(
|
||||
beast::Journal::Sink& sink,
|
||||
std::string const& prefix = "")
|
||||
: Sink(sink), sink_(sink), prefix_(prefix)
|
||||
{
|
||||
}
|
||||
|
||||
explicit WrappedSink(
|
||||
beast::Journal const& journal,
|
||||
std::string const& prefix = "")
|
||||
: WrappedSink(journal.sink(), prefix)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
prefix(std::string const& s)
|
||||
{
|
||||
prefix_ = s;
|
||||
}
|
||||
|
||||
bool
|
||||
active(beast::severities::Severity level) const override
|
||||
{
|
||||
return sink_.active(level);
|
||||
}
|
||||
|
||||
bool
|
||||
console() const override
|
||||
{
|
||||
return sink_.console();
|
||||
}
|
||||
|
||||
void
|
||||
console(bool output) override
|
||||
{
|
||||
sink_.console(output);
|
||||
}
|
||||
|
||||
beast::severities::Severity
|
||||
threshold() const override
|
||||
{
|
||||
return sink_.threshold();
|
||||
}
|
||||
|
||||
void
|
||||
threshold(beast::severities::Severity thresh) override
|
||||
{
|
||||
sink_.threshold(thresh);
|
||||
}
|
||||
|
||||
void
|
||||
write(beast::severities::Severity level, std::string const& text) override
|
||||
{
|
||||
using beast::Journal;
|
||||
sink_.write(level, prefix_ + text);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace beast
|
||||
|
||||
#endif
|
||||
165
include/xrpl/beast/utility/Zero.h
Normal file
165
include/xrpl/beast/utility/Zero.h
Normal file
@@ -0,0 +1,165 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2014, Tom Ritchford <tom@swirly.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_ZERO_H_INCLUDED
|
||||
#define BEAST_UTILITY_ZERO_H_INCLUDED
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** Zero allows classes to offer efficient comparisons to zero.
|
||||
|
||||
Zero is a struct to allow classes to efficiently compare with zero without
|
||||
requiring an rvalue construction.
|
||||
|
||||
It's often the case that we have classes which combine a number and a unit.
|
||||
In such cases, comparisons like t > 0 or t != 0 make sense, but comparisons
|
||||
like t > 1 or t != 1 do not.
|
||||
|
||||
The class Zero allows such comparisons to be easily made.
|
||||
|
||||
The comparing class T either needs to have a method called signum() which
|
||||
returns a positive number, 0, or a negative; or there needs to be a signum
|
||||
function which resolves in the namespace which takes an instance of T and
|
||||
returns a positive, zero or negative number.
|
||||
*/
|
||||
|
||||
struct Zero
|
||||
{
|
||||
explicit Zero() = default;
|
||||
};
|
||||
|
||||
namespace {
|
||||
static constexpr Zero zero{};
|
||||
}
|
||||
|
||||
/** Default implementation of signum calls the method on the class. */
|
||||
template <typename T>
|
||||
auto
|
||||
signum(T const& t)
|
||||
{
|
||||
return t.signum();
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
namespace zero_helper {
|
||||
|
||||
// For argument dependent lookup to function properly, calls to signum must
|
||||
// be made from a namespace that does not include overloads of the function..
|
||||
template <class T>
|
||||
auto
|
||||
call_signum(T const& t)
|
||||
{
|
||||
return signum(t);
|
||||
}
|
||||
|
||||
} // namespace zero_helper
|
||||
} // namespace detail
|
||||
|
||||
// Handle operators where T is on the left side using signum.
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator==(T const& t, Zero)
|
||||
{
|
||||
return detail::zero_helper::call_signum(t) == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator!=(T const& t, Zero)
|
||||
{
|
||||
return detail::zero_helper::call_signum(t) != 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator<(T const& t, Zero)
|
||||
{
|
||||
return detail::zero_helper::call_signum(t) < 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator>(T const& t, Zero)
|
||||
{
|
||||
return detail::zero_helper::call_signum(t) > 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator>=(T const& t, Zero)
|
||||
{
|
||||
return detail::zero_helper::call_signum(t) >= 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator<=(T const& t, Zero)
|
||||
{
|
||||
return detail::zero_helper::call_signum(t) <= 0;
|
||||
}
|
||||
|
||||
// Handle operators where T is on the right side by
|
||||
// reversing the operation, so that T is on the left side.
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator==(Zero, T const& t)
|
||||
{
|
||||
return t == zero;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator!=(Zero, T const& t)
|
||||
{
|
||||
return t != zero;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator<(Zero, T const& t)
|
||||
{
|
||||
return t > zero;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator>(Zero, T const& t)
|
||||
{
|
||||
return t < zero;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator>=(Zero, T const& t)
|
||||
{
|
||||
return t <= zero;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator<=(Zero, T const& t)
|
||||
{
|
||||
return t >= zero;
|
||||
}
|
||||
|
||||
} // namespace beast
|
||||
|
||||
#endif
|
||||
72
include/xrpl/beast/utility/hash_pair.h
Normal file
72
include/xrpl/beast/utility/hash_pair.h
Normal file
@@ -0,0 +1,72 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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_HASH_PAIR_H_INCLUDED
|
||||
#define BEAST_UTILITY_HASH_PAIR_H_INCLUDED
|
||||
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <boost/utility/base_from_member.hpp>
|
||||
|
||||
namespace std {
|
||||
|
||||
/** Specialization of std::hash for any std::pair type. */
|
||||
template <class First, class Second>
|
||||
struct hash<std::pair<First, Second>>
|
||||
: private boost::base_from_member<std::hash<First>, 0>,
|
||||
private boost::base_from_member<std::hash<Second>, 1>
|
||||
{
|
||||
private:
|
||||
using first_hash = boost::base_from_member<std::hash<First>, 0>;
|
||||
using second_hash = boost::base_from_member<std::hash<Second>, 1>;
|
||||
|
||||
public:
|
||||
hash()
|
||||
{
|
||||
}
|
||||
|
||||
hash(
|
||||
std::hash<First> const& first_hash_,
|
||||
std::hash<Second> const& second_hash_)
|
||||
: first_hash(first_hash_), second_hash(second_hash_)
|
||||
{
|
||||
}
|
||||
|
||||
std::size_t
|
||||
operator()(std::pair<First, Second> const& value)
|
||||
{
|
||||
std::size_t result(first_hash::member(value.first));
|
||||
boost::hash_combine(result, second_hash::member(value.second));
|
||||
return result;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
operator()(std::pair<First, Second> const& value) const
|
||||
{
|
||||
std::size_t result(first_hash::member(value.first));
|
||||
boost::hash_combine(result, second_hash::member(value.second));
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif
|
||||
44
include/xrpl/beast/utility/maybe_const.h
Normal file
44
include/xrpl/beast/utility/maybe_const.h
Normal file
@@ -0,0 +1,44 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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_MAYBE_CONST_H_INCLUDED
|
||||
#define BEAST_UTILITY_MAYBE_CONST_H_INCLUDED
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** Makes T const or non const depending on a bool. */
|
||||
template <bool IsConst, class T>
|
||||
struct maybe_const
|
||||
{
|
||||
explicit maybe_const() = default;
|
||||
using type = typename std::conditional<
|
||||
IsConst,
|
||||
typename std::remove_const<T>::type const,
|
||||
typename std::remove_const<T>::type>::type;
|
||||
};
|
||||
|
||||
/** Alias for omitting `typename`. */
|
||||
template <bool IsConst, class T>
|
||||
using maybe_const_t = typename maybe_const<IsConst, T>::type;
|
||||
|
||||
} // namespace beast
|
||||
|
||||
#endif
|
||||
80
include/xrpl/beast/utility/rngfill.h
Normal file
80
include/xrpl/beast/utility/rngfill.h
Normal file
@@ -0,0 +1,80 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2014, 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_RANDOM_RNGFILL_H_INCLUDED
|
||||
#define BEAST_RANDOM_RNGFILL_H_INCLUDED
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
|
||||
namespace beast {
|
||||
|
||||
template <class Generator>
|
||||
void
|
||||
rngfill(void* buffer, std::size_t bytes, Generator& g)
|
||||
{
|
||||
using result_type = typename Generator::result_type;
|
||||
|
||||
while (bytes >= sizeof(result_type))
|
||||
{
|
||||
auto const v = g();
|
||||
std::memcpy(buffer, &v, sizeof(v));
|
||||
buffer = reinterpret_cast<std::uint8_t*>(buffer) + sizeof(v);
|
||||
bytes -= sizeof(v);
|
||||
}
|
||||
|
||||
assert(bytes < sizeof(result_type));
|
||||
|
||||
#ifdef __GNUC__
|
||||
// gcc 11.1 (falsely) warns about an array-bounds overflow in release mode.
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||
#endif
|
||||
|
||||
if (bytes > 0)
|
||||
{
|
||||
auto const v = g();
|
||||
std::memcpy(buffer, &v, bytes);
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
}
|
||||
|
||||
template <
|
||||
class Generator,
|
||||
std::size_t N,
|
||||
class = std::enable_if_t<N % sizeof(typename Generator::result_type) == 0>>
|
||||
void
|
||||
rngfill(std::array<std::uint8_t, N>& a, Generator& g)
|
||||
{
|
||||
using result_type = typename Generator::result_type;
|
||||
auto i = N / sizeof(result_type);
|
||||
result_type* p = reinterpret_cast<result_type*>(a.data());
|
||||
while (i--)
|
||||
*p++ = g();
|
||||
}
|
||||
|
||||
} // namespace beast
|
||||
|
||||
#endif
|
||||
84
include/xrpl/beast/utility/temp_dir.h
Normal file
84
include/xrpl/beast/utility/temp_dir.h
Normal file
@@ -0,0 +1,84 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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_TEMP_DIR_H_INCLUDED
|
||||
#define BEAST_UTILITY_TEMP_DIR_H_INCLUDED
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** 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()
|
||||
{
|
||||
// use non-throwing calls in the destructor
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::remove_all(path_, ec);
|
||||
// TODO: warn/notify if ec set ?
|
||||
}
|
||||
|
||||
/// 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();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace beast
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user