Loading...
Searching...
No Matches
factory.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * Factory for volumetric field writers.
5 *
6 * Writers are registered by format name. Core registers "cube" (Gaussian
7 * cube, the default) and "xsf" (XCrySDen); a build-time add-on may register
8 * more from its registration function, which every entry point runs before it
9 * reads input (io/addon_registration.hpp).
10 */
11
12#include <memory>
13#include <stdexcept>
14#include <string>
15#include <vector>
16
19#include "io/volumetric/xsf.hpp"
21#include "utils/registry.hpp"
22
23namespace q3 {
24
25/// The registry create_volumetric_field_writer() builds writers from.
29 seeded.add(std::make_unique<NamedFactory<VolumetricFieldWriter>>(
30 std::vector<std::string>{"cube"},
31 [] { return std::make_unique<CubeVolumetricFieldWriter>(); }));
32 seeded.add(std::make_unique<NamedFactory<VolumetricFieldWriter>>(
33 std::vector<std::string>{"xsf"},
34 [] { return std::make_unique<XsfVolumetricFieldWriter>(); }));
35 return seeded;
36 }();
37 return registry;
38}
39
40inline std::unique_ptr<VolumetricFieldWriter>
41create_volumetric_field_writer(const std::string& format) {
42 const auto& registry = volumetric_field_writer_registry();
43 if (const auto* factory = registry.find(format))
44 return factory->create();
45 throw std::runtime_error("Unknown volumetric format '" + format +
46 "'. Supported: " + registry.canonical_names(", "));
47}
48
49} // namespace q3
A registry entry that builds one implementation of Product.
An ordered set of interchangeable implementations of Entry, found by name.
Definition registry.hpp:33
void add(std::unique_ptr< Entry > entry)
Add entry; throws std::logic_error when one of its names is already registered.
Definition registry.hpp:36
std::unique_ptr< VolumetricFieldWriter > create_volumetric_field_writer(const std::string &format)
Definition factory.hpp:41
Registry< NamedFactory< VolumetricFieldWriter > > & volumetric_field_writer_registry()
The registry create_volumetric_field_writer() builds writers from.
Definition factory.hpp:26