Rework TemplateString for standard compliance

Creating an std::integer_sequence from a user-defined string literal is
a non-standard gcc extension. This alternative implementation should
conform to C++20.
This commit is contained in:
Willi Schinmeyer
2024-12-05 19:00:23 +05:00
committed by Garux
parent 14e1cc95ad
commit 27c5b9e78b
5 changed files with 20 additions and 15 deletions
+1 -1
View File
@@ -107,7 +107,7 @@ static void parseBspFile( const char *bspPath, StrList& outShaders, StrList& out
}
for ( size_t i = 0; i < bspShaders.size(); ++i ){
if ( drawsurfSHs[i] && !( bspShaders[i].surfaceFlags & GetRequiredSurfaceParm( "nodraw"_Tstring ).surfaceFlags ) ){ // also sort out nodraw patches
if ( drawsurfSHs[i] && !( bspShaders[i].surfaceFlags & GetRequiredSurfaceParm<"nodraw">().surfaceFlags ) ){ // also sort out nodraw patches
res2list( pk3Shaders, bspShaders[i].shader );
}
}
+1 -1
View File
@@ -340,7 +340,7 @@ static void ConvertBrush( FILE *f, int bspBrushNum, const Vector3& origin, bool
/* find out if brush is detail */
int contentFlag = 0;
if( !( bspShaders[bspBrushes[bspBrushNum].shaderNum].contentFlags & GetRequiredSurfaceParm( "structural"_Tstring ).contentFlags ) ){ // sort out structural transparent brushes, e.g. hints
if( !( bspShaders[bspBrushes[bspBrushNum].shaderNum].contentFlags & GetRequiredSurfaceParm<"structural">().contentFlags ) ){ // sort out structural transparent brushes, e.g. hints
for( const auto& leaf : bspLeafs ){
if( leaf.cluster >= 0 )
for( auto id = bspLeafBrushes.cbegin() + leaf.firstBSPLeafBrush, end = id + leaf.numBSPLeafBrushes; id != end; ++id ){
+2 -2
View File
@@ -496,10 +496,10 @@ static void SetBrushContents( brush_t& b ){
else if ( s->compileFlags & C_FOG ){
b.contentShader = s->shaderInfo;
}
else if ( b.contentShader->contentFlags & GetRequiredSurfaceParm( "playerclip"_Tstring ).contentFlags ){
else if ( b.contentShader->contentFlags & GetRequiredSurfaceParm<"playerclip">().contentFlags ){
continue;
}
else if ( s->contentFlags & GetRequiredSurfaceParm( "playerclip"_Tstring ).contentFlags ){
else if ( s->contentFlags & GetRequiredSurfaceParm<"playerclip">().contentFlags ){
b.contentShader = s->shaderInfo;
}
else if (!( b.contentShader->compileFlags & C_SOLID )){
+15 -10
View File
@@ -67,6 +67,7 @@
#include "bitflags.h"
#include <list>
#include <forward_list>
#include <algorithm>
#include "qmath.h"
#include <cstddef>
@@ -1662,17 +1663,21 @@ void TCMod( const tcMod_t& mod, Vector2& st );
bool ApplySurfaceParm( const char *name, int *contentFlags, int *surfaceFlags, int *compileFlags );
const surfaceParm_t *GetSurfaceParm( const char *name );
// Encode the string as a type
template <char... chars>
using TemplateString = std::integer_sequence<char, chars...>;
// Create a user defined literal operator
template <typename T, T... chars>
constexpr TemplateString<chars...> operator""_Tstring() { return { }; }
// Encode the string as a structural literal class type
template <std::size_t N>
struct TemplateString
{
consteval TemplateString( const char(&string)[N] ) {
std::copy_n( string, N, m_data );
ENSURE( string[N - 1] == '\0' && "TemplateString must be null-terminated" ); // consteval ensures this is evaluated at compile time, despite not being a static_assert
}
char m_data[N];
};
/// \brief returns statically evaluated \c surfaceParm_t for the given name or emits \c Error
template<char... chars>
const surfaceParm_t &GetRequiredSurfaceParm( const TemplateString<chars...> ){
static constexpr char str[sizeof...(chars) + 1] = { chars..., '\0' }; // Recover the character data
static const surfaceParm_t *const sp = GetSurfaceParm( str );
template<TemplateString string>
const surfaceParm_t &GetRequiredSurfaceParm(){
static const surfaceParm_t *const sp = GetSurfaceParm( string.m_data ); // null-termination ensured in constructor
ENSURE( sp != nullptr );
return *sp;
}
+1 -1
View File
@@ -383,7 +383,7 @@ static void TriangulatePatchSurface( const entity_t& e, mapDrawSurface_t *ds ){
memcpy( dsNew, ds, sizeof( *ds ) );
/* if the patch is nonsolid, then discard it */
if ( !( ds->shaderInfo->compileFlags & C_SOLID ) && !( ds->shaderInfo->contentFlags & GetRequiredSurfaceParm( "playerclip"_Tstring ).contentFlags ) ) {
if ( !( ds->shaderInfo->compileFlags & C_SOLID ) && !( ds->shaderInfo->contentFlags & GetRequiredSurfaceParm<"playerclip">().contentFlags ) ) {
ClearSurface( ds );
}