Skip to content

Commit 9125ef9

Browse files
committed
Merge pull request #4398
86fe1b8 update coding.md to reflect changes by pull (Philip Kaufmann) e10dcf2 ensure clean and consistent "namespace" usage (Philip Kaufmann)
2 parents 5bc77b2 + 86fe1b8 commit 9125ef9

File tree

10 files changed

+74
-46
lines changed

10 files changed

+74
-46
lines changed

doc/coding.md

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,65 @@ Coding
44
Please be consistent with the existing coding style.
55

66
Block style:
7-
8-
bool Function(char* psz, int n)
9-
{
10-
// Comment summarising what this section of code does
11-
for (int i = 0; i < n; i++)
12-
{
13-
// When something fails, return early
14-
if (!Something())
15-
return false;
16-
...
17-
}
18-
19-
// Success return is usually at the end
20-
return true;
21-
}
22-
7+
```c++
8+
bool Function(char* psz, int n)
9+
{
10+
// Comment summarising what this section of code does
11+
for (int i = 0; i < n; i++)
12+
{
13+
// When something fails, return early
14+
if (!Something())
15+
return false;
16+
...
17+
}
18+
19+
// Success return is usually at the end
20+
return true;
21+
}
22+
```
2323
- ANSI/Allman block style
2424
- 4 space indenting, no tabs
2525
- No extra spaces inside parenthesis; please don't do ( this )
2626
- No space after function names, one space after if, for and while
27+
- Includes need to be ordered alphabetically, separate own and foreign headers with a new-line (example key.cpp):
28+
```c++
29+
#include "key.h"
30+
31+
#include "crypto/sha2.h"
32+
#include "util.h"
2733
34+
#include <openssl/foo.h>
35+
```
36+
- Class or struct keywords in header files need to be ordered alphabetically:
37+
```c++
38+
class CAlpha;
39+
class CBeta;
40+
```
41+
- When using namespace keyword use the following form:
42+
```c++
43+
namespace Foo {
44+
45+
...
46+
47+
} // Foo
48+
```
2849
Variable names begin with the type in lowercase, like nSomeVariable.
2950
Please don't put the first word of the variable name in lowercase like
3051
someVariable.
3152
3253
Common types:
3354
34-
n integer number: short, unsigned short, int, unsigned int, int64, uint64, sometimes char if used as a number
35-
d double, float
36-
f flag
37-
hash uint256
38-
p pointer or array, one p for each level of indirection
39-
psz pointer to null terminated string
40-
str string object
41-
v vector or similar list objects
42-
map map or multimap
43-
set set or multiset
44-
bn CBigNum
55+
n integer number: short, unsigned short, int, unsigned int, int64, uint64, sometimes char if used as a number
56+
d double, float
57+
f flag
58+
hash uint256
59+
p pointer or array, one p for each level of indirection
60+
psz pointer to null terminated string
61+
str string object
62+
v vector or similar list objects
63+
map map or multimap
64+
set set or multiset
65+
bn CBigNum
4566
4667
Doxygen comments
4768
-----------------

src/base58.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ int CBase58Data::CompareTo(const CBase58Data& b58) const {
186186
}
187187

188188
namespace {
189+
189190
class CBitcoinAddressVisitor : public boost::static_visitor<bool> {
190191
private:
191192
CBitcoinAddress *addr;
@@ -196,7 +197,8 @@ namespace {
196197
bool operator()(const CScriptID &id) const { return addr->Set(id); }
197198
bool operator()(const CNoDestination &no) const { return false; }
198199
};
199-
};
200+
201+
} // anon namespace
200202

201203
bool CBitcoinAddress::Set(const CKeyID &id) {
202204
SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);

src/checkpoints.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <boost/assign/list_of.hpp> // for 'map_list_of()'
1313
#include <boost/foreach.hpp>
1414

15-
namespace Checkpoints
16-
{
15+
namespace Checkpoints {
16+
1717
typedef std::map<int, uint256> MapCheckpoints;
1818

1919
// How many times we expect transactions after the last checkpoint to
@@ -161,4 +161,5 @@ namespace Checkpoints
161161
}
162162
return NULL;
163163
}
164-
}
164+
165+
} // namespace Checkpoints

src/checkpoints.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class uint256;
1313
/** Block-chain checkpoints are compiled-in sanity checks.
1414
* They are updated every release or three.
1515
*/
16-
namespace Checkpoints
17-
{
16+
namespace Checkpoints {
17+
1818
// Returns true if block passes checkpoint checks
1919
bool CheckBlock(int nHeight, const uint256& hash);
2020

@@ -27,6 +27,7 @@ namespace Checkpoints
2727
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks = true);
2828

2929
extern bool fEnabled;
30-
}
30+
31+
} //namespace Checkpoints
3132

3233
#endif

src/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CWallet;
1212

1313
namespace boost {
1414
class thread_group;
15-
};
15+
} // namespace boost
1616

1717
extern CWallet* pwalletMain;
1818

src/key.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,7 @@ const unsigned char vchMaxModHalfOrder[32] = {
377377

378378
const unsigned char vchZero[0] = {};
379379

380-
381-
}; // end of anonymous namespace
380+
} // anon namespace
382381

383382
bool CKey::Check(const unsigned char *vch) {
384383
return CompareBigEndian(vch, 32, vchZero, 0) > 0 &&

src/main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const string strMessageMagic = "Bitcoin Signed Message:\n";
7272

7373
// Internal stuff
7474
namespace {
75+
7576
struct CBlockIndexWorkComparator
7677
{
7778
bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
@@ -120,7 +121,8 @@ namespace {
120121
};
121122
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
122123
map<uint256, pair<NodeId, list<uint256>::iterator> > mapBlocksToDownload;
123-
}
124+
125+
} // anon namespace
124126

125127
//////////////////////////////////////////////////////////////////////////////
126128
//
@@ -130,6 +132,7 @@ namespace {
130132
// These functions dispatch to one or all registered wallets
131133

132134
namespace {
135+
133136
struct CMainSignals {
134137
// Notifies listeners of updated transaction data (transaction, and optionally the block it is found in.
135138
boost::signals2::signal<void (const CTransaction &, const CBlock *)> SyncTransaction;
@@ -144,7 +147,8 @@ struct CMainSignals {
144147
// Tells listeners to broadcast their data.
145148
boost::signals2::signal<void ()> Broadcast;
146149
} g_signals;
147-
}
150+
151+
} // anon namespace
148152

149153
void RegisterWallet(CWalletInterface* pwalletIn) {
150154
g_signals.SyncTransaction.connect(boost::bind(&CWalletInterface::SyncTransaction, pwalletIn, _1, _2));
@@ -274,7 +278,6 @@ void MarkBlockAsReceived(const uint256 &hash, NodeId nodeFrom = -1) {
274278
state->nLastBlockReceive = GetTimeMicros();
275279
mapBlocksInFlight.erase(itInFlight);
276280
}
277-
278281
}
279282

280283
// Requires cs_main.
@@ -310,7 +313,7 @@ void MarkBlockAsInFlight(NodeId nodeid, const uint256 &hash) {
310313
mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
311314
}
312315

313-
}
316+
} // anon namespace
314317

315318
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
316319
LOCK(cs_main);

src/net.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@
2828
#include <boost/signals2/signal.hpp>
2929
#include <openssl/rand.h>
3030

31-
3231
class CAddrMan;
3332
class CBlockIndex;
3433
class CNode;
3534

3635
namespace boost {
3736
class thread_group;
38-
}
37+
} // namespace boost
3938

4039
/** Time between pings automatically sent out for latency probing and keepalive (in seconds). */
4140
static const int PING_INTERVAL = 2 * 60;

src/script.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
974974

975975

976976
namespace {
977+
977978
/** Wrapper that serializes like CTransaction, but with the modifications
978979
* required for the signature hash done in-place
979980
*/
@@ -1066,7 +1067,8 @@ class CTransactionSignatureSerializer {
10661067
::Serialize(s, txTo.nLockTime, nType, nVersion);
10671068
}
10681069
};
1069-
}
1070+
1071+
} // anon namespace
10701072

10711073
uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
10721074
{
@@ -1092,7 +1094,6 @@ uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsig
10921094
return ss.GetHash();
10931095
}
10941096

1095-
10961097
// Valid signature cache, to avoid doing expensive ECDSA signature checking
10971098
// twice for every transaction (once when accepted into memory pool, and
10981099
// again when accepted into the block chain)

src/util.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@
7777
// See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
7878
// http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
7979
namespace boost {
80+
8081
namespace program_options {
8182
std::string to_internal(const std::string&);
8283
}
83-
}
8484

85+
} // namespace boost
8586

8687
using namespace std;
8788

0 commit comments

Comments
 (0)