H3D API  2.4.1
RefCountSField.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
26 //
28 #ifndef __REFCOUNTSFIELD_H__
29 #define __REFCOUNTSFIELD_H__
30 
31 #include <H3DUtil/AutoRef.h>
33 #include <H3D/Field.h>
34 
35 namespace H3D {
36 
41  template< class RefClass, class BaseField = H3D::Field >
42  class RefCountSField: public BaseField {
43  public:
47  typedef RefClass typed_class_type;
51  typedef RefClass * typed_value_type;
53  typedef RefClass class_type;
55  typedef RefClass * value_type;
56 
58  RefCountSField(): value( NULL ) {
59  AUTOREF_DEBUG_NAME( value, "RefCountSField::value")
60  value.rc_field = this;
61  };
62 
64  RefCountSField( RefClass *_value ) : value( _value ) {
65  AUTOREF_DEBUG_NAME( value, "RefCountSField::value")
66  value.rc_field = this;
67  }
68 
70  inline virtual void setValue( const AutoRef< RefClass > &n, int id = 0 );
71 
73  inline virtual void setValue( RefClass *n, int id = 0 );
74 
76  inline virtual RefClass *getValue( int id = 0 );
77 
78  protected:
80  inline virtual void update();
81 
82  virtual RefClass *preOnAdd( RefClass *n ) {
83  return n;
84  }
85 
86  virtual RefClass *preOnRemove( RefClass *n ) {
87  return n;
88  }
89 
95  class RefCountSFieldAutoRef: public AutoRef< RefClass > {
96  public :
97 
99  explicit RefCountSFieldAutoRef( RefClass* n = 0) throw() :
100  AutoRef< RefClass >( n ),
101  rc_field( NULL ) {
102 
103  }
104 
106  RefCountSFieldAutoRef& operator=(RefClass* n) throw() {
107  this->reset( n );
108  return *this;
109  }
110 
112  template< class RefClassClass >
113  RefCountSFieldAutoRef& operator=(const AutoRef<RefClassClass> &n)
114  throw() {
115  this->reset( n.get() );
116  return *this;
117  }
118 
120  ~RefCountSFieldAutoRef() throw() {
121  this->reset( NULL );
122  }
123 
131  inline virtual void ref( RefClass *n ) {
132  if( rc_field && n ) {
134  rc_field->onAdd( rc_field->preOnAdd( n ) );
135  }
136  }
137 
144  inline virtual void unref( RefClass *n ) {
145  if( rc_field && n ) {
146  rc_field->onRemove( rc_field->preOnRemove( n ) );
148  }
149  }
151  RefCountSField *rc_field;
152  };
153 #ifdef __BORLANDC__
154  friend class RefCountSFieldAutoRef;
155 #endif
156 H3D_PUSH_WARNINGS()
157 H3D_DISABLE_UNUSED_PARAMETER_WARNING()
164  virtual void onAdd( RefClass *n ) {
165 #ifdef REF_COUNT_DEBUG
166  Console(LogLevel::Debug) << "RefCountSField: Add node" << endl;
167 #endif
168  }
169 
176  virtual void onRemove( RefClass *n ) {
177 #ifdef REF_COUNT_DEBUG
178  Console(LogLevel::Debug) << "RefCountSField: Remove node" << endl;
179 #endif
180  }
181 
183  RefCountSFieldAutoRef value;
184  };
185 H3D_POP_WARNINGS()
186 
187  template< class RefClass, class BaseField >
188  void RefCountSField< RefClass, BaseField >::update() {
189 #ifdef REF_COUNT_DEBUG
190  Console(LogLevel::Debug) << "RefCountSField(" << name << ")::update() event_ptr = "
191  << event.ptr << endl;
192 #endif
193  value = static_cast< RefCountSField* >(this->event.ptr)->getValue();
194 
195  }
196 
197  template< class RefClass, class BaseField >
199  int id ) {
200  this->checkAccessTypeSet( id );
201 #ifdef REF_COUNT_DEBUG
202  Console(LogLevel::Debug) << "RefCountSField(" << name << ")::setValue()" << endl;
203 #endif
204  value = n;
205  // generate an event.
206  this->startEvent();
207  }
208 
209  template< class RefClass, class BaseField >
211  const AutoRef< RefClass > &n,
212  int id ) {
213  this->setValue( n.get(), id );
214  }
215 
216  template< class RefClass, class BaseField >
218 #ifdef REF_COUNT_DEBUG
219  Console(LogLevel::Debug) << "RefCountSField(" << name << ")::getValue()" << endl;
220 #endif
221  this->checkAccessTypeGet( id );
222  // check that the field is up-to-date first
223  this->upToDate();
224  return value.get();
225  }
226 
227 }
228 
229 #endif
Contains the Field class.
AutoRef< RefCountedClassType > & operator=(const AutoRef< RefCountedClassType > &ar)
virtual void unref(RefCountedClassType *n)
RefCountedClassType * get() const
void reset(RefCountedClassType *p=0)
virtual void ref(RefCountedClassType *n)
RefCountSField is almost like any SField but it encapsulates a pointer to a RefCountedClass.
Definition: RefCountSField.h:42
RefCountSField(RefClass *_value)
Constructor.
Definition: RefCountSField.h:64
virtual void onRemove(RefClass *n)
This function will be called when the value of RefCountSField changes.
Definition: RefCountSField.h:176
RefClass * value_type
The type of the value in this field.
Definition: RefCountSField.h:55
virtual void setValue(const AutoRef< RefClass > &n, int id=0)
Set the value of the field.
Definition: RefCountSField.h:210
virtual void onAdd(RefClass *n)
This function will be called when the value of RefCountSField changes.
Definition: RefCountSField.h:164
RefClass typed_class_type
The same as class_type, but if e.g.
Definition: RefCountSField.h:47
virtual void update()
Make the field up to date if an event has occured.
Definition: RefCountSField.h:188
virtual void setValue(RefClass *n, int id=0)
Set the value of the field.
Definition: RefCountSField.h:198
RefCountSFieldAutoRef value
The encapsulated value.
Definition: RefCountSField.h:183
RefCountSField()
Constructor. Default value of value is NULL.
Definition: RefCountSField.h:58
virtual RefClass * getValue(int id=0)
Get the value of the field.
Definition: RefCountSField.h:217
RefClass class_type
The type of which this field contains a pointer to.
Definition: RefCountSField.h:53
RefClass * typed_value_type
The same as value_type, but if e.g.
Definition: RefCountSField.h:51
Type getValue(const char *s, const char *&rest)
Function that reads characters from a char * and converts them to a given type.
Definition: X3DFieldConversion.h:134
H3D API namespace.
Definition: Anchor.h:38