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