Panda3D

eggNameUniquifier.h

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