Skip to content

Commit 1f47262

Browse files
committed
Rename to_shared() to make_shared(), add make_shared(vargs...).
1 parent 80b07e9 commit 1f47262

9 files changed

Lines changed: 61 additions & 43 deletions

File tree

include/bitcoin/system/data/memory.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,33 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
3030
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
3131
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
3232

33-
/// shared_ptr
33+
/// make_shared (non-const)
3434
/// ---------------------------------------------------------------------------
3535

36+
/// Create default shared pointer.
37+
template <typename Type>
38+
inline std::shared_ptr<Type> make_shared() NOEXCEPT
39+
{
40+
return std::make_shared<Type>();
41+
}
42+
3643
/// Create shared pointer to non-const from moved instance.
3744
template <typename Type>
3845
inline std::shared_ptr<Type> make_shared(Type&& value) NOEXCEPT
3946
{
4047
return std::make_shared<Type>(std::forward<Type>(value));
4148
}
4249

43-
/// Create default shared pointer.
44-
template <typename Type>
45-
inline std::shared_ptr<Type> to_shared() NOEXCEPT
50+
/// Create shared pointer to non-const from vargs instance.
51+
template <typename Type, typename... Args>
52+
inline std::shared_ptr<Type> make_shared(Args&& ...args) NOEXCEPT
4653
{
47-
return std::make_shared<Type>();
54+
return std::make_shared<Type>(std::forward<Args>(args)...);
4855
}
4956

57+
/// to_shared (const)
58+
/// ---------------------------------------------------------------------------
59+
5060
/// Create shared pointer to const from instance pointer.
5161
template <typename Type>
5262
inline std::shared_ptr<const Type> to_shared(Type* value) NOEXCEPT

include/bitcoin/system/impl/hash/scrypt.ipp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ block_mix(rblock_t& rblock) NOEXCEPT
329329
// Make 2R working blocks (1 rblock).
330330
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
331331
// [P * (R * 128)] bytes heap allocated.
332-
const auto ptr = to_shared<rblock_t>();
332+
const auto ptr = make_shared<rblock_t>();
333333
if (!ptr) return false;
334334
auto& yrblock = *ptr;
335335
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -356,7 +356,7 @@ block_mix(rblock_t& rblock) NOEXCEPT
356356
// Make 2R working blocks (1 rblock).
357357
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
358358
// [P * (R * 128)] bytes heap allocated.
359-
const auto ptr = to_shared<rblock_t>();
359+
const auto ptr = make_shared<rblock_t>();
360360
if (!ptr) return false;
361361
auto& yrblock = *ptr;
362362
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -375,7 +375,7 @@ block_mix(rblock_t& rblock) NOEXCEPT
375375
// Make R working blocks (half rblock).
376376
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
377377
// [P * (R * 64)] bytes heap allocated.
378-
const auto ptr = to_shared<std_array<block_t, R>>();
378+
const auto ptr = make_shared<std_array<block_t, R>>();
379379
if (!ptr) return false;
380380
auto& yblock = *ptr;
381381
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -394,7 +394,7 @@ block_mix(rblock_t& rblock) NOEXCEPT
394394
// Make R-1 working blocks.
395395
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396396
// [P * (sub1(R) * 64)] bytes heap allocated.
397-
const auto ptr = to_shared<std_array<block_t, sub1(R)>>();
397+
const auto ptr = make_shared<std_array<block_t, sub1(R)>>();
398398
if (!ptr) return false;
399399
auto& yblock = *ptr;
400400
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -436,7 +436,7 @@ romix(rblock_t& rblock) NOEXCEPT
436436
// Make a working set of W rblocks.
437437
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
438438
// [P * (W * (R * 128))] bytes heap allocated.
439-
const auto ptr = to_shared<wrblock_t>();
439+
const auto ptr = make_shared<wrblock_t>();
440440
if (!ptr) return false;
441441
auto& wrblocks = *ptr;
442442
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -479,7 +479,7 @@ CLASS::hash(data_array<Size>& out, const data_slice& password,
479479
// Make a working set of P rblocks.
480480
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
481481
// [P * (R * 128)] bytes heap allocated.
482-
const auto ptr = to_shared<prblock_t>();
482+
const auto ptr = make_shared<prblock_t>();
483483
if (!ptr) return false;
484484
auto& prblocks = *ptr;
485485
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

src/chain/block.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ BC_PUSH_WARNING(NO_UNGUARDED_POINTERS)
4848
// ----------------------------------------------------------------------------
4949

5050
block::block() NOEXCEPT
51-
: block(to_shared<chain::header>(), to_shared<transaction_cptrs>(),
51+
: block(make_shared<chain::header>(), make_shared<transaction_cptrs>(),
5252
false)
5353
{
5454
}
@@ -66,8 +66,8 @@ block::block(const chain::header& header,
6666

6767
block::block(const chain::header::cptr& header,
6868
const transactions_cptr& txs) NOEXCEPT
69-
: block(header ? header : to_shared<chain::header>(),
70-
txs ? txs : to_shared<transaction_cptrs>(), true)
69+
: block(header ? header : make_shared<chain::header>(),
70+
txs ? txs : make_shared<transaction_cptrs>(), true)
7171
{
7272
}
7373

@@ -208,7 +208,7 @@ const chain::header::cptr block::header_ptr() const NOEXCEPT
208208
// Roll up inputs for concurrent prevout processing.
209209
const inputs_cptr block::inputs_ptr() const NOEXCEPT
210210
{
211-
const auto inputs = to_shared<input_cptrs>();
211+
const auto inputs = make_shared<input_cptrs>();
212212
const auto append_ins = [&inputs](const auto& tx) NOEXCEPT
213213
{
214214
const auto& tx_ins = tx->inputs_ptr();

src/chain/input.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const witness& input::no_witness() NOEXCEPT
5555
// static/private
5656
const witness::cptr& input::no_witness_cptr() NOEXCEPT
5757
{
58-
static const auto empty = to_shared<const chain::witness>();
58+
static const auto empty = make_shared<const chain::witness>();
5959
return empty;
6060
}
6161

@@ -76,9 +76,9 @@ const chain::witness::cptr& input::get_witness_cptr() const NOEXCEPT
7676
// Default metadata is spent, invalid, max_size_t value.
7777
input::input() NOEXCEPT
7878
: input(
79-
to_shared<chain::point>(),
80-
to_shared<chain::script>(),
81-
to_shared<chain::witness>(),
79+
make_shared<chain::point>(),
80+
make_shared<chain::script>(),
81+
make_shared<chain::witness>(),
8282
0, false)
8383
{
8484
}
@@ -88,7 +88,7 @@ input::input(chain::point&& point, chain::script&& script,
8888
: input(
8989
to_shared(std::move(point)),
9090
to_shared(std::move(script)),
91-
to_shared<chain::witness>(),
91+
make_shared<chain::witness>(),
9292
sequence, true)
9393
{
9494
}
@@ -98,17 +98,17 @@ input::input(const chain::point& point, const chain::script& script,
9898
: input(
9999
to_shared(point),
100100
to_shared(script),
101-
to_shared<chain::witness>(),
101+
make_shared<chain::witness>(),
102102
sequence, true)
103103
{
104104
}
105105

106106
input::input(const chain::point::cptr& point,
107107
const chain::script::cptr& script, uint32_t sequence) NOEXCEPT
108108
: input(
109-
point ? point : to_shared<chain::point>(),
110-
script ? script : to_shared<chain::script>(),
111-
to_shared<chain::witness>(),
109+
point ? point : make_shared<chain::point>(),
110+
script ? script : make_shared<chain::script>(),
111+
make_shared<chain::witness>(),
112112
sequence, true)
113113
{
114114
}

src/chain/operation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const data_chunk& operation::no_data() NOEXCEPT
6868
// static/private
6969
const chunk_cptr& operation::no_data_cptr() NOEXCEPT
7070
{
71-
static const auto empty = to_shared<const data_chunk>();
71+
static const auto empty = make_shared<const data_chunk>();
7272
return empty;
7373
}
7474

src/chain/output.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const uint64_t output::not_found = sighash_null_value;
4848
// Invalid default used in signature hashing (validity ignored).
4949
// Invalidity is also used to determine that a prevout is not found.
5050
output::output() NOEXCEPT
51-
: output(output::not_found, to_shared<chain::script>(), false)
51+
: output(output::not_found, make_shared<chain::script>(), false)
5252
{
5353
}
5454

@@ -63,7 +63,7 @@ output::output(uint64_t value, const chain::script& script) NOEXCEPT
6363
}
6464

6565
output::output(uint64_t value, const chain::script::cptr& script) NOEXCEPT
66-
: output(value, script ? script : to_shared<chain::script>(), true)
66+
: output(value, script ? script : make_shared<chain::script>(), true)
6767
{
6868
}
6969

src/chain/script_extract.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace chain {
3535

3636
const chunk_cptr& script::witness_program() const NOEXCEPT
3737
{
38-
static const auto empty = to_shared<const data_chunk>();
38+
static const auto empty = make_shared<const data_chunk>();
3939
return is_witness_program_pattern(ops()) ? ops().at(1).data_ptr() : empty;
4040
}
4141

src/chain/transaction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
4848

4949
transaction::transaction() NOEXCEPT
5050
: transaction(0,
51-
to_shared<input_cptrs>(),
52-
to_shared<output_cptrs>(),
51+
make_shared<input_cptrs>(),
52+
make_shared<output_cptrs>(),
5353
0, false, false)
5454
{
5555
}
@@ -112,8 +112,8 @@ transaction::transaction(uint32_t version,
112112
const chain::inputs_cptr& inputs, const chain::outputs_cptr& outputs,
113113
uint32_t locktime, bool segregated, bool valid) NOEXCEPT
114114
: version_(version),
115-
inputs_(inputs ? inputs : to_shared<input_cptrs>()),
116-
outputs_(outputs ? outputs : to_shared<output_cptrs>()),
115+
inputs_(inputs ? inputs : make_shared<input_cptrs>()),
116+
outputs_(outputs ? outputs : make_shared<output_cptrs>()),
117117
locktime_(locktime),
118118
segregated_(segregated),
119119
valid_(valid),

test/data/memory.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,25 @@ struct type
3535

3636
using test_array_shared_ptr = std::shared_ptr<data_array<3>>;
3737

38+
BOOST_AUTO_TEST_CASE(memory__make_shared__array_default__default_values)
39+
{
40+
const test_array_shared_ptr ptr = make_shared<data_array<3>>();
41+
BOOST_REQUIRE_EQUAL(ptr->at(0), 0);
42+
BOOST_REQUIRE_EQUAL(ptr->at(1), 0);
43+
BOOST_REQUIRE_EQUAL(ptr->at(2), 0);
44+
}
45+
3846
BOOST_AUTO_TEST_CASE(memory__make_shared__array_move__expected_values)
3947
{
40-
const test_array_shared_ptr ptr = make_shared<data_array<3>>(data_array<3>{ 1, 2, 3 });
48+
const test_array_shared_ptr ptr = make_shared(data_array<3>{ 1, 2, 3 });
49+
BOOST_REQUIRE_EQUAL(ptr->at(0), 1);
50+
BOOST_REQUIRE_EQUAL(ptr->at(1), 2);
51+
BOOST_REQUIRE_EQUAL(ptr->at(2), 3);
52+
}
53+
54+
BOOST_AUTO_TEST_CASE(memory__make_shared__array_vargs__expected_values)
55+
{
56+
const test_array_shared_ptr ptr = make_shared<data_array<3>>({ 1, 2, 3 });
4157
BOOST_REQUIRE_EQUAL(ptr->at(0), 1);
4258
BOOST_REQUIRE_EQUAL(ptr->at(1), 2);
4359
BOOST_REQUIRE_EQUAL(ptr->at(2), 3);
@@ -47,14 +63,6 @@ BOOST_AUTO_TEST_CASE(memory__make_shared__array_move__expected_values)
4763

4864
using test_const_array_shared_ptr = std::shared_ptr<const data_array<3>>;
4965

50-
BOOST_AUTO_TEST_CASE(memory__to_shared1__array_default__default_values)
51-
{
52-
const test_const_array_shared_ptr ptr = to_shared<data_array<3>>();
53-
BOOST_REQUIRE_EQUAL(ptr->at(0), 0);
54-
BOOST_REQUIRE_EQUAL(ptr->at(1), 0);
55-
BOOST_REQUIRE_EQUAL(ptr->at(2), 0);
56-
}
57-
5866
BOOST_AUTO_TEST_CASE(memory__to_shared5__array_copy__expected_values)
5967
{
6068
const data_array<3> copy{ 1, 2, 3 };
@@ -74,9 +82,9 @@ BOOST_AUTO_TEST_CASE(memory__to_shared5__array_move__expected_values)
7482

7583
using test_shared_ptr = std::shared_ptr<const type>;
7684

77-
BOOST_AUTO_TEST_CASE(memory__to_shared1__always__default)
85+
BOOST_AUTO_TEST_CASE(memory__make_shared__always__default)
7886
{
79-
const test_shared_ptr ptr = to_shared<type>();
87+
const test_shared_ptr ptr = make_shared<type>();
8088
BOOST_REQUIRE_EQUAL(ptr->left, 0);
8189
BOOST_REQUIRE_EQUAL(ptr->right, type::expected);
8290
}

0 commit comments

Comments
 (0)