Skip to content

Commit

Permalink
some upgrades to vmallocator.h
Browse files Browse the repository at this point in the history
Make `SCP_vector`, `SCP_list`, `SCP_set`, and `SCP_multiset` subclasses of their STL counterparts.  This allows helper functions to be added to each, including the `contains()` function which is not available until C++20.  (This has minor effects on other parts of the codebase that treated `SCP_` and `std::` as synonyms.)  Since there is now a nontrivial amount of code that needs to be in a .cpp file, create vmallocator.cpp.

Note that `SCP_string` truncation has been refactored to use length rather than C-string size to be consistent with other `std::basic_string` functions.

Also, remove some enum class hashing code which is no longer necessary now that the codebase is at C++17.
  • Loading branch information
Goober5000 committed Dec 16, 2024
1 parent 91b54c7 commit b5270fd
Show file tree
Hide file tree
Showing 24 changed files with 265 additions and 210 deletions.
2 changes: 1 addition & 1 deletion code/ai/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,6 @@ void do_random_sidethrust(ai_info *aip, ship_info *sip);
void ai_formation_object_recalculate_slotnums(int form_objnum, int exiting_objnum = -1);


bool test_line_of_sight(vec3d* from, vec3d* to, std::unordered_set<int>&& excluded_object_ids = {}, float threshold = 10.0f, bool test_for_shields = false, bool test_for_hull = true, float* first_intersect_dist = nullptr, object** first_intersect_obj = nullptr);
bool test_line_of_sight(const vec3d* from, const vec3d* to, SCP_unordered_set<int>&& excluded_object_ids = {}, float threshold = 10.0f, bool test_for_shields = false, bool test_for_hull = true, float* first_intersect_dist = nullptr, object** first_intersect_obj = nullptr);

#endif
5 changes: 3 additions & 2 deletions code/ai/aicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16795,15 +16795,16 @@ void maybe_cheat_fire_synaptic(object *objp)
}
}

bool test_line_of_sight(vec3d* from, vec3d* to, std::unordered_set<int>&& excluded_object_ids, float threshold, bool test_for_shields, bool test_for_hull, float* first_intersect_dist, object** first_intersect_obj) {
bool test_line_of_sight(const vec3d* from, const vec3d* to, SCP_unordered_set<int>&& excluded_object_ids, float threshold, bool test_for_shields, bool test_for_hull, float* first_intersect_dist, object** first_intersect_obj)
{
bool collides = false;

for (object* objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp)) {
if (objp->flags[Object::Object_Flags::Should_be_dead])
continue;

//Don't collision check against excluded objects
if (excluded_object_ids.count(OBJ_INDEX(objp)) > 0)
if (excluded_object_ids.contains(OBJ_INDEX(objp)))
continue;

int model_num = 0;
Expand Down
84 changes: 0 additions & 84 deletions code/globalincs/systemvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,87 +440,3 @@ DCF(detail, "Turns on/off parts of the game for speed testing" )
}
}
#endif

// Stuff that can't be included in vmallocator.h

std::locale SCP_default_locale("");

void SCP_tolower(char *str)
{
for (; *str != '\0'; ++str)
*str = SCP_tolower(*str);
}

void SCP_toupper(char *str)
{
for (; *str != '\0'; ++str)
*str = SCP_toupper(*str);
}

// this is a bit naive but it is good enough for the time being
void SCP_totitle(char *str)
{
bool prev_alpha = false;

for (; *str != '\0'; ++str)
{
bool this_alpha = (*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z');

if (this_alpha)
{
if (prev_alpha)
*str = SCP_tolower(*str);
else
*str = SCP_toupper(*str);
}

prev_alpha = this_alpha;
}
}

bool lcase_equal(const SCP_string& _Left, const SCP_string& _Right)
{
if (_Left.size() != _Right.size())
return false;

auto l_it = _Left.cbegin();
auto r_it = _Right.cbegin();

while (l_it != _Left.cend())
{
if (SCP_tolower(*l_it) != SCP_tolower(*r_it))
return false;

++l_it;
++r_it;
}

return true;
}

bool lcase_lessthan(const SCP_string& _Left, const SCP_string& _Right)
{
auto l_it = _Left.cbegin();
auto r_it = _Right.cbegin();

while (true)
{
if (l_it == _Left.cend())
return (r_it != _Right.cend());
else if (r_it == _Right.cend())
return false;

auto lch = SCP_tolower(*l_it);
auto rch = SCP_tolower(*r_it);

if (lch < rch)
return true;
else if (lch > rch)
return false;

++l_it;
++r_it;
}

return true;
}
128 changes: 128 additions & 0 deletions code/globalincs/vmallocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Stuff that can't be included in vmallocator.h

#include "globalincs/vmallocator.h"

std::locale SCP_default_locale("");

void SCP_tolower(SCP_string &str)
{
std::for_each(str.begin(), str.end(), [](char &ch) { ch = SCP_tolower(ch); });
}

void SCP_toupper(SCP_string &str)
{
std::for_each(str.begin(), str.end(), [](char &ch) { ch = SCP_toupper(ch); });
}

void SCP_tolower(char *str)
{
for (; *str != '\0'; ++str)
*str = SCP_tolower(*str);
}

void SCP_toupper(char *str)
{
for (; *str != '\0'; ++str)
*str = SCP_toupper(*str);
}

// in-place modification of string to title case; this is a bit naive but it is good enough for the time being
void SCP_totitle(char *str)
{
bool prev_alpha = false;

for (; *str != '\0'; ++str)
{
bool this_alpha = (*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z');

if (this_alpha)
{
if (prev_alpha)
*str = SCP_tolower(*str);
else
*str = SCP_toupper(*str);
}

prev_alpha = this_alpha;
}
}

// in-place modification of string to title case; same naive algorithm as above
void SCP_totitle(SCP_string &str)
{
SCP_string title_str;
bool prev_alpha = false;

std::for_each(str.begin(), str.end(), [&prev_alpha](char &ch)
{
bool this_alpha = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');

if (this_alpha)
{
if (prev_alpha)
ch = SCP_tolower(ch);
else
ch = SCP_toupper(ch);
}

prev_alpha = this_alpha;
});
}

bool SCP_truncate(SCP_string &str, size_t len)
{
if (str.length() > len)
{
str.resize(len);
return true;
}
else
return false;
}

bool lcase_equal(const SCP_string& _Left, const SCP_string& _Right)
{
if (_Left.size() != _Right.size())
return false;

auto l_it = _Left.cbegin();
auto r_it = _Right.cbegin();

while (l_it != _Left.cend())
{
if (SCP_tolower(*l_it) != SCP_tolower(*r_it))
return false;

++l_it;
++r_it;
}

return true;
}

bool lcase_lessthan(const SCP_string& _Left, const SCP_string& _Right)
{
auto l_it = _Left.cbegin();
auto r_it = _Right.cbegin();

while (true)
{
if (l_it == _Left.cend())
return (r_it != _Right.cend());
else if (r_it == _Right.cend())
return false;

auto lch = SCP_tolower(*l_it);
auto rch = SCP_tolower(*r_it);

if (lch < rch)
return true;
else if (lch > rch)
return false;

++l_it;
++r_it;
}

return true;
}
Loading

0 comments on commit b5270fd

Please sign in to comment.