Open Inventor Release 2024.2.0
 
Loading...
Searching...
No Matches
SoBase.h
1/*=======================================================================
2 * Copyright 1991-1996, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 *
5 * UNPUBLISHED -- Rights reserved under the copyright laws of the United
6 * States. Use of a copyright notice is precautionary only and does not
7 * imply publication or disclosure.
8 *
9 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
10 * Use, duplication or disclosure by the Government is subject to restrictions
11 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
12 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
13 * in similar or successor clauses in the FAR, or the DOD or NASA FAR
14 * Supplement. Contractor/manufacturer is Silicon Graphics, Inc.,
15 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
16 *
17 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
18 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
19 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
20 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
21 * GRAPHICS, INC.
22**=======================================================================*/
23/*=======================================================================
24** Author : Paul S. Strauss (MMM yyyy)
25** Modified by : Nick Thompson (MMM yyyy)
26** Modified by : Gavin Bell (MMM yyyy)
27**=======================================================================*/
28/*=======================================================================
29 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
30 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
31 *** ***
32 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
33 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
34 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
35 *** ***
36 *** RESTRICTED RIGHTS LEGEND ***
37 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
38 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
39 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
40 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
41 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
42 *** ***
43 *** COPYRIGHT (C) 1996-2023 BY FEI S.A.S, ***
44 *** BORDEAUX, FRANCE ***
45 *** ALL RIGHTS RESERVED ***
46**=======================================================================*/
47/*=======================================================================
48** Modified by : VSG (MMM YYYY)
49**=======================================================================*/
50
51
52#ifndef _SO_BASE_
53#define _SO_BASE_
54
55#include <Inventor/misc/SoBasic.h>
56#include <Inventor/misc/SoAuditorList.h>
57#include <Inventor/misc/SoRefCounter.h>
58#include <Inventor/SbString.h>
59#include <Inventor/SoTypedObject.h>
60#include <Inventor/threads/SbThreadSpinlock.h>
61
62class SoBaseList;
63class SoInput;
64class SoNode;
65class SoNotList;
66class SoOutput;
67class SoPath;
68
69#ifdef _WIN32
70#pragma warning(push)
71#pragma warning(disable:4251)
72#endif
73
75//
76// Class: SoBase
77//
78// Base class for most important SO classes. All subclasses of this
79// class may be read and written, in the form:
80// NameOfClass {
81// <stuff inside>
82// }
83// where NameOfClass is the thing returned by getFileName(). For example,
84// the "FileName" of the SoPath class is "Path".
85//
87
88
111class SoBase: public SoRefCounter, public SoTypedObject {
112 public:
113
119 virtual void touch() { startNotify(); }
120
125
131 virtual SbName getName() const;
132
154 virtual void setName(const SbName &name);
155
160 inline void setSynchronizable( const bool b );
161
165 inline bool isSynchronizable() const ;
166
167 private:
168
169 // Constructor is protected - this is an abstract class
170 SoBase();
171
172 // Virtual destructor so that subclasses are deleted properly
173 virtual ~SoBase();
174
179 virtual bool notifyDelete() const;
180
181 // Actually deletes an instance. Allows subclasses to do other
182 // stuff before the deletion if necessary.
183 virtual void destroy();
184
185 // Returns current write counter
186 static uint32_t getCurrentWriteCounter();
187
188 // Returns TRUE if the instance has multiple write references
189 SbBool hasMultipleWriteRefs() const
190 { return writeStuff.multWriteRef; }
191
192 // Unknown nodes and engines write a different name for themselves
193 // than their typeId; this virtual method lets them do that (by
194 // default the typeId name is returned)
195 virtual const char* getFileFormatName() const;
196
197 // This set of enums is used when reading and writing the base.
198 // Each enum represents a different bit in a bit field
199 // which will be written.
200 enum BaseFlags {
201 IS_ENGINE = 1,
202 IS_GROUP = 2
203 };
204
205 // Reads stuff into instance of subclass. Return FALSE on error.
206 // If reading binary file format, the flags specify whether the
207 // object was written as an engine or a group; unknown nodes and
208 // groups need this information to read themselves in properly.
209 virtual SbBool readInstance(SoInput *in, unsigned short flags) = 0;
210
211 private:
212
213 // Setup type information
214 static void initClass();
215 static void exitClass();
216
217 // Enable/disable ART synchronization. If false, all ART serialization is disabled.
218 // Can be usefull to disable serialization in some part of code.
219 // Default is true. Return previous value.
220 static bool setSynchronizationEnabled(bool value)
221 {
222 bool old = s_synchronizationEnabled;
223 s_synchronizationEnabled = value;
224 return old;
225 }
226
227 static void resetWriteCounter();
228
229 // Increments the current write counter at the start of a write operation
230 static void incrementCurrentWriteCounter();
231
232 // Decrements the current write counter after a write operation,
233 // in some rare cases
234 static void decrementCurrentWriteCounter();
235
236 // Initiates notification from an instance. The default method
237 // does nothing, because some classes (path, sensor) never
238 // initiate notification. This is used by touch().
239 virtual void startNotify();
240
241 // Propagates modification notification through an instance. The
242 // default method here does not create and add a new record. It
243 // merely propagates the current record list to all auditors. This
244 // method may be used by subclasses to do the propagation after
245 // modifying the list appropriately.
246 virtual void notify(SoNotList *list);
247
248 // Adds/removes an auditor to/from list
249 void addAuditor(void *auditor, SoNotRec::Type type);
250 void removeAuditor(void *auditor, SoNotRec::Type type);
251
252 // Returns auditor list-- used by SoField and SoEngineOutput to
253 // trace forward connections
254 const SoAuditorList &getAuditors() const { return auditors; }
255
256 // Internal methods used to maintain the global name dictionary
257 static void addName(SoBase *, const char *);
258 static void removeName(SoBase *, const char *);
259
260 // Helper routines used to get stuff out of nameDict
261 static SoBase *getNamedBase(const SbName &, const SoType&);
262 static int getNamedBases(const SbName &, SoBaseList &, const SoType&);
263
264 // Set the net name of an instance.
265 virtual void setNetName( const SbName &netName);
266
267 // Get the net name of an instance.
268 virtual SbName getNetName() const;
269
270 // Compute automatically the net name of the instance.
271 virtual void generateNetName();
272
273 // Sets this a ScaleViz synchronized object.
274 inline void setSynchronized( const bool b );
275
276 // Gets the ScaleViz synchronized state of this object.
277 inline bool isSynchronized() const;
278
287 SbBool writeHeader(SoOutput* , SbBool , SbBool ) const;
288 void writeFooter(SoOutput* ) const;
289
290 virtual bool isGroup() { return false; }
291
292 // Reads one instance of some subclass of SoBase. Returns pointer
293 // to read-in instance in base, or NULL on EOF. Returns FALSE on
294 // error. The last parameter is a subclass type to match. If
295 // the returned base is not of this type, it is an error. A type
296 // of SoBase::getClassTypeId() will match any base.
297 static SbBool read(SoInput *in, SoBase *&base, const SoType &expectedType, SbBool createAndRead = TRUE);
298
299 // Adds a reference to the instance when writing. isFromField
300 // indicates whether the reference is from a field-to-field
301 // connection.
302 virtual void addWriteReference(SoOutput *out, SbBool isFromField = FALSE);
303
304 // Returns TRUE if the instance should be written, based on the
305 // write-reference info already accumulated
306 SbBool shouldWrite();
307
308 // This defaults to "+" and is used when naming nodes that are DEF's and USE'd.
309 // The ivdowngrade converter needs to set it to other than "+" since that was
310 // an illegal character for Inventor V1.0 names.
311 static void setInstancePrefix(const SbString &c);
312
313 // These are all packed into one word to save space
314 struct WriteStuff {
315 // This contains the value of the counter the last time the
316 // instance was written.
317 unsigned int writeCounter : 26;
318
319 // This is TRUE if the object is named.
320 unsigned int hasName : 1;
321 unsigned int hasNetName : 1;
322
323 // This is TRUE if more than one reference is made to the
324 // instance for writing, meaning that we need to DEF it
325 unsigned int multWriteRef : 1;
326
327 // This is TRUE if the instance was referenced via a
328 // field-to-field connection, which is not a "real" reference
329 unsigned int writeRefFromField : 1;
330
331 // This is TRUE if the instance was already synchronized
332 // through ScaleViz
333 unsigned int isSynchronized : 1;
334
335 // This is TRUE if the instance can be synchronized
336 // through ScaleViz
337 unsigned int isSynchronizable : 1;
338
339 // Default Constructor
340 WriteStuff();
341 };
342
343 WriteStuff writeStuff;
344
345 private:
346
347 static SoType classTypeId;
348
349 static SbString instancePrefix;
350
351 // This is incremented once per write operation. It is used to
352 // determine which instances to write.
353 static uint32_t s_currentWriteCounter;
354 static SbThreadRWMutex s_writeCounterMutex;
355
356 // List of auditors: objects to pass notification to
357 SoAuditorList auditors;
358
359 // If false, disable all ART serialization. True by default.
360 static bool s_synchronizationEnabled;
361
362 // These are used internally by writeHeader()
363 void writeDef(SoOutput *, int) const;
364 void writeRef(SoOutput *, int) const;
365 void writeAnnotation(SoOutput *) const;
366 void writeAnnotation1(SoOutput *) const;
367
368 static SbBool readReference(SoInput *in, SoBase *&base);
369 static SbBool readBase(SoInput *in, SbName &className, SoBase *&base, SbBool createAndRead = TRUE);
370 static SbBool readBaseInstance( SoInput *in,
371 const SbName &className, const SbName &refName,
372 const SbName &netName, SoBase *&base, SbBool createAndRead = TRUE);
373 static SoBase* createInstance(SoInput *in, SbName className, unsigned short ioFlags);
374
375 static void flushInput(SoInput *in);
376
377 // This dictionary stores SbPLists keyed by name (the SbPLists
378 // contain SoBases-- a BaseList isn't used because we don't want
379 // the items on the list to be reference counted, otherwise they
380 // will never get deleted).
381 static SbDict *nameObjDict;
382
383 // And this dictionary maps the other way, from an SoBase * to a
384 // name.
385 static SbDict *objNameDict;
386
387 friend class SoVRMLDB;
388};
389
390#ifdef _WIN32
391#pragma warning(pop)
392#endif
393
398{
399public:
400 SoBaseInitializer(SoBase*);
401 ~SoBaseInitializer();
402 static inline bool isActive()
403 {
404 s_activeMutex.lock();
405 bool ret = m_active;
406 s_activeMutex.unlock();
407 return ret;
408 }
409private:
410 SoBase* m_base;
411 bool m_sync;
412 bool m_prevActive;
413 static bool m_active;
414 static SbThreadSpinlock s_activeMutex;
415};
416
417
418void
420{
421 writeStuff.isSynchronizable = b;
422}
423
424bool
426{
427 return writeStuff.isSynchronizable && s_synchronizationEnabled;
428}
429
430void
431SoBase::setSynchronized( const bool b )
432{
433 writeStuff.isSynchronized = b;
434}
435
436bool
437SoBase::isSynchronized() const
438{
439 return writeStuff.isSynchronized;
440}
441
442#endif /* _SO_BASE_ */
443
Character string stored in a hash table.
Definition SbName.h:162
Class for smart character strings.
Definition SbString.h:202
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Portable readers...
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Portable spinloc...
void lock()
Request exclusive access for this spinlock.
void unlock()
Release exclusive access.
Base class for all nodes, paths, and engines.
Definition SoBase.h:111
virtual void touch()
Marks an instance as modified, simulating a change to it.
Definition SoBase.h:119
bool isSynchronizable() const
Gets the ScaleViz synchronizable state of this object.
Definition SoBase.h:425
virtual void setName(const SbName &name)
Sets the name of an instance.
friend class SoVRMLDB
Definition SoBase.h:387
virtual SbName getName() const
Returns the name of an instance.
void setSynchronizable(const bool b)
Sets this to be a ScaleViz synchronizable object.
Definition SoBase.h:419
static SoType getClassTypeId()
Returns type identifier for this class.
Maintains a list of pointers to instances of the SoBase classes.
Definition SoBaseList.h:79
Used to read Open Inventor data files.
Definition SoInput.h:363
Abstract base class for all database nodes.
Definition SoNode.h:145
Used to write Open Inventor data files.
Definition SoOutput.h:186
Path that points to a list of hierarchical nodes.
Definition SoPath.h:187
Base class for ref-counted objects.
Stores runtime type information.
Definition SoType.h:98
Base class for object storing runtime type information.
int SbBool
Boolean type.
Definition SbBase.h:87