aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/vm/Reader.hpp
blob: e75042f250f823da2066e8ad3c33dcdd095d2027 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once

#include <string_view>

#include <lauf/reader.h>

namespace OpenVic::Vm {
   template<typename Tag>
   struct BasicReader {
      BasicReader(BasicReader&&) = default;
      BasicReader& operator=(BasicReader&&) = default;

      BasicReader(BasicReader const&) = delete;
      BasicReader& operator=(BasicReader const&) = delete;

      ~BasicReader() {
         if (_handle == nullptr) {
            return;
         }
         lauf_destroy_reader(_handle);
      }

      lauf_reader* handle() {
         return _handle;
      }

      const lauf_reader* handle() const {
         return _handle;
      }

      operator lauf_reader*() {
         return _handle;
      }

      operator const lauf_reader*() const {
         return _handle;
      }

      void set_path(const char* path) {
         lauf_reader_set_path(_handle, path);
      }

      struct StringTag;
      struct FileTag;
      struct StdinTag;

   protected:
      BasicReader(lauf_reader* reader) : _handle(reader) {}

   private:
      lauf_reader* _handle;
   };

   template<>
   struct BasicReader<BasicReader<void>::StringTag> : BasicReader<void> {
      using BasicReader<void>::BasicReader;
      BasicReader(const char* cstr) : BasicReader(lauf_create_cstring_reader(cstr)) {}
      BasicReader(std::string_view view) : BasicReader(lauf_create_string_reader(view.data(), view.size())) {}
   };

   template<>
   struct BasicReader<BasicReader<void>::FileTag> : BasicReader<void> {
      using BasicReader<void>::BasicReader;
      BasicReader(const char* path) : BasicReader(lauf_create_file_reader(path)) {}

      bool is_valid() {
         return handle() != nullptr;
      }
   };

   template<>
   struct BasicReader<BasicReader<void>::StdinTag> : BasicReader<void> {
      using BasicReader<void>::BasicReader;
      BasicReader() : BasicReader(lauf_create_stdin_reader()) {}
   };

   using StringReader = BasicReader<BasicReader<void>::StringTag>;
   using FileReader = BasicReader<BasicReader<void>::FileTag>;
   using CinReader = BasicReader<BasicReader<void>::StdinTag>;
}