mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Add ripple_common module
Conflicts: Builds/VisualStudio2012/RippleD.vcxproj.filters
This commit is contained in:
241
src/ripple/common/functional/counted_bind.h
Normal file
241
src/ripple/common/functional/counted_bind.h
Normal file
@@ -0,0 +1,241 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
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 RIPPLE_COUNTED_BIND_H_INCLUDED
|
||||
#define RIPPLE_COUNTED_BIND_H_INCLUDED
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
namespace detail {
|
||||
|
||||
// Wrapper for managing the handler count
|
||||
template <class Handler, class Counter>
|
||||
class counted_bind_wrapper
|
||||
{
|
||||
public:
|
||||
template <class H>
|
||||
counted_bind_wrapper (H&& h, Counter& c)
|
||||
: m_handler (std::forward <H> (h))
|
||||
, m_counter (c)
|
||||
{
|
||||
++m_counter;
|
||||
}
|
||||
|
||||
counted_bind_wrapper (counted_bind_wrapper&& w)
|
||||
: m_handler (std::move (w.m_handler))
|
||||
, m_counter (w.m_counter)
|
||||
{
|
||||
++m_counter;
|
||||
}
|
||||
|
||||
counted_bind_wrapper (counted_bind_wrapper const& w)
|
||||
: m_handler (w.m_handler)
|
||||
, m_counter (w.m_counter)
|
||||
{
|
||||
++m_counter;
|
||||
}
|
||||
|
||||
~counted_bind_wrapper ()
|
||||
{
|
||||
--m_counter;
|
||||
}
|
||||
|
||||
//counted_bind_wrapper& operator= (counted_bind_wrapper const&) = delete;
|
||||
|
||||
#if 0
|
||||
// When variadic template arguments are supported
|
||||
template <typename ...Args>
|
||||
void operator () (Args&& ...args) const
|
||||
{
|
||||
m_handler (std::forward <Args> (args)...);
|
||||
}
|
||||
|
||||
#else
|
||||
void operator() ()
|
||||
{
|
||||
m_handler ();
|
||||
}
|
||||
|
||||
void operator() () const
|
||||
{
|
||||
m_handler ();
|
||||
}
|
||||
|
||||
template <class P1>
|
||||
void operator() (P1 const& p1)
|
||||
{
|
||||
m_handler (p1);
|
||||
}
|
||||
|
||||
template <class P1>
|
||||
void operator() (P1 const& p1) const
|
||||
{
|
||||
m_handler (p1);
|
||||
}
|
||||
|
||||
template <class P1, class P2>
|
||||
void operator() (P1 const& p1, P2 const& p2)
|
||||
{
|
||||
m_handler (p1, p2);
|
||||
}
|
||||
|
||||
template <class P1, class P2>
|
||||
void operator() (P1 const& p1, P2 const& p2) const
|
||||
{
|
||||
m_handler (p1, p2);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3)
|
||||
{
|
||||
m_handler (p1, p2, p3);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3) const
|
||||
{
|
||||
m_handler (p1, p2, p3);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4)
|
||||
{
|
||||
m_handler (p1, p2, p3, p4);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4) const
|
||||
{
|
||||
m_handler (p1, p2, p3, p4);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4, class P5>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5)
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4, class P5>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5) const
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4, class P5, class P6>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5, P6 const& p6)
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5, p6);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4, class P5, class P6>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5, P6 const& p6) const
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5, p6);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4,
|
||||
class P5, class P6, class P7>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5, P6 const& p6, P7 const& p7)
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5, p6, p7);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4,
|
||||
class P5, class P6, class P7>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5, P6 const& p6, P7 const& p7) const
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5, p6, p7);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4,
|
||||
class P5, class P6, class P7, class P8>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5, P6 const& p6, P7 const& p7, P8 const& p8)
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5, p6, p7, p8);
|
||||
}
|
||||
|
||||
template <class P1, class P2, class P3, class P4,
|
||||
class P5, class P6, class P7, class P8>
|
||||
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4,
|
||||
P5 const& p5, P6 const& p6, P7 const& p7, P8 const& p8) const
|
||||
{
|
||||
m_handler (p1, p2, p3, p4, p5, p6, p7, p8);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
private:
|
||||
Handler m_handler;
|
||||
Counter& m_counter;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Provides a counted_bind replacement for bind which counts pending I/Os.
|
||||
Derive your class from this class and then call the bind member
|
||||
function instead.
|
||||
*/
|
||||
class enable_counted_bind
|
||||
{
|
||||
public:
|
||||
typedef std::size_t size_type;
|
||||
|
||||
private:
|
||||
typedef std::atomic <size_type> counter_type;
|
||||
|
||||
public:
|
||||
enable_counted_bind ()
|
||||
: m_count (0)
|
||||
{
|
||||
}
|
||||
|
||||
/** Return the number of binds pending completion. */
|
||||
size_type bind_count () const
|
||||
{
|
||||
return m_count.load ();
|
||||
}
|
||||
|
||||
/** Returns a wrapper that calls the handler and manages the counter. */
|
||||
template <class Handler>
|
||||
detail::counted_bind_wrapper <Handler, counter_type> wrap (Handler&& h)
|
||||
{
|
||||
return detail::counted_bind_wrapper <Handler, counter_type> (
|
||||
std::forward <Handler> (h), m_count);
|
||||
}
|
||||
|
||||
private:
|
||||
counter_type m_count;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
176
src/ripple/common/functional/impl/counted_bind.cpp
Normal file
176
src/ripple/common/functional/impl/counted_bind.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
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.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "../counted_bind.h"
|
||||
|
||||
namespace ripple {
|
||||
|
||||
using namespace beast;
|
||||
|
||||
class TrackedHandler
|
||||
{
|
||||
public:
|
||||
explicit TrackedHandler (Journal journal)
|
||||
: m_journal (journal)
|
||||
{
|
||||
m_journal.info << "Constructor";
|
||||
}
|
||||
|
||||
TrackedHandler (TrackedHandler const& h)
|
||||
: m_journal (h.m_journal)
|
||||
{
|
||||
m_journal.info << "Copy Constructor";
|
||||
}
|
||||
|
||||
TrackedHandler (TrackedHandler&& h)
|
||||
: m_journal (std::move (h.m_journal))
|
||||
{
|
||||
m_journal.info << "Move Constructor";
|
||||
}
|
||||
|
||||
// VFALCO NOTE What the heck does this do?
|
||||
TrackedHandler (TrackedHandler const&& h)
|
||||
: m_journal (h.m_journal)
|
||||
{
|
||||
m_journal.info << "Move Constructor (const)";
|
||||
}
|
||||
|
||||
~TrackedHandler ()
|
||||
{
|
||||
m_journal.info << "Destructor";
|
||||
}
|
||||
|
||||
void operator () () const
|
||||
{
|
||||
m_journal.info << "Function call";
|
||||
}
|
||||
|
||||
private:
|
||||
Journal m_journal;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <class Handler>
|
||||
struct HandlerWrapper
|
||||
{
|
||||
#if 1
|
||||
// Universal constructor
|
||||
template <class H>
|
||||
HandlerWrapper (H&& h, std::nullptr_t) // dummy arg
|
||||
: m_handler (std::forward <H> (h))
|
||||
{
|
||||
}
|
||||
|
||||
#else
|
||||
// move construct
|
||||
HandlerWrapper (Handler&& h, nullptr_t)
|
||||
: m_handler (std::move (h))
|
||||
{
|
||||
}
|
||||
|
||||
// copy construct
|
||||
HandlerWrapper (Handler const& h, nullptr_t)
|
||||
: m_handler (h)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
template <typename ...Args>
|
||||
void operator () (Args&& ...args)
|
||||
{
|
||||
m_handler (std::forward <Args> (args)...);
|
||||
}
|
||||
|
||||
#else
|
||||
void operator() () const
|
||||
{
|
||||
m_handler ();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Handler const m_handler;
|
||||
};
|
||||
|
||||
template <class Handler>
|
||||
HandlerWrapper <
|
||||
typename std::remove_reference <Handler>::type> make_handler (Handler&& h)
|
||||
{
|
||||
typedef typename std::remove_reference <Handler>::type handler_type;
|
||||
return HandlerWrapper <handler_type> (
|
||||
std::forward <handler_type> (h), nullptr);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class CountedBindTests : public UnitTest
|
||||
{
|
||||
public:
|
||||
void runTest ()
|
||||
{
|
||||
beginTestCase ("Move");
|
||||
|
||||
Journal const j (journal());
|
||||
|
||||
{
|
||||
j.info << "w1";
|
||||
TrackedHandler h (j);
|
||||
HandlerWrapper <TrackedHandler> w1 (std::move (h), nullptr);
|
||||
w1 ();
|
||||
}
|
||||
|
||||
{
|
||||
j.info << "w2";
|
||||
HandlerWrapper <TrackedHandler> w2 ((TrackedHandler (j)), nullptr);
|
||||
w2 ();
|
||||
}
|
||||
|
||||
{
|
||||
j.info << "w3";
|
||||
TrackedHandler const h (j);
|
||||
HandlerWrapper <TrackedHandler> w3 (h, nullptr);
|
||||
w3 ();
|
||||
}
|
||||
|
||||
{
|
||||
j.info << "w4";
|
||||
auto w4 (make_handler (TrackedHandler (j)));
|
||||
w4 ();
|
||||
}
|
||||
|
||||
{
|
||||
j.info << "w5";
|
||||
TrackedHandler const h (j);
|
||||
auto w5 (make_handler (h));
|
||||
w5 ();
|
||||
}
|
||||
|
||||
pass ();
|
||||
}
|
||||
|
||||
CountedBindTests () : UnitTest ("counted_bind", "ripple", runManual)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
static CountedBindTests countedBindTests;
|
||||
|
||||
}
|
||||
24
src/ripple/common/ripple_common.cpp
Normal file
24
src/ripple/common/ripple_common.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
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.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "BeastConfig.h"
|
||||
|
||||
#include "../../beast/modules/beast_core/beast_core.h" // for UnitTest
|
||||
|
||||
#include "functional/impl/counted_bind.cpp"
|
||||
Reference in New Issue
Block a user