RTFX  0.3
Real time special effects collaborative visualization and production library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
RTFXPropertyArray.h
Go to the documentation of this file.
1 #ifndef __RTFX_PROPERTY_ARRAY__
2 #define __RTFX_PROPERTY_ARRAY__
3 
4 
5 
6 #include "RTFXBoostIncludes.h"
7 
8 #include "RTFXMath.h"
9 #include "RTFXRecorder.h"
10 #include "RTFXHelpers.h"
11 #include "RTFXProperty.h"
12 
21 using namespace std;
22 
23 namespace RTFX
24 {
32  template<class T>
33  class RTFXAPI RTFXPropertyArray: public RTFXProperty
34  {
35  public:
38  {
39  SetType( RTFXProperty::BASICARRAY );
40  value = NULL;
41  length = 0;
42  }
43 
46  {
47  free( value );
48  value = NULL;
49  length = 0;
50  }
51 
53  void Copy( RTFXProperty * _p )
54  {
55  SetID( _p->GetID() );
56  SetName( _p->GetName() );
57  SetDescription( _p->GetDescription() );
58  SetOwner( _p->GetOwner() );
59  SetChanged( _p->GetChanged() );
60  SetType( _p->GetType() );
61  SetDelete( _p->GetDelete() );
62 
64  SetValue( ptr_->GetValue(), ptr_->GetLength() );
65  }
66 
68  void SetValue( T * _value, RTFXBIGINT _length )
69  {
70  boost::lock_guard<boost::mutex> lock( *dataLock );
71 
72  // If the current value is not null, then we must free the existing memory
73  if ( value == NULL )
74  free( value );
75 
76  // allocate memory for this value
77  value = ( T * )malloc( _length * sizeof( T ) );
78 
79  // copy the data into it
80  memcpy( value, _value, _length * sizeof( T ) );
81 
82  // set length value
83  length = _length;
84 
85  SetChanged( true );
86  }
87 
89  void SetValue( T * _value, RTFXBIGINT _start, RTFXBIGINT _end )
90  {
91  boost::lock_guard<boost::mutex> lock( *dataLock );
92 
93  T * newdata_ = value;
94  if ( _end > length )
95  {
96  free( value );
97 
98  newdata_ = ( T * )malloc( _end * sizeof( T ) );
99  memcpy( newdata_, _value, length * sizeof( T ) );
100  value = newdata_;
101  }
102 
103  for ( RTFXBIGINT i = 0; i < _end - _start; i ++ )
104  {
105  value[_start+i] = _value[i];
106  }
107 
108  SetChanged( true );
109  }
110 
112  T * GetValue()
113  {
114  boost::lock_guard<boost::mutex> lock( *dataLock );
115 
116  SetChanged( false );
117 
118  return value;
119  }
120 
122  T GetValueAt( RTFXBIGINT _index )
123  {
124  boost::lock_guard<boost::mutex> lock( *dataLock );
125  SetChanged( false );
126  if ( _index >= length )
127  {
128 
129  return 0;
130  }
131  else
132  {
133  T v_ = value[ _index ];
134 
135  return v_;
136  }
137  }
138 
140  void SetValueAt( T _value, RTFXBIGINT _index )
141  {
142  if ( _index >= length )
143  return;
144  else
145  value[ _index ] = _value;
146 
147  SetChanged( true );
148  }
149 
151  RTFXBIGINT GetLength() { return length; }
152 
155  bool EqualTo( RTFXProperty * _other )
156  {
157  // Check if Property types match (just in case)
158  if ( GetType() == _other->GetType() )
159  {
160  if ( memcmp( value, (( RTFXPropertyArray * )_other)->value, length ) == 0 )
161  return true;
162  }
163  return false;
164  }
165 
166  private:
167 
169  T * value;
170 
172  RTFXBIGINT length;
173 
175  friend class boost::serialization::access;
176 
177  template<class Archive>
178  void save( Archive &_ar, const unsigned int _version ) const
179  {
180  _ar << boost::serialization::base_object<RTFXProperty>( *this );
181 
182  /*(char * tmp_ = new char[sizeof( RTFXBIGINT )];
183  EncodeLong( length, tmp_ );
184  for ( unsigned int i = 0; i < sizeof( RTFXBIGINT ); i ++ )
185  _ar << tmp_[i];*/
186  _ar << length;
187 
188  for ( unsigned int i = 0; i < length; i ++ )
189  {
190  //if ( i % 100000 == 0 )
191  // debug( ostringstream().flush() << "RTFXPropertyArray<T>::save: serializing ... " << i << endl );
192  _ar << value[i];
193  }
194  }
195 
196  template<class Archive>
197  void load( Archive &_ar, const unsigned int _version )
198  {
199  _ar >> boost::serialization::base_object<RTFXProperty>( *this );
200 
201  /*char * tmp_ = new char[sizeof( RTFXBIGINT )];
202  for ( unsigned int i = 0; i < sizeof( RTFXBIGINT ); i ++ )
203  _ar >> tmp_[i];
204 
205  DecodeLong( length, tmp_ );*/
206  _ar >> length;
207 
208  value = ( T * ) malloc( length * sizeof( T ) );
209 
210  for ( unsigned int i = 0; i < length; i ++ )
211  _ar >> value[i];
212  }
213 
214  template<class Archive>
215  void serialize( Archive &_ar, const unsigned int _version )
216  {
217  boost::serialization::split_member( _ar, (*this), _version );
218  }
219  // BOOST_SERIALIZATION_SPLIT_MEMBER()
220  };
221 }
222 
223 BOOST_CLASS_EXPORT_KEY( RTFX::RTFXPropertyArray<unsigned int> )
224 BOOST_CLASS_EXPORT_KEY( RTFX::RTFXPropertyArray<int> )
225 BOOST_CLASS_EXPORT_KEY( RTFX::RTFXPropertyArray<float> )
226 BOOST_CLASS_EXPORT_KEY( RTFX::RTFXPropertyArray<double> )
227 BOOST_CLASS_EXPORT_KEY( RTFX::RTFXPropertyArray<bool> )
228 BOOST_CLASS_EXPORT_KEY( RTFX::RTFXPropertyArray<char> )
229 
230 #endif