You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw/components/esm/refid.cpp

163 lines
3.9 KiB
C++

Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2 years ago
#include "refid.hpp"
#include <ostream>
#include <sstream>
#include <stdexcept>
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2 years ago
namespace ESM
{
namespace
{
const std::string emptyString;
struct GetRefString
{
const std::string& operator()(EmptyRefId /*v*/) const { return emptyString; }
const std::string& operator()(StringRefId v) const { return v.getValue(); }
template <class T>
const std::string& operator()(const T& v) const
{
std::ostringstream stream;
stream << "RefId is not a string: " << v;
throw std::runtime_error(stream.str());
}
};
struct IsEqualToString
{
const std::string_view mRhs;
bool operator()(StringRefId v) const noexcept { return v == mRhs; }
template <class T>
bool operator()(const T& /*v*/) const noexcept
{
return false;
}
};
struct IsLessThanString
{
const std::string_view mRhs;
bool operator()(StringRefId v) const noexcept { return v < mRhs; }
template <class T>
bool operator()(const T& /*v*/) const noexcept
{
return false;
}
};
struct IsGreaterThanString
{
const std::string_view mLhs;
bool operator()(StringRefId v) const noexcept { return mLhs < v; }
template <class T>
bool operator()(const T& /*v*/) const noexcept
{
return true;
}
};
struct StartsWith
{
const std::string_view mPrefix;
bool operator()(StringRefId v) const { return v.startsWith(mPrefix); }
template <class T>
bool operator()(const T& /*v*/) const
{
return false;
}
};
struct Contains
{
const std::string_view mSubString;
bool operator()(StringRefId v) const { return v.contains(mSubString); }
template <class T>
bool operator()(const T& /*v*/) const
{
return false;
}
};
}
const RefId RefId::sEmpty = {};
std::string EmptyRefId::toString() const
{
return std::string();
}
std::string EmptyRefId::toDebugString() const
{
return "Empty{}";
}
std::ostream& operator<<(std::ostream& stream, EmptyRefId value)
{
return stream << value.toDebugString();
}
bool RefId::operator==(std::string_view rhs) const
{
return std::visit(IsEqualToString{ rhs }, mValue);
}
bool operator<(RefId lhs, std::string_view rhs)
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2 years ago
{
return std::visit(IsLessThanString{ rhs }, lhs.mValue);
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2 years ago
}
bool operator<(std::string_view lhs, RefId rhs)
{
return std::visit(IsGreaterThanString{ lhs }, rhs.mValue);
}
std::ostream& operator<<(std::ostream& stream, RefId value)
{
return std::visit([&](auto v) -> std::ostream& { return stream << v; }, value.mValue);
}
RefId RefId::stringRefId(std::string_view value)
{
if (value.empty())
return RefId();
return RefId(StringRefId(value));
}
const std::string& RefId::getRefIdString() const
{
return std::visit(GetRefString{}, mValue);
}
std::string RefId::toString() const
{
return std::visit([](auto v) { return v.toString(); }, mValue);
}
std::string RefId::toDebugString() const
{
return std::visit([](auto v) { return v.toDebugString(); }, mValue);
}
bool RefId::startsWith(std::string_view prefix) const
{
return std::visit(StartsWith{ prefix }, mValue);
}
bool RefId::contains(std::string_view subString) const
{
return std::visit(Contains{ subString }, mValue);
}
}