From ad340b52a0a4e86edd5b54b01d74aac731a6a812 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 4 Feb 2008 18:44:17 +0000 Subject: basic cvar implementation --- src/core/Makefile.am | 4 +-- src/core/cvar.cc | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/cvar.h | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/core/cvar.cc create mode 100644 src/core/cvar.h diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 27477dc..8afa759 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -1,13 +1,13 @@ METASOURCES = AUTO INCLUDES = -I$(top_srcdir)/src -libcore_la_SOURCES = applicationinterface.cc commandbuffer.cc func.cc \ +libcore_la_SOURCES = applicationinterface.cc commandbuffer.cc cvar.cc func.cc \ gameinterface.cc libcore_la_LDFLAGS = -avoid-version -no-undefined libcore_la_LIBADD = $(top_builddir)/src/math/libmath.la \ $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/filesystem/libfilesystem.la noinst_LTLIBRARIES = libcore.la -noinst_HEADERS = applicationinterface.h commandbuffer.h core.h func.h \ +noinst_HEADERS = applicationinterface.h commandbuffer.h core.h cvar.h func.h \ gameinterface.h diff --git a/src/core/cvar.cc b/src/core/cvar.cc new file mode 100644 index 0000000..ef3f06c --- /dev/null +++ b/src/core/cvar.cc @@ -0,0 +1,92 @@ +/* + core/cvar.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "cvar.h" +#include + +namespace core { + +Cvar_t::Cvar_t(const char *cvarname, unsigned int cvarflags) +{ + cvar_name.assign(cvarname); + cvar_flags = cvarflags; +} + +Cvar_t::Cvar_t(const std::string &cvarname, unsigned int cvarflags) +{ + cvar_name = cvarname; + cvar_flags = cvarflags; +} + +Cvar_t & Cvar_t::operator=(const std::string &cvarvalue) +{ + cvar_value = cvarvalue; + return (*this); +} + +Cvar_t & Cvar_t::operator=(const char *cvarvalue) +{ + cvar_value.assign(cvarvalue); + return (*this); +} + +Cvar_t & Cvar_t::operator=(int cvarvalue) +{ + std::stringstream s; + s << cvarvalue; + s >> cvar_value; + return (*this); +} + +namespace cvar +{ + +std::map registry; + +Cvar set(const char *cvarname, const char *value) +{ + Cvar c = registry[std::string(cvarname)]; + if (!c) { + // not found, create a new variable and assign value + c = new Cvar_t(cvarname); + registry[std::string(cvarname)] = c; + (*c) = value; + } + return c; +} + +void unset(const char *cvarname) +{ + Cvar c = registry[std::string(cvarname)]; + if (c) { + registry.erase(std::string(cvarname)); + delete c; + } +} + +void unset(const std::string &cvarname) +{ + Cvar c = registry[cvarname]; + if (c) { + registry.erase(cvarname); + delete c; + } +} + +Cvar find(const std::string &cvarname) +{ + return registry[cvarname]; +} + +Cvar find(const char *cvarname) +{ + return registry[std::string(cvarname)]; +} + +} // namespace cvar + +} // namespace core + diff --git a/src/core/cvar.h b/src/core/cvar.h new file mode 100644 index 0000000..f407395 --- /dev/null +++ b/src/core/cvar.h @@ -0,0 +1,63 @@ +/* + core/cvar.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_CORE_CVAR_H__ +#define __INCLUDED_CORE_CVAR_H__ + +#include + +namespace core { + +/** new cvars have to be added through the cvar::set functions + * and not created directly. + */ +class Cvar_t { +public: + Cvar_t(const char *cvarname, unsigned int cvarflags = 0); + Cvar_t(const std::string &cvarname, unsigned int cvarflags = 0); + + inline const std::string &name() const { return cvar_name; } + inline const std::string &value() const {return cvar_value; } + + Cvar_t &operator=(const char *cvarvalue); + Cvar_t &operator=(const std::string &cvarvalue); + Cvar_t &operator=(int cvarvalue); + +private: + std::string cvar_name; + std::string cvar_value; + unsigned int cvar_flags; +}; + +typedef Cvar_t *Cvar; + +/// the cvar registry +namespace cvar +{ + +/// create a new cvar containing a string value +Cvar set(const char *cvarname, const char *value); + +/// create a new cvar containing an integer value +Cvar set(const char *cvarname, int value); + +/// delete a cvar +void unset(const char *cvarname); + +// delete a cvar +void unset(const std::string &cvarname); + +/// search for a named cvar, returns 0 if not found +Cvar find(const std::string &cvarname); +/// search for a named cvar, returns 0 if not found +Cvar find(const char *cvarname); + +} // namespace cvar + +} // namespace core + +#endif // __INCLUDED_CORE_CVAR_H__ + -- cgit v1.2.3