Panda3D
internalName.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 internalName.h
10  * @author drose
11  * @date 2004-07-15
12  */
13 
14 #ifndef INTERNALNAME_H
15 #define INTERNALNAME_H
16 
17 #include "pandabase.h"
18 
20 #include "pointerTo.h"
21 #include "pmap.h"
22 #include "lightMutex.h"
23 #include "lightMutexHolder.h"
24 
25 class FactoryParams;
26 
27 /**
28  * Encodes a string name in a hash table, mapping it to a pointer. This is
29  * used to tokenify names so they may be used efficiently in low-level Panda
30  * structures, for instance to differentiate the multiple sets of texture
31  * coordinates that might be stored on a Geom.
32  *
33  * InternalNames are hierarchical, with the '.' used by convention as a
34  * separator character. You can construct a single InternalName as a
35  * composition of one or more other names, or by giving it a source string
36  * directly.
37  */
38 class EXPCL_PANDA_GOBJ InternalName final : public TypedWritableReferenceCount {
39 private:
40  InternalName(InternalName *parent, const std::string &basename);
41 
42 public:
43  INLINE static PT(InternalName) make(const std::string &name);
44 
45  template<int N>
46  INLINE static PT(InternalName) make(const char (&literal)[N]);
47 
48 PUBLISHED:
49  virtual ~InternalName();
50  virtual bool unref() const;
51 
52  static PT(InternalName) make(const std::string &name, int index);
53  PT(InternalName) append(const std::string &basename);
54 
55  INLINE InternalName *get_parent() const;
56  std::string get_name() const;
57  std::string join(const std::string &sep) const;
58  INLINE const std::string &get_basename() const;
59 
60  MAKE_PROPERTY(parent, get_parent);
61  MAKE_PROPERTY(name, get_name);
62  MAKE_PROPERTY(basename, get_basename);
63 
64  int find_ancestor(const std::string &basename) const;
65  const InternalName *get_ancestor(int n) const;
66  const InternalName *get_top() const;
67  std::string get_net_basename(int n) const;
68 
69  void output(std::ostream &out) const;
70 
71  // Some predefined built-in names.
72  INLINE static PT(InternalName) get_root();
73  INLINE static PT(InternalName) get_error();
74  INLINE static PT(InternalName) get_vertex();
75  INLINE static PT(InternalName) get_normal();
76  INLINE static PT(InternalName) get_tangent();
77  INLINE static PT(InternalName) get_tangent_name(const std::string &name);
78  INLINE static PT(InternalName) get_binormal();
79  INLINE static PT(InternalName) get_binormal_name(const std::string &name);
80  INLINE static PT(InternalName) get_texcoord();
81  INLINE static PT(InternalName) get_texcoord_name(const std::string &name);
82  INLINE static PT(InternalName) get_color();
83  INLINE static PT(InternalName) get_rotate();
84  INLINE static PT(InternalName) get_size();
85  INLINE static PT(InternalName) get_aspect_ratio();
86  INLINE static PT(InternalName) get_transform_blend();
87  INLINE static PT(InternalName) get_transform_weight();
88  INLINE static PT(InternalName) get_transform_index();
89  INLINE static PT(InternalName) get_morph(InternalName *column, const std::string &slider);
90  INLINE static PT(InternalName) get_index();
91  INLINE static PT(InternalName) get_world();
92  INLINE static PT(InternalName) get_camera();
93  INLINE static PT(InternalName) get_model();
94  INLINE static PT(InternalName) get_view();
95 
96 #ifdef HAVE_PYTHON
97  // These versions are exposed to Python, which have additional logic to map
98  // from Python interned strings.
99  EXTENSION(static PT(InternalName) make(PyObject *str));
100 #endif
101 
102 public:
103 #ifdef HAVE_PYTHON
104  // It's OK for us to define it here since these are just pointers of which
105  // the reference is maintained indefinitely.
106  typedef phash_map<PyObject *, InternalName *, pointer_hash> PyInternTable;
107  static PyInternTable _py_intern_table;
108 #endif
109 
110 private:
111  PT(InternalName) _parent;
112  std::string _basename;
113 
114  typedef phash_map<std::string, InternalName *, string_hash> NameTable;
115  NameTable _name_table;
116  LightMutex _name_table_lock;
117 
118  typedef phash_map<const char *, PT(InternalName), pointer_hash> LiteralTable;
119  static LiteralTable _literal_table;
120  static LightMutex _literal_table_lock;
121 
122  static PT(InternalName) _root;
123  static PT(InternalName) _error;
124  static PT(InternalName) _default;
125  static PT(InternalName) _vertex;
126  static PT(InternalName) _normal;
127  static PT(InternalName) _tangent;
128  static PT(InternalName) _binormal;
129  static PT(InternalName) _texcoord;
130  static PT(InternalName) _color;
131  static PT(InternalName) _rotate;
132  static PT(InternalName) _size;
133  static PT(InternalName) _aspect_ratio;
134  static PT(InternalName) _transform_blend;
135  static PT(InternalName) _transform_weight;
136  static PT(InternalName) _transform_index;
137  static PT(InternalName) _index;
138  static PT(InternalName) _world;
139  static PT(InternalName) _camera;
140  static PT(InternalName) _model;
141  static PT(InternalName) _view;
142 
143 public:
144  // Datagram stuff
145  static void register_with_read_factory();
146  virtual void write_datagram(BamWriter *manager, Datagram &me);
147 
148  virtual void finalize(BamReader *manager);
149 
150 protected:
151  static TypedWritable *make_from_bam(const FactoryParams &params);
152  static TypedWritable *make_texcoord_from_bam(const FactoryParams &params);
153 
154 public:
155  static TypeHandle get_class_type() {
156  return _type_handle;
157  }
158  static void init_type() {
159  TypedWritableReferenceCount::init_type();
160  register_type(_type_handle, "InternalName",
161  TypedWritableReferenceCount::get_class_type());
162  // The _texcoord_type_handle is defined only to support older bam files,
163  // generated before we renamed the type to InternalName.
164  register_type(_texcoord_type_handle, "TexCoordName",
165  TypedWritableReferenceCount::get_class_type());
166  }
167  virtual TypeHandle get_type() const {
168  return get_class_type();
169  }
170  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
171 
172 private:
173  static TypeHandle _type_handle;
174  static TypeHandle _texcoord_type_handle;
175 };
176 
177 // We can safely redefine this as a no-op.
178 template<>
179 INLINE void PointerToBase<InternalName>::update_type(To *ptr) {}
180 
181 INLINE std::ostream &operator << (std::ostream &out, const InternalName &tcn);
182 
183 /**
184  * This is a const pointer to an InternalName, and should be used in lieu of a
185  * CPT(InternalName) in function arguments. The extra feature that it offers
186  * is that it has a constructor to automatically convert from a string, so
187  * that strings are coerced by the compiler when passed to such a function.
188  */
189 #ifdef CPPPARSER
190 // The construct below confuses interrogate, so we give it a typedef.
192 #else
193 class CPT_InternalName : public ConstPointerTo<InternalName> {
194 public:
195  INLINE CPT_InternalName(const To *ptr = nullptr);
196  INLINE CPT_InternalName(const PointerTo<InternalName> &copy);
197  INLINE CPT_InternalName(PointerTo<InternalName> &&from) noexcept;
199  INLINE CPT_InternalName(ConstPointerTo<InternalName> &&from) noexcept;
200  INLINE CPT_InternalName(const std::string &name);
201 
202  template<int N>
203  INLINE CPT_InternalName(const char (&literal)[N]);
204 
205  INLINE CPT_InternalName &operator = (const To *ptr);
206  INLINE CPT_InternalName &operator = (const PointerTo<InternalName> &copy);
207  INLINE CPT_InternalName &operator = (const ConstPointerTo<InternalName> &copy);
208  INLINE CPT_InternalName &operator = (PointerTo<InternalName> &&from) noexcept;
209  INLINE CPT_InternalName &operator = (ConstPointerTo<InternalName> &&from) noexcept;
210 };
211 
212 INLINE void swap(CPT_InternalName &one, CPT_InternalName &two) noexcept {
213  one.swap(two);
214 }
215 #endif // CPPPARSER
216 
217 #include "internalName.I"
218 
219 #endif
pandabase.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
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
pointer_hash
This is the default hash_compare class, which assumes the Key is a pointer value.
Definition: stl_compares.h:131
lightMutex.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
InternalName
Encodes a string name in a hash table, mapping it to a pointer.
Definition: internalName.h:38
typedWritableReferenceCount.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
LightMutex
This is a standard, non-reentrant mutex, similar to the Mutex class.
Definition: lightMutex.h:39
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritable::finalize
virtual void finalize(BamReader *manager)
Called by the BamReader to perform any final actions needed for setting up the object after all objec...
Definition: typedWritable.cxx:113
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PointerToBase
This is the base class for PointerTo and ConstPointerTo.
Definition: pointerToBase.h:29
lightMutexHolder.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritable::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: typedWritable.cxx:54
CPT_InternalName
This is a const pointer to an InternalName, and should be used in lieu of a CPT(InternalName) in func...
Definition: internalName.h:193
PointerTo
This file defines the classes PointerTo and ConstPointerTo (and their abbreviations,...
Definition: pointerTo.h:69
ReferenceCount::unref
virtual bool unref() const
Explicitly decrements the reference count.
Definition: referenceCount.I:179
TypedWritableReferenceCount
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
Definition: typedWritableReferenceCount.h:31
internalName.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
ConstPointerTo< InternalName >
pointerTo.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.