Changeset - r9582:cb7711765a81
[Not reviewed]
master
0 3 0
smatz - 16 years ago 2008-06-24 09:15:45
smatz@openttd.org
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
Removes indirect dependency on <string> for 20 files, reduces binary size by 16kB
3 files changed with 33 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/blitter/factory.hpp
Show inline comments
 
@@ -5,22 +5,29 @@
 
#ifndef BLITTER_FACTORY_HPP
 
#define BLITTER_FACTORY_HPP
 

	
 
#include "base.hpp"
 
#include "../debug.h"
 
#include "../string_func.h"
 
#include <string>
 
#include <map>
 

	
 
/**
 
 * The base factory, keeping track of all blitters.
 
 */
 
class BlitterFactoryBase {
 
private:
 
	char *name;
 
	typedef std::map<std::string, BlitterFactoryBase *> Blitters;
 
	const char *name;
 

	
 
	struct StringCompare {
 
		bool operator () (const char *a, const char *b) const
 
		{
 
			return strcmp(a, b) < 0;
 
		}
 
	};
 

	
 
	typedef std::map<const char *, BlitterFactoryBase *, StringCompare> Blitters;
 

	
 
	static Blitters &GetBlitters()
 
	{
 
		static Blitters &s_blitters = *new Blitters();
 
		return s_blitters;
 
	}
 
@@ -55,13 +62,13 @@ public:
 

	
 
	virtual ~BlitterFactoryBase()
 
	{
 
		if (this->name == NULL) return;
 
		GetBlitters().erase(this->name);
 
		if (GetBlitters().empty()) delete &GetBlitters();
 
		free(this->name);
 
		free((void *)this->name);
 
	}
 

	
 
	/**
 
	 * Find the requested blitter and return his class.
 
	 * @param name the blitter to select.
 
	 * @post Sets the blitter so GetCurrentBlitter() returns it too.
src/driver.cpp
Show inline comments
 
@@ -153,13 +153,15 @@ void DriverFactoryBase::RegisterDriver(c
 

	
 
	/* Prefix the name with driver type to make it unique */
 
	char buf[32];
 
	strecpy(buf, GetDriverTypeName(type), lastof(buf));
 
	strecpy(buf + 5, name, lastof(buf));
 

	
 
	std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(buf, this));
 
	const char *longname = strdup(buf);
 

	
 
	std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(longname, this));
 
	assert(P.second);
 
}
 

	
 
/**
 
 * Build a human readable list of available drivers, grouped by type.
 
 */
 
@@ -191,10 +193,17 @@ DriverFactoryBase::~DriverFactoryBase() 
 

	
 
	/* Prefix the name with driver type to make it unique */
 
	char buf[32];
 
	strecpy(buf, GetDriverTypeName(type), lastof(buf));
 
	strecpy(buf + 5, this->name, lastof(buf));
 

	
 
	GetDrivers().erase(buf);
 
	Drivers::iterator it = GetDrivers().find(buf);
 
	assert(it != GetDrivers().end());
 

	
 
	const char *longname = (*it).first;
 

	
 
	GetDrivers().erase(it);
 
	free((void *)longname);
 

	
 
	if (GetDrivers().empty()) delete &GetDrivers();
 
	free(this->name);
 
	free((void *)this->name);
 
}
src/driver.h
Show inline comments
 
@@ -5,13 +5,12 @@
 
#ifndef DRIVER_H
 
#define DRIVER_H
 

	
 
#include "debug.h"
 
#include "core/enum_type.hpp"
 
#include "string_func.h"
 
#include <string>
 
#include <map>
 

	
 
bool GetDriverParamBool(const char * const *parm, const char *name);
 
int GetDriverParamInt(const char * const *parm, const char *name, int def);
 

	
 
class Driver {
 
@@ -34,15 +33,23 @@ public:
 
DECLARE_POSTFIX_INCREMENT(Driver::Type);
 

	
 

	
 
class DriverFactoryBase {
 
private:
 
	Driver::Type type;
 
	char *name;
 
	const char *name;
 
	int priority;
 
	typedef std::map<std::string, DriverFactoryBase *> Drivers;
 

	
 
	struct StringCompare {
 
		bool operator () (const char *a, const char *b) const
 
		{
 
			return strcmp(a, b) < 0;
 
		}
 
	};
 

	
 
	typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers;
 

	
 
	static Drivers &GetDrivers()
 
	{
 
		static Drivers &s_drivers = *new Drivers();
 
		return s_drivers;
 
	}
0 comments (0 inline, 0 general)