VIPRA Documentation
Loading...
Searching...
No Matches
input.hpp
1#pragma once
2
3#include <cassert>
4#include <optional>
5#include <stdexcept>
6#include <vector>
7
8#include "vipra/concepts/string_view.hpp"
9
10#include "vipra/modules/module.hpp"
11
12#include "vipra/util/crtp.hpp"
13
14// TODO(rolland): remember to add in documentation that inputs should hold off on loading until their load() method is called
15
16namespace VIPRA::Modules {
17
18template <typename module_t>
19class Input : public Util::CRTP<Input<module_t>> {
20 using Util::CRTP<Input<module_t>>::self;
21
22 public:
23 void load()
24 {
25 // TODO(rolland) maybe have load_impl return a bool and handle if it failed?
26 if ( _loaded ) return;
27 _loaded = true;
28 self().load_impl();
29 }
30
39 template <typename data_t, Concepts::StringView... keys_t>
40 auto get(keys_t&&... keys) const -> std::optional<data_t>
41 {
42 assert(_loaded);
43
44 try {
45 return self().template get_impl<data_t>(
46 std::string_view(std::forward<keys_t>(keys))...);
47 }
48 catch ( std::exception& ex ) {
49 // TODO(rolland): if the input module has an issue should we just let it throw or just return nullopt like this?
50 // If the module has an issue getting the value return nullopt
51 return std::nullopt;
52 }
53 }
54
55 void set_loaded(bool loaded) { _loaded = loaded; }
56
57 private:
58 bool _loaded = false;
59};
60
61} // namespace VIPRA::Modules
Definition input.hpp:19
auto get(keys_t &&... keys) const -> std::optional< data_t >
Uses the Input implementation to get a value at {keys}.
Definition input.hpp:40
Definition crtp.hpp:6
Definition string_view.hpp:9