Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProfileViewer.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Viewing profiling information (timing and other statistics)
20  */
21 
22 #ifndef INCLUDED_PROFILE_VIEWER
23 #define INCLUDED_PROFILE_VIEWER
24 
25 #include "lib/input.h"
26 #include "ps/CStr.h"
27 #include "ps/Singleton.h"
28 
29 class ScriptInterface;
30 class CScriptVal;
31 
32 /**
33  * Struct ProfileColumn: Describes one column of an AbstractProfileTable.
34  */
36 {
37  /// Title of the column
38  CStr title;
39 
40  /// Recommended width of the column, in pixels.
41  size_t width;
42 
43  ProfileColumn(const CStr& t, size_t w) : title(t), width(w) { }
44 };
45 
46 
47 /**
48  * Class AbstractProfileTable: Profile table data model.
49  *
50  * Clients that wish to display debug information in the profile viewer
51  * have to implement this class and hook it into CProfileViewer.
52  *
53  * Note that the profiling system is robust against deletion of
54  * object instances in the sense that it will automatically remove
55  * an AbstractProfileTable instance from its internal records when
56  * you delete it.
57  * Conversely, deleting an AbstractProfileTable instance is the responsibility
58  * of its creator.
59  */
61 {
62 public:
63  virtual ~AbstractProfileTable();
64 
65  /**
66  * GetName: Short descriptive name of this table (should be static).
67  *
68  * @return Descriptive name of this table.
69  */
70  virtual CStr GetName() = 0;
71 
72  /**
73  * GetTitle: Longer, explanatory text (can be dynamic).
74  *
75  * @return Title for the table.
76  */
77  virtual CStr GetTitle() = 0;
78 
79 
80  /**
81  * GetNumberRows
82  *
83  * @return Number of rows in this table.
84  */
85  virtual size_t GetNumberRows() = 0;
86 
87  /**
88  * GetColumnDescriptions
89  *
90  * @return A vector describing all columns of the table.
91  */
92  virtual const std::vector<ProfileColumn>& GetColumns() = 0;
93 
94  /**
95  * GetCellText
96  *
97  * @param row Row index (the first row has index 0).
98  * @param col Column index (the first column has index 0).
99  *
100  * @return Text to be displayed in the given cell.
101  */
102  virtual CStr GetCellText(size_t row, size_t col) = 0;
103 
104  /**
105  * GetChild: Return a row's child table if the child is expandable.
106  *
107  * @param row Row index (the first row has index 0).
108  *
109  * @return Pointer to the child table if the given row has one.
110  * Otherwise, return 0.
111  */
112  virtual AbstractProfileTable* GetChild(size_t row) = 0;
113 
114  /**
115  * IsHighlightRow
116  *
117  * @param row Row index (the first row has index 0).
118  *
119  * @return true if the row should be highlighted in a special color.
120  */
121  virtual bool IsHighlightRow(size_t row) { UNUSED2(row); return false; }
122 };
123 
124 
126 
127 /**
128  * Class CProfileViewer: Manage and display profiling tables.
129  */
130 class CProfileViewer : public Singleton<CProfileViewer>
131 {
132  friend class AbstractProfileTable;
133 
134 public:
135  CProfileViewer();
136  ~CProfileViewer();
137 
138  /**
139  * RenderProfile: Render the profile display using OpenGL if the user
140  * has enabled it.
141  */
142  void RenderProfile();
143 
144  /**
145  * Input: Filter and handle any input events that the profile display
146  * is interested in.
147  *
148  * In particular, this function handles enable/disable of the profile
149  * display as well as navigating the information tree.
150  *
151  * @param ev The incoming event.
152  *
153  * @return IN_PASS or IN_HANDLED depending on whether the event relates
154  * to the profiling display.
155  */
156  InReaction Input(const SDL_Event_* ev);
157 
158  /**
159  * AddRootTable: Add a new profile table as a root table (i.e. the
160  * tables that you cycle through via the profile hotkey).
161  *
162  * @note Tables added via this function are automatically removed from
163  * the list of root tables when they are deleted.
164  *
165  * @param table This table is added as a root table.
166  * @param front If true then the table will be the new first in the list,
167  * else it will be the last.
168  */
169  void AddRootTable(AbstractProfileTable* table, bool front = false);
170 
171  /**
172  * InputThunk: Delegate to the singleton's Input() member function
173  * if the singleton has been initialized.
174  *
175  * This allows our input handler to be installed via in_add_handler
176  * like a normal, global function input handler.
177  */
178  static InReaction InputThunk(const SDL_Event_* ev);
179 
180  /**
181  * SaveToFile: Save the current profiler data (for all profile tables)
182  * to a file in the 'logs' directory.
183  */
184  void SaveToFile();
185 
186  /**
187  * SaveToJS: Return a script value containing the current profiler data
188  * (for all profile tables).
189  */
190  CScriptVal SaveToJS(ScriptInterface& scriptInterface);
191 
192  /**
193  * ShowTable: Set the named profile table to be the displayed one. If it
194  * is not found, no profile is displayed.
195  *
196  * @param table The table name (matching AbstractProfileTable::GetName),
197  * or the empty string to display no table.
198  */
199  void ShowTable(const CStr& table);
200 
201 private:
203 };
204 
205 #define g_ProfileViewer CProfileViewer::GetSingleton()
206 
207 #endif
virtual const std::vector< ProfileColumn > & GetColumns()=0
GetColumnDescriptions.
Class AbstractProfileTable: Profile table data model.
Definition: ProfileViewer.h:60
virtual CStr GetName()=0
GetName: Short descriptive name of this table (should be static).
InReaction Input(const SDL_Event_ *ev)
Input: Filter and handle any input events that the profile display is interested in.
Class CProfileViewer: Manage and display profiling tables.
A trivial wrapper around a jsval.
Definition: ScriptVal.h:29
void RenderProfile()
RenderProfile: Render the profile display using OpenGL if the user has enabled it.
void SaveToFile()
SaveToFile: Save the current profiler data (for all profile tables) to a file in the &#39;logs&#39; directory...
void AddRootTable(AbstractProfileTable *table, bool front=false)
AddRootTable: Add a new profile table as a root table (i.e.
void ShowTable(const CStr &table)
ShowTable: Set the named profile table to be the displayed one.
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
InReaction
Definition: input.h:34
virtual size_t GetNumberRows()=0
GetNumberRows.
static InReaction InputThunk(const SDL_Event_ *ev)
InputThunk: Delegate to the singleton&#39;s Input() member function if the singleton has been initialized...
CProfileViewerInternals * m
virtual AbstractProfileTable * GetChild(size_t row)=0
GetChild: Return a row&#39;s child table if the child is expandable.
virtual CStr GetTitle()=0
GetTitle: Longer, explanatory text (can be dynamic).
CScriptVal SaveToJS(ScriptInterface &scriptInterface)
SaveToJS: Return a script value containing the current profiler data (for all profile tables)...
ProfileColumn(const CStr &t, size_t w)
Definition: ProfileViewer.h:43
Struct ProfileColumn: Describes one column of an AbstractProfileTable.
Definition: ProfileViewer.h:35
CStr title
Title of the column.
Definition: ProfileViewer.h:38
virtual CStr GetCellText(size_t row, size_t col)=0
GetCellText.
Abstraction around a SpiderMonkey JSContext.
virtual bool IsHighlightRow(size_t row)
IsHighlightRow.
size_t width
Recommended width of the column, in pixels.
Definition: ProfileViewer.h:41
virtual ~AbstractProfileTable()