H3D API  2.4.1
RefCountMField.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 __REFCOUNTMFIELD_H__
29 #define __REFCOUNTMFIELD_H__
30 
31 #include <H3D/MField.h>
33 
34 namespace H3D {
35 
40 
46  template< class RefClass >
48  public MFieldBase< RefClass *,
49  FieldInternals::MFNodeAutoRefVector< RefClass >,
50  Field > {
51  public:
52 
54 
55  typedef MFieldBase< RefClass *,
56  FieldInternals::MFNodeAutoRefVector< RefClass >,
58 
62 
63 
66  AUTOREF_DEBUG_NAME( value, "RefCountMField::value")
67  this->value.owner = this;
68  }
69 
72  MFieldBase< RefClass *,
73  FieldInternals::MFNodeAutoRefVector< RefClass >,
74  Field >( sz ){
75  AUTOREF_DEBUG_NAME( value, "RefCountMField::value")
76  this->value.owner = this;
77  }
78 
79  ~RefCountMField() {
80  this->value.clear();
81  };
82 
84  inline virtual const NodeVector &getValue( int id = 0 ) {
85  this->checkAccessTypeGet( id );
86  this->upToDate();
87  return this->value;
88  }
89 
91  inline virtual RefClass * getValueByIndex(
92  typename BaseFieldType::size_type i,
93  int id = 0 ) {
94  this->checkAccessTypeGet( id );
95  this->upToDate();
96  // i < 0 is never true..
97  if( /* i < 0 || */i >= this->value.size() ) {
98  stringstream s;
99  s << "Trying to access value outside the bounds of field "
100  << this->getFullName() << ". Field has size " << this->value.size()
101  << ". ";
102  throw InvalidIndex( i, s.str(), H3D_FULL_LOCATION );
103  }
104  return this->value[i];
105  }
106 
114  int id = 0 ) {
115  this->checkAccessTypeSet( id );
116  this->value.set( i, v );
117  // generate an event.
118  this->startEvent();
119  }
120 
125  inline virtual void setValue( const AutoRefVector< RefClass > &v,
126  int id = 0 ) {
127  this->checkAccessTypeSet( id );
128  this->value = v;
129  this->startEvent();
130  }
131 
136  inline virtual void setValue( const vector< RefClass * > &v, int id = 0 ) {
137  this->checkAccessTypeSet( id );
138  this->value = v;
139  this->startEvent();
140  }
141 
146  inline virtual void swap( vector< RefClass * > &v, int id = 0 ) {
147  this->checkAccessTypeSet( id );
148  this->checkAccessTypeGet( id );
149  this->upToDate();
150  this->value.swap( v );
151  this->startEvent();
152  }
153 
158  inline virtual void erase( RefClass *a, int id = 0 ) {
159  this->checkAccessTypeSet( id );
160  this->upToDate();
161  this->value.erase( a );
162  this->startEvent();
163  }
164 
169  inline virtual void erase( unsigned int pos, int id = 0 ) {
170  this->checkAccessTypeSet( id );
171  this->upToDate();
172  this->value.erase( pos );
173  this->startEvent();
174  }
175 
181  inline virtual void insert( unsigned int pos,
182  RefClass *a,
183  int id = 0 ) {
184  this->checkAccessTypeSet( id );
185  this->upToDate();
186  this->value.insert( pos, a );
187  this->startEvent();
188  }
189 
191  virtual string getTypeName() {
192  return classTypeName();
193  }
194 
196  static string classTypeName() {
197  return "RefCountMField";
198  }
199 
200  protected:
201  friend class FieldInternals::MFNodeAutoRefVector<RefClass>;
202 
204  inline virtual void update() {
205  this->value = static_cast< RefCountMField* >(this->event.ptr)->getValue();
206  }
207 
208  virtual RefClass *preOnAdd( RefClass *n ) {
209  return n;
210  }
211 
212  virtual RefClass *preOnRemove( RefClass *n ) {
213  return n;
214  }
215 
216 H3D_PUSH_WARNINGS()
217 H3D_DISABLE_UNUSED_PARAMETER_WARNING()
222  virtual void onAdd( RefClass *n ) {
223 #ifdef REF_COUNT_DEBUG
224  Console(LogLevel::Debug) << "RefCountMField::onAdd( " << getFullName() << " = " << this
225  << ", n = " << n << ") " << endl;
226 #endif
227  }
228 
233  virtual void onRemove( RefClass *n ) {
234 #ifdef REF_COUNT_DEBUG
235  Console(LogLevel::Debug) << "RefCountMField::onRemove( " << getFullName() << " = "
236  << this << ", n = " << n << ") " << endl;
237 #endif
238  }
239 H3D_POP_WARNINGS()
240  };
241 
242 }
243 
244 #endif
#define H3D_FULL_LOCATION
Contains the MFNodeAutoRefVector class.
Contains base classes used to create multivalued fields.
std::vector< RefCountedClassType * >::size_type size_type
std::vector< RefCountedClassType * >::value_type value_type
The Field class.
Definition: Field.h:46
The common base class for MField types and MFNode.
Definition: MField.h:105
FieldInternals::MFNodeAutoRefVector< RefClass > value
The encapsulated vector.
Definition: MField.h:346
VectorClass::size_type size_type
An unsigned integral type.
Definition: MField.h:118
Thrown if the index given to getValueByIndex() is outside the boundaries.
Definition: RefCountMField.h:61
RefCountMField is almost like any MField but it encapsulates a vector of RefCoundtedClass pointers.
Definition: RefCountMField.h:50
virtual void insert(unsigned int pos, RefClass *a, int id=0)
Insert an element before the index given by pos.
Definition: RefCountMField.h:181
virtual void erase(RefClass *a, int id=0)
Erase the first element equal to a.
Definition: RefCountMField.h:158
RefCountMField(typename FieldInternals::MFNodeAutoRefVector< RefClass >::size_type sz)
Creates an RefCountMField with space reserved for size nodes.
Definition: RefCountMField.h:71
virtual void setValue(const vector< RefClass * > &v, int id=0)
Set the value of the field.
Definition: RefCountMField.h:136
virtual void onRemove(RefClass *n)
This function will be called when values of RefCountMField changes.
Definition: RefCountMField.h:233
virtual string getTypeName()
Returns a string name for this field type i.e. SFNode.
Definition: RefCountMField.h:191
static string classTypeName()
Returns a string name for this field type i.e. SFNode.
Definition: RefCountMField.h:196
virtual const NodeVector & getValue(int id=0)
Get the value of the MField.
Definition: RefCountMField.h:84
void setValue(typename FieldInternals::MFNodeAutoRefVector< RefClass >::size_type i, const typename FieldInternals::MFNodeAutoRefVector< RefClass >::value_type &v, int id=0)
Set the value of an element in the field.
Definition: RefCountMField.h:112
virtual void onAdd(RefClass *n)
This function will be called when values of RefCountMField changes.
Definition: RefCountMField.h:222
virtual void setValue(const AutoRefVector< RefClass > &v, int id=0)
Set the value of the field.
Definition: RefCountMField.h:125
virtual RefClass * getValueByIndex(typename BaseFieldType::size_type i, int id=0)
Get the value of the MField.
Definition: RefCountMField.h:91
virtual void update()
Make the field up to date given that an event has occured.
Definition: RefCountMField.h:204
virtual void erase(unsigned int pos, int id=0)
Erase the element at the specified index.
Definition: RefCountMField.h:169
RefCountMField()
Default constructor. Creates an empty RefCountMField.
Definition: RefCountMField.h:65
virtual void swap(vector< RefClass * > &v, int id=0)
Swaps the contents of two vectors.
Definition: RefCountMField.h:146
H3D_VALUE_EXCEPTION(string, InvalidType)
An exception thrown when a field is of the wrong type when it is checked.
H3D API namespace.
Definition: Anchor.h:38
AutoRefVector< Node > NodeVector
NodeVector is a vector of nodes that manages reference counting on the Nodes added to or removed from...
Definition: RefCountMField.h:39