aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/scripts/Script.hpp
blob: 8efc27729be756808687a5e66a89cc667986cc1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once

#include "openvic-simulation/dataloader/NodeTools.hpp"

namespace OpenVic {
   template<typename... _Context>
   struct Script {
   private:
      ast::NodeCPtr _root;

   protected:
      virtual bool _parse_script(ast::NodeCPtr root, _Context... context) = 0;

   public:
      Script() : _root { nullptr } {}
      Script(Script&&) = default;

      constexpr bool has_defines_node() const {
         return _root != nullptr;
      }

      constexpr NodeTools::NodeCallback auto expect_script() {
         return NodeTools::assign_variable_callback(_root);
      }

      bool parse_script(bool can_be_null, _Context... context) {
         if (_root == nullptr) {
            if (!can_be_null) {
               Logger::error("Null/missing script node!");
            }
            return can_be_null;
         }
         const bool ret = _parse_script(_root, context...);
         _root = nullptr;
         return ret;
      }
   };
}