diff --git a/engine/ObjList.cpp b/engine/ObjList.cpp index 16177fbd..39e6307c 100644 --- a/engine/ObjList.cpp +++ b/engine/ObjList.cpp @@ -90,9 +90,7 @@ ObjList *ObjList::operator[](int index) const ObjList *ObjList::find(const GenObject *obj) const { -#ifdef DEBUG - Debugger debug("ObjList::find","(%p) [%p]",obj,this); -#endif + DDebug("ObjList::find(%p) [%p]",obj,this); const ObjList *n = this; while (n && (n->get() != obj)) n = n->next(); @@ -100,6 +98,19 @@ ObjList *ObjList::find(const GenObject *obj) const return const_cast(n); } +ObjList *ObjList::find(const String &str) const +{ + DDebug("ObjList::find(\"%s\") [%p]",str.c_str(),this); + const ObjList *n = this; + while (n) { + if (n->get() && str.matches(n->get()->toString())) + break; + n = n->next(); + } + DDebug(DebugInfo,"ObjList::find returning %p",n); + return const_cast(n); +} + GenObject *ObjList::set(const GenObject *obj, bool delold) { if (m_obj == obj) diff --git a/engine/String.cpp b/engine/String.cpp index 93a1e831..31c00a64 100644 --- a/engine/String.cpp +++ b/engine/String.cpp @@ -136,6 +136,13 @@ void StringMatchPrivate::fixup() count = c; } +static String s_empty; + +const String& String::empty() +{ + return s_empty; +} + String::String() : m_string(0), m_length(0), m_hash(0), m_matches(0) { @@ -789,3 +796,18 @@ NamedString::NamedString(const char *name, const char *value) { DDebug(DebugAll,"NamedString::NamedString(\"%s\",\"%s\") [%p]",name,value,this); } + +const String& GenObject::toString() const +{ + return String::empty(); +} + +const String& String::toString() const +{ + return *this; +} + +const String& NamedString::toString() const +{ + return m_name; +} diff --git a/yatengine.h b/yatengine.h index 3ebfeae5..a17ec910 100644 --- a/yatengine.h +++ b/yatengine.h @@ -151,7 +151,9 @@ bool Debug(const char *facility, int level, const char *format, ...) FORMAT_CHEC void Output(const char *format, ...) FORMAT_CHECK(1); /** - * An object that logs messages on creation and destruction + * This class is used as an automatic variable that logs messages on creation + * and destruction (when the instruction block is left or function returns) + * @short An object that logs messages on creation and destruction */ class Debugger { @@ -206,6 +208,8 @@ struct TokenDict { int value; }; +class String; + /** * An object with just a public virtual destructor */ @@ -222,6 +226,14 @@ public: */ virtual void destruct() { delete this; } + + /** + * Get a string representation of this object + * @return A reference to a String representing this object + * which is either null, the object itself (for objects derived from + * String) or some form of identification + */ + virtual const String& toString() const; }; /** @@ -350,6 +362,13 @@ public: */ ObjList *find(const GenObject *obj) const; + /** + * Get the item in the list that holds an object by String value + * @param str String value (toString) of the object to search for + * @return Pointer to the found item or NULL + */ + ObjList *find(const String &str) const; + /** * Insert an object at this point * @param obj Pointer to the object to insert @@ -465,6 +484,11 @@ public: */ virtual ~String(); + /** + * A static null String + */ + static const String &empty(); + /** * Get the value of the stored string. * @return The stored C string which may be NULL. @@ -530,6 +554,12 @@ public: */ String& trimBlanks(); + /** + * Override GenObject's method to return this String + * @return A reference to this String + */ + virtual const String& toString() const; + /** * Convert the string to an integer value. * @param defvalue Default to return if the string is not a number @@ -767,11 +797,11 @@ public: * @param value String to check for match * @return True if matches, false otherwise */ - virtual bool matches(const String &value) + virtual bool matches(const String &value) const { return operator==(value); } /** - * Checks if matches a regular expression + * Checks if matches a regular expression and fill the match substrings * @param rexp Regular expression to check for match * @return True if matches, false otherwise */ @@ -979,7 +1009,7 @@ public: * @param value String to check for match * @return True if matches, false otherwise */ - virtual bool matches(const String &value) + virtual bool matches(const String &value) const { return matches(value.safe()); } protected: @@ -1015,6 +1045,12 @@ public: inline const String& name() const { return m_name; } + /** + * Get a string representation of this object + * @return A reference to the name of this object + */ + virtual const String& toString() const; + /** * Value assignment operator */