csutil/callstack.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csutil/callstack.h
Go to the documentation of this file.

    00001 /*
    00002   Call stack creation helper
    00003   Copyright (C) 2004 by Jorrit Tyberghein
    00004             (C) 2004 by Frank Richter
    00005
    00006   This library is free software; you can redistribute it and/or
    00007   modify it under the terms of the GNU Library General Public
    00008   License as published by the Free Software Foundation; either
    00009   version 2 of the License, or (at your option) any later version.
    00010
    00011   This library is distributed in the hope that it will be useful,
    00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014   Library General Public License for more details.
    00015
    00016   You should have received a copy of the GNU Library General Public
    00017   License along with this library; if not, write to the Free
    00018   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00019 */
    00020 #ifndef __CS_UTIL_CALLSTACK_H__
    00021 #define __CS_UTIL_CALLSTACK_H__
    00022
    00027 #include "csextern.h"
    00028 #include "csutil/csstring.h"
    00029
    00030 // Avoid using csRefCount, which uses the ref tracker, which in turn uses csCallStack
    00032 class CS_CRYSTALSPACE_EXPORT csCallStack
    00033 {
    00034 protected:
    00035   int ref_count;
    00036
    00037   virtual ~csCallStack() {}
    00038 public:
    00039   csCallStack () : ref_count (1) { }
    00040
    00041   void IncRef () { ref_count++; }
    00042   void DecRef ()
    00043   {
    00044     ref_count--;
    00045     if (ref_count <= 0)
    00046       Free ();
    00047   }
    00048   int GetRefCount () const { return ref_count; }
    00049
    00053   virtual void Free() = 0;
    00054
    00056   virtual size_t GetEntryCount () = 0;
    00057   //{@
    00063   virtual bool GetFunctionName (size_t num, char*& str) = 0;
    00064   bool GetFunctionName (size_t num, csString& str)
    00065   {
    00066     char* cstr;
    00067     if (GetFunctionName (num, cstr))
    00068     {
    00069       str = cstr;
    00070       free (cstr);
    00071       return true;
    00072     }
    00073     return false;
    00074   }
    00076   //{@
    00082   virtual bool GetLineNumber (size_t num, char*& str) = 0;
    00083   bool GetLineNumber (size_t num, csString& str)
    00084   {
    00085     char* cstr;
    00086     if (GetLineNumber (num, cstr))
    00087     {
    00088       str = cstr;
    00089       free (cstr);
    00090       return true;
    00091     }
    00092     return false;
    00093   }
    00095   //{@
    00100   virtual bool GetParameters (size_t num, char*& str) = 0;
    00101   bool GetParameters (size_t num, csString& str)
    00102   {
    00103     char* cstr;
    00104     if (GetParameters (num, cstr))
    00105     {
    00106       str = cstr;
    00107       free (cstr);
    00108       return true;
    00109     }
    00110     return false;
    00111   }
    00113
    00118   void Print (FILE* f = stdout, bool brief = false)
    00119   {
    00120     for (size_t i = 0; i < GetEntryCount(); i++)
    00121     {
    00122       char* s;
    00123       bool hasFunc = GetFunctionName (i, s);
    00124       fprintf (f, "%s", hasFunc ? (const char*)s : "<unknown>");
    00125       if (hasFunc) free (s);
    00126       if (!brief && (GetLineNumber (i, s)))
    00127       {
    00128         fprintf (f, " @%s", (const char*)s);
    00129         free (s);
    00130       }
    00131       if (!brief && (GetParameters (i, s)))
    00132       {
    00133         fprintf (f, " (%s)", (const char*)s);
    00134         free (s);
    00135       }
    00136       fprintf (f, "\n");
    00137     }
    00138     fflush (f);
    00139   }
    00145   csString GetEntryAll (size_t i, bool brief = false)
    00146   {
    00147     csString line;
    00148     char* s;
    00149     bool hasFunc = GetFunctionName (i, s);
    00150     line << (hasFunc ? (const char*)s : "<unknown>");
    00151     if (hasFunc) free (s);
    00152     if (!brief && GetLineNumber (i, s))
    00153     {
    00154       line << " @" << s;
    00155       free (s);
    00156     }
    00157     if (!brief && GetParameters (i, s))
    00158     {
    00159       line << " (" << s << ")";
    00160       free (s);
    00161     }
    00162     return line;
    00163   }
    00164 };
    00165
    00167 class CS_CRYSTALSPACE_EXPORT csCallStackHelper
    00168 {
    00169 public:
    00181   static csCallStack* CreateCallStack (int skip = 0, bool fast = false);
    00182 };
    00183
    00184 #endif // __CS_UTIL_CALLSTACK_H__