00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Sevag Gharibian 00008 * Side Effects 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_InfoTree.h 00015 * 00016 * COMMENTS: This class is a tree implemented recursively (i.e. each node in 00017 * the tree is itself another tree). Each UT_InfoTree has a name, 00018 * an array of property arrays, and finally an array of children 00019 * which are also UT_InfoTree objects. 00020 * 00021 * Put simply, this is a tree in which each node can have as many 00022 * children as it likes, and at each node we can store data in the 00023 * form of strings (which are stored in arrays/rows, in case we 00024 * want to display each row in a spreadsheet table). 00025 * 00026 * SEE UT_InfoTree.C doc for how to create an UT_InfoTree. 00027 * 00028 * NOTE: Each direct child must have a unique name amongst the 00029 * other children directly beneath us- see declaration of 00030 * addChildBranch() for more details. 00031 * 00032 */ 00033 00034 #ifndef __UT_InfoTree_h__ 00035 #define __UT_InfoTree_h__ 00036 00037 #include "UT_API.h" 00038 #include <iostream.h> 00039 00040 #include "UT_PtrArray.h" 00041 #include "UT_FloatArray.h" 00042 #include "UT_StringArray.h" 00043 00044 #define TREE_TOP_DEFAULT_NAME "Head of Tree" 00045 00046 //////////////////////////////////////////////////////////////////////////////// 00047 // 00048 // NAME: ut_PropertyRow 00049 // 00050 // Simply a UT_StringArray, except we provide an append method which takes 00051 // an int as a parameter to make the life of our clients easier 00052 // 00053 //////////////////////////////////////////////////////////////////////////////// 00054 00055 class UT_API ut_PropertyRow : public UT_StringArray 00056 { 00057 public: 00058 ut_PropertyRow(); 00059 ~ut_PropertyRow(); 00060 00061 void append (const char *property); 00062 void append (int property); 00063 }; 00064 00065 //////////////////////////////////////////////////////////////////////////////// 00066 // 00067 // NAME: UT_InfoTree 00068 // 00069 /// This is a tree in which each node can have as many children as it likes, 00070 /// and at each node we can store data in the form of strings (which are 00071 /// stored in arrays/rows, in case we want to display each row in a 00072 /// spreadsheet table). 00073 // 00074 //////////////////////////////////////////////////////////////////////////////// 00075 00076 class UT_API UT_InfoTree 00077 { 00078 public: 00079 /// User should always call this constructor with no parameters specified. 00080 /// The parameters are only needed when branches are created internally. 00081 UT_InfoTree (UT_InfoTree *tree_top = NULL, UT_InfoTree *parent = NULL, 00082 UT_String name = TREE_TOP_DEFAULT_NAME); 00083 00084 /// Recursively deletes all my data and all children, which in turn delete 00085 /// all their data and children, etc... 00086 ~UT_InfoTree (); 00087 00088 /// Clears all our row and column information. Doesn't affect child 00089 /// branches. 00090 void clearProperties(); 00091 /// Clears all child branches. Doesn't affect our properties. 00092 void clearChildren(); 00093 00094 /// Create a child or subtree under this tree with the name 'branch_name' 00095 /// The caller should NOT call delete on the pointer returned when they are 00096 /// done with it, since this class' destructor will take care of any 00097 /// children created in addChildBranch(..). 00098 /// 00099 /// IMPORTANT NOTE: It is assumed that all branches on the same level will 00100 /// have *unique* names, BUT this is NOT enforced to prevent a performance 00101 /// hit since these trees can get rebuilt pretty often to accurately reflect 00102 /// the data they are containing. BOTTOM LINE: Make sure 'branch_name' is 00103 /// unique among 'myChildBranches' (otherwise there'll be trouble when we 00104 /// decide to traverse a tree given a path of branches - system won't crash, 00105 /// but will always choose the first available branch on a level with the 00106 /// name it is looking for). 00107 UT_InfoTree *addChildBranch (const char 00108 *branch_name); 00109 00110 /// HOW TO USE: You may add up to 4 properties to a single row at once using 00111 /// this method. If you still need to add more properties to this SAME 00112 /// row/array after that, simply use the returned ut_PropertyRow* to call 00113 /// 'append()' on and add more properties. To add properties that are 00114 /// ints, use the same technique, since there is an 'append()' method in 00115 /// ut_PropertyRow that takes an int parameter (or check the overridden 00116 /// version of addProperties below). BOTTOM LINE - if you want maximum 00117 /// flexibility, call this with no parameters and then append each property 00118 /// manually to the returned ut_PropertyRow*. 00119 ut_PropertyRow *addProperties(const char *a1 = NULL, 00120 const char *a2 = NULL, 00121 const char *a3 = NULL, 00122 const char *a4 = NULL); 00123 00124 /// A convenience method - there is often the need to add a name/value pair 00125 /// where the value is an int, so here is an overridden version of 00126 /// addProperties to make life easier! NOTE: NO default parameters for this 00127 /// version, parameters specified must be valid. 00128 ut_PropertyRow *addProperties(const char *a1, int v1); 00129 00130 /// Add column headings for this node to be used when the info is displayed 00131 /// in a table. THE NUMBER OF HEADINGS ADDED HERE WILL DETERMINE THE NUMBER 00132 /// OF COLUMNS DISPLAYED. Note: This class does NOT ensure that the # of 00133 /// column headings matches the width of each property array in 00134 /// 'myProperties'. Instead, we only display as many properties per row as 00135 /// we have number of columns. 00136 /// 00137 /// The width parameter must either be specified on all columns or on 00138 /// none. If set on all columns, it represents the fraction of the total 00139 /// width that gets allocated to that column (the values get normalized). 00140 /// If any column has a width specified of <= 0.0, the whole thing 00141 /// reverts to the default behavior of allocating all columns with 00142 /// equal width. 00143 void addColumnHeading(const char *label, 00144 fpreal width = -1.0); 00145 void addColumnHeading(int label, 00146 fpreal width = -1.0); 00147 00148 /// Copies all the information from some other tree into this tree. 00149 /// Only the row and column informatino is copied. Child branches are 00150 /// not copied or referenced. 00151 void copy(const UT_InfoTree &src); 00152 /// Merges the row and column information from another tree. This 00153 /// only works if this tree starts out empty, or the column headings 00154 /// ofthis tree and the source tree match. If the merge couldn't happen, 00155 /// this function returns false. Otherwise it returns true. 00156 bool merge(const UT_InfoTree &src); 00157 00158 /// Get a pointer to my parent 00159 UT_InfoTree *getParentBranch () const; 00160 00161 /// Get a pointer to the very top node in the tree 00162 UT_InfoTree *getTreeTop () const; 00163 00164 /// Return this node's name 00165 const UT_String &getName () const; 00166 00167 /// In case we decide to change the name of this node of the tree 00168 void setName (const char *new_name); 00169 00170 /// Get a ptr to a specific descendent branch of this tree, given a 00171 /// UT_StringArray of branch names. e.g. if you want to traverse down the 00172 /// tree through a child branch named "Antigen" and get a pointer to that 00173 /// branch's child called "Rocks", then your UT_StringArray should simply 00174 /// have 2 elements, "Antigen" and "Rocks", respectively in that order. 00175 /// Method returns NULL if no such valid path found. 00176 /// 00177 /// NOTE: Again, this assumes all branches at the same level have unique 00178 /// names, as stated above for 'addChildBranch()'. 00179 UT_InfoTree* getDescendentPtr(UT_StringArray &path); 00180 00181 //Accessors for my child branches and data properties 00182 const UT_PtrArray<UT_InfoTree *> *getChildBranches() const; 00183 const UT_PtrArray<ut_PropertyRow *> *getProperties() const; 00184 const ut_PropertyRow *getColumnHeadings() const; 00185 const UT_FloatArray *getColumnWidths() const; 00186 00187 /// Write the tree to 'out' for debugging purposes. The caller should not 00188 /// specify leading_space, it is for internal use during recursive calls. 00189 /// NOTE that this function's output is a bit messy, it is solely intended 00190 /// for debugging use to get an idea of what your tree looks like. 00191 void dumpToFile(ostream &out, 00192 UT_String leading_space = "") const; 00193 00194 const char *className() const; 00195 00196 private: 00197 UT_InfoTree *myParent; //My immediate parent 00198 UT_InfoTree *myTreeTop; //Head of the entire 00199 //tree 00200 UT_String myName; //Name of this branch 00201 UT_PtrArray<UT_InfoTree *> myChildBranches; //My underlying 00202 //branches 00203 UT_PtrArray<ut_PropertyRow *> myProperties; //My properties 00204 ut_PropertyRow myColumnHeadings; //Labels for column 00205 //headings when 00206 //displaying data 00207 UT_FloatArray myColumnWidths; 00208 }; 00209 00210 #endif
1.5.9