Add ripple_common module

Conflicts:
	Builds/VisualStudio2012/RippleD.vcxproj.filters
This commit is contained in:
Vinnie Falco
2014-01-07 12:29:08 -08:00
parent da3881a486
commit 3c7fc31c95
7 changed files with 481 additions and 2 deletions

View File

@@ -65,6 +65,7 @@ UI_HEADERS_DIR += ../../src/ripple_basics
SOURCES += \
../../src/ripple/beast/ripple_beast.cpp \
../../src/ripple/beast/ripple_beastc.c \
../../src/ripple/common/ripple_common.cpp \
../../src/ripple/http/ripple_http.cpp \
../../src/ripple/json/ripple_json.cpp \
../../src/ripple/peerfinder/ripple_peerfinder.cpp \

View File

@@ -22,6 +22,13 @@
<ClCompile Include="..\..\build\proto\ripple.pb.cc" />
<ClCompile Include="..\..\src\ripple\beast\ripple_beast.cpp" />
<ClCompile Include="..\..\src\ripple\beast\ripple_beastc.c" />
<ClCompile Include="..\..\src\ripple\common\functional\impl\counted_bind.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\common\ripple_common.cpp" />
<ClCompile Include="..\..\src\ripple\http\impl\Port.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
@@ -1532,6 +1539,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple_core\nodestore\backend\RocksDBFactory.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple_core\nodestore\impl\BatchWriter.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
@@ -2128,6 +2141,7 @@
<ClInclude Include="..\..\src\ripple\algorithm\api\CycledSet.h" />
<ClInclude Include="..\..\src\ripple\algorithm\api\DecayingSample.h" />
<ClInclude Include="..\..\src\ripple\algorithm\api\DiscreteClock.h" />
<ClInclude Include="..\..\src\ripple\common\functional\counted_bind.h" />
<ClInclude Include="..\..\src\ripple\http\api\Handler.h" />
<ClInclude Include="..\..\src\ripple\http\api\Server.h" />
<ClInclude Include="..\..\src\ripple\http\api\Port.h" />
@@ -2487,6 +2501,7 @@
<ClInclude Include="..\..\src\ripple_core\nodestore\backend\LevelDBFactory.h" />
<ClInclude Include="..\..\src\ripple_core\nodestore\backend\MemoryFactory.h" />
<ClInclude Include="..\..\src\ripple_core\nodestore\backend\NullFactory.h" />
<ClInclude Include="..\..\src\ripple_core\nodestore\backend\RocksDBFactory.h" />
<ClInclude Include="..\..\src\ripple_core\nodestore\impl\BatchWriter.h" />
<ClInclude Include="..\..\src\ripple_core\nodestore\impl\DatabaseImp.h" />
<ClInclude Include="..\..\src\ripple_core\nodestore\impl\DecodedBlob.h" />

View File

@@ -289,6 +289,15 @@
<Filter Include="[1] Ripple\rocksdb\rocksdb\include\utilities">
<UniqueIdentifier>{013f0eb9-bf1d-4b11-8c2c-3fd99d4b8ef5}</UniqueIdentifier>
</Filter>
<Filter Include="[1] Ripple\common">
<UniqueIdentifier>{b99052d6-a903-4dfd-9c68-ff767a7d8f63}</UniqueIdentifier>
</Filter>
<Filter Include="[1] Ripple\common\functional">
<UniqueIdentifier>{70365f6a-d1e6-45f2-a064-0e842b0fdd78}</UniqueIdentifier>
</Filter>
<Filter Include="[1] Ripple\common\functional\impl">
<UniqueIdentifier>{9b8137bd-737d-4825-98bf-897a0635293a}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\ripple_basics\containers\RangeSet.cpp">
@@ -1401,8 +1410,14 @@
<ClCompile Include="..\..\src\ripple\types\impl\RippleAssets.cpp">
<Filter>[1] Ripple\types\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\types\impl\RippleAssets.h">
<Filter>[1] Ripple\types\impl</Filter>
<ClCompile Include="..\..\src\ripple\common\ripple_common.cpp">
<Filter>[1] Ripple\common</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\common\functional\impl\counted_bind.cpp">
<Filter>[1] Ripple\common\functional\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple_core\nodestore\backend\RocksDBFactory.cpp">
<Filter>[2] Old Ripple\ripple_core\nodestore\backend</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
@@ -2880,6 +2895,12 @@
<ClInclude Include="..\..\src\ripple\types\api\RippleAssets.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\common\functional\counted_bind.h">
<Filter>[1] Ripple\common\functional</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple_core\nodestore\backend\RocksDBFactory.h">
<Filter>[2] Old Ripple\ripple_core\nodestore\backend</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\src\ripple_data\protocol\ripple.proto">

View File

@@ -156,6 +156,7 @@ COMPILED_FILES.extend (['src/ripple/beast/ripple_beastc.c'])
# New-style Ripple unity sources
#
COMPILED_FILES.extend([
'src/ripple/common/ripple_common.cpp',
'src/ripple/http/ripple_http.cpp',
'src/ripple/json/ripple_json.cpp',
'src/ripple/peerfinder/ripple_peerfinder.cpp',

View 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

View 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;
}

View 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"