1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
/*!
\class SoType Inventor/SoType.h
\brief The SoType class is the basis for the run-time type system in Coin.
\ingroup general
Many of the classes in the Coin library must have their type
information registered before any instances are created (including,
but not limited to: engines, nodes, fields, actions, nodekits and
manipulators). The use of SoType to store this information provides
lots of various functionality for working with class hierarchies,
comparing class types, instantiating objects from classnames, etc
etc.
It is for instance possible to do things like this:
\code
void cleanLens(SoNode * anode)
{
assert(anode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId()));
if (anode->getTypeId() == SoPerspectiveCamera::getClassTypeId()) {
// do something..
}
else if (anode->getTypeId() == SoOrthographicCamera::getClassTypeId()) {
// do something..
}
else {
SoDebugError::postWarning("cleanLens", "Unknown camera type %s!\n",
anode->getTypeId().getName());
}
}
\endcode
A notable feature of the SoType class is that it is only 16 bits
long and therefore should be passed around by value for efficiency
reasons.
One important note about the use of SoType to register class
information: super classes must be registered before any of their
derived classes are.
See also \ref dynload_overview for some additional SoType-related
information.
*/
/*!
\page dynload_overview Dynamic Loading of Extension Nodes
When Coin tries to get hold of a node type object (SoType) for a
class based on the name string of the node type, it will - if no
such node type has been initialized yet - scan the file system for a
dynamically loadable extension node with that given name. This can
be completely disabled by setting the environment variable
COIN_NO_SOTYPE_DYNLOAD to a positive integer value, new from Coin
v2.5.0.
On UNIX, extensions nodes are regular .so files.
On Win32, extension nodes are built as DLLs.
On Mac OS X systems, extension nodes are built as .dylib files. (Note:
The extension nodes have to be built using the flag "-dynamiclib",
not "-bundle".)
Whether the dynamically loadable objects should be named with
or without the "lib" prefix is optional. Both schemes will work.
People don't usually program in a way so that they instantiate
new nodes through the node class' SoType object, but that is the
way nodes are created when model files are loaded. This means
that for all Coin applications that load model files, the
custom extension nodes will automatically be supported for the
model files without you having to modify their source code and
rebuild the applications.
See ftp://ftp.coin3d.org/pub/coin/src/dynloadsample.tar.gz for
an example using two dynamically loadable extension nodes. You
only use an examiner viewer to view the two extension nodes in
action.
Only a limited set of C++ compilers are supported as of yet. This
is because, to initialize the extension node, it is necessary to
know something about how the C++ compiler mangles the initClass
symbol. If we don't know that, there is no way to locate the
initClass method in the library, which means the extension node can
\e not make itself known to the type system.
If your C++ compiler is not supported, the source file to add
support for a new compiler in is src/misc/cppmangle.icc. It is
fairly trivial to add support for new compilers, but if you don't
understand how, just ask us about it. Patches with support for new
compilers are of course very welcome.
\sa SoType
\ingroup nodes
\since Coin 2.0
*/
// *************************************************************************
#include <Inventor/SoType.h>
#include <assert.h>
#include <stdlib.h> // NULL
#include <string.h> // strcmp()
#include <cctype> // toupper()
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/lists/SoTypeList.h>
// MSVC++ needs 'SbName.h' and 'SbString.h' to compile
#include <Inventor/SbName.h>
#include <Inventor/SbString.h>
#include <Inventor/SoDB.h>
#include <Inventor/lists/SbList.h>
#include <Inventor/C/tidbits.h>
#include <Inventor/C/glue/dl.h>
#include "tidbitsp.h"
#include "misc/SbHash.h"
#include "coindefs.h"
#ifndef COIN_WORKAROUND_NO_USING_STD_FUNCS
using std::toupper;
#endif // !COIN_WORKAROUND_NO_USING_STD_FUNCS
#include "cppmangle.icc"
// *************************************************************************
struct SoTypeData {
SoTypeData(const SbName theName,
const SoType type = SoType::badType(),
const SbBool ispublic = FALSE,
const uint16_t theData = 0,
const SoType theParent = SoType::badType(),
const SoType::instantiationMethod createMethod = NULL)
: name(theName), type(type), isPublic(ispublic), data(theData),
parent(theParent), method(createMethod) { };
SbName name;
SoType type;
SbBool isPublic;
uint16_t data;
SoType parent;
SoType::instantiationMethod method;
};
// OBSOLETED: this code was only active for GCC 2.7.x, and I don't
// think we support that old compiler version anyhow. Do look into if
// this is what the old SGI MIPSpro CC compiler for IRIX6.2 needs to
// stop spitting out all those linker warnings, though. 20000208 mortene.
#if 0 // obsoleted
// #if defined(NEED_TEMPLATE_DEFINITION)
template class SbList<SoTypeData *>;
// [...]
#endif // obsoleted
// *************************************************************************
// list of SoType internal data structures, indexed over SoType 'data' id.
SbList<SoTypeData *> * SoType::typedatalist = NULL;
// hash map from type name to SoType 'data' id.
typedef SbHash<int16_t, const char *> Name2IdMap;
static Name2IdMap * type_dict = NULL;
// hash map from type name to handle for dynamically loaded library
typedef SbHash<cc_libhandle, const char *> Name2HandleMap;
static Name2HandleMap * module_dict = NULL;
// hash map for flagging all the shared library names we have tried
typedef SbHash<void *, const char *> NameMap;
static NameMap * dynload_tries = NULL;
// *************************************************************************
/*!
\typedef SoType::instantiationMethod
This is a convenience typedef for the function signature of a typed
class' instantiation method. It is an extension on the original
Inventor API. Mostly only useful for internal purposes.
An instantation method will take no arguments and returns a
void-pointer to a newly allocated and initialized object of the
class type.
*/
// *************************************************************************
/*!
This static method initializes the type system.
*/
void
SoType::init(void)
{
coin_atexit((coin_atexit_f *)SoType::clean, CC_ATEXIT_SOTYPE);
// If any of these assert fails, it is probably because
// SoType::init() has been called for a second time. --mortene
assert(SoType::typedatalist == NULL);
SoType::typedatalist = new SbList<SoTypeData *>;
type_dict = new Name2IdMap;
SoType::typedatalist->append(new SoTypeData(SbName("BadType")));
type_dict->put(SbName("BadType").getString(), 0);
dynload_tries = new NameMap;
}
// Clean up internal resource usage.
void
SoType::clean(void)
{
// clean SoType::typedatalist (first delete structures)
const int num = SoType::typedatalist->getLength();
for (int i = 0; i < num; i++) delete (*SoType::typedatalist)[i];
delete SoType::typedatalist;
SoType::typedatalist = NULL;
delete dynload_tries;
dynload_tries = NULL;
delete type_dict;
type_dict = NULL;
delete module_dict;
module_dict = NULL;
}
/*!
This method creates and registers a new class type.
Classes that do not inherit any other class should use
SoType::badType() for the first argument. Abstract classes should
use \c NULL for the \a method argument.
The value passed in for the \a data parameter can be retrieved with
SoType::getData().
*/
const SoType
SoType::createType(const SoType parent, const SbName name,
const instantiationMethod method,
const uint16_t data)
{
#if COIN_DEBUG
// We don't use SoType::fromName() to test if a type with this name
// already exists to avoid loading extension nodes in this context.
// You should be able to "override" dynamically loadable nodes in program
// code.
// FIXME: We ought to factor out and expose this functionality - testing
// if a class type is already loaded and registered - in the public API.
// 20040831 larsa (ref could-have-been-used-to-fix-upgrader-slowness-bug)
int16_t discard;
if (type_dict->get(name.getString(), discard)) {
SoDebugError::post("SoType::createType",
"a type with name ``%s'' already created",
name.getString());
return SoType::fromName(name.getString());
}
#endif // COIN_DEBUG
#if COIN_DEBUG && 0 // debug
SoDebugError::postInfo("SoType::createType", "%s", name.getString());
#endif // debug
SoType newType;
newType.index = SoType::typedatalist->getLength();
SoTypeData * typeData = new SoTypeData(name, newType, TRUE, data, parent, method);
SoType::typedatalist->append(typeData);
// add to dictionary for fast lookup
type_dict->put(name.getString(), newType.getKey());
return newType;
}
/*!
This method removes class type from the class system.
Returns FALSE if a type with the given name doesn't exist.
\since Coin 3.0
*/
SbBool
SoType::removeType(const SbName & name)
{
int16_t index = 0;
if (!type_dict->get(name.getString(), index)) {
SoDebugError::post("SoType::removeType",
"type with name ``%s'' not found",
name.getString());
return FALSE;
}
type_dict->remove(name.getString());
SoTypeData *typedata = (*SoType::typedatalist)[index];
(*SoType::typedatalist)[index] = NULL;
delete typedata;
#if COIN_DEBUG && 0 // debug
SoDebugError::postInfo("SoType::removeType", "%s", name.getString());
#endif // debug
return TRUE;
}
/*!
This method makes a new class's instantiation method override
the instantiation method of an existing class.
The new type should be a C++ subclass of the original class type, but
this won't be checked though.
If \c NULL is passed as the second argument, the type will be
considered uninstantiable -- it does not revert the configuration to
the default setting as one might think.
Here's a \e complete code examples which shows how to fully override
a built-in Coin node class, so that a) your application-specific
extension class gets instantiated instead of the built-in class upon
scenegraph import, and b) it gets written out properly upon export:
\code
#include <Inventor/SoDB.h>
#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoWWWInline.h>
////// MyWWWInline ////////////////////////////////////////////////////
class MyWWWInline : public SoWWWInline {
SO_NODE_HEADER(MyWWWInline);
public:
static void initClass(void);
MyWWWInline(void);
protected:
virtual ~MyWWWInline();
virtual SbBool readInstance(SoInput * in, unsigned short flags);
virtual const char * getFileFormatName(void) const;
};
SO_NODE_SOURCE(MyWWWInline);
MyWWWInline::MyWWWInline(void)
{
SO_NODE_CONSTRUCTOR(MyWWWInline);
// Fool the library to believe this is an internal class, so it gets
// written out in the same manner as the built-in classes, instead
// of as en extension class. There are slight differences, which you
// want to avoid when overriding a class like we do with MyWWWInline
// vs SoWWWInline here.
this->isBuiltIn = TRUE;
}
MyWWWInline::~MyWWWInline()
{
}
void
MyWWWInline::initClass(void)
{
SO_NODE_INIT_CLASS(MyWWWInline, SoWWWInline, "SoWWWInline");
// Override instantiation method, so we get MyWWWInline instead of
// SoWWWInline instances upon scenegraph import.
(void)SoType::overrideType(SoWWWInline::getClassTypeId(),
MyWWWInline::createInstance);
}
// Override SoBase::getFileFormatName() to make node get written as
// "WWWInline" instead of "MyWWWInline".
const char *
MyWWWInline::getFileFormatName(void) const
{
return "WWWInline";
}
SbBool
MyWWWInline::readInstance(SoInput * in, unsigned short flags)
{
SoDebugError::postInfo("MyWWWInline::readInstance", "hepp");
return SoWWWInline::readInstance(in, flags);
}
////// main() /////////////////////////////////////////////////////////
int
main(int argc, char ** argv)
{
SoDB::init();
MyWWWInline::initClass();
const char * ivscene =
"#Inventor V2.1 ascii\n\n"
"Separator {"
" WWWInline { }"
"}";
SoInput in;
in.setBuffer((void *)ivscene, strlen(ivscene));
SoSeparator * root = SoDB::readAll(&in);
root->ref();
SoOutput out;
SoWriteAction wa(&out);
wa.apply(root);
root->unref();
return 0;
}
\endcode
*/
const SoType
SoType::overrideType(const SoType originalType,
const instantiationMethod method)
{
(*SoType::typedatalist)[(int)originalType.getKey()]->method = method;
return originalType;
}
// FIXME: when doing multi-threaded programming with Coin, one might want to
// override a type only for the local thread. A new method in the API seems
// to be needed (overrideThreadLocalType?). This change will of course have
// far-reaching consequences for the whole SoType class implementation.
// 2002-01-24 larsa
// FIXME: investigate how So-prefix stripping works for non-builtin
// extension nodes. It probably works as for builtins, given that the
// class in question isn't named with an "So" prefix. 20030223 larsa
#ifdef _MSC_VER
typedef void __cdecl initClassFunction(void);
#else
typedef void initClassFunction(void);
#endif
/*!
This static function returns the SoType object associated with name \a name.
Type objects for builtin types can be retreived by name both with and
without the "So" prefix. For dynamically loadable extension nodes, the
name given to this function must match exactly.
If no node type with the given name has been initialized, a dynamically
loadable extension node with the given name is searched for. If one is
found, it is loaded and initialized, and the SoType object for the
newly initialized class type returned. If no module is found, or the
initialization of the module fails, SoType::badType() is returned.
Support for dynamically loadable extension nodes varies from
platform to platform, and from compiler suite to compiler suite.
So far code built with the following compilers are supported: GNU
GCC v2, GNU GCC v3, Microsoft Visual C++ v6 (and probably v7, not
tested), SGI MIPSPro v7.
Extensions built with compilers that are known to be binary
compatible with the above compilers are also supported, such as
e.g. the Intel x86 compiler compatible with MSVC++.
To support dynamic loading for other compilers, we need to know how
the compiler mangles the "static void classname::initClass(void)"
symbol. If your compiler is not supported, tell us at \c
coin-support@coin3d.org which it is and send us the output of a
symbol-dump on the shared object. Typically you can do
\code
$ nm <Node>.so | grep initClass
\endcode
to find the relevant mangled symbol.
*/
SoType
SoType::fromName(const SbName name)
{
static int enable_dynload = -1;
if (enable_dynload == -1) {
enable_dynload = TRUE; // the default setting
const char * env = coin_getenv("COIN_NO_SOTYPE_DYNLOAD");
if (env && atoi(env) > 0) enable_dynload = FALSE;
}
assert((type_dict != NULL) && "SoType static class data not yet initialized");
// It should be possible to specify a type name with the "So" prefix
// and get the correct type id, even though the types in some type
// hierarchies are named internally without the prefix.
SbString tmp(name.getString());
if ( tmp.compareSubString("So") == 0 ) tmp = tmp.getSubString(2);
SbName noprefixname(tmp);
int16_t index = 0;
if (!type_dict->get(name.getString(), index) &&
!type_dict->get(noprefixname.getString(), index)) {
if ( !SoDB::isInitialized() ) {
return SoType::badType();
}
if (enable_dynload) {
// find out which C++ name mangling scheme the compiler uses
static mangleFunc * manglefunc = getManglingFunction();
if ( manglefunc == NULL ) {
// dynamic loading is not yet supported for this compiler suite
static long first = 1;
if ( first ) {
const char * env = coin_getenv("COIN_DEBUG_DL");
if (env && (atoi(env) > 0)) {
SoDebugError::post("SoType::fromName",
"unable to figure out the C++ name mangling scheme");
}
first = 0;
}
return SoType::badType();
}
SbString mangled = manglefunc(name.getString());
if ( module_dict == NULL ) {
module_dict = new Name2HandleMap;
}
// FIXME: should we search the application code for the initClass()
// symbol first? dlopen(NULL) might not be portable enough, but it
// could be a cool feature. 20030223 larsa
// FIXME: We probably should use loadable modules (type MH_BUNDLE)
// instead of shared libraries for dynamic extension nodes, on Mac
// OS X, since (1) this is the Recommended Way for dynamically
// loadable code and (2) it allows us to unload them when they are
// no longer needed. Note that this would require major changes to
// the Mac cc_dl_open() and cc_dl_sym() code. 20030318 kyrah
static const char * modulenamepatterns[] = {
"%s.so", "lib%s.so", "%s.dll", "lib%s.dll", "%s.dylib", "lib%s.dylib",
NULL
};
SbString modulenamestring;
cc_libhandle handle = NULL;
int i;
for ( i = 0; (modulenamepatterns[i] != NULL) && (handle == NULL); i++ ) {
modulenamestring.sprintf(modulenamepatterns[i], name.getString());
// We need to move the name string to an SbName since we use
// the name string pointer for hash tables and need identical
// names to produce the same pointers.
SbName module(modulenamestring.getString());
// Register all the module names we have tried so we don't try
// them again.
if (dynload_tries == NULL) dynload_tries = new NameMap;
void * dummy;
if (dynload_tries->get(module.getString(), dummy))
continue; // already tried
dynload_tries->put(module.getString(), NULL);
cc_libhandle idx = NULL;
if ( module_dict->get(module.getString(), idx) ) {
// Module has been loaded, but type is not yet finished initializing.
// SoType::badType() is here the expected return value. See below.
return SoType::badType();
}
// FIXME: should we maybe use a Coin-specific search path variable
// instead of the LD_LIBRARY_PATH one? 20020216 larsa
handle = cc_dl_open(module.getString());
if ( handle != NULL ) {
// We register the module so we don't recurse infinitely in the
// initClass() function which calls SoType::fromName() on itself
// which expects SoType::badType() in return. See above.
module_dict->put(module.getString(), handle);
if (i > 0) {
// We now know the file pattern used on this system, so we
// should prioritize that pattern first.
const char * pattern = modulenamepatterns[i];
modulenamepatterns[i] = modulenamepatterns[0];
modulenamepatterns[0] = pattern;
}
}
}
if ( handle == NULL ) return SoType::badType();
// find and invoke the initClass() function.
// FIXME: declspec stuff
initClassFunction * initClass = (initClassFunction *) cc_dl_sym(handle, mangled.getString());
if ( initClass == NULL ) {
// FIXME: if a module is found and opened and initialization
// fails, the remaining module name patterns are not tried.
// might trigger as a problem one day... 2030224 larsa
#if COIN_DEBUG
SoDebugError::postWarning("SoType::fromName",
"Mangled symbol %s not found in module %s. "
"It might be compiled with the wrong compiler / "
"compiler-settings or something similar.",
mangled.getString(), modulenamestring.getString());
#endif
cc_dl_close(handle);
return SoType::badType();
}
initClass();
// We run these tests to get the index.
if (!type_dict->get(name.getString(), index) &&
!type_dict->get(noprefixname.getString(), index)) {
assert(0 && "how did this happen?");
}
}
}
assert(index >= 0 && index < SoType::typedatalist->getLength());
assert(((*SoType::typedatalist)[index]->name == name) ||
((*SoType::typedatalist)[index]->name == noprefixname));
return (*SoType::typedatalist)[index]->type;
}
/*!
Find and return a type from the given key ID.
*/
SoType
SoType::fromKey(uint16_t key)
{
assert(SoType::typedatalist);
assert(key < SoType::typedatalist->getLength());
return (*SoType::typedatalist)[(int)key]->type;
}
/*!
This method returns the name of the SoBase-derived class type the SoType
object is configured for.
*/
SbName
SoType::getName(void) const
{
assert(!this->isBad());
return (*SoType::typedatalist)[(int)this->getKey()]->name;
}
/*!
\fn uint16_t SoType::getData(void) const
This method returns a type specific data variable.
*/
uint16_t
SoType::getData(void) const
{
assert(!this->isBad());
return (*SoType::typedatalist)[(int)this->getKey()]->data;
}
/*!
This method returns the SoType type for the parent class of the
SoBase-derived class the SoType object is configured for.
*/
const SoType
SoType::getParent(void) const
{
assert(!this->isBad());
return (*SoType::typedatalist)[(int)this->getKey()]->parent;
}
/*!
This method returns an illegal type, useful for returning errors.
\sa SbBool SoType::isBad() const
*/
SoType
SoType::badType(void)
{
SoType bad;
// Important note: internally in Coin (in the various initClass()
// methods for nodes, engines, fields, etc etc), we depend on the
// bitpattern for SoType::badType() to equal 0x0000.
bad.index = 0;
return bad;
}
/*!
\fn SbBool SoType::isBad(void) const
This method returns TRUE if the SoType object represents an illegal class
type.
*/
/*!
This method returns \c TRUE if the given type is derived from (or \e
is) the \a parent type, and \c FALSE otherwise.
*/
SbBool
SoType::isDerivedFrom(const SoType parent) const
{
assert(!this->isBad());
if (parent.isBad()) {
#if COIN_DEBUG
SoDebugError::postWarning("SoType::isDerivedFrom",
"can't compare type '%s' against an invalid type",
this->getName().getString());
#endif // COIN_DEBUG
return FALSE;
}
SoType type = *this;
do {
#if COIN_DEBUG && 0 // debug
SoDebugError::postInfo("SoType::isDerivedFrom",
"this: '%s' parent: '%s'",
type.getName().getString(),
parent.getName().getString());
#endif // debug
if (type == parent) return TRUE;
type = (*SoType::typedatalist)[(int)type.getKey()]->parent;
} while (!type.isBad());
return FALSE;
}
/*!
This method appends all the class types derived from \a type to \a list,
and returns the number of types added to the list. Internal types are not
included in the list, nor are they counted.
\a type itself is also added to the list, as a type is seen as a derivation
of its own type.
NB: do not write code which depends in any way on the order of the
elements returned in \a list.
Here is a small, stand-alone example which shows how this method can
be used for introspection, listing all subclasses of the SoBase
superclass:
\code
#include <stdio.h>
#include <Inventor/SoDB.h>
#include <Inventor/lists/SoTypeList.h>
static void
list_subtypes(SoType t, unsigned int indent = 0)
{
SoTypeList tl;
SoType::getAllDerivedFrom(t, tl);
for (unsigned int i=0; i < indent; i++) { printf(" "); }
printf("%s\n", t.getName().getString());
indent++;
for (int j=0; j < tl.getLength(); j++) {
if (tl[j].getParent() == t) { // only interested in direct descendents
list_subtypes(tl[j], indent);
}
}
}
int
main(void)
{
SoDB::init();
list_subtypes(SoType::fromName("SoBase"));
return 0;
}
\endcode
*/
int
SoType::getAllDerivedFrom(const SoType type, SoTypeList & list)
{
assert(type != SoType::badType() && "argument is badType()");
int counter = 0;
int n = SoType::typedatalist->getLength();
for (int i = 0; i < n; i++) {
if ((*SoType::typedatalist)[i]) {
SoType chktype = (*SoType::typedatalist)[i]->type;
if (!chktype.isInternal() && chktype.isDerivedFrom(type)) {
list.append(chktype);
counter++;
}
}
}
return counter;
}
/*!
This method returns \c FALSE for abstract base classes, and \c TRUE
for class types that can be instantiated.
*/
SbBool
SoType::canCreateInstance(void) const
{
return ((*SoType::typedatalist)[(int)this->getKey()]->method != NULL);
}
/*!
This method instantiates an object of the current type.
For types that can not be instantiated, \c NULL is returned.
\DANGEROUS_ALLOC_RETURN
This is not harmful if you only call SoType::createInstance() on
types for reference counted class-types, though. These include all
nodes, engines, paths, nodekits, draggers and manipulators.
*/
void *
SoType::createInstance(void) const
{
if (this->canCreateInstance()) {
return (*((*SoType::typedatalist)[(int)this->getKey()]->method))();
}
else {
#if COIN_DEBUG
SoDebugError::postWarning("SoType::createInstance",
"can't create instance of class type '%s', "
" use SoType::canCreateInstance()",
this->getName().getString());
#endif // COIN_DEBUG
return NULL;
}
}
/*!
This function returns the number of types registered in the run-time type
system.
*/
int
SoType::getNumTypes(void)
{
// FIXME: typedatalist can contain entries for removed types, so the number
// returned from this method is potentially too high. kintel 20080605.
return SoType::typedatalist->getLength();
}
/*!
Returns a pointer to the method used to instantiate objects of the given
type.
\COIN_FUNCTION_EXTENSION
\since Coin 2.0
*/
SoType::instantiationMethod
SoType::getInstantiationMethod(void) const
{
return (*SoType::typedatalist)[(int)this->getKey()]->method;
}
/*!
\fn int16_t SoType::getKey(void) const
This method returns the type's index in the internal typelist.
*/
/*!
This method turns the specific type into an internal type.
*/
void
SoType::makeInternal(void)
{
(*SoType::typedatalist)[(int)this->getKey()]->isPublic = FALSE;
}
/*!
This function returns TRUE if the type is an internal type.
*/
SbBool
SoType::isInternal(void) const
{
return (*SoType::typedatalist)[(int)this->getKey()]->isPublic == FALSE;
}
/*!
\fn SbBool SoType::operator != (const SoType type) const
Check type inequality.
*/
/*!
\fn SbBool SoType::operator == (const SoType type) const
Check type equality.
*/
/*!
\fn SbBool SoType::operator < (const SoType type) const
Comparison operator for sorting type data according to some internal
criterion.
*/
/*!
\fn SbBool SoType::operator <= (const SoType type) const
Comparison operator for sorting type data according to some internal
criterion.
*/
/*!
\fn SbBool SoType::operator >= (const SoType type) const
Comparison operator for sorting type data according to some internal
criterion.
*/
/*!
\fn SbBool SoType::operator > (const SoType type) const
Comparison operator for sorting type data according to some internal
criterion.
*/
#ifdef COIN_TEST_SUITE
#include <Inventor/SoType.h>
#include <Inventor/SbName.h>
#include <Inventor/nodes/SoNode.h>
static void * createInstance(void)
{
return (void *)0x1234;
}
BOOST_AUTO_TEST_CASE(testRemoveType)
{
BOOST_CHECK_MESSAGE(SoType::fromName(SbName("MyClass")) == SoType::badType(),
"Type didn't init to badType");
SoType newtype = SoType::createType(SoNode::getClassTypeId(), SbName("MyClass"), createInstance, 0);
BOOST_CHECK_MESSAGE(SoType::fromName(SbName("MyClass")) != SoType::badType(),
"Type didn't init correctly");
bool success = SoType::removeType(SbName("MyClass"));
BOOST_CHECK_MESSAGE(success,
"removeType() failed");
BOOST_CHECK_MESSAGE(SoType::fromName(SbName("MyClass")) == SoType::badType(),
"Type didn't deregister correctly");
}
#endif // COIN_TEST_SUITE
|