Sorted Socket.
[UnsignedByte.git] / src / Core / Command / CommandObject.h
blob75ba033a497d0e86ffbc57692c46d6ae141ece70
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #pragma once
22 /**
23 * @file CommandObject.h
24 * This file contains the CommandObject class.
26 * @see CommandObject
27 */
29 #include "Assert.h"
30 #include "Types.h"
33 /**
34 * This template defines a function binding.
36 * A binding is made to a name, allowing some sort of reflection later on.
37 */
38 template <class T>
39 class CommandObject
41 public:
42 typedef void (T::*CommandFunction)(const std::string& argument); /**< The type of the function to bind on. */
44 /** Construct a CommandObject, binding the specified function to the specified name. */
45 CommandObject(const char* name, CommandFunction command, bool fullname = false);
47 /** Destructor, a noop. */
48 ~CommandObject();
51 /** Execute the stored function with the specified owner and argument. */
52 void Run(T* owner, const std::string& argument) const;
54 /** Return the name of the bound function. */
55 const char* getName() const;
57 /** Whether, when performing a lookup on this function, only the full alias should be allowed. */
58 bool fullName() const;
60 protected:
61 const char* m_name; /**< The name of the bound function. */
62 CommandFunction m_command; /**< The bound function. */
63 bool m_fullName; /**< Whether only the full alias should be allowed. */
66 template <class T>
67 const char* CommandObject<T>::getName() const
69 return m_name;
72 template <class T>
73 bool CommandObject<T>::fullName() const
75 return m_fullName;
78 template <class T>
79 CommandObject<T>::CommandObject(const char* name, CommandFunction command, bool fullname) :
80 m_name(name),
81 m_command(command),
82 m_fullName(fullname)
84 Assert(name);
87 template <class T>
88 CommandObject<T>::~CommandObject()
92 template <class T>
93 void CommandObject<T>::Run(T* owner, const std::string& argument) const
95 Assert(m_command);
96 Assert(owner);
98 (owner->*m_command)(argument);