CEDRIC  Revision_backup-2009-02
TypedSwitch.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2008 Boris Iven, Max Hermann
4  *
5  * This file is part of the "CEDRIC Event Display" application.
6  *
7  *****************************************************************************/
8 
9 #ifndef TYPEDSWITCH_H
10 #define TYPEDSWITCH_H
11 
12 #define TYPEDSWITCH_ADDCHILD_MACRO( OSGADDFUNC ) \
13  T* node = dynamic_cast<T*>( child ); \
14  if( node && !childrenAllowed() \
15  && !(node->getClassName().compare(getClassName())==0) )\
16  return false; \
17  if( !OSGADDFUNC ) return false; \
18  if( node ) updateType( node->getType() ); \
19  return true;
20 
21 #include <osg/Switch>
22 #include <osg/Node>
23 
24 #include <string>
25 
26 class TypedSwitch : public osg::Switch
27 {
28 public:
29  typedef TypedSwitch T;
30 
32 
33  TypedSwitch( std::string className ): m_className(className) {}
34 
35  virtual bool childrenAllowed() { return true; }
36 
37  const std::string& getClassName() const { return m_className; }
38  int getType() const { return m_type; }
39 
40 
41  virtual bool addChild( osg::Node* child ) { TYPEDSWITCH_ADDCHILD_MACRO( Switch::addChild(child) ) }
42  virtual bool insertChild( unsigned int index, osg::Node *child ) { TYPEDSWITCH_ADDCHILD_MACRO( Switch::insertChild(index,child) ) }
43  virtual bool setChild( unsigned int index, osg::Node *child ) { TYPEDSWITCH_ADDCHILD_MACRO( Switch::setChild(index,child) ) }
44 
45  virtual bool addChild( osg::Node* child, bool value ) { TYPEDSWITCH_ADDCHILD_MACRO( Switch::addChild(child,value) ) }
46  virtual bool insertChild( unsigned int index, osg::Node *child, bool value ) { TYPEDSWITCH_ADDCHILD_MACRO( Switch::insertChild(index,child,value) ) }
47 
48  virtual bool replaceChild( osg::Node *origChild, osg::Node *child )
49  {
50  if( !Switch::replaceChild( origChild, child ) )
51  return false;
52 
53  updateType();
54  return true;
55  }
56 
57  virtual bool removeChildren( unsigned int pos, unsigned int numChildrenToRemove )
58  {
59  if( !( (pos+numChildrenToRemove-1) < _children.size() && numChildrenToRemove > 0 ) )
60  return false;
61 
62  bool atLeastOneTNodeRemoved = false;
63  for( unsigned int i=pos; i < pos+numChildrenToRemove; ++i )
64  {
65  T* child = dynamic_cast<T*>( getChild( i ) );
66  if( child )
67  {
68  atLeastOneTNodeRemoved = true;
69  break; // leave for-loop
70  }
71  }
72 
73  if( !Switch::removeChildren( pos, numChildrenToRemove ) )
74  return false;
75 
76  if( atLeastOneTNodeRemoved ) updateType();
77  return true;
78  }
79 
80 protected:
81  void updateType( int type=UNSPECIFIED )
82  {
83  // if type is not specified acquire it from children
84  if( type == UNSPECIFIED )
85  {
86  int acquired_type = ((getNumChildren()>0) ? GROUP : UNSPECIFIED);
87  for( unsigned int i=0; i < getNumChildren(); ++i )
88  {
89  T* child = dynamic_cast<T*>( getChild(i) );
90  if( child )
91  acquired_type |= child->getType();
92  }
93  m_type = acquired_type;
94  }
95  else
96  // add to own type
97  m_type |= type;
98 
99  // tell all parents
100  for( unsigned int i=0; i < getNumParents(); ++i )
101  {
102  T* parent = dynamic_cast<T*>( getParent(i) );
103  if( parent )
104  parent->updateType( m_type );
105  }
106  }
107 
108  int m_type;
109  std::string m_className;
110 };
111 
112 #endif
int m_type
Definition: TypedSwitch.h:108
virtual bool insertChild(unsigned int index, osg::Node *child, bool value)
Definition: TypedSwitch.h:46
TypedSwitch(std::string className)
Definition: TypedSwitch.h:33
virtual bool childrenAllowed()
Definition: TypedSwitch.h:35
Definition: TypedSwitch.h:31
#define TYPEDSWITCH_ADDCHILD_MACRO(OSGADDFUNC)
Definition: TypedSwitch.h:12
ReservedTypes
Definition: TypedSwitch.h:31
const std::string & getClassName() const
Definition: TypedSwitch.h:37
virtual bool removeChildren(unsigned int pos, unsigned int numChildrenToRemove)
Definition: TypedSwitch.h:57
virtual bool setChild(unsigned int index, osg::Node *child)
Definition: TypedSwitch.h:43
int getType() const
Definition: TypedSwitch.h:38
Definition: TypedSwitch.h:31
Definition: TypedSwitch.h:26
virtual bool addChild(osg::Node *child)
Definition: TypedSwitch.h:41
std::string m_className
Definition: TypedSwitch.h:109
virtual bool insertChild(unsigned int index, osg::Node *child)
Definition: TypedSwitch.h:42
virtual bool addChild(osg::Node *child, bool value)
Definition: TypedSwitch.h:45
void updateType(int type=UNSPECIFIED)
Definition: TypedSwitch.h:81
virtual bool replaceChild(osg::Node *origChild, osg::Node *child)
Definition: TypedSwitch.h:48
TypedSwitch T
Definition: TypedSwitch.h:29