aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/csv/ValueNode.cpp
blob: 84c375c9df53755a3c736187ac95f543ff1a3c49 (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
#include <openvic-dataloader/csv/ValueNode.hpp>

using namespace ovdl;
using namespace ovdl::csv;

ValueNode::ValueNode() = default;

ValueNode::ValueNode(std::string_view string, position_type position)
   : ValueNode(std::initializer_list<internal_value_type> { std::string(string) }, position) {
}
ValueNode::ValueNode(std::vector<internal_value_type> value_list, position_type position)
   : _value_list(value_list),
     _position(position) {
}
ValueNode::ValueNode(std::initializer_list<internal_value_type> value_list, position_type position)
   : _value_list(value_list),
     _position(position) {
}

void ValueNode::set_position(position_type position) {
   _position = position;
}

ValueNode::position_type ValueNode::get_position() const {
   return _position;
}

void ValueNode::set_as_list(internal_value_type value) {
   _value_list.assign({ value });
}

void ValueNode::add_to_list(internal_value_type value) {
   _value_list.push_back(value);
}

bool ValueNode::list_is_empty() const {
   return _value_list.empty();
}

std::string ValueNode::make(std::string_view prefix, std::optional<std::string_view> suffix) const {
   std::vector<std::string> pre_joined(_value_list.size());

   for (auto&& value : _value_list) {
      const auto& result = std::visit([&](auto&& arg) -> std::string {
         using T = std::decay_t<decltype(arg)>;
         if constexpr (std::is_same_v<T, std::string>) {
            return arg;
         } else if constexpr (std::is_same_v<T, Placeholder>) {
            return arg.as_string(prefix, suffix);
         }
      },
         value);
      pre_joined.push_back(result);
   }

   return fmt::format("{}", fmt::join(pre_joined, ""));
}