Panda3D

eggNameUniquifier.h
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file eggNameUniquifier.h
10 * @author drose
11 * @date 2000-11-09
12 */
13
14 #ifndef EGGNAMEUNIQUIFIER_H
15 #define EGGNAMEUNIQUIFIER_H
16
17 /*
18 * EggNameUniquifier This is a utility class for renaming nodes in an egg
19 * hierarchy so that no two nodes share the same name. It's useful, for
20 * instance, as a preprocess before translating the egg hierarchy to a scene
21 * graph format that doesn't tolerate two identically-named nodes; it's also
22 * particularly useful for guaranteeing that VertexPools and Textures do not
23 * have conflicting names. This is actually an abstract class; in order to
24 * use it, you must derive your own class and redefine some key functions (but
25 * see EggPoolUniquifier and EggGroupUniquifier). You must define at least the
26 * following function: virtual string get_category(EggNode *node); This
27 * function defines the particular category that the particular node should be
28 * grouped into. All nodes that share the same category name will be
29 * considered in the same name pool and may not have the same name; two nodes
30 * that have different categories will be allowed to keep the same name. If
31 * the category is the empty string, the node will not be considered for
32 * uniquification. You may also define the following function: virtual string
33 * filter_name(EggNode *node); This returns the name of the node, or at least
34 * the name it ought to be. This provides a hook for, for instance, filtering
35 * out invalid characters before the node name is uniquified. virtual string
36 * generate_name(EggNode *node, const string &category, int index); This
37 * returns a new name for the given node, once a node has been identified as
38 * having the same name as another node. It may use any algorithm you please
39 * to generate a new name, using any combination of the node's original name,
40 * the category (as returned by get_category()), andor the supplied unique
41 * index number. If this function returns a name that happens to collide with
42 * some other already-existing node, it will simply be called again (with a
43 * new index number) until it finally returns a unique name.
44 */
45
46 #include "pandabase.h"
47
48 #include "eggObject.h"
49
50 #include "pmap.h"
51
52 class EggNode;
53
54 /**
55 * This is a handy class for guaranteeing unique node names in an egg
56 * hierarchy. It is an abstract class; to use it you must subclass off of it.
57 * See the comment above.
58 */
59 class EXPCL_PANDA_EGG EggNameUniquifier : public EggObject {
60 PUBLISHED:
61 EggNameUniquifier();
62 ~EggNameUniquifier();
63
64 void clear();
65
66 void uniquify(EggNode *node);
67
68 EggNode *get_node(const std::string &category, const std::string &name) const;
69 bool has_name(const std::string &category, const std::string &name) const;
70 bool add_name(const std::string &category, const std::string &name,
71 EggNode *node = nullptr);
72
73 virtual std::string get_category(EggNode *node)=0;
74 virtual std::string filter_name(EggNode *node);
75 virtual std::string generate_name(EggNode *node,
76 const std::string &category, int index);
77
78 private:
79 typedef pmap<std::string, EggNode *> UsedNames;
80 typedef pmap<std::string, UsedNames> Categories;
81
82 Categories _categories;
83 int _index;
84
85 public:
86
87 static TypeHandle get_class_type() {
88 return _type_handle;
89 }
90 static void init_type() {
91 EggObject::init_type();
92 register_type(_type_handle, "EggNameUniquifier",
93 EggObject::get_class_type());
94 }
95 virtual TypeHandle get_type() const {
96 return get_class_type();
97 }
98 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
99
100 private:
101 static TypeHandle _type_handle;
102
103 };
104
105 #endif
EggNameUniquifier
This is a handy class for guaranteeing unique node names in an egg hierarchy.
Definition: eggNameUniquifier.h:59
EggNode
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:36
EggObject
The highest-level base class in the egg directory.
Definition: eggObject.h:29
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
pmap
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
eggObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22