summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJared Adams <jaxad0127@gmail.com>2010-08-10 18:02:30 -0600
committerJared Adams <jaxad0127@gmail.com>2010-08-10 18:06:01 -0600
commit96187972ff9ac50a2a7fa280abbb21bd3c7f0737 (patch)
tree3a0385dcd57e437b25e1242109ce7f2c6c16396b
parentfa2cf9bea837e17d08218d0070a60905adec268f (diff)
downloadmana-client-96187972ff9ac50a2a7fa280abbb21bd3c7f0737.tar.gz
mana-client-96187972ff9ac50a2a7fa280abbb21bd3c7f0737.tar.bz2
mana-client-96187972ff9ac50a2a7fa280abbb21bd3c7f0737.tar.xz
mana-client-96187972ff9ac50a2a7fa280abbb21bd3c7f0737.zip
Add some new methods to the Event class
Adds get methods with default values, and has methods that return true if the variable exists. Reviewed-by: Chuck Miller
-rw-r--r--src/event.cpp28
-rw-r--r--src/event.h91
2 files changed, 112 insertions, 7 deletions
diff --git a/src/event.cpp b/src/event.cpp
index d503ad58..a2f233ac 100644
--- a/src/event.cpp
+++ b/src/event.cpp
@@ -55,6 +55,13 @@ int Event::getInt(const std::string &key) const throw (BadEvent)
return static_cast<IntData *>(it->second)->getData();
}
+bool Event::hasInt(const std::string &key)
+{
+ VariableMap::const_iterator it = mData.find(key);
+ return !(it == mData.end()
+ || it->second->getType() != VariableData::DATA_INT);
+}
+
void Event::setString(const std::string &key, const std::string &value) throw (BadEvent)
{
if (mData.find(key) != mData.end())
@@ -76,6 +83,13 @@ const std::string &Event::getString(const std::string &key) const throw (BadEven
}
+bool Event::hasString(const std::string &key)
+{
+ VariableMap::const_iterator it = mData.find(key);
+ return !(it == mData.end()
+ || it->second->getType() != VariableData::DATA_STRING);
+}
+
void Event::setFloat(const std::string &key, double value) throw (BadEvent)
{
if (mData.find(key) != mData.end())
@@ -96,6 +110,13 @@ double Event::getFloat(const std::string &key) const throw (BadEvent)
return static_cast<FloatData *>(it->second)->getData();
}
+bool Event::hasFloat(const std::string &key)
+{
+ VariableMap::const_iterator it = mData.find(key);
+ return !(it == mData.end()
+ || it->second->getType() != VariableData::DATA_FLOAT);
+}
+
void Event::setBool(const std::string &key, bool value) throw (BadEvent)
{
if (mData.find(key) != mData.end())
@@ -116,4 +137,11 @@ bool Event::getBool(const std::string &key) const throw (BadEvent)
return static_cast<BoolData *>(it->second)->getData();
}
+bool Event::hasBool(const std::string &key)
+{
+ VariableMap::const_iterator it = mData.find(key);
+ return !(it == mData.end()
+ || it->second->getType() != VariableData::DATA_BOOL);
+}
+
} // namespace Mana
diff --git a/src/event.h b/src/event.h
index 1e057a05..7acd2190 100644
--- a/src/event.h
+++ b/src/event.h
@@ -40,33 +40,110 @@ typedef std::map<std::string, VariableData *> VariableMap;
class Event
{
public:
- // String passed can be retivered with getName()
- // and is to used to identify what type of event
- // this is.
+ /**
+ * Makes an event with the given name.
+ */
Event(const std::string &name)
{ mEventName = name; }
~Event();
+ /**
+ * Returns the name of the event.
+ */
const std::string &getName() const
{ return mEventName; }
- // Sets or gets a interger with a key to identify
+ /**
+ * Sets the given variable to the given integer, if it isn't already set.
+ */
void setInt(const std::string &key, int value) throw (BadEvent);
+
+ /**
+ * Returns the given variable if it is set and an integer.
+ */
int getInt(const std::string &key) const throw (BadEvent);
- // Sets or gets a string with a key to identify
+ /**
+ * Returns the given variable if it is set and an integer, returning the
+ * given default otherwise.
+ */
+ inline int getInt(const std::string &key, int defaultValue) const
+ { try { return getInt(key); } catch (BadEvent) { return defaultValue; }}
+
+ /**
+ * Returns true if the given variable exists and is an integer.
+ */
+ bool hasInt(const std::string &key);
+
+ /**
+ * Sets the given variable to the given string, if it isn't already set.
+ */
void setString(const std::string &key, const std::string &value) throw (BadEvent);
+
+ /**
+ * Returns the given variable if it is set and a string.
+ */
const std::string &getString(const std::string &key) const throw (BadEvent);
- // Sets or gets a floating point number with key to identify
+ /**
+ * Returns the given variable if it is set and a string, returning the
+ * given default otherwise.
+ */
+ inline const std::string &getString(const std::string &key,
+ const std::string &defaultValue) const
+ { try { return getString(key); } catch (BadEvent) { return defaultValue; }}
+
+ /**
+ * Returns true if the given variable exists and is a string.
+ */
+ bool hasString(const std::string &key);
+
+ /**
+ * Sets the given variable to the given floating-point, if it isn't already
+ * set.
+ */
void setFloat(const std::string &key, double value) throw (BadEvent);
+
+ /**
+ * Returns the given variable if it is set and a floating-point.
+ */
double getFloat(const std::string &key) const throw (BadEvent);
- // Sets or gets a boolean with key to identify
+ /**
+ * Returns the given variable if it is set and a floating-point, returning
+ * the given default otherwise.
+ */
+ inline double getFloat(const std::string &key, float defaultValue) const
+ { try { return getFloat(key); } catch (BadEvent) { return defaultValue; }}
+
+ /**
+ * Returns true if the given variable exists and is a floating-point.
+ */
+ bool hasFloat(const std::string &key);
+
+ /**
+ * Sets the given variable to the given boolean, if it isn't already set.
+ */
void setBool(const std::string &key, bool value) throw (BadEvent);
+
+ /**
+ * Returns the given variable if it is set and a boolean.
+ */
bool getBool(const std::string &key) const throw (BadEvent);
+ /**
+ * Returns the given variable if it is set and a boolean, returning the
+ * given default otherwise.
+ */
+ inline bool getBool(const std::string &key, bool defaultValue) const
+ { try { return getBool(key); } catch (BadEvent) { return defaultValue; }}
+
+ /**
+ * Returns true if the given variable exists and is a boolean.
+ */
+ bool hasBool(const std::string &key);
+
private:
std::string mEventName;