00001 // Filename: eggNameUniquifier.h 00002 // Created by: drose (09Nov00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #ifndef EGGNAMEUNIQUIFIER_H 00020 #define EGGNAMEUNIQUIFIER_H 00021 00022 /////////////////////////////////////////////////////////////////// 00023 // 00024 // EggNameUniquifier 00025 // 00026 // This is a utility class for renaming nodes in an egg hierarchy so 00027 // that no two nodes share the same name. It's useful, for instance, 00028 // as a preprocess before translating the egg hierarchy to a scene 00029 // graph format that doesn't tolerate two identically-named nodes; 00030 // it's also particularly useful for guaranteeing that VertexPools and 00031 // Textures do not have conflicting names. 00032 // 00033 // This is actually an abstract class; in order to use it, you must 00034 // derive your own class and redefine some key functions (but see 00035 // EggPoolUniquifier and EggGroupUniquifier). 00036 // 00037 // You must define at least the following function: 00038 // 00039 // virtual string get_category(EggNode *node); 00040 // 00041 // This function defines the particular category that the 00042 // particular node should be grouped into. All nodes that share 00043 // the same category name will be considered in the same name pool 00044 // and may not have the same name; two nodes that have different 00045 // categories will be allowed to keep the same name. 00046 // 00047 // If the category is the empty string, the node will not be 00048 // considered for uniquification. 00049 // 00050 // 00051 // You may also define the following function: 00052 // 00053 // virtual string filter_name(EggNode *node); 00054 // 00055 // This returns the name of the node, or at least the name it ought 00056 // to be. This provides a hook for, for instance, filtering out 00057 // invalid characters before the node name is uniquified. 00058 // 00059 // 00060 // virtual string generate_name(EggNode *node, 00061 // const string &category, int index); 00062 // 00063 // This returns a new name for the given node, once a node has been 00064 // identified as having the same name as another node. It may use 00065 // any algorithm you please to generate a new name, using any 00066 // combination of the node's original name, the category (as 00067 // returned by get_category()), and/or the supplied unique index 00068 // number. 00069 // 00070 // If this function returns a name that happens to collide with 00071 // some other already-existing node, it will simply be called again 00072 // (with a new index number) until it finally returns a unique 00073 // name. 00074 // 00075 /////////////////////////////////////////////////////////////////// 00076 00077 #include <pandabase.h> 00078 00079 #include "eggObject.h" 00080 00081 #include "pmap.h" 00082 00083 class EggNode; 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Class : EggNameUniquifier 00087 // Description : This is a handy class for guaranteeing unique node 00088 // names in an egg hierarchy. It is an abstract class; 00089 // to use it you must subclass off of it. See the 00090 // comment above. 00091 //////////////////////////////////////////////////////////////////// 00092 class EXPCL_PANDAEGG EggNameUniquifier : public EggObject { 00093 public: 00094 EggNameUniquifier(); 00095 ~EggNameUniquifier(); 00096 00097 void clear(); 00098 00099 void uniquify(EggNode *node); 00100 00101 EggNode *get_node(const string &category, const string &name) const; 00102 bool has_name(const string &category, const string &name) const; 00103 bool add_name(const string &category, const string &name, 00104 EggNode *node = NULL); 00105 00106 virtual string get_category(EggNode *node)=0; 00107 virtual string filter_name(EggNode *node); 00108 virtual string generate_name(EggNode *node, 00109 const string &category, int index); 00110 00111 private: 00112 typedef pmap<string, EggNode *> UsedNames; 00113 typedef pmap<string, UsedNames> Categories; 00114 00115 Categories _categories; 00116 int _index; 00117 00118 public: 00119 00120 static TypeHandle get_class_type() { 00121 return _type_handle; 00122 } 00123 static void init_type() { 00124 EggObject::init_type(); 00125 register_type(_type_handle, "EggNameUniquifier", 00126 EggObject::get_class_type()); 00127 } 00128 virtual TypeHandle get_type() const { 00129 return get_class_type(); 00130 } 00131 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00132 00133 private: 00134 static TypeHandle _type_handle; 00135 00136 }; 00137 00138 #endif 00139 00140