Skip to content

Conversation

jtimon
Copy link
Contributor

@jtimon jtimon commented Jan 9, 2018

There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
This starts as a +10-43 diff

@@ -24,9 +26,12 @@ class CBaseChainParams
const std::string& DataDir() const { return strDataDir; }
int RPCPort() const { return nRPCPort; }

protected:
CBaseChainParams() {}
CBaseChainParams(const std::string& strDataDir, int nRPCPort) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codestyle: just use data_dir and rpc_port.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then nRPCPort(rpc_port), strDataDir(data_dir) {} constructor initializer list.

Copy link
Contributor

@jimpo jimpo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK.


protected:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that subclasses are removed, these can be private I think.

@@ -15,6 +15,8 @@
*/
class CBaseChainParams
{
/** Default constructor is private to force the use of the parametrized one */
CBaseChainParams() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use C++11 deleted function syntax? CBaseChainParams() = delete.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -73,11 +35,11 @@ const CBaseChainParams& BaseParams()
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain)
{
if (chain == CBaseChainParams::MAIN)
return std::unique_ptr<CBaseChainParams>(new CBaseMainParams());
return std::unique_ptr<CBaseChainParams>(new CBaseChainParams("", 8332));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could use MakeUnique<CBaseChainParams>("", 8332)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, but it seems it's a C++14 feature, not C++11. :(

Copy link
Contributor

@ryanofsky ryanofsky Jan 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++14 provides make_unique, but MakeUnique is just a function in util:

bitcoin/src/util.h

Lines 336 to 338 in 0910cbe

//! Substitute for C++14 std::make_unique.
template <typename T, typename... Args>
std::unique_ptr<T> MakeUnique(Args&&... args)

@jtimon jtimon force-pushed the b16-baseparams-nohierarchy branch from a26c919 to 9b69179 Compare January 11, 2018 20:09
@jtimon
Copy link
Contributor Author

jtimon commented Jan 11, 2018

Fixed nits

@promag
Copy link
Contributor

promag commented Jan 11, 2018

utACK after #12128 (comment) is addressed.

@jtimon jtimon force-pushed the b16-baseparams-nohierarchy branch from 9b69179 to bbd0d1c Compare January 11, 2018 22:19
@jtimon
Copy link
Contributor Author

jtimon commented Jan 11, 2018

Fixed last nit (using util function MakeUnique).

@jtimon jtimon force-pushed the b16-baseparams-nohierarchy branch from bbd0d1c to 277b4a9 Compare January 12, 2018 23:11
Copy link
Contributor

@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 277b4a994c83f131024ba8e24e3dc247078a0c82 but I agree with @jimpo's nit that the default constructor should just be deleted. (Could delete the copy constructors too, but that doesn't achieve a lot)

int nRPCPort;
std::string strDataDir;

/** Default constructor is private to force the use of the parametrized one */
CBaseChainParams() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a non-default constructor already prevents automatic declaration of the default constructor; but "CBaseChainParams() = delete;" seems clearer still.

Also, since nRPCPort and strDataDir are set via initializer list, and aren't fiddled with later (they're private and all the member functions are marked const), they could be marked "const" too.

Tested with these changes:

+    const int nRPCPort;
+    const std::string strDataDir; 

+    CBaseChainParams() = delete;
+    CBaseChainParams(const CBaseChainParams&) = delete;
+    CBaseChainParams& operator=(const CBaseChainParams&) = delete;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a non-default constructor already prevents automatic declaration of the default constructor

Does it? I really think that's not the case, do you have any reference?

but "CBaseChainParams() = delete;" seems clearer still.

Perhaps. I've never seen this contruct but if people prefer this I'm happy to change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(= delete is new in C++11)

Reference: http://en.cppreference.com/w/cpp/language/default_constructor

If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.

[[c++11:]] If some user-declared constructors are present, the user may still force the automatic generation of a default constructor by the compiler that would be implicitly-declared otherwise with the keyword default.

Example:

class X { public: int a; char b; X(int _a, char _b) : a(_a), b(_b) { } };
X x;

gcc:

test.cc:2:3: error: no matching function for call to ‘X::X()’
 X x;
   ^
test.cc:1:34: note: candidate: X::X(int, char)
test.cc:1:34: note:   candidate expects 2 arguments, 0 provided
test.cc:1:7: note: candidate: constexpr X::X(const X&)
test.cc:1:7: note:   candidate expects 1 argument, 0 provided
test.cc:1:7: note: candidate: constexpr X::X(X&&)
test.cc:1:7: note:   candidate expects 1 argument, 0 provided

clang:

test.cc:2:3: error: no matching constructor for initialization of 'X'
X x;
  ^
test.cc:1:7: note: candidate constructor (the implicit copy constructor) not
      viable: requires 1 argument, but 0 were provided
test.cc:1:7: note: candidate constructor (the implicit move constructor) not
      viable: requires 1 argument, but 0 were provided
test.cc:1:34: note: candidate constructor not viable: requires 2 arguments,
      but 0 were provided
class X { public: int a; char b; X(int _a, char _b) : a(_a), b(_b) { } };
                                 ^

Copy link
Contributor Author

@jtimon jtimon Feb 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about this, I see how your interest in also prohibiting the copy constructor and the override of the assign operator may make sense in many cases, but I think in this case we're just fine prohibiting only the default contructor. Anything copied from an instance initialized with the parametrized contructor should be fine.
But thank you for teaching me newer c++ features, I love to be able to ditch that hackish design pattern.
I will

CBaseChainParams() = delete;

Actually the fact that an upgrade of the language supports what I was "simulating" gives me much more confidence on that part. Thank you again for stopping me from reinventing the wheel.

@jtimon jtimon force-pushed the b16-baseparams-nohierarchy branch from 277b4a9 to 1687cb4 Compare February 8, 2018 21:07
@jtimon
Copy link
Contributor Author

jtimon commented Feb 8, 2018

Fixed nit (moved to deleted constructor).

@maflcko
Copy link
Member

maflcko commented Feb 8, 2018

utACK 1687cb4

@jimpo
Copy link
Contributor

jimpo commented Feb 8, 2018

utACK 1687cb4

@promag
Copy link
Contributor

promag commented Feb 9, 2018

utACK 1687cb4. Nice change.

@ajtowns
Copy link
Contributor

ajtowns commented Feb 9, 2018

utACK 1687cb4

@laanwj laanwj merged commit 1687cb4 into bitcoin:master Feb 10, 2018
laanwj added a commit that referenced this pull request Feb 10, 2018
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón)

Pull request description:

  There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  This starts as a +10-43 diff

Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d
@jtimon jtimon deleted the b16-baseparams-nohierarchy branch February 11, 2018 20:15
Mengerian pushed a commit to Mengerian/bitcoin-abc that referenced this pull request Aug 6, 2019
Summary:
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón)

Pull request description:

  There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  This starts as a +10-43 diff

Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d

Backport of Core PR12128
bitcoin/bitcoin#12128

Test Plan:
  make check

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3804
jonspock pushed a commit to jonspock/devault that referenced this pull request Sep 8, 2019
Summary:
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón)

Pull request description:

  There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  This starts as a +10-43 diff

Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d

Backport of Core PR12128
bitcoin/bitcoin#12128

Test Plan:
  make check

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3804
jonspock pushed a commit to jonspock/devault that referenced this pull request Sep 9, 2019
Summary:
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón)

Pull request description:

  There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  This starts as a +10-43 diff

Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d

Backport of Core PR12128
bitcoin/bitcoin#12128

Test Plan:
  make check

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3804
proteanx pushed a commit to devaultcrypto/devault that referenced this pull request Sep 10, 2019
Summary:
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón)

Pull request description:

  There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  This starts as a +10-43 diff

Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d

Backport of Core PR12128
bitcoin/bitcoin#12128

Test Plan:
  make check

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3804
xdustinface pushed a commit to xdustinface/dash that referenced this pull request Dec 10, 2020
1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón)

Pull request description:

  There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  This starts as a +10-43 diff

Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d
xdustinface added a commit to dashpay/dash that referenced this pull request Dec 10, 2020
…ugh (#3865)

1687cb4 Refactor: One CBaseChainParams should be enough (Jorge Timón)

Pull request description:

  There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  This starts as a +10-43 diff

Tree-SHA512: 0a7dd64ab785416550b541787c6083540e4962d76b6cffa806bb3593aec2daf1752dfe65ac5cd51b34ad5c31dd8292c422b483fdd2d37d0b7e68725498ed4c2d

Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
furszy added a commit to PIVX-Project/PIVX that referenced this pull request May 26, 2021
a58be04 Use a unique RPC port for regtest (Fuzzbawls)
d9f1710 One CBaseChainParams should be enough (Fuzzbawls)

Pull request description:

  Coming from bitcoin#12128

  > There's no need for class hierarchy with CBaseChainParams, it is just a struct with 2 fields.
  > This starts as a +10-43 diff

  Since we already require c++14, can use `std::make_unique` directly.

  Note: I did not change the regtest RPC port here, but maybe this is the perfect opportunity to do so if we want it to not conflict with testnet's RPC port

ACKs for top commit:
  random-zebra:
    utACK a58be04
  furszy:
    utACK a58be04

Tree-SHA512: 2c4d70931e7228cec35113ed28732410efc6f0daca75eef7abc2f4337919fca170407635044ca4096a5af5a802280f414f5422b6d2607fbeca1e62f78386107d
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants