aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/Region.cpp
blob: d8c5fc10675ddcf9dcee5d8fe7d7342d7cd615a7 (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
81
82
83
84
85
86
87
88
89
90
91
#include "Region.hpp"

#include "openvic-simulation/map/ProvinceDefinition.hpp"
#include "openvic-simulation/types/Colour.hpp"

using namespace OpenVic;

bool ProvinceSet::add_province(ProvinceDefinition const* province) {
   if (locked) {
      Logger::error("Cannot add province to province set - locked!");
      return false;
   }
   if (province == nullptr) {
      Logger::error("Cannot add province to province set - null province!");
      return false;
   }
   if (contains_province(province)) {
      Logger::warning("Cannot add province ", province->get_identifier(), " to province set - already in the set!");
      return false;
   }
   provinces.push_back(province);
   return true;
}

bool ProvinceSet::remove_province(ProvinceDefinition const* province) {
   if (locked) {
      Logger::error("Cannot remove province from province set - locked!");
      return false;
   }
   if (province == nullptr) {
      Logger::error("Cannot remove province from province set - null province!");
      return false;
   }
   const decltype(provinces)::const_iterator it = std::find(provinces.begin(), provinces.end(), province);
   if (it == provinces.end()) {
      Logger::warning("Cannot remove province ", province->get_identifier(), " from province set - already not in the set!");
      return false;
   }
   provinces.erase(it);
   return true;
}

void ProvinceSet::lock(bool log) {
   if (locked) {
      Logger::error("Failed to lock province set - already locked!");
   } else {
      locked = true;
      if (log) {
         Logger::info("Locked province set with ", size(), " provinces");
      }
   }
}

bool ProvinceSet::is_locked() const {
   return locked;
}

void ProvinceSet::reset() {
   provinces.clear();
   locked = false;
}

bool ProvinceSet::empty() const {
   return provinces.empty();
}

size_t ProvinceSet::size() const {
   return provinces.size();
}

void ProvinceSet::reserve(size_t size) {
   if (locked) {
      Logger::error("Failed to reserve space for ", size, " items in province set - already locked!");
   } else {
      provinces.reserve(size);
   }
}

void ProvinceSet::reserve_more(size_t size) {
   OpenVic::reserve_more(*this, size);
}

bool ProvinceSet::contains_province(ProvinceDefinition const* province) const {
   return province != nullptr && std::find(provinces.begin(), provinces.end(), province) != provinces.end();
}

ProvinceSetModifier::ProvinceSetModifier(std::string_view new_identifier, ModifierValue&& new_values)
   : Modifier { new_identifier, std::move(new_values) } {}

Region::Region(std::string_view new_identifier, colour_t new_colour, bool new_meta)
   : HasIdentifierAndColour { new_identifier, new_colour, false }, meta { new_meta } {}