aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/store-cast.hh
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2022-03-09 15:27:48 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2022-03-11 13:32:33 +0000
commita03b1fd7f60788304f358d5f4dc063c7c9e650a9 (patch)
treee5ec34a44b96ad4b384aca9343033292b26606fb /src/libstore/store-cast.hh
parent678d1c2aa0f499466c723d3461277dc197515f57 (diff)
Deduplicate the Store downcasting with a template
Diffstat (limited to 'src/libstore/store-cast.hh')
-rw-r--r--src/libstore/store-cast.hh16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/libstore/store-cast.hh b/src/libstore/store-cast.hh
new file mode 100644
index 000000000..ff62fc359
--- /dev/null
+++ b/src/libstore/store-cast.hh
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "store-api.hh"
+
+namespace nix {
+
+template<typename T>
+T & require(Store & store)
+{
+ auto * castedStore = dynamic_cast<T *>(&store);
+ if (!castedStore)
+ throw UsageError("%s not supported by store '%s'", T::operationName, store.getUri());
+ return *castedStore;
+}
+
+}