Skip to content

Commit 10358c6

Browse files
authored
Merge pull request #13139 from NixOS/singleton-pattern
Simplify plugin registrations
2 parents bd80a4f + 060c34b commit 10358c6

File tree

13 files changed

+61
-60
lines changed

13 files changed

+61
-60
lines changed

src/libcmd/command.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
namespace nix {
1616

17-
RegisterCommand::Commands * RegisterCommand::commands = nullptr;
18-
1917
nix::Commands RegisterCommand::getCommandsFor(const std::vector<std::string> & prefix)
2018
{
2119
nix::Commands res;
22-
for (auto & [name, command] : *RegisterCommand::commands)
20+
for (auto & [name, command] : RegisterCommand::commands())
2321
if (name.size() == prefix.size() + 1) {
2422
bool equal = true;
2523
for (size_t i = 0; i < prefix.size(); ++i)

src/libcmd/include/nix/cmd/command.hh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,16 @@ struct StorePathCommand : public StorePathsCommand
285285
struct RegisterCommand
286286
{
287287
typedef std::map<std::vector<std::string>, std::function<ref<Command>()>> Commands;
288-
static Commands * commands;
288+
289+
static Commands & commands()
290+
{
291+
static Commands commands;
292+
return commands;
293+
}
289294

290295
RegisterCommand(std::vector<std::string> && name, std::function<ref<Command>()> command)
291296
{
292-
if (!commands)
293-
commands = new Commands;
294-
commands->emplace(name, command);
297+
commands().emplace(name, command);
295298
}
296299

297300
static nix::Commands getCommandsFor(const std::vector<std::string> & prefix);

src/libcmd/include/nix/cmd/legacy.hh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ typedef std::function<void(int, char * *)> MainFunction;
1212
struct RegisterLegacyCommand
1313
{
1414
typedef std::map<std::string, MainFunction> Commands;
15-
static Commands * commands;
15+
16+
static Commands & commands() {
17+
static Commands commands;
18+
return commands;
19+
}
1620

1721
RegisterLegacyCommand(const std::string & name, MainFunction fun)
1822
{
19-
if (!commands) commands = new Commands;
20-
(*commands)[name] = fun;
23+
commands()[name] = fun;
2124
}
2225
};
2326

src/libcmd/legacy.cc

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/libcmd/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ sources = files(
7171
'installable-flake.cc',
7272
'installable-value.cc',
7373
'installables.cc',
74-
'legacy.cc',
7574
'markdown.cc',
7675
'misc-store-flags.cc',
7776
'network-proxy.cc',

src/libexpr/include/nix/expr/primops.hh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ constexpr size_t conservativeStackReservation = 16;
2727
struct RegisterPrimOp
2828
{
2929
typedef std::vector<PrimOp> PrimOps;
30-
static PrimOps * primOps;
30+
31+
static PrimOps & primOps()
32+
{
33+
static PrimOps primOps;
34+
return primOps;
35+
}
3136

3237
/**
3338
* You can register a constant by passing an arity of 0. fun

src/libexpr/primops.cc

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,13 +4713,9 @@ static RegisterPrimOp primop_splitVersion({
47134713
*************************************************************/
47144714

47154715

4716-
RegisterPrimOp::PrimOps * RegisterPrimOp::primOps;
4717-
4718-
47194716
RegisterPrimOp::RegisterPrimOp(PrimOp && primOp)
47204717
{
4721-
if (!primOps) primOps = new PrimOps;
4722-
primOps->push_back(std::move(primOp));
4718+
primOps().push_back(std::move(primOp));
47234719
}
47244720

47254721

@@ -4973,14 +4969,12 @@ void EvalState::createBaseEnv(const EvalSettings & evalSettings)
49734969
)",
49744970
});
49754971

4976-
if (RegisterPrimOp::primOps)
4977-
for (auto & primOp : *RegisterPrimOp::primOps)
4978-
if (experimentalFeatureSettings.isEnabled(primOp.experimentalFeature))
4979-
{
4980-
auto primOpAdjusted = primOp;
4981-
primOpAdjusted.arity = std::max(primOp.args.size(), primOp.arity);
4982-
addPrimOp(std::move(primOpAdjusted));
4983-
}
4972+
for (auto & primOp : RegisterPrimOp::primOps())
4973+
if (experimentalFeatureSettings.isEnabled(primOp.experimentalFeature)) {
4974+
auto primOpAdjusted = primOp;
4975+
primOpAdjusted.arity = std::max(primOp.args.size(), primOp.arity);
4976+
addPrimOp(std::move(primOpAdjusted));
4977+
}
49844978

49854979
for (auto & primOp : evalSettings.extraPrimOps) {
49864980
auto primOpAdjusted = primOp;

src/libfetchers/fetchers.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@ namespace nix::fetchers {
1212

1313
using InputSchemeMap = std::map<std::string_view, std::shared_ptr<InputScheme>>;
1414

15-
std::unique_ptr<InputSchemeMap> inputSchemes = nullptr;
15+
static InputSchemeMap & inputSchemes()
16+
{
17+
static InputSchemeMap inputSchemeMap;
18+
return inputSchemeMap;
19+
}
1620

1721
void registerInputScheme(std::shared_ptr<InputScheme> && inputScheme)
1822
{
19-
if (!inputSchemes)
20-
inputSchemes = std::make_unique<InputSchemeMap>();
2123
auto schemeName = inputScheme->schemeName();
22-
if (inputSchemes->count(schemeName) > 0)
24+
if (!inputSchemes().emplace(schemeName, std::move(inputScheme)).second)
2325
throw Error("Input scheme with name %s already registered", schemeName);
24-
inputSchemes->insert_or_assign(schemeName, std::move(inputScheme));
2526
}
2627

27-
nlohmann::json dumpRegisterInputSchemeInfo() {
28+
nlohmann::json dumpRegisterInputSchemeInfo()
29+
{
2830
using nlohmann::json;
2931

3032
auto res = json::object();
3133

32-
for (auto & [name, scheme] : *inputSchemes) {
34+
for (auto & [name, scheme] : inputSchemes()) {
3335
auto & r = res[name] = json::object();
3436
r["allowedAttrs"] = scheme->allowedAttrs();
3537
}
@@ -57,7 +59,7 @@ Input Input::fromURL(
5759
const Settings & settings,
5860
const ParsedURL & url, bool requireTree)
5961
{
60-
for (auto & [_, inputScheme] : *inputSchemes) {
62+
for (auto & [_, inputScheme] : inputSchemes()) {
6163
auto res = inputScheme->inputFromURL(settings, url, requireTree);
6264
if (res) {
6365
experimentalFeatureSettings.require(inputScheme->experimentalFeature());
@@ -91,8 +93,8 @@ Input Input::fromAttrs(const Settings & settings, Attrs && attrs)
9193
};
9294

9395
std::shared_ptr<InputScheme> inputScheme = ({
94-
auto i = inputSchemes->find(schemeName);
95-
i == inputSchemes->end() ? nullptr : i->second;
96+
auto i = get(inputSchemes(), schemeName);
97+
i ? *i : nullptr;
9698
});
9799

98100
if (!inputScheme) return raw();

src/libstore/include/nix/store/store-api.hh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -902,12 +902,11 @@ struct StoreFactory
902902

903903
struct Implementations
904904
{
905-
static std::vector<StoreFactory> * registered;
905+
static std::vector<StoreFactory> & registered();
906906

907907
template<typename T, typename TConfig>
908908
static void add()
909909
{
910-
if (!registered) registered = new std::vector<StoreFactory>();
911910
StoreFactory factory{
912911
.uriSchemes = TConfig::uriSchemes(),
913912
.create =
@@ -919,7 +918,7 @@ struct Implementations
919918
-> std::shared_ptr<StoreConfig>
920919
{ return std::make_shared<TConfig>(StringMap({})); })
921920
};
922-
registered->push_back(factory);
921+
registered().push_back(factory);
923922
}
924923
};
925924

src/libstore/store-api.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ ref<Store> openStore(StoreReference && storeURI)
13551355
return std::make_shared<LocalStore>(params);
13561356
},
13571357
[&](const StoreReference::Specified & g) {
1358-
for (const auto & implem : *Implementations::registered)
1358+
for (const auto & implem : Implementations::registered())
13591359
if (implem.uriSchemes.count(g.scheme))
13601360
return implem.create(g.scheme, g.authority, params);
13611361

@@ -1399,6 +1399,10 @@ std::list<ref<Store>> getDefaultSubstituters()
13991399
return stores;
14001400
}
14011401

1402-
std::vector<StoreFactory> * Implementations::registered = 0;
1402+
std::vector<StoreFactory> & Implementations::registered()
1403+
{
1404+
static std::vector<StoreFactory> registered;
1405+
return registered;
1406+
}
14031407

14041408
}

0 commit comments

Comments
 (0)