Spicy
global-state.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <map>
6 #include <memory>
7 #include <optional>
8 #include <string>
9 #include <vector>
10 
11 namespace spicy::rt {
12 struct Parser;
13 } // namespace spicy::rt
14 
15 // We collect all (or most) of the runtime's global state centrally. That's
16 // 1st good to see what we have (global state should be minimal) and 2nd
17 // helpful to ensure that JIT maps things correctly. Note that all code
18 // accessing any of this state is in charge of ensuring thread-safety itself.
19 // These globals are generally initialized through spicy::rt::init();
20 
21 namespace spicy::rt::detail {
22 
24 struct GlobalState {
25  GlobalState() = default;
26  ~GlobalState();
27 
28  GlobalState(const GlobalState&) = delete;
29  GlobalState(GlobalState&&) noexcept = delete;
30  GlobalState& operator=(const GlobalState&) = delete;
31  GlobalState& operator=(GlobalState&&) noexcept = delete;
32 
34  bool runtime_is_initialized = false;
35 
40  std::vector<const Parser*> parsers;
41 
43  std::optional<const Parser*> default_parser;
44 
49  std::map<std::string, std::vector<const Parser*>> parsers_by_name;
50 
52  std::map<std::string, std::vector<const Parser*>> parsers_by_mime_type;
53 };
54 
59 extern GlobalState* __global_state;
60 
62 extern GlobalState* createGlobalState();
63 
68 inline auto globalState() {
69  if ( __global_state )
70  return __global_state;
71 
72  return createGlobalState();
73 }
74 
75 } // namespace spicy::rt::detail
std::map< std::string, std::vector< const Parser * > > parsers_by_mime_type
Definition: global-state.h:52
std::map< std::string, std::vector< const Parser * > > parsers_by_name
Definition: global-state.h:49
Definition: global-state.h:24
std::optional< const Parser * > default_parser
Definition: global-state.h:43
Definition: global-state.h:21
std::vector< const Parser * > parsers
Definition: global-state.h:40
bool runtime_is_initialized
Definition: global-state.h:34