Fix the registration of stores

This commit is contained in:
regnat 2020-09-09 11:18:12 +02:00
parent 7d5bdf8b56
commit fa32560169
9 changed files with 27 additions and 19 deletions

View file

@ -50,6 +50,6 @@ struct DummyStore : public Store
{ unsupported("buildDerivation"); } { unsupported("buildDerivation"); }
}; };
[[maybe_unused]] static RegisterStoreImplementation<DummyStore> regStore(); static RegisterStoreImplementation<DummyStore> regStore;
} }

View file

@ -170,6 +170,6 @@ protected:
}; };
[[maybe_unused]] static RegisterStoreImplementation<HttpBinaryCacheStore> regStore(); static RegisterStoreImplementation<HttpBinaryCacheStore> regStore;
} }

View file

@ -326,6 +326,6 @@ public:
} }
}; };
[[maybe_unused]] static RegisterStoreImplementation<LegacySSHStore> regStore(); static RegisterStoreImplementation<LegacySSHStore> regStore;
} }

View file

@ -96,6 +96,6 @@ std::vector<std::string> LocalBinaryCacheStore::uriPrefixes()
return {"file"}; return {"file"};
} }
[[maybe_unused]] static RegisterStoreImplementation<LocalBinaryCacheStore> regStore(); static RegisterStoreImplementation<LocalBinaryCacheStore> regStore;
} }

View file

@ -982,6 +982,6 @@ std::exception_ptr RemoteStore::Connection::processStderr(Sink * sink, Source *
return nullptr; return nullptr;
} }
[[maybe_unused]] static RegisterStoreImplementation<UDSRemoteStore> regStore(); static RegisterStoreImplementation<UDSRemoteStore> regStore;
} }

View file

@ -431,7 +431,7 @@ struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
}; };
[[maybe_unused]] static RegisterStoreImplementation<S3BinaryCacheStoreImpl> regStore(); static RegisterStoreImplementation<S3BinaryCacheStoreImpl> regStore;
} }

View file

@ -76,6 +76,6 @@ ref<RemoteStore::Connection> SSHStore::openConnection()
return conn; return conn;
} }
[[maybe_unused]] static RegisterStoreImplementation<SSHStore> regStore(); static RegisterStoreImplementation<SSHStore> regStore;
} }

View file

@ -1093,7 +1093,7 @@ ref<Store> openStore(const std::string & uri_,
return ref<Store>(store); return ref<Store>(store);
} }
for (auto implem : *implementations) { for (auto implem : *Implementations::registered) {
auto store = implem.open(uri, params); auto store = implem.open(uri, params);
if (store) { if (store) {
store->warnUnknownSettings(); store->warnUnknownSettings();
@ -1136,5 +1136,6 @@ std::list<ref<Store>> getDefaultSubstituters()
return stores; return stores;
} }
std::vector<StoreFactory> * Implementations::registered = 0;
} }

View file

@ -749,25 +749,32 @@ std::list<ref<Store>> getDefaultSubstituters();
struct StoreFactory struct StoreFactory
{ {
std::vector<std::string> uriPrefixes;
std::function<std::shared_ptr<Store> (const std::string & uri, const Store::Params & params)> open; std::function<std::shared_ptr<Store> (const std::string & uri, const Store::Params & params)> open;
}; };
typedef std::vector<StoreFactory> Implementations; struct Implementations
static Implementations * implementations = new Implementations; {
static std::vector<StoreFactory> * registered;
template<typename T>
static void add()
{
if (!registered) registered = new std::vector<StoreFactory>();
StoreFactory factory{
.open =
([](const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store>
{ return std::make_shared<T>(uri, params); }),
};
registered->push_back(factory);
}
};
template<typename T> template<typename T>
struct RegisterStoreImplementation struct RegisterStoreImplementation
{ {
RegisterStoreImplementation() RegisterStoreImplementation()
{ {
StoreFactory factory{ Implementations::add<T>();
.uriPrefixes = T::uriPrefixes(),
.open =
([](const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store>
{ return std::make_shared<T>(uri, params); })
};
implementations->push_back(factory);
} }
}; };