1
0
forked from 0ad/0ad

Some range-based for loops and some style cleanup.

This was SVN commit r16888.
This commit is contained in:
leper 2015-07-29 01:07:23 +00:00
parent b4d517c261
commit a07add11c6
8 changed files with 105 additions and 121 deletions

View File

@ -44,11 +44,10 @@ CObjectEntry::CObjectEntry(CObjectBase* base, CSimulation2& simulation) :
{
}
template<typename T, typename S> static void delete_pair_2nd(std::pair<T,S> v) { delete v.second; }
CObjectEntry::~CObjectEntry()
{
std::for_each(m_Animations.begin(), m_Animations.end(), delete_pair_2nd<CStr, CSkeletonAnim*>);
for (const std::pair<CStr, CSkeletonAnim*>& anim : m_Animations)
delete anim.second;
delete m_Model;
}

View File

@ -18,13 +18,14 @@
#include "precompiled.h"
#include "CLogger.h"
#include "CConsole.h"
#include "graphics/FontMetrics.h"
#include "graphics/ShaderManager.h"
#include "graphics/TextRenderer.h"
#include "lib/ogl.h"
#include "lib/timer.h"
#include "lib/utf8.h"
#include "ps/CConsole.h"
#include "ps/Profile.h"
#include "renderer/Renderer.h"
@ -223,15 +224,15 @@ void CLogger::Render()
// and attempt to lock the mutex recursively which is forbidden)
CScopeLock lock(m_Mutex);
for (std::deque<RenderedMessage>::iterator it = m_RenderMessages.begin(); it != m_RenderMessages.end(); ++it)
for (const RenderedMessage& msg : m_RenderMessages)
{
const char* type;
if (it->method == Normal)
if (msg.method == Normal)
{
type = "info";
textRenderer.Color(0.0f, 0.8f, 0.0f);
}
else if (it->method == Warning)
else if (msg.method == Warning)
{
type = "warning";
textRenderer.Color(1.0f, 1.0f, 0.0f);
@ -244,10 +245,10 @@ void CLogger::Render()
CMatrix3D savedTransform = textRenderer.GetTransform();
textRenderer.PrintfAdvance(L"[%8.3f] %hs: ", it->time, type);
textRenderer.PrintfAdvance(L"[%8.3f] %hs: ", msg.time, type);
// Display the actual message in white so it's more readable
textRenderer.Color(1.0f, 1.0f, 1.0f);
textRenderer.Put(0.0f, 0.0f, it->message.c_str());
textRenderer.Put(0.0f, 0.0f, msg.message.c_str());
textRenderer.SetTransform(savedTransform);

View File

@ -17,13 +17,14 @@
#include "precompiled.h"
#include "ConfigDB.h"
#include <boost/algorithm/string.hpp>
#include "CLogger.h"
#include "ConfigDB.h"
#include "Filesystem.h"
#include "ThreadUtil.h"
#include "lib/allocators/shared_ptr.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/ThreadUtil.h"
typedef std::map<CStr, CConfigValueSet> TConfigMap;
TConfigMap CConfigDB::m_Map[CFG_LAST];
@ -96,7 +97,7 @@ std::string EscapeString(const CStr& str)
Get(it->second[0], value);\
return;\
}\
for (int search_ns = ns; search_ns >= 0; search_ns--)\
for (int search_ns = ns; search_ns >= 0; --search_ns)\
{\
it = m_Map[search_ns].find(name);\
if (it != m_Map[search_ns].end())\
@ -113,7 +114,7 @@ GETVAL(double)
GETVAL(std::string)
#undef GETVAL
void CConfigDB::GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values)
void CConfigDB::GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const
{
CHECK_NS(;);
@ -125,7 +126,7 @@ void CConfigDB::GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet
return;
}
for (int search_ns = ns; search_ns >= 0; search_ns--)
for (int search_ns = ns; search_ns >= 0; --search_ns)
{
it = m_Map[search_ns].find(name);
if (it != m_Map[search_ns].end())
@ -136,7 +137,7 @@ void CConfigDB::GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet
}
}
EConfigNamespace CConfigDB::GetValueNamespace(EConfigNamespace ns, const CStr& name)
EConfigNamespace CConfigDB::GetValueNamespace(EConfigNamespace ns, const CStr& name) const
{
CHECK_NS(CFG_LAST);
@ -145,7 +146,7 @@ EConfigNamespace CConfigDB::GetValueNamespace(EConfigNamespace ns, const CStr& n
if (it != m_Map[CFG_COMMAND].end())
return CFG_COMMAND;
for (int search_ns = ns; search_ns >= 0; search_ns--)
for (int search_ns = ns; search_ns >= 0; --search_ns)
{
it = m_Map[search_ns].find(name);
if (it != m_Map[search_ns].end())
@ -155,7 +156,7 @@ EConfigNamespace CConfigDB::GetValueNamespace(EConfigNamespace ns, const CStr& n
return CFG_LAST;
}
std::map<CStr, CConfigValueSet> CConfigDB::GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix)
std::map<CStr, CConfigValueSet> CConfigDB::GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const
{
CScopeLock s(&cfgdb_mutex);
std::map<CStr, CConfigValueSet> ret;
@ -164,20 +165,14 @@ std::map<CStr, CConfigValueSet> CConfigDB::GetValuesWithPrefix(EConfigNamespace
// Loop upwards so that values in later namespaces can override
// values in earlier namespaces
for (int search_ns = 0; search_ns <= ns; search_ns++)
{
for (TConfigMap::iterator it = m_Map[search_ns].begin(); it != m_Map[search_ns].end(); ++it)
{
if (boost::algorithm::starts_with(it->first, prefix))
ret[it->first] = it->second;
}
}
for (int search_ns = 0; search_ns <= ns; ++search_ns)
for (const std::pair<CStr, CConfigValueSet>& p : m_Map[search_ns])
if (boost::algorithm::starts_with(p.first, prefix))
ret[p.first] = p.second;
for (TConfigMap::iterator it = m_Map[CFG_COMMAND].begin(); it != m_Map[CFG_COMMAND].end(); ++it)
{
if (boost::algorithm::starts_with(it->first, prefix))
ret[it->first] = it->second;
}
for (const std::pair<CStr, CConfigValueSet>& p : m_Map[CFG_COMMAND])
if (boost::algorithm::starts_with(p.first, prefix))
ret[p.first] = p.second;
return ret;
}
@ -365,7 +360,7 @@ bool CConfigDB::Reload(EConfigNamespace ns)
return true;
}
bool CConfigDB::WriteFile(EConfigNamespace ns)
bool CConfigDB::WriteFile(EConfigNamespace ns) const
{
CHECK_NS(false);
@ -373,7 +368,7 @@ bool CConfigDB::WriteFile(EConfigNamespace ns)
return WriteFile(ns, m_ConfigFile[ns]);
}
bool CConfigDB::WriteFile(EConfigNamespace ns, const VfsPath& path)
bool CConfigDB::WriteFile(EConfigNamespace ns, const VfsPath& path) const
{
CHECK_NS(false);
@ -381,14 +376,13 @@ bool CConfigDB::WriteFile(EConfigNamespace ns, const VfsPath& path)
shared_ptr<u8> buf;
AllocateAligned(buf, 1*MiB, maxSectorSize);
char* pos = (char*)buf.get();
TConfigMap &map = m_Map[ns];
for (TConfigMap::const_iterator it = map.begin(); it != map.end(); ++it)
for (const std::pair<CStr, CConfigValueSet>& p : m_Map[ns])
{
size_t i;
pos += sprintf(pos, "%s = ", it->first.c_str());
for (i = 0; i < it->second.size() - 1; ++i)
pos += sprintf(pos, "\"%s\", ", EscapeString(it->second[i]).c_str());
pos += sprintf(pos, "\"%s\"\n", EscapeString(it->second[i]).c_str());
pos += sprintf(pos, "%s = ", p.first.c_str());
for (i = 0; i < p.second.size() - 1; ++i)
pos += sprintf(pos, "\"%s\", ", EscapeString(p.second[i]).c_str());
pos += sprintf(pos, "\"%s\"\n", EscapeString(p.second[i]).c_str());
}
const size_t len = pos - (char*)buf.get();

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games.
/* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -27,10 +27,9 @@
#ifndef INCLUDED_CONFIGDB
#define INCLUDED_CONFIGDB
#include "CStr.h"
#include "Singleton.h"
#include "lib/file/vfs/vfs_path.h"
#include "ps/CStr.h"
#include "ps/Singleton.h"
// Namespace priorities: User supersedes mod supersedes system.
// Command-line arguments override everything.
@ -77,20 +76,20 @@ public:
* will search CFG_COMMAND first, and then all namespaces from the specified
* namespace down.
*/
void GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values);
void GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const;
/**
* Returns the namespace that the value returned by GetValues was defined in,
* or CFG_LAST if it wasn't defined at all.
*/
EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr& name);
EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr& name) const;
/**
* Retrieve a map of values corresponding to settings whose names begin
* with the given prefix;
* will search all namespaces from default up to the specified namespace.
*/
std::map<CStr, CConfigValueSet> GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix);
std::map<CStr, CConfigValueSet> GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const;
/**
* Save a config value in the specified namespace. If the config variable
@ -125,7 +124,7 @@ public:
* true: if the config namespace was successfully written to the file
* false: if an error occurred
*/
bool WriteFile(EConfigNamespace ns, const VfsPath& path);
bool WriteFile(EConfigNamespace ns, const VfsPath& path) const;
/**
* Write the current state of the specified config namespace to the file
@ -135,7 +134,7 @@ public:
* true: if the config namespace was successfully written to the file
* false: if an error occurred
*/
bool WriteFile(EConfigNamespace ns);
bool WriteFile(EConfigNamespace ns) const;
};
@ -144,4 +143,4 @@ public:
#define CFG_GET_VAL(name, destination)\
g_ConfigDB.GetValue(CFG_USER, name, destination)
#endif
#endif // INCLUDED_CONFIGDB

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Wildfire Games.
/* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -21,12 +21,12 @@
#include <boost/tokenizer.hpp>
#include "lib/input.h"
#include "ConfigDB.h"
#include "CLogger.h"
#include "CConsole.h"
#include "CStr.h"
#include "ps/CConsole.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "ps/ConfigDB.h"
#include "ps/Globals.h"
#include "KeyName.h"
#include "ps/KeyName.h"
static bool unified[UNIFIED_LAST - UNIFIED_SHIFT];
@ -64,13 +64,11 @@ std::map<std::string, bool> g_HotkeyStatus;
// all key combinations that trigger it.
static void LoadConfigBindings()
{
std::map<CStr, CConfigValueSet> bindings = g_ConfigDB.GetValuesWithPrefix(CFG_COMMAND, "hotkey.");
for (std::map<CStr, CConfigValueSet>::iterator bindingsIt = bindings.begin(); bindingsIt != bindings.end(); ++bindingsIt)
for (const std::pair<CStr, CConfigValueSet>& configPair : g_ConfigDB.GetValuesWithPrefix(CFG_COMMAND, "hotkey."))
{
std::string hotkeyName = bindingsIt->first.substr(7); // strip the "hotkey." prefix
for (CConfigValueSet::iterator it = bindingsIt->second.begin(); it != bindingsIt->second.end(); ++it)
std::string hotkeyName = configPair.first.substr(7); // strip the "hotkey." prefix
for (const CStr& hotkey : configPair.second)
{
const CStr& hotkey = *it;
std::vector<SKey> keyCombination;
// Iterate through multiple-key bindings (e.g. Ctrl+I)
@ -118,25 +116,21 @@ void LoadHotkeys()
// Set up the state of the hotkeys given no key is down.
// i.e. find those hotkeys triggered by all negations.
for( std::map<int, KeyMapping>::iterator mapIt = g_HotkeyMap.begin(); mapIt != g_HotkeyMap.end(); ++mapIt )
{
KeyMapping& hotkeyMap = mapIt->second;
for( std::vector<SHotkeyMapping>::iterator it = hotkeyMap.begin(); it != hotkeyMap.end(); ++it )
for (const std::pair<int, KeyMapping>& p : g_HotkeyMap)
for (const SHotkeyMapping& hotkey : p.second)
{
if( !it->negated )
if (!hotkey.negated)
continue;
bool allNegated = true;
for( std::vector<SKey>::iterator j = it->requires.begin(); j != it->requires.end(); ++j )
if( !j->negated )
for (const SKey& k : hotkey.requires)
if (!k.negated)
allNegated = false;
if( allNegated )
g_HotkeyStatus[it->name] = true;
if (allNegated)
g_HotkeyStatus[hotkey.name] = true;
}
}
}
void UnloadHotkeys()
@ -160,11 +154,11 @@ bool isNegated(const SKey& key)
return true;
}
InReaction HotkeyInputHandler( const SDL_Event_* ev )
InReaction HotkeyInputHandler(const SDL_Event_* ev)
{
int keycode = 0;
switch( ev->ev.type )
switch(ev->ev.type)
{
case SDL_KEYDOWN:
case SDL_KEYUP:
@ -232,46 +226,46 @@ InReaction HotkeyInputHandler( const SDL_Event_* ev )
// Just send them to this handler; don't let the imaginary event codes leak back to real SDL.
SDL_Event_ phantom;
phantom.ev.type = ( ( ev->ev.type == SDL_KEYDOWN ) || ( ev->ev.type == SDL_MOUSEBUTTONDOWN ) ) ? SDL_KEYDOWN : SDL_KEYUP;
if( ( keycode == SDLK_LSHIFT ) || ( keycode == SDLK_RSHIFT ) )
phantom.ev.type = ((ev->ev.type == SDL_KEYDOWN) || (ev->ev.type == SDL_MOUSEBUTTONDOWN)) ? SDL_KEYDOWN : SDL_KEYUP;
if ((keycode == SDLK_LSHIFT) || (keycode == SDLK_RSHIFT))
{
phantom.ev.key.keysym.sym = (SDLKEY)UNIFIED_SHIFT;
unified[0] = ( phantom.ev.type == SDL_KEYDOWN );
HotkeyInputHandler( &phantom );
unified[0] = (phantom.ev.type == SDL_KEYDOWN);
HotkeyInputHandler(&phantom);
}
else if( ( keycode == SDLK_LCTRL ) || ( keycode == SDLK_RCTRL ) )
else if ((keycode == SDLK_LCTRL) || (keycode == SDLK_RCTRL))
{
phantom.ev.key.keysym.sym = (SDLKEY)UNIFIED_CTRL;
unified[1] = ( phantom.ev.type == SDL_KEYDOWN );
HotkeyInputHandler( &phantom );
unified[1] = (phantom.ev.type == SDL_KEYDOWN);
HotkeyInputHandler(&phantom);
}
else if( ( keycode == SDLK_LALT ) || ( keycode == SDLK_RALT ) )
else if ((keycode == SDLK_LALT) || (keycode == SDLK_RALT))
{
phantom.ev.key.keysym.sym = (SDLKEY)UNIFIED_ALT;
unified[2] = ( phantom.ev.type == SDL_KEYDOWN );
HotkeyInputHandler( &phantom );
unified[2] = (phantom.ev.type == SDL_KEYDOWN);
HotkeyInputHandler(&phantom);
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
else if( ( keycode == SDLK_LGUI ) || ( keycode == SDLK_RGUI ) )
else if ((keycode == SDLK_LGUI) || (keycode == SDLK_RGUI))
#else // SDL 1.2
else if( ( keycode == SDLK_LSUPER ) || ( keycode == SDLK_RSUPER ) || ( keycode == SDLK_LMETA ) || ( keycode == SDLK_RMETA) )
else if ((keycode == SDLK_LSUPER) || (keycode == SDLK_RSUPER) || (keycode == SDLK_LMETA) || (keycode == SDLK_RMETA))
#endif
{
phantom.ev.key.keysym.sym = (SDLKEY)UNIFIED_SUPER;
unified[3] = ( phantom.ev.type == SDL_KEYDOWN );
HotkeyInputHandler( &phantom );
unified[3] = (phantom.ev.type == SDL_KEYDOWN);
HotkeyInputHandler(&phantom);
}
// Check whether we have any hotkeys registered for this particular keycode
if( g_HotkeyMap.find(keycode) == g_HotkeyMap.end() )
return( IN_PASS );
if (g_HotkeyMap.find(keycode) == g_HotkeyMap.end())
return (IN_PASS);
// Inhibit the dispatch of hotkey events caused by real keys (not fake mouse button
// events) while the console is up.
bool consoleCapture = false;
if( g_Console->IsActive() && keycode < CUSTOM_SDL_KEYCODE )
if (g_Console->IsActive() && keycode < CUSTOM_SDL_KEYCODE)
consoleCapture = true;
// Here's an interesting bit:
@ -295,32 +289,32 @@ InReaction HotkeyInputHandler( const SDL_Event_* ev )
std::vector<const char*> closestMapNames;
size_t closestMapMatch = 0;
for (std::vector<SHotkeyMapping>::iterator it = g_HotkeyMap[keycode].begin(); it < g_HotkeyMap[keycode].end(); ++it)
for (const SHotkeyMapping& hotkey : g_HotkeyMap[keycode])
{
// If a key has been pressed, and this event triggers on its release, skip it.
// Similarly, if the key's been released and the event triggers on a keypress, skip it.
if (it->negated == typeKeyDown)
if (hotkey.negated == typeKeyDown)
continue;
// Check for no unpermitted keys
bool accept = true;
for (std::vector<SKey>::iterator itKey = it->requires.begin(); itKey != it->requires.end() && accept; ++itKey)
accept = isNegated(*itKey);
for (const SKey& k : hotkey.requires)
accept = isNegated(k);
if (accept && !(consoleCapture && it->name != "console.toggle"))
if (accept && !(consoleCapture && hotkey.name != "console.toggle"))
{
// Check if this is an equally precise or more precise match
if (it->requires.size() + 1 >= closestMapMatch)
if (hotkey.requires.size() + 1 >= closestMapMatch)
{
// Check if more precise
if (it->requires.size() + 1 > closestMapMatch)
if (hotkey.requires.size() + 1 > closestMapMatch)
{
// Throw away the old less-precise matches
closestMapNames.clear();
closestMapMatch = it->requires.size() + 1;
closestMapMatch = hotkey.requires.size() + 1;
}
closestMapNames.push_back(it->name.c_str());
closestMapNames.push_back(hotkey.name.c_str());
}
}
}
@ -335,25 +329,25 @@ InReaction HotkeyInputHandler( const SDL_Event_* ev )
// -- KEYUP SECTION --
for (std::vector<SHotkeyMapping>::iterator it = g_HotkeyMap[keycode].begin(); it < g_HotkeyMap[keycode].end(); ++it)
for (const SHotkeyMapping& hotkey : g_HotkeyMap[keycode])
{
// If it's a keydown event, won't cause HotKeyUps in anything that doesn't
// use this key negated => skip them
// If it's a keyup event, won't cause HotKeyUps in anything that does use
// this key negated => skip them too.
if (it->negated != typeKeyDown)
if (hotkey.negated != typeKeyDown)
continue;
// Check for no unpermitted keys
bool accept = true;
for (std::vector<SKey>::iterator itKey = it->requires.begin(); itKey != it->requires.end() && accept; ++itKey)
accept = isNegated(*itKey);
for (const SKey& k : hotkey.requires)
accept = isNegated(k);
if (accept)
{
SDL_Event_ hotkeyNotification;
hotkeyNotification.ev.type = SDL_HOTKEYUP;
hotkeyNotification.ev.user.data1 = const_cast<char*>(it->name.c_str());
hotkeyNotification.ev.user.data1 = const_cast<char*>(hotkey.name.c_str());
in_push_priority_event(&hotkeyNotification);
}
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -43,6 +43,8 @@ extern int g_xres, g_yres;
struct CProfileViewerInternals
{
NONCOPYABLE(CProfileViewerInternals); // because of the ofstream
public:
CProfileViewerInternals() {}
/// Whether the profiling display is currently visible
@ -60,11 +62,6 @@ struct CProfileViewerInternals
/// File for saved profile output (reset when the game is restarted)
std::ofstream outputStream;
private:
// Cannot be copied/assigned, because of the ofstream
CProfileViewerInternals(const CProfileViewerInternals& rhs);
const CProfileViewerInternals& operator=(const CProfileViewerInternals& rhs);
};

View File

@ -392,24 +392,24 @@ PSRETURN CXeromyces::CreateXMB(const xmlDocPtr doc, WriteBuffer& writeBuffer)
i = 0;
u32 elementCount = (u32)elementNames.size();
writeBuffer.Append(&elementCount, 4);
for (it = elementNames.begin(); it != elementNames.end(); ++it)
for (const std::string& n : elementNames)
{
u32 textLen = (u32)it->length()+1;
u32 textLen = (u32)n.length()+1;
writeBuffer.Append(&textLen, 4);
writeBuffer.Append((void*)it->c_str(), textLen);
elementIDs[*it] = i++;
writeBuffer.Append((void*)n.c_str(), textLen);
elementIDs[n] = i++;
}
// Output attribute names
i = 0;
u32 attributeCount = (u32)attributeNames.size();
writeBuffer.Append(&attributeCount, 4);
for (it = attributeNames.begin(); it != attributeNames.end(); ++it)
for (const std::string& n : attributeNames)
{
u32 textLen = (u32)it->length()+1;
u32 textLen = (u32)n.length()+1;
writeBuffer.Append(&textLen, 4);
writeBuffer.Append((void*)it->c_str(), textLen);
attributeIDs[*it] = i++;
writeBuffer.Append((void*)n.c_str(), textLen);
attributeIDs[n] = i++;
}
OutputElement(xmlDocGetRootElement(doc), writeBuffer, elementIDs, attributeIDs);

View File

@ -276,8 +276,8 @@ CSoundManager::~CSoundManager()
}
AL_CHECK;
for (std::map<std::wstring, CSoundGroup*>::iterator it = m_SoundGroups.begin(); it != m_SoundGroups.end(); ++it)
delete it->second;
for (const std::pair<std::wstring, CSoundGroup*>& p : m_SoundGroups)
delete p.second;
m_SoundGroups.clear();
if (m_PlayListItems)