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
 
/* $Id$ */
 

	
 
/** @file factory.hpp Factory to 'query' all available blitters. */
 

	
 
#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;
 
	}
 

	
 
	static Blitter **GetActiveBlitter()
 
	{
 
		static Blitter *s_blitter = NULL;
 
		return &s_blitter;
 
	}
 

	
 
protected:
 
	/**
 
	 * Register a blitter internally, based on his name.
 
	 * @param name the name of the blitter.
 
	 * @note an assert() will be trigger if 2 blitters with the same name try to register.
 
	 */
 
	void RegisterBlitter(const char *name)
 
	{
 
		/* Don't register nameless Blitters */
 
		if (name == NULL) return;
 

	
 
		this->name = strdup(name);
 

	
 
		std::pair<Blitters::iterator, bool> P = GetBlitters().insert(Blitters::value_type(name, this));
 
		assert(P.second);
 
	}
 

	
 
public:
 
	BlitterFactoryBase() :
 
		name(NULL)
 
	{}
 

	
 
	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.
 
	 */
 
	static Blitter *SelectBlitter(const char *name)
 
	{
 
		const char *default_blitter = "8bpp-optimized";
 

	
 
#if defined(__APPLE__)
 
		/* MacOS X 10.5 removed 8bpp fullscreen support.
 
		 * Because of this we will pick 32bpp by default */
 
		if (MacOSVersionIsAtLeast(10, 5, 0)) {
 
			default_blitter = "32bpp-anim";
 
		}
 
#endif /* defined(__APPLE__) */
 
		if (GetBlitters().size() == 0) return NULL;
 
		const char *bname = (StrEmpty(name)) ? default_blitter : name;
 

	
 
		Blitters::iterator it = GetBlitters().begin();
 
		for (; it != GetBlitters().end(); it++) {
 
			BlitterFactoryBase *b = (*it).second;
src/driver.cpp
Show inline comments
 
@@ -135,66 +135,75 @@ const Driver *DriverFactoryBase::SelectD
 
		}
 
		usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer);
 
	}
 
}
 

	
 
/**
 
 * Register a driver internally, based on its name.
 
 * @param name the name of the driver.
 
 * @note an assert() will be trigger if 2 driver with the same name try to register.
 
 */
 
void DriverFactoryBase::RegisterDriver(const char *name, Driver::Type type, int priority)
 
{
 
	/* Don't register nameless Drivers */
 
	if (name == NULL) return;
 

	
 
	this->name = strdup(name);
 
	this->type = type;
 
	this->priority = priority;
 

	
 
	/* 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.
 
 */
 
char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
 
{
 
	for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
 
		p += snprintf(p, last - p, "List of %s drivers:\n", GetDriverTypeName(type));
 

	
 
		for (int priority = 10; priority >= 0; priority--) {
 
			Drivers::iterator it = GetDrivers().begin();
 
			for (; it != GetDrivers().end(); it++) {
 
				DriverFactoryBase *d = (*it).second;
 
				if (d->type != type) continue;
 
				if (d->priority != priority) continue;
 
				p += snprintf(p, last - p, "%18s: %s\n", d->name, d->GetDescription());
 
			}
 
		}
 

	
 
		p += snprintf(p, last - p, "\n");
 
	}
 

	
 
	return p;
 
}
 

	
 
/** Frees memory used for this->name
 
 */
 
DriverFactoryBase::~DriverFactoryBase() {
 
	if (this->name == NULL) return;
 

	
 
	/* 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
 
/* $Id$ */
 

	
 
/** @file driver.h Base for all drivers (video, sound, music, etc). */
 

	
 
#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 {
 
public:
 
	virtual const char *Start(const char * const *parm) = 0;
 

	
 
	virtual void Stop() = 0;
 

	
 
	virtual ~Driver() { }
 

	
 
	enum Type {
 
		DT_BEGIN = 0,
 
		DT_SOUND = 0,
 
		DT_MUSIC,
 
		DT_VIDEO,
 
		DT_END,
 
	};
 
};
 

	
 
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;
 
	}
 

	
 
	static Driver **GetActiveDriver(Driver::Type type)
 
	{
 
		static Driver *s_driver[3] = { NULL, NULL, NULL };
 
		return &s_driver[type];
 
	}
 

	
 
	static const char *GetDriverTypeName(Driver::Type type)
 
	{
 
		static const char *driver_type_name[] = { "sound", "music", "video" };
 
		return driver_type_name[type];
 
	}
 

	
 
protected:
 
	void RegisterDriver(const char *name, Driver::Type type, int priority);
 

	
 
public:
 
	DriverFactoryBase() :
0 comments (0 inline, 0 general)