Skip to content

Commit 51c30f7

Browse files
committed
Initial idea to implement paths as using a "mixin" approach on top of String and HeapString types.
#ICE-214 State In Progress
1 parent 9fc38d8 commit 51c30f7

File tree

10 files changed

+130
-35
lines changed

10 files changed

+130
-35
lines changed

source/code/core/collections/public/ice/shard_container.hxx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ namespace ice
3232
Args&&... args
3333
) const noexcept -> ice::ncount;
3434

35+
template<typename T>
36+
inline constexpr bool inspect_first(ice::ShardID shardid, T& payload) const noexcept;
37+
38+
template<typename T>
39+
inline constexpr bool inspect_last(ice::ShardID shard, T& payload) const noexcept;
40+
3541
template<typename T, ice::ContainerLogic Logic>
3642
inline constexpr auto inspect_all(
3743
ice::ShardID shardid,
@@ -44,12 +50,6 @@ namespace ice
4450
Fn&& callback
4551
) noexcept -> ice::ncount;
4652

47-
template<typename T>
48-
inline constexpr bool inspect_first(ice::ShardID shardid, T& payload) const noexcept;
49-
50-
template<typename T>
51-
inline constexpr bool inspect_last(ice::ShardID shard, T& payload) const noexcept;
52-
5353
inline constexpr void remove_all_of(this ShardContainer& self, ice::ShardID shardid) noexcept;
5454
};
5555

@@ -137,6 +137,20 @@ namespace ice
137137
return { count, sizeof(ice::ShardID) };
138138
}
139139

140+
template<typename T>
141+
inline constexpr bool ShardContainer::inspect_first(ice::ShardID shardid, T& payload) const noexcept
142+
{
143+
ice::Shard const shard = this->find_first_of(shardid);
144+
return ice::shard_inspect(shard, payload);
145+
}
146+
147+
template<typename T>
148+
inline constexpr bool ShardContainer::inspect_last(ice::ShardID shardid, T& payload) const noexcept
149+
{
150+
ice::Shard const shard = this->find_last_of(shardid);
151+
return ice::shard_inspect(shard, payload);
152+
}
153+
140154
template<typename T, typename Fn>
141155
inline constexpr auto ShardContainer::inspect_each(ice::ShardID shardid, Fn&& callback) noexcept -> ice::ncount
142156
{
@@ -152,20 +166,6 @@ namespace ice
152166
return { count, sizeof(ice::Shard) };
153167
}
154168

155-
template<typename T>
156-
inline constexpr bool ShardContainer::inspect_first(ice::ShardID shardid, T& payload) const noexcept
157-
{
158-
ice::Shard const shard = this->find_first_of(shardid);
159-
return ice::shard_inspect(shard, payload);
160-
}
161-
162-
template<typename T>
163-
inline constexpr bool ShardContainer::inspect_last(ice::ShardID shardid, T& payload) const noexcept
164-
{
165-
ice::Shard const shard = this->find_last_of(shardid);
166-
return ice::shard_inspect(shard, payload);
167-
}
168-
169169
inline constexpr void ShardContainer::remove_all_of(
170170
this ShardContainer& self,
171171
ice::ShardID shardid

source/code/core/collections/public/ice/string/string_concepts.hxx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ namespace ice::concepts
4747

4848
} // namespace ice::concepts
4949

50+
namespace ice::string
51+
{
52+
53+
template<ice::concepts::StringType StringT>
54+
using ConstCorrectCharType = std::conditional_t<
55+
std::is_const_v<typename std::remove_reference_t<StringT>>,
56+
typename std::remove_reference_t<StringT>::CharType const,
57+
typename std::remove_reference_t<StringT>::CharType
58+
>;
59+
60+
template<ice::concepts::StringType StringT>
61+
using CharType = ConstCorrectCharType<StringT>;
62+
63+
} // namespace ice::string
64+
5065
namespace ice::string::detail
5166
{
5267

source/code/core/core/public/ice/build/platform.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ namespace ice::build
164164
# define ISP_ARCHFAM_X86 1
165165
# define ISP_ARCHFAM_ARM 0
166166
# define ISP_ARCHFAM_WEBASM 0
167+
# define ISP_TEXT(val) L ## val
167168
#elif defined(__ANDROID__)
168169
# define ISP_UNIX 1
169170
# define ISP_LINUX 0
@@ -185,6 +186,7 @@ namespace ice::build
185186
# define ISP_ARCHFAM_WEBASM 0
186187
static constexpr Platform current_platform = platform_android_x64_clang;
187188
# endif
189+
# define ISP_TEXT(val) val
188190
#elif defined(EMSCRIPTEN)
189191
# define ISP_UNIX 1
190192
# define ISP_LINUX 0
@@ -200,6 +202,7 @@ namespace ice::build
200202
# define ISP_ARCHFAM_WEBASM 1
201203

202204
static constexpr Platform current_platform = platform_webapp_webasm32_clang;
205+
# define ISP_TEXT(val) val
203206
#elif __unix__ && !__clang__
204207
# define ISP_UNIX 1
205208
# define ISP_LINUX 1
@@ -214,6 +217,7 @@ namespace ice::build
214217
# define ISP_ARCHFAM_WEBASM 0
215218

216219
static constexpr Platform current_platform = platform_unix_x64_gcc;
220+
# define ISP_TEXT(val) val
217221
#elif __unix__ && __clang__
218222
# define ISP_UNIX 1
219223
# define ISP_LINUX 1
@@ -228,6 +232,7 @@ namespace ice::build
228232
# define ISP_ARCHFAM_WEBASM 0
229233

230234
static constexpr Platform current_platform = platform_linux_x64_clang;
235+
# define ISP_TEXT(val) val
231236
#else
232237
# define ISP_UNIX 0
233238
# define ISP_WINDOWS 0
@@ -241,6 +246,7 @@ namespace ice::build
241246
# define ISP_ARCHFAM_WEBASM 0
242247

243248
static_assert(false, "Unknow platform!");
249+
# define ISP_TEXT(val) ISP_UNDEFINED
244250
#endif
245251

246252
constexpr auto arch_family(Architecture arch) noexcept -> ArchFamily

source/code/core/utils/public/ice/native_file.hxx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ namespace ice::native_file
1818

1919
#if ISP_WINDOWS
2020
using File = ice::win32::FileHandle;
21-
using FilePath = ice::WString;
22-
using HeapFilePath = ice::HeapString<ice::wchar>;
21+
using FilePath = ice::BasicPath<ice::wchar>;
22+
struct HeapFilePath : public ice::HeapString<ice::wchar>, public ice::PathString
23+
{
24+
using HeapString<ice::wchar>::HeapString;
25+
using HeapString<ice::wchar>::operator ice::BasicString<ice::wchar>;
26+
27+
constexpr operator FilePath() const noexcept { return { _data, _size }; }
28+
};
2329
# define ISP_PATH_LITERAL(val) L##val
2430
#elif ISP_UNIX
2531
using File = ice::unix_::FileHandle;

source/code/core/utils/public/ice/path_utils.hxx

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,70 @@
44
#pragma once
55
#include <ice/string.hxx>
66
#include <ice/heap_string.hxx>
7+
#include <ice/log_formatters.hxx>
78

8-
namespace ice::path
9+
namespace ice
910
{
1011

11-
//! \note On windows: starts with a drive letter, on linux: checks for starting backslash.
12-
//! \return true If the path is absolute.
13-
bool is_absolute(ice::String path) noexcept;
12+
struct PathString
13+
{
14+
template<ice::concepts::SupportedCharType CharT>
15+
static constexpr ice::BasicString<CharT> Separator_Dot;
16+
template<ice::concepts::SupportedCharType CharT>
17+
static constexpr ice::BasicString<CharT> Separator_Drive;
18+
template<ice::concepts::SupportedCharType CharT>
19+
static constexpr ice::BasicString<CharT> Separator_Directory;
20+
21+
template<> constexpr ice::BasicString<char> Separator_Dot<char> = ".";
22+
template<> constexpr ice::BasicString<char> Separator_Drive<char> = ":";
23+
template<> constexpr ice::BasicString<char> Separator_Directory<char> = "\\/";
24+
template<> constexpr ice::BasicString<ice::wchar> Separator_Dot<ice::wchar> = L".";
25+
template<> constexpr ice::BasicString<ice::wchar> Separator_Drive<ice::wchar> = L":";
26+
template<> constexpr ice::BasicString<ice::wchar> Separator_Directory<ice::wchar> = L"\\/";
27+
28+
template<typename Self>
29+
bool is_absolute(this Self const& self) noexcept
30+
{
31+
using CharType = ice::string::CharType<Self>;
32+
33+
if constexpr (ice::build::is_windows)
34+
{
35+
if (self.size() >= 3_count)
36+
{
37+
return self[1] == Separator_Drive<CharType>[0] && Separator_Directory<CharType>.find_first_of(self[2]) != ice::nindex_none;
38+
}
39+
return false;
40+
}
41+
else
42+
{
43+
return self.not_empty() && self.front() == Separator_Directory<CharType>[1];
44+
}
45+
}
46+
47+
template<typename Self>
48+
bool is_relative(this Self const& self) noexcept
49+
{
50+
return self.is_absolute() == false;
51+
}
52+
};
53+
54+
template<ice::concepts::SupportedCharType CharT>
55+
struct BasicPath : public ice::BasicString<CharT>, public ice::PathString
56+
{
57+
using BasicString<CharT>::BasicString;
58+
using BasicString<CharT>::operator std::basic_string_view<CharT>;
59+
60+
constexpr BasicPath(ice::BasicString<CharT> str) noexcept
61+
: BasicString<CharT>{ str }
62+
{ }
63+
};
64+
65+
using Path = ice::BasicPath<char>;
66+
67+
} // namespace
68+
69+
namespace ice::path
70+
{
1471

1572
//! \return true If the path is just the root part. (ex. 'C:/' or '/')
1673
bool is_absolute_root(ice::String path) noexcept;
@@ -71,3 +128,13 @@ namespace ice::path
71128
auto replace_extension(ice::HeapString<ice::wchar>& path, ice::WString extension) noexcept -> ice::WString;
72129

73130
} // namespace ice::path
131+
132+
template<typename CharType>
133+
struct fmt::formatter<ice::BasicPath<CharType>> : public fmt::formatter<std::basic_string_view<CharType>>
134+
{
135+
template<typename FormatContext>
136+
constexpr auto format(ice::BasicPath<CharType> value, FormatContext& ctx) const noexcept
137+
{
138+
return fmt::formatter<std::basic_string_view<CharType>>::format(value, ctx);
139+
}
140+
};

source/code/systems/resource_system/private/resource_provider_filelist.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace ice
2626
for (ice::ResourceFileEntry const& entry : entries)
2727
{
2828
using enum native_file::PathFlags;
29-
if (ice::path::is_absolute(entry.path) == false)
29+
if (entry.path.is_relative())
3030
{
3131
file_path = ice::native_file::path_from_strings<Normalized>(
3232
_named_allocator, entry.basepath, entry.path
@@ -44,7 +44,7 @@ namespace ice
4444
);
4545

4646
ice::native_file::FilePath const file_path_str = file_path;
47-
ICE_ASSERT_CORE(file_path_str.starts_with((ice::native_file::FilePath)base_path));
47+
ICE_ASSERT_CORE(file_path_str.starts_with(base_path));
4848
}
4949

5050
ice::ncount const basepath_size = entry.basepath.is_empty()

source/code/systems/resource_system/public/ice/resource_provider.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <ice/native_aio.hxx>
1212
#include <ice/task_types.hxx>
1313
#include <ice/task_expected.hxx>
14+
#include <ice/path_utils.hxx>
1415
#include <ice/uri.hxx>
1516

1617
namespace ice
@@ -85,8 +86,8 @@ namespace ice
8586

8687
struct ResourceFileEntry
8788
{
88-
ice::String path;
89-
ice::String basepath = {};
89+
ice::Path path;
90+
ice::Path basepath = {};
9091
};
9192

9293
auto create_resource_provider(

source/code/tools/hsc_packer/private/hsc_packer.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class HailStormPackerApp final : public ice::tool::ToolApp<HailStormPackerApp>
188188
// }
189189

190190
// Prepare the output file name.
191-
_param_output = hscp_process_directory(_allocator, _param_output);
191+
_param_output = hscp_process_directory(_allocator, ice::Path{ _param_output });
192192

193193
// The paths that will be searched for loose file resources.
194194
ice::UniquePtr<ice::ResourceProvider> fsprov = ice::create_resource_provider(
@@ -234,7 +234,7 @@ class HailStormPackerApp final : public ice::tool::ToolApp<HailStormPackerApp>
234234
auto run_explicit() noexcept -> ice::i32
235235
{
236236
// Prepare the output file name.
237-
_param_output = hscp_process_directory(_allocator, _param_output);
237+
_param_output = hscp_process_directory(_allocator, ice::Path{ _param_output });
238238

239239
// The paths that will be searched for loose file resources.
240240
ice::Array<ice::ResourceFileEntry> files{ _allocator };

source/code/tools/hsc_packer/private/hsc_packer_app.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#include <ice/log_module.hxx>
99
#include <ice/tool_app.hxx>
1010

11-
auto hscp_process_directory(ice::Allocator& alloc, ice::String dir) noexcept -> ice::HeapString<>
11+
auto hscp_process_directory(ice::Allocator& alloc, ice::Path dir) noexcept -> ice::HeapString<>
1212
{
1313
ice::HeapString<> searched_utf8_path{ alloc, dir };
14-
if (ice::path::is_absolute(dir) == false)
14+
if (dir.is_relative())
1515
{
1616
searched_utf8_path = ice::app::workingdir();
1717
ice::path::join(searched_utf8_path, dir);

source/code/tools/hsc_packer/private/hsc_packer_app.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <ice/native_file.hxx>
1212
#include <ice/params.hxx>
1313

14-
auto hscp_process_directory(ice::Allocator& alloc, ice::String dir) noexcept -> ice::HeapString<>;
14+
auto hscp_process_directory(ice::Allocator& alloc, ice::Path dir) noexcept -> ice::HeapString<>;
1515

1616
static constexpr ice::LogTagDefinition LogTag_Main = ice::create_log_tag(ice::LogTag::None, "hsc-packer");
1717
static constexpr ice::LogTagDefinition LogTag_Details = ice::create_log_tag(LogTag_Main, "details");

0 commit comments

Comments
 (0)