• entries
37
68
• views
46554

# Console variables and commands

349 views

Looks like my cvar and command code is nice and good. Console commands are no longer the bulky class they originally were; Now, they are simple functors, taking a vector of string. I'm using a simple templated Functor struct, from which command functors and cvar observers functors are typedefed.

template <class T> struct Functor{	virtual void operator() (T* t) = 0;};typedef Functor CmdFunctor;typedef Functor CvarFunctor;

My cvar class itself isn't the shining use of OO design that commands are. In fact, they're still quite a throwback to procedural programming like Quake's cvars. However, Quake's cvars are of proven design. Each cvar object maintains its value type, its value, and its observers. I don't keep a global list of observers because it's unlikely that any given observer will want to know every single cvar change -- it's much more likely that an observer will only watch a single cvar and nothing else.

My cvars have their type set at construction time, and after that moment, will never change type. However, assignment and conversion operators are defined so that values can be set as any type and converted dynamically. I'm using boost::lexical_cast to do the conversions, so any code that tries to set (or sometimes get) a cvar value will need to catch either std::bad_cast or boost::bad_lexical_cast.

I'm using std::foreach and another functor to call each observer when a cvar value is changed. When a cvar is successfully changed, the setters call a simple function which ensures that each observer is notified of the change.

template <class T, class C> class Call{	C* res;public:	Call (C* c = null) : res(c) { }	void operator() (T* x) { (*x)(res); }};class cvar{	typedef std::list Watchlist;	inline void CallWatchers ()	{		Call call(&this);		for_each(observers.begin(), observers.end(), call);	};};

I also have a function for my console class which takes a command line and breaks it into a vector of string. It formerly broke the line into an array of char* (much like the argv parameter to main()) but I figured that it'd be better (and less of a hassle for memory management) going the STL route. This member function will be public (and static) so it can break down lpCmdLine in the WinMain function.

## Create an account

Register a new account