Skip to content

Commit

Permalink
Define ARRAYSIZE as function. Use it where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Apr 25, 2024
1 parent 19712bf commit 449ad49
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 28 deletions.
7 changes: 4 additions & 3 deletions cl_dll/ammo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "cl_util.h"
#include "parsemsg.h"
#include "pm_shared.h"
#include "arraysize.h"

#include "ammohistory.h"
#include "ammoregistry.h"
Expand Down Expand Up @@ -141,7 +142,7 @@ void WeaponsResource::AddWeapon(WEAPON *wp)
{
// Check user preferences
bool foundUserPreference = false;
for (int i=0; i<sizeof(bucketPreferences)/sizeof(bucketPreferences[0]); ++i)
for (int i=0; i<ARRAYSIZE(bucketPreferences); ++i)
{
const BucketPreference& pref = bucketPreferences[i];
if (pref.szName[0] == '\0')
Expand Down Expand Up @@ -474,7 +475,7 @@ int CHudAmmo::VidInit( void )
m_HUD_selection = gHUD.GetSpriteIndex( "selection" );

char bucketName[8] = "bucket";
for (int i=0; i<sizeof(m_HUD_buckets)/sizeof(m_HUD_buckets[0]); ++i)
for (int i=0; i<ARRAYSIZE(m_HUD_buckets); ++i)
{
bucketName[6] = '0' + i + 1;
bucketName[7] = '\0';
Expand Down Expand Up @@ -1214,7 +1215,7 @@ void DrawAmmoBar( WEAPON *p, int x, int y, int width, int height )
int CHudAmmo::SpriteIndexForSlot(int iSlot)
{
int result = -1;
if (iSlot >=0 && iSlot < sizeof(m_HUD_buckets)/sizeof(m_HUD_buckets[0]))
if (iSlot >=0 && iSlot < ARRAYSIZE(m_HUD_buckets))
{
result = m_HUD_buckets[iSlot];
}
Expand Down
4 changes: 0 additions & 4 deletions cl_dll/ev_hldm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,10 +1427,6 @@ enum EGON_FIREMODE
#define EGON_SOUND_RUN "weapons/egon_run3.wav"
#define EGON_SOUND_STARTUP "weapons/egon_windup2.wav"

#if !defined(ARRAYSIZE)
#define ARRAYSIZE(p) ( sizeof(p) /sizeof(p[0]) )
#endif

BEAM *pBeam;
BEAM *pBeam2;
TEMPENTITY *pFlare; // Vit_amiN: egon's beam flare
Expand Down
15 changes: 8 additions & 7 deletions cl_dll/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "cl_util.h"
#include "parsemsg.h"
#include "parsetext.h"
#include "arraysize.h"
#if USE_VGUI
#include "vgui_int.h"
#include "vgui_TeamFortressViewport.h"
Expand Down Expand Up @@ -183,7 +184,7 @@ class CHLVoiceStatusHelper : public IVoiceStatusHelper
{
color[0] = color[1] = color[2] = 255;

if( entindex >= 0 && entindex < sizeof(g_PlayerExtraInfo)/sizeof(g_PlayerExtraInfo[0]) )
if( entindex >= 0 && entindex < ARRAYSIZE(g_PlayerExtraInfo) )
{
int iTeam = g_PlayerExtraInfo[entindex].teamnumber;

Expand Down Expand Up @@ -918,7 +919,7 @@ void CHud::ParseClientFeatures()

unsigned int i = 0;
bool shouldContinue = true;
for (i = 0; shouldContinue && i<sizeof(colors)/sizeof(colors[0]); ++i)
for (i = 0; shouldContinue && i<ARRAYSIZE(colors); ++i)
{
if (strcmp(keyName, colors[i].name) == 0)
{
Expand All @@ -927,7 +928,7 @@ void CHud::ParseClientFeatures()
break;
}
}
for (i = 0; shouldContinue && i<sizeof(integers)/sizeof(integers[0]); ++i)
for (i = 0; shouldContinue && i<ARRAYSIZE(integers); ++i)
{
if (strcmp(keyName, integers[i].name) == 0)
{
Expand All @@ -936,7 +937,7 @@ void CHud::ParseClientFeatures()
break;
}
}
for (i = 0; shouldContinue && i<sizeof(configurableBooleans)/sizeof(configurableBooleans[0]); ++i)
for (i = 0; shouldContinue && i<ARRAYSIZE(configurableBooleans); ++i)
{
if ((subKey = strStartsWith(keyName, configurableBooleans[i].name)))
{
Expand All @@ -945,7 +946,7 @@ void CHud::ParseClientFeatures()
break;
}
}
for (i = 0; shouldContinue && i<sizeof(configurableFloats)/sizeof(configurableFloats[0]); ++i)
for (i = 0; shouldContinue && i<ARRAYSIZE(configurableFloats); ++i)
{
if ((subKey = strStartsWith(keyName, configurableFloats[i].name)))
{
Expand All @@ -954,7 +955,7 @@ void CHud::ParseClientFeatures()
break;
}
}
for (i = 0; shouldContinue && i<sizeof(configurableBounds)/sizeof(configurableBounds[0]); ++i)
for (i = 0; shouldContinue && i<ARRAYSIZE(configurableBounds); ++i)
{
if ((subKey = strStartsWith(keyName, configurableBounds[i].name)))
{
Expand All @@ -963,7 +964,7 @@ void CHud::ParseClientFeatures()
break;
}
}
for (i = 0; shouldContinue && i<sizeof(booleans)/sizeof(booleans[0]); ++i)
for (i = 0; shouldContinue && i<ARRAYSIZE(booleans); ++i)
{
if (strcmp(keyName, booleans[i].name) == 0)
{
Expand Down
3 changes: 2 additions & 1 deletion cl_dll/hud_caption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "cl_util.h"
#include "parsemsg.h"
#include "parsetext.h"
#include "arraysize.h"

#include <algorithm>

Expand Down Expand Up @@ -118,7 +119,7 @@ int CHudCaption::MsgFunc_Caption(const char *pszName, int iSize, void *pbuf)

void CHudCaption::AddSubtitle(const Subtitle_t &sub)
{
if ((unsigned)sub_count < sizeof(subtitles)/sizeof(subtitles[0]))
if ((unsigned)sub_count < ARRAYSIZE(subtitles))
{
subtitles[sub_count] = sub;
sub_count++;
Expand Down
3 changes: 2 additions & 1 deletion cl_dll/hud_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "cl_util.h"
#include "parsemsg.h"
#include "r_efx.h"
#include "arraysize.h"

#define MAX_CLIENTS 32

Expand Down Expand Up @@ -159,7 +160,7 @@ int CHud::MsgFunc_WallPuffs(const char *pszName, int iSize, void *pbuf)
wallPuffs[3] = READ_SHORT();

wallPuffCount = 0;
for (int i=0; i<sizeof(wallPuffs)/sizeof(wallPuffs[0]); ++i)
for (int i=0; i<ARRAYSIZE(wallPuffs); ++i)
{
if (wallPuffs[i])
wallPuffCount++;
Expand Down
2 changes: 1 addition & 1 deletion cl_dll/input_goldsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// 02/21/97 JCB Added extended DirectInput code to support external controllers.

#include "input_mouse.h"
#include "arraysize.h"

#if SUPPORT_GOLDSOURCE_INPUT

Expand All @@ -28,7 +29,6 @@
#endif

#if USE_SDL2
#define ARRAYSIZE(p) ( sizeof(p) /sizeof(p[0]) )
#include <dlfcn.h>
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_gamecontroller.h>
Expand Down
3 changes: 2 additions & 1 deletion dlls/ammoregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define AMMO_LOG(...) ALERT(at_aiconsole, ##__VA_ARGS__ )
#define AMMO_ERROR(...) ALERT(at_error, ##__VA_ARGS__ )
#endif
#include "arraysize.h"


void AmmoType::SetName(const char *ammoName)
Expand Down Expand Up @@ -107,7 +108,7 @@ int AmmoRegistry::IndexOf(const char *name) const
{
if (!name)
return -1;
for (int i = 0; i<sizeof(ammoTypes)/sizeof(ammoTypes[0]); ++i)
for (int i = 0; i<ARRAYSIZE(ammoTypes); ++i)
{
if (!ammoTypes[i].IsValid())
continue;
Expand Down
2 changes: 1 addition & 1 deletion dlls/gonome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ const char *CDeadGonome::m_szPoses[] = { "dead_on_stomach1", "dead_on_back", "de

const char* CDeadGonome::getPos(int pos) const
{
return m_szPoses[pos % (sizeof(m_szPoses)/sizeof(const char*))];
return m_szPoses[pos % ARRAYSIZE(m_szPoses)];
}

LINK_ENTITY_TO_CLASS(monster_gonome_dead, CDeadGonome)
Expand Down
4 changes: 2 additions & 2 deletions dlls/maprules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ void CGamePlayerSettings::EquipPlayer(CBaseEntity *pPlayer)
SF_PLAYER_SETTINGS_SPORELAUNCHER,
SF_PLAYER_SETTINGS_MEDKIT
};
const int weaponIds[sizeof(weaponFlags)/sizeof(int)] = {
const int weaponIds[ARRAYSIZE(weaponFlags)] = {
WEAPON_CROWBAR,
WEAPON_GLOCK,
WEAPON_PYTHON,
Expand Down Expand Up @@ -1156,7 +1156,7 @@ void CGamePlayerSettings::EquipPlayer(CBaseEntity *pPlayer)
}
}

for (i=0; i<sizeof(weaponFlags)/sizeof(int); ++i)
for (i=0; i<ARRAYSIZE(weaponFlags); ++i)
{
if (pev->spawnflags & weaponFlags[i])
{
Expand Down
2 changes: 0 additions & 2 deletions dlls/saverestore.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ class CRestore : public CSaveRestoreBuffer

#define MAX_ENTITYARRAY 64

//#define ARRAYSIZE(p) (sizeof(p)/sizeof(p[0]))

#define IMPLEMENT_SAVERESTORE(derivedClass,baseClass) \
int derivedClass::Save( CSave &save )\
{\
Expand Down
2 changes: 1 addition & 1 deletion dlls/zombie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ const char *CDeadZombieSoldier::m_szPoses[] = { "dead_on_back", "dead_on_stomach

const char* CDeadZombieSoldier::getPos(int pos) const
{
return m_szPoses[pos % (sizeof(m_szPoses)/sizeof(const char*))];
return m_szPoses[pos % ARRAYSIZE(m_szPoses)];
}

LINK_ENTITY_TO_CLASS( monster_zombie_soldier_dead, CDeadZombieSoldier )
Expand Down
5 changes: 1 addition & 4 deletions engine/eiface.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdio.h>
#include "custom.h"
#include "cvardef.h"
#include "arraysize.h"
//
// Defines entity interface between engine and DLLs.
// This header file included by engine files and DLL files.
Expand Down Expand Up @@ -383,10 +384,6 @@ typedef struct
short flags;
} TYPEDESCRIPTION;

#if !defined(ARRAYSIZE)
#define ARRAYSIZE(p) (sizeof(p)/sizeof(p[0]))
#endif

typedef struct
{
// Initialize/shutdown the game (one-time call after loading of game .dll )
Expand Down
13 changes: 13 additions & 0 deletions game_shared/arraysize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#ifndef ARRAYSIZE_H
#define ARRAYSIZE_H

#include <cstddef>

template <typename T, size_t N>
constexpr size_t ARRAYSIZE(T (&arr)[N]) noexcept
{
return N;
}

#endif

0 comments on commit 449ad49

Please sign in to comment.