wxldebug.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 // Purpose:     Lua and wxLua debugging code
00003 // Author:      J. Winwood, John Labenski
00004 // Created:     June 2003
00005 // Copyright:   (c) 2002 Lomtick Software. All rights reserved.
00006 // Licence:     wxWidgets licence
00007 /////////////////////////////////////////////////////////////////////////////
00008 
00009 #ifndef WX_LUA_DEBUG_H
00010 #define WX_LUA_DEBUG_H
00011 
00012 #include "wx/dynarray.h"
00013 #include "wx/treectrl.h" // for wxTreeItemData
00014 
00015 #include "wxluadebug/include/wxluadebugdefs.h"
00016 #include "wxlua/include/wxlstate.h"
00017 
00018 class WXDLLIMPEXP_WXLUADEBUG wxLuaDebugData;
00019 
00020 // ----------------------------------------------------------------------------
00021 // wxLuaDebugItem - A class to store an item from Lua for wxLuaDebugData
00022 //
00023 // It is typically used to store table[key] = value pair info. However it may
00024 // be used to store stack information as well.
00025 // ----------------------------------------------------------------------------
00026 
00027 enum wxLuaDebugItem_Type
00028 {
00029     WXLUA_DEBUGITEM_LOCALS    = 0x0100, // This wxLuaDebugItem is the parent for local variables
00030 
00031     WXLUA_DEBUGITEM_EXPANDED  = 0x0200, // for wxLuaStackDialog
00032 
00033     WXLUA_DEBUGITEM_IS_REFED  = 0x1000, // This item was created with a new
00034                                         // wxluaR_ref() rather than using an existing one.
00035     WXLUA_DEBUGITEM_KEY_REF   = 0x2000, // The ref is for the key
00036     WXLUA_DEBUGITEM_VALUE_REF = 0x4000, // The ref is for the value
00037 };
00038 
00039 class WXDLLIMPEXP_WXLUADEBUG wxLuaDebugItem
00040 {
00041 public:
00042     wxLuaDebugItem(const wxLuaDebugItem &debugDataItem);
00043     wxLuaDebugItem(const wxString &itemKey,   int itemKeyType,
00044                    const wxString &itemValue, int itemValueType,
00045                    const wxString &itemSource,
00046                    int lua_ref, int idx = 0, int flag = 0);
00047 
00048     // The key has the typical meaning of the key in a Lua table
00049     wxString GetKey() const             { return m_itemKey; }
00050     int      GetKeyType() const         { return m_itemKeyType; }
00051     wxString GetKeyTypeString() const   { return wxluaT_typename(NULL, m_itemKeyType); }
00052 
00053     // The value has the typical meaning of the value for the key in a Lua table
00054     wxString GetValue() const           { return m_itemValue; }
00055     int      GetValueType() const       { return m_itemValueType; }
00056     wxString GetValueTypeString() const { return wxluaT_typename(NULL, m_itemValueType); }
00057 
00058     // The lua_Debug.source value when enumerating the stack or a stack item
00059     wxString GetSource() const          { return m_itemSource; }
00060 
00061     int      GetRef() const             { return m_lua_ref; }  // wxluaR_ref() reference
00062     int      GetIndex() const           { return m_index; }    // stack index or table level index
00063     int      GetFlag() const            { return m_flag; }     // see wxLuaDebugItem_Type
00064     bool     GetFlagBit(int mask) const { return WXLUA_HASBIT(m_flag, mask); }
00065 
00066     // If GetFlagBit(WXLUA_DEBUGITEM_KEY_REFED) try to convert GetKey() to a number
00067     // else if GetFlagBit(WXLUA_DEBUGITEM_VALUE_REFED) try to convert GetValue() to a number
00068     // Asserts if neither or both of the bits are set.
00069     bool     GetRefPtr(long& ptr) const;
00070 
00071     void     SetFlag(int flag)             { m_flag = flag; }
00072     void     SetFlagBit(int bit, bool set) { m_flag = WXLUA_SETBIT(m_flag, bit, set); }
00073     void     SetRef(int lua_ref)           { m_lua_ref = lua_ref; } // only if you've wxluaR_unref()ed it
00074 
00075     // Get a human readable string for debugging
00076     wxString ToString() const
00077     {
00078         return wxString::Format(wxT("Key: '%s' KeyType: %d '%s' Value: '%s' ValueType: %d '%s' Ref: %d Idx: %d Flag: %x HasSrc: %d"),
00079             m_itemKey.c_str(), m_itemKeyType, GetKeyTypeString().c_str(),
00080             m_itemValue.c_str(), m_itemValueType, GetValueTypeString().c_str(),
00081             m_lua_ref, m_index, m_flag, (int)!m_itemSource.IsEmpty());
00082     }
00083 
00084     // implementation
00085 
00086     wxString   m_itemKey;
00087     int        m_itemKeyType;
00088     wxString   m_itemValue;
00089     int        m_itemValueType;
00090     wxString   m_itemSource;
00091     int        m_lua_ref;
00092     int        m_index;
00093     int        m_flag;
00094 };
00095 
00096 #if defined(WXMAKINGDLL_WXLUADEBUG) || defined(WXUSINGDLL)
00097     WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(wxLuaDebugItem *, wxLuaDebugItemArray, WXDLLIMPEXP_WXLUADEBUG);
00098 #else
00099     WX_DEFINE_SORTED_ARRAY(wxLuaDebugItem *, wxLuaDebugItemArray);
00100 #endif
00101 
00102 // ----------------------------------------------------------------------------
00103 // wxLuaDebugData - a wxObject ref counted container for a wxLuaDebugItemArray
00104 // The destructor deletes the array items.
00105 // ----------------------------------------------------------------------------
00106 
00107 // an invalid wxLuaDebugData for comparison (like wxNullBitmap)
00108 extern WXDLLIMPEXP_DATA_WXLUADEBUG(wxLuaDebugData) wxNullLuaDebugData;
00109 
00110 class WXDLLIMPEXP_WXLUADEBUG wxLuaDebugData : public wxObject
00111 {
00112 public:
00113     wxLuaDebugData(bool create);
00114     wxLuaDebugData(const wxLuaDebugData &debugData) { Ref(debugData); }
00115 
00116     virtual ~wxLuaDebugData() {} // make gcc happy even though it's not used
00117 
00118     // Has this been created with its ref data?
00119     bool Ok() const { return (m_refData != NULL); }
00120 
00121     // Get the data array, please use safe array access functions if possible
00122     wxLuaDebugItemArray* GetArray();
00123     const wxLuaDebugItemArray* GetArray() const;
00124 
00125     // wxArray functions mapped to the internal array w/ error checking
00126     //   The wxLuaDebugItem items added must be created with 'new' and
00127     //   will be deleted when this class is destroyed.
00128     size_t GetCount() const;
00129     wxLuaDebugItem* Item(size_t index) const;
00130     void Add(wxLuaDebugItem* item);
00131 
00132     //-------------------------------------------------------------------------
00133 
00134     // fill this with the stack entries for the wxLuaState
00135     //   returns the number of stack entries added
00136     int EnumerateStack(const wxLuaState& wxlState);
00137     // fill this with the locals from a particular stack frame, if an item on the stack is a
00138     //   table then add a reference to it in the references array
00139     int EnumerateStackEntry(const wxLuaState& wxlState, int stack_frame, wxArrayInt& references);
00140     // fill this with the name and value of items in a table at the given reference,
00141     //   if the table has a sub table then add a reference to it to the references array
00142     int EnumerateTable(const wxLuaState& wxlState, int nRef, int nEntry, wxArrayInt& references);
00143 
00144     //-------------------------------------------------------------------------
00145     // These functions are static to allow them to be used in other places to
00146     //    give a consistent feel to the display of Lua values.
00147 
00148     // Get information about the item at the 'stack_idx'. Returns the lua_type(L, stack_idx),
00149     //   fills 'wxl_type' with the WXLUA_TXXX type and 'value' with a human readable value.
00150     static int GetTypeValue(const wxLuaState& wxlState, int stack_idx, int* wxl_type, wxString& value);
00151     // Get a wxString description about the table at the stack_idx in the Lua stack
00152     static wxString GetTableInfo(const wxLuaState& wxlState, int stack_idx);
00153     // Get a wxString description about user data at the stack_idx in the Lua stack
00154     //  if full then try to look up the name of the user data from the bindings
00155     static wxString GetUserDataInfo(const wxLuaState& wxlState, int stack_idx, bool full_userdata);
00156 
00157     //-------------------------------------------------------------------------
00158 
00159     // Make a full copy of the array and return it.
00160     wxLuaDebugData Copy() const;
00161 
00162     // Ref this table if it hasn't been refed already, returns ref # or LUA_NOREF if not refed
00163     int RefTable(lua_State* L, int stack_idx, int* flag_type, int extra_flag, wxArrayInt& references);
00164 
00165     // Sorting function for the wxLuaDebugItemArray, sorts by name
00166     static int SortFunction(wxLuaDebugItem *elem1, wxLuaDebugItem *elem2 );
00167 
00168     // operators
00169     bool operator == (const wxLuaDebugData& debugData) const
00170         { return m_refData == debugData.m_refData; }
00171     bool operator != (const wxLuaDebugData& debugData) const
00172         { return m_refData != debugData.m_refData; }
00173 
00174     wxLuaDebugData& operator = (const wxLuaDebugData& debugData)
00175     {
00176         if ( (*this) != debugData )
00177             Ref(debugData);
00178         return *this;
00179     }
00180 };
00181 
00182 // ----------------------------------------------------------------------------
00183 // wxLuaCheckStack - Dump the contents of the lua_State for debugging
00184 // ----------------------------------------------------------------------------
00185 
00186 class WXDLLIMPEXP_WXLUADEBUG wxLuaCheckStack
00187 {
00188 public:
00189     // Create a instance, remembers lua_gettop(), 'msg' can be used to add
00190     // information about where or why this was created.
00191     // If 'print_to_console' then all functions below that return a string will also
00192     // print to the console as well.
00193     wxLuaCheckStack(lua_State* L, const wxString &msg = wxEmptyString, bool print_to_console = true);
00194     // Prints out the starting top and ending top if 'print_to_console' in constructor
00195     ~wxLuaCheckStack();
00196 
00197     // Returns a string comparing the starting and current lua_gettop() with additional msg
00198     wxString TestStack(const wxString &msg = wxEmptyString);
00199 
00200     // Returns a string of the current items on the stack with their types.
00201     wxString DumpStack(const wxString& msg = wxEmptyString);
00202 
00203     // Returns a string of all of the global variables and subtables with additional msg.
00204     wxString DumpGlobals(const wxString& msg = wxEmptyString);
00205     // Dump the table and its subtables from the globals index with additional msg.
00206     // The name may be of the form "table1.subtable2.subtable3..."
00207     wxString DumpTable(const wxString& tableName, const wxString& msg = wxEmptyString);
00208     // Dump the table and its subtables at the stack_idx with additional msg.
00209     wxString DumpTable(int stack_idx, const wxString& msg = wxEmptyString);
00210 
00211     // Dump the contents of the table at the stack_idx to a string. 'tablename' and 'msg' are
00212     // for informational messages, 'tableArray' is used to avoid recursion and should be empty
00213     // for the initial call, and 'indent' is used to track indentation level for each subtable.
00214     wxString DumpTable(int stack_idx, const wxString& tablename, const wxString& msg, wxSortedArrayString& tableArray, int indent);
00215 
00216     // Print a message to the console if 'print_to_console' in constructor.
00217     void OutputMsg(const wxString& msg) const;
00218 
00219     // implementation
00220 
00221     lua_State* m_luaState;
00222     wxString   m_msg;
00223     int        m_top;
00224     bool       m_print_to_console;
00225 };
00226 
00227 #endif // WX_LUA_DEBUG_H
Generated on Tue Jul 13 10:30:39 2010 for wxLua by  doxygen 1.6.3