Skip to content

Commit cd7889e

Browse files
committed
Move generic containers into core/containers
Move tracked allocating containers to memory namespace
1 parent c8d3d19 commit cd7889e

152 files changed

Lines changed: 1129 additions & 818 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/openvic-simulation/GameManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include <spdlog/spdlog.h>
1010

11+
#include "openvic-simulation/core/memory/OrderedSet.hpp"
1112
#include "openvic-simulation/dataloader/Dataloader.hpp"
12-
#include "openvic-simulation/types/OrderedContainers.hpp"
1313
#include "openvic-simulation/utility/Logger.hpp"
1414

1515
using namespace OpenVic;
@@ -76,7 +76,7 @@ bool GameManager::load_mods(memory::vector<memory::string> const& mods_to_find)
7676

7777
bool ret = true;
7878

79-
vector_ordered_set<Mod const*> load_list;
79+
memory::vector_ordered_set<Mod const*> load_list;
8080

8181
/* Check loaded mod descriptors for requested mods, using either full name or user directory name
8282
* (Historical Project Mod 0.4.6 or HPM both valid, for example), and load them plus their dependencies.
@@ -95,7 +95,7 @@ bool GameManager::load_mods(memory::vector<memory::string> const& mods_to_find)
9595
}
9696

9797
Mod const* mod_ptr = &*it;
98-
vector_ordered_set<Mod const*> dependencies = mod_ptr->generate_dependency_list(&ret);
98+
memory::vector_ordered_set<Mod const*> dependencies = mod_ptr->generate_dependency_list(&ret);
9999
if(!ret) {
100100
continue;
101101
}

src/openvic-simulation/InstanceManager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <function2/function2.hpp>
66

77
#include "openvic-simulation/console/ConsoleInstance.hpp"
8+
#include "openvic-simulation/core/memory/FlagStrings.hpp"
89
#include "openvic-simulation/country/CountryInstanceManager.hpp"
910
#include "openvic-simulation/country/CountryInstanceDeps.hpp"
1011
#include "openvic-simulation/diplomacy/CountryRelation.hpp"
@@ -21,7 +22,6 @@
2122
#include "openvic-simulation/politics/PoliticsInstanceManager.hpp"
2223
#include "openvic-simulation/population/PopDeps.hpp"
2324
#include "openvic-simulation/types/Date.hpp"
24-
#include "openvic-simulation/types/FlagStrings.hpp"
2525
#include "openvic-simulation/utility/ThreadPool.hpp"
2626
#include "openvic-simulation/utility/Containers.hpp"
2727

@@ -50,7 +50,7 @@ namespace OpenVic {
5050
ResourceGatheringOperationDeps rgo_deps;
5151
ProvinceInstanceDeps province_instance_deps;
5252

53-
FlagStrings PROPERTY_REF(global_flags);
53+
memory::FlagStrings PROPERTY_REF(global_flags);
5454

5555
CountryInstanceManager PROPERTY_REF(country_instance_manager);
5656
UnitInstanceManager PROPERTY_REF(unit_instance_manager);

src/openvic-simulation/console/ConsoleInstance.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include <fmt/color.h>
88
#include <fmt/core.h>
99

10+
#include "openvic-simulation/core/memory/StringMap.hpp"
1011
#include "openvic-simulation/types/Colour.hpp"
1112
#include "openvic-simulation/types/Date.hpp"
12-
#include "openvic-simulation/types/OrderedContainers.hpp"
1313
#include "openvic-simulation/utility/Containers.hpp"
1414
#include "openvic-simulation/utility/Getters.hpp"
1515

@@ -98,7 +98,7 @@ namespace OpenVic {
9898
Technology const* validate_tech_name(std::string_view value_string);
9999

100100
private:
101-
string_map_t<execute_command_func_t> commands;
101+
memory::string_map_t<execute_command_func_t> commands;
102102

103103
write_func_t write_func;
104104

File renamed without changes.
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "openvic-simulation/core/template/Concepts.hpp"
4+
5+
namespace OpenVic {
6+
/* Intermediate struct that "remembers" Case, instead of just decomposing it into its hash and equal components,
7+
* needed so that templates can deduce the Case with which a type was defined. */
8+
template<template<typename...> typename Container, string_map_case Case, typename... Args>
9+
struct template_case_container_t : Container<Args..., typename Case::hash, typename Case::equal> {
10+
using container_t = Container<Args..., typename Case::hash, typename Case::equal>;
11+
using container_t::container_t;
12+
13+
using case_t = Case;
14+
};
15+
}
File renamed without changes.
File renamed without changes.

src/openvic-simulation/types/CowVector.hpp renamed to src/openvic-simulation/core/container/CowVector.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <vector>
1313

1414
#include "openvic-simulation/core/Assert.hpp"
15-
#include "openvic-simulation/types/BasicIterator.hpp"
16-
#include "openvic-simulation/utility/Allocator.hpp"
15+
#include "openvic-simulation/core/container/BasicIterator.hpp"
16+
#include "openvic-simulation/core/Allocator.hpp"
1717
#include "openvic-simulation/core/Compare.hpp"
1818
#include "openvic-simulation/core/template/Concepts.hpp"
1919
#include "openvic-simulation/core/Typedefs.hpp"

src/openvic-simulation/types/FixedVector.hpp renamed to src/openvic-simulation/core/container/FixedVector.hpp

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
#include <cassert>
55
#include <cstddef>
66
#include <memory>
7+
#include <type_traits>
78
#include <utility>
89

910
#include "openvic-simulation/core/Assert.hpp"
1011
#include "openvic-simulation/core/template/Concepts.hpp"
1112
#include "openvic-simulation/core/Typedefs.hpp"
1213

13-
namespace OpenVic::_detail {
14+
namespace OpenVic {
1415
//fixed capacity + not movable + not copyable
1516
template <typename T, typename Allocator = std::allocator<T>>
1617
class FixedVector {
@@ -27,6 +28,7 @@ namespace OpenVic::_detail {
2728
using reference = T&;
2829
using size_type = size_t;
2930
using value_type = T;
31+
using allocator_type = Allocator;
3032

3133
constexpr size_t size() const { return _size; }
3234
constexpr size_t capacity() const { return _max_size; }
@@ -39,15 +41,21 @@ namespace OpenVic::_detail {
3941
* @brief Creates an uninitialised vector with fixed capacity
4042
*/
4143
explicit FixedVector(const size_t capacity)
44+
: FixedVector(capacity, Allocator()) {}
45+
46+
FixedVector(const size_t capacity, std::type_identity_t<Allocator> const& alloc)
4247
: _max_size(capacity),
4348
_size(0),
44-
_allocator(),
49+
_allocator(alloc),
4550
_data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {}
4651

4752
FixedVector(const size_t size, T const& value_for_all_indices)
53+
: FixedVector(size, value_for_all_indices, Allocator()) {}
54+
55+
FixedVector(const size_t size, T const& value_for_all_indices, std::type_identity_t<Allocator> const& alloc)
4856
: _max_size(size),
4957
_size(size),
50-
_allocator(),
58+
_allocator(alloc),
5159
_data_start_ptr(allocator_traits::allocate(_allocator, size)) {
5260
std::fill(_data_start_ptr, _data_start_ptr + size, value_for_all_indices);
5361
}
@@ -59,9 +67,19 @@ namespace OpenVic::_detail {
5967
// The type must be constructible from the generator's single return value
6068
&& std::constructible_from<T, decltype(std::declval<GeneratorTemplateType>()(std::declval<size_t>()))>
6169
FixedVector(const size_t size, GeneratorTemplateType&& generator)
70+
: FixedVector(size, std::forward<GeneratorTemplateType>(generator), Allocator()) {
71+
}
72+
73+
//Generator (size_t i) -> U (where T is constructable from U)
74+
template<typename GeneratorTemplateType>
75+
// The generator must NOT return a tuple
76+
requires (!specialization_of<std::remove_cvref_t<std::invoke_result_t<GeneratorTemplateType, size_t>>, std::tuple>)
77+
// The type must be constructible from the generator's single return value
78+
&& std::constructible_from<T, decltype(std::declval<GeneratorTemplateType>()(std::declval<size_t>()))>
79+
FixedVector(const size_t size, GeneratorTemplateType&& generator, std::type_identity_t<Allocator> const& alloc)
6280
: _max_size(size),
6381
_size(size),
64-
_allocator(),
82+
_allocator(alloc),
6583
_data_start_ptr(allocator_traits::allocate(_allocator, size)) {
6684
for (size_t i = 0; i < size; ++i) {
6785
allocator_traits::construct(
@@ -88,9 +106,28 @@ namespace OpenVic::_detail {
88106
};
89107
}
90108
FixedVector(const size_t size, GeneratorTemplateType&& generator)
109+
: FixedVector(size, std::forward<GeneratorTemplateType>(generator), Allocator()) {
110+
}
111+
112+
//Generator (size_t i) -> std::tuple<Args...> (where T is constructable from Args)
113+
template<typename GeneratorTemplateType>
114+
// The generator must return a tuple
115+
requires specialization_of<std::remove_cvref_t<std::invoke_result_t<GeneratorTemplateType, size_t>>, std::tuple>
116+
// The tuple must be constructible into a T
117+
&& requires(GeneratorTemplateType&& generator) {
118+
{
119+
std::apply(
120+
[](auto&&... args) {
121+
T obj{std::forward<decltype(args)>(args)...};
122+
},
123+
generator(std::declval<size_t>())
124+
)
125+
};
126+
}
127+
FixedVector(const size_t size, GeneratorTemplateType&& generator, std::type_identity_t<Allocator> const& alloc)
91128
: _max_size(size),
92129
_size(size),
93-
_allocator(),
130+
_allocator(alloc),
94131
_data_start_ptr(allocator_traits::allocate(_allocator, size)) {
95132
for (size_t i = 0; i < size; ++i) {
96133
std::apply(
@@ -115,7 +152,7 @@ namespace OpenVic::_detail {
115152
clear();
116153
allocator_traits::deallocate(_allocator, _data_start_ptr, _max_size);
117154
}
118-
155+
119156
using iterator = T*;
120157
using const_iterator = const T*;
121158

@@ -126,7 +163,7 @@ namespace OpenVic::_detail {
126163
iterator end() { return begin() + _size; }
127164
const_iterator end() const { return begin() + _size; }
128165
const_iterator cend() const { return cbegin() + _size; }
129-
166+
130167
using reverse_iterator = std::reverse_iterator<iterator>;
131168
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
132169

@@ -198,14 +235,9 @@ namespace OpenVic::_detail {
198235
}
199236
_size = 0;
200237
}
201-
};
202-
}
203238

204-
#include <foonathan/memory/std_allocator.hpp>
205-
206-
#include "openvic-simulation/utility/MemoryTracker.hpp"
207-
208-
namespace OpenVic::memory {
209-
template<typename T, class RawAllocator = foonathan::memory::default_allocator>
210-
using FixedVector = _detail::FixedVector<T, foonathan::memory::std_allocator<T, tracker<RawAllocator>>>;
239+
allocator_type get_allocator() const {
240+
return _allocator;
241+
}
242+
};
211243
}

0 commit comments

Comments
 (0)