Bläddra i källkod

Make some non-const static variables const or constexpr

Ivan Savenko 1 år sedan
förälder
incheckning
6e399eb21a

+ 1 - 1
client/gui/CGuiHandler.cpp

@@ -191,7 +191,7 @@ void CGuiHandler::drawFPSCounter()
 	int y = screen->h-20;
 	int width3digitFPSIncludingPadding = 48;
 	int heightFPSTextIncludingPadding = 11;
-	static SDL_Rect overlay = { x, y, width3digitFPSIncludingPadding, heightFPSTextIncludingPadding};
+	SDL_Rect overlay = { x, y, width3digitFPSIncludingPadding, heightFPSTextIncludingPadding};
 	uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
 	SDL_FillRect(screen, &overlay, black);
 

+ 1 - 1
client/lobby/CBonusSelection.cpp

@@ -137,7 +137,7 @@ void CBonusSelection::createBonusesIcons()
 	const std::vector<CampaignBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
 	groupBonuses = std::make_shared<CToggleGroup>(std::bind(&IServerAPI::setCampaignBonus, CSH, _1));
 
-	static const char * bonusPics[] =
+	constexpr std::array bonusPics =
 	{
 		"SPELLBON.DEF",	// Spell
 		"TWCRPORT.DEF", // Monster

+ 1 - 1
client/lobby/CSelectionBase.cpp

@@ -154,7 +154,7 @@ InfoCard::InfoCard()
 
 		iconDifficulty = std::make_shared<CToggleGroup>(0);
 		{
-			static const char * difButns[] = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
+			constexpr std::array difButns = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
 
 			for(int i = 0; i < 5; i++)
 			{

+ 2 - 2
client/mainmenu/CMainMenu.cpp

@@ -278,9 +278,9 @@ CMainMenuConfig::CMainMenuConfig()
 
 }
 
-CMainMenuConfig & CMainMenuConfig::get()
+const CMainMenuConfig & CMainMenuConfig::get()
 {
-	static CMainMenuConfig config;
+	static const CMainMenuConfig config;
 	return config;
 }
 

+ 1 - 1
client/mainmenu/CMainMenu.h

@@ -125,7 +125,7 @@ public:
 class CMainMenuConfig
 {
 public:
-	static CMainMenuConfig & get();
+	static const CMainMenuConfig & get();
 	const JsonNode & getConfig() const;
 	const JsonNode & getCampaigns() const;
 

+ 4 - 4
client/render/CBitmapHandler.cpp

@@ -157,11 +157,11 @@ SDL_Surface * BitmapHandler::loadBitmapFromDir(const ImagePath & path)
 		ret->format->palette->colors[0].g ==   0 &&
 		ret->format->palette->colors[0].b == 255 )
 	{
-		static SDL_Color shadow[3] =
+		constexpr std::array shadow =
 		{
-			{   0,   0,   0,   0},// 100% - transparency
-			{   0,   0,   0,  32},//  75% - shadow border,
-			{   0,   0,   0, 128},//  50% - shadow body
+			SDL_Color{   0,   0,   0,   0},// 100% - transparency
+			SDL_Color{   0,   0,   0,  32},//  75% - shadow border,
+			SDL_Color{   0,   0,   0, 128},//  50% - shadow body
 		};
 
 		CSDL_Ext::setColorKey(ret, ret->format->palette->colors[0]);

+ 2 - 2
client/windows/CCastleInterface.cpp

@@ -1427,11 +1427,11 @@ CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance *
 
 	state = LOCPLINT->cb->canBuildStructure(town, building->bid);
 
-	static int panelIndex[12] =
+	constexpr std::array panelIndex =
 	{
 		3, 3, 3, 0, 0, 2, 2, 1, 2, 2,  3,  3
 	};
-	static int iconIndex[12] =
+	constexpr std::array iconIndex =
 	{
 		-1, -1, -1, 0, 0, 1, 2, -1, 1, 1, -1, -1
 	};

+ 1 - 1
client/windows/CPuzzleWindow.cpp

@@ -76,7 +76,7 @@ void CPuzzleWindow::showAll(Canvas & to)
 
 void CPuzzleWindow::show(Canvas & to)
 {
-	static int animSpeed = 2;
+	constexpr int animSpeed = 2;
 
 	if(currentAlpha < animSpeed)
 	{

+ 1 - 1
client/windows/GUIClasses.cpp

@@ -1558,7 +1558,7 @@ CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
 
 	//data for information table:
 	// fields[row][column] = list of id's of players for this box
-	static std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
+	constexpr std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
 		{ &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes,       &SThievesGuildInfo::gold,
 		  &SThievesGuildInfo::woodOre,    &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
 		  &SThievesGuildInfo::artifacts,  &SThievesGuildInfo::army,              &SThievesGuildInfo::income };

+ 1 - 3
launcher/modManager/cdownloadmanager_moc.cpp

@@ -54,9 +54,7 @@ CDownloadManager::FileEntry & CDownloadManager::getEntry(QNetworkReply * reply)
 		if(entry.reply == reply)
 			return entry;
 	}
-	assert(0);
-	static FileEntry errorValue;
-	return errorValue;
+	throw std::runtime_error("Failed to find download entry");
 }
 
 void CDownloadManager::downloadFinished(QNetworkReply * reply)

+ 1 - 1
launcher/modManager/cmodlistmodel_moc.cpp

@@ -38,7 +38,7 @@ QString CModListModel::modIndexToName(const QModelIndex & index) const
 
 QString CModListModel::modTypeName(QString modTypeID) const
 {
-	static QMap<QString, QString> modTypes = {
+	static const QMap<QString, QString> modTypes = {
 		{"Translation", tr("Translation")},
 		{"Town",        tr("Town")       },
 		{"Test",        tr("Test")       },

+ 1 - 1
lib/CGeneralTextHandler.cpp

@@ -502,7 +502,7 @@ CGeneralTextHandler::CGeneralTextHandler():
 	readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
 	readToVector("core.xtrainfo", "DATA/XTRAINFO.TXT" );
 
-	static const char * QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
+	static const std::string QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
 	if (CResourceHandler::get()->existsResource(TextPath::builtin(QE_MOD_COMMANDS)))
 		readToVector("vcmi.quickExchange", QE_MOD_COMMANDS);
 

+ 1 - 1
lib/JsonDetail.cpp

@@ -1259,7 +1259,7 @@ namespace Validation
 
 	const TFormatMap & getKnownFormats()
 	{
-		static TFormatMap knownFormats = createFormatMap();
+		static const TFormatMap knownFormats = createFormatMap();
 		return knownFormats;
 	}
 

+ 1 - 1
lib/battle/BattleInfo.cpp

@@ -443,7 +443,7 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const
 	}
 
 	//native terrain bonuses
-	static auto nativeTerrain = std::make_shared<CreatureTerrainLimiter>();
+	auto nativeTerrain = std::make_shared<CreatureTerrainLimiter>();
 	
 	curB->addNewBonus(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::STACKS_SPEED, BonusSource::TERRAIN_NATIVE, 1,  BonusSourceID())->addLimiter(nativeTerrain));
 	curB->addNewBonus(std::make_shared<Bonus>(BonusDuration::ONE_BATTLE, BonusType::PRIMARY_SKILL, BonusSource::TERRAIN_NATIVE, 1, BonusSourceID(), BonusSubtypeID(PrimarySkill::ATTACK))->addLimiter(nativeTerrain));

+ 1 - 1
mapeditor/Animation.cpp

@@ -161,7 +161,7 @@ DefFile::DefFile(std::string Name):
 	#endif // 0
 
 	//First 8 colors in def palette used for transparency
-	static QRgb H3Palette[8] =
+	constexpr std::array H3Palette =
 	{
 		qRgba(0, 0, 0,   0), // 100% - transparency
 		qRgba(0, 0, 0,  32), //  75% - shadow border,