-
Notifications
You must be signed in to change notification settings - Fork 5
/
LBPRhObjectWrapper.h
1060 lines (846 loc) · 24.7 KB
/
LBPRhObjectWrapper.h
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#ifndef LBPRHLIB
#error RHLIB header included in non-Rhino compile
#endif
#ifdef _DEBUG
#include "LBPRhException.h"
#endif
#include "LBPLibUtilities.h"
#include "LBP_UUID.h"
#include "ILBPRhWrapper.h"
#include "ON_SimpleMap.h"
#if defined _MSC_VER && _MSC_VER < 1400
#define RHWRAP_VER 3
#error "This should no longer happen"
#else
#define RHWRAP_VER 4
#endif
#if RHWRAP_VER == 4
enum lbprh_wrapper_eType { lwt_geometry,/*lwt_material,lwt_light,*/lwt_attributes,lwt_object, };
#endif
#if RHWRAP_VER == 3
enum lbprh_wrapper_eType { lwt_geometry,/*lwt_material,lwt_light,*/ };
#endif
template<class T> class CLBPRhObjectWrapper : public ILBPRhWrapper<T>
{
public:
CLBPRhObjectWrapper(const CRhinoDoc& doc, const UUID& uuidObject, lbprh_wrapper_eType type = lwt_geometry);
CLBPRhObjectWrapper(const CRhinoObject * pRhinoObject, lbprh_wrapper_eType type = lwt_geometry);
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
CLBPRhObjectWrapper(const CRhinoDoc& doc, const CRhinoObjectAttributes* pAttributes);
#endif
//END RHINO 4+ SPECIFIC CODE
virtual ~CLBPRhObjectWrapper();
virtual eLBPRhWrapperType WrapperType(void) const { return lbprh_wt_object; }
//type = attributes, returns CRhinoObjectAttributes*
//type = geometry, returns CRhinoObject*
//type = object, returns CRhinoObject*
inline const ON_Object* OriginalObject() const {return m_pObject;}
inline UUID OriginalUuid() const { return m_uuid; }
//Cause the wrapper to put the object into a writable state
inline void Modify() { UserDataToModify(); }
//DEPRECATED - use "Suspend" instead
bool TransformObject(const ON_Xform& xform);
//Forces the object to be committed and replaced immediately - ie, bypasses the lazy update
//feature.
//Implemented using CRhinoDoc::TransformObject
virtual CRhinoObject* ModifyObject();
//Force the wrapper to modify the actual object
//This is useful when the type does not cause a CRhinoObject to be
//cloned - eg. Attributes. After this, the object will be
//closed with a full "ReplaceObject" call instead of a ModifyObjectAttributes
//Note: Multiple calls to this function do nothing, so you can use the
//function to get the modified object over and over
CRhinoObject* ModifiedObject() const;
//Call this one to get the ready-modified CRhinoObject clone
//Will return NULL if this clone is not a CRhinoObject or there are no modifications
//Helper functions to get around the const_casting problem
//inherent in modifying duplicated objects
//Note "ModifiedAttributes" does not require ModifyObject to be called first.
//"ModifiedGeometry" does require a call to ModifyObject first.
CRhinoObjectAttributes* ModifiedAttributes() const;
ON_Geometry* ModifiedGeometry() const;
//Nasty access to reference count for people using the back-door
//modification capture method
bool LastReference(void) const { return 1==m_dwRefCount; }
inline bool ModificationsPending() const { return NULL != m_pClone;}
//Determines whether there is a current clone
//(ie - Modify() or UserDataToModify() has been called)
//Note - the following 2 functions have no effect if the original object was not in the database.
//In that case, modifications are made directly on the object.
virtual void UndoModifications(); //Revert the wrapper back to the read-state, and undo all modifications
//Note -the function cannot be correctly virtualized because it is called from the destructor
/*virtual*/bool CommitChanges(); //Call to write changes to the document before destruction
//leaves the wrapper in a re-usabled state
public:
class Suspend
{
public:
Suspend(CLBPRhObjectWrapper<T>& wrapper)
: m_wrapper(wrapper)
{ m_wrapper.ReleaseObject(); }
~Suspend() {m_wrapper.RecaptureObject();}
const CRhinoObject* Object() const { return m_wrapper.OriginalRhinoObject(); }
UUID Uuid() const { return m_wrapper.OriginalUuid(); }
private:
CLBPRhObjectWrapper<T>& m_wrapper;
};
public:
virtual bool RemoveData();
virtual bool UserDataPresent() const;
public:
//Copying data
//Ensure that a wrapper is not constructed for the target
//(debug builds will prohibit this)
bool CopyToObject(const CRhinoObject* pObject) const;
bool CopyToObject(const UUID& uuidObject) const;
//Ensure that a wrapper is not constructed for the source
//(debug builds will prohibit this)
bool CopyFromObject(const CRhinoObject* pObject);
bool CopyFromObject(const UUID& uuidObject);
static const T* DefaultData() { return &g_ud; }
protected:
//Implement your setters and getters based on these functions
virtual const T* UserData(eLBPRhWrapper_DefaultUse du = lbprh_dont_use_defaults) const;
virtual T* UserDataToModify(void);
//Override this to get modification events
virtual void OnModification() const {};
const CRhinoObject* OriginalRhinoObject() const;
CRhinoDoc* Document() const { return LBPRhDocFromRuntimeSerialNumber(m_runtime_doc_serial_number); }
private:
//IMPLEMENTATION DETAILS ONLY
//Null attaches blank user data, otherwise the data is copied
T* AttachData(const T* pExistingData = NULL);
void CommonCtor(const ON_Object* pObject, lbprh_wrapper_eType type);
void Clone() const;
ON_Object* GetTarget() const;
bool OriginalObjectIsCRhinoObject() const;
//const CRhinoObject* OriginalRhinoObject() const;
const CRhinoObjectAttributes* OriginalAttributes() const;
void RenewOriginalObject(const UUID& uuid);
//Make copying illegal
CLBPRhObjectWrapper(const CLBPRhObjectWrapper& src);
const CLBPRhObjectWrapper& operator=(const CLBPRhObjectWrapper& src);
private:
const ON_Object* m_pObject;
mutable ON_Object* m_pClone;
lbprh_wrapper_eType m_type;
bool m_bObjectInDatabase;
UUID m_uuid;
const unsigned int m_runtime_doc_serial_number;
private:
//For defaults implementation
static T g_ud;
static bool g_bDefaultsSet;
//Reference counted map implementation
public:
DWORD AddRef() { return ++m_dwRefCount; }
DWORD ReleaseRef() { return --m_dwRefCount; }
private:
friend class Suspend;
void ReleaseObject();
void RecaptureObject();
private:
DWORD m_dwRefCount;
};
template<class T> T CLBPRhObjectWrapper<T>::g_ud;
template<class T> bool CLBPRhObjectWrapper<T>::g_bDefaultsSet = false;
//////////////////////////////////////////////////////////////////////////////////
// Template implementation below
//////////////////////////////////////////////////////////////////////////////////
//
// Template implementation
//
template <class T>
CLBPRhObjectWrapper<T>::CLBPRhObjectWrapper(const CRhinoDoc& doc, const UUID& uuidObject, lbprh_wrapper_eType type)
: m_runtime_doc_serial_number(LBPRhDocRuntimeSerialNumber(doc))
{
const CRhinoObject* pObject = doc.LookupObject(uuidObject);
ASSERT(NULL != pObject);
m_uuid = uuidObject;
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
if(lwt_attributes == type)
CommonCtor(&pObject->Attributes(), type);
else
#endif
//END RHINO 4+ SPECIFIC CODE
CommonCtor(pObject, type);
}
template <class T>
CLBPRhObjectWrapper<T>::CLBPRhObjectWrapper(const CRhinoObject* pObject, lbprh_wrapper_eType type)
: m_runtime_doc_serial_number(LBPRhObjectDocSerialNumber(pObject))
{
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
if(lwt_attributes == type)
CommonCtor(&pObject->Attributes(), type);
else
#endif
//END RHINO 4+ SPECIFIC CODE
CommonCtor(pObject, type);
//This object may not be in the database.
//Test to see if it is:
//Done here because it's more efficient - otherwise we have to do two lookups
//in the UUID constructor unnecessarily
// if(!IsObjectInDatabase(pObject))
if (NULL == pObject->Document())
{
m_bObjectInDatabase = false;
}
m_uuid = pObject->Attributes().m_uuid;
}
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
template <class T>
CLBPRhObjectWrapper<T>::CLBPRhObjectWrapper(const CRhinoDoc& doc, const CRhinoObjectAttributes* pAttributes)
: m_runtime_doc_serial_number(LBPRhDocRuntimeSerialNumber(doc))
{
CommonCtor(pAttributes, lwt_attributes);
m_uuid = pAttributes->m_uuid;
}
#endif
//END RHINO 4+ SPECIFIC CODE
template <class T>
void CLBPRhObjectWrapper<T>::CommonCtor(const ON_Object* pObject, lbprh_wrapper_eType type)
{
m_pClone = NULL;
if(!g_bDefaultsSet)
{
g_ud.SetToDefaultsImpl(lbprh_wt_object);
g_bDefaultsSet = true;
}
m_type = type;
m_pObject = pObject;
m_bObjectInDatabase = true;
m_dwRefCount = 0;
}
template <class T>
CLBPRhObjectWrapper<T>::~CLBPRhObjectWrapper()
{
CommitChanges();
}
template <class T>
void CLBPRhObjectWrapper<T>::UndoModifications()
{
if(m_pClone)
{
if(m_bObjectInDatabase)
{
delete m_pClone;
}
m_pClone = NULL;
}
}
template <class T>
void CLBPRhObjectWrapper<T>::Clone() const
{
ASSERT(NULL == m_pClone);
if(m_pClone) return;
switch(m_type)
{
case lwt_geometry:
if(m_bObjectInDatabase)
m_pClone = m_pObject->Duplicate();
else
//Const cast is OK because we know this object can be modified
m_pClone = const_cast<ON_Object*>(m_pObject);
break;
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
case lwt_object:
if(m_bObjectInDatabase)
m_pClone = m_pObject->Duplicate();
else
//Const cast is OK because we know this object can be modified
m_pClone = const_cast<ON_Object*>(m_pObject);
break;
case lwt_attributes:
if(m_bObjectInDatabase)
m_pClone = new CRhinoObjectAttributes(*OriginalAttributes());
else
//Const cast is OK because we know this object can be modified
m_pClone = const_cast<ON_Object*>(m_pObject);
break;
#endif
//END RHINO 4+ SPECIFIC CODE
default:
//TODO: implement other types
break;
}
}
template <class T>
ON_Object* CLBPRhObjectWrapper<T>::GetTarget() const
{
switch(m_type)
{
case lwt_geometry:
if(m_pClone)
return const_cast<ON_Geometry*>(static_cast<CRhinoObject*>(m_pClone)->Geometry());
else
return const_cast<ON_Geometry*>(OriginalRhinoObject()->Geometry());
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
case lwt_attributes:
if(m_pClone)
return CRhinoObjectAttributes::Cast(m_pClone) ?
m_pClone
:
const_cast<CRhinoObjectAttributes*>(&static_cast<CRhinoObject*>(m_pClone)->Attributes());
else
//The original object is "attributes" in all cases
return const_cast<ON_Object*>(m_pObject);
case lwt_object:
if(m_pClone)
return m_pClone;
else
return const_cast<ON_Object*>(m_pObject);
#endif
//END RHINO 4+ SPECIFIC CODE
default:
//TODO: Implement other types
return NULL;
}
}
template <class T>
bool CLBPRhObjectWrapper<T>::RemoveData()
{
if(!UserDataPresent()) return true;
bool bWasCloned = false;
if (!m_pClone)
{
Clone();
bWasCloned = true;
}
if (!m_pClone) return NULL;
//T* pUserData = NULL;
ON_Object* pTarget = GetTarget();//m_pClone;
if(pTarget)
{
BOOL bDetached = FALSE;
ON_UserData* pUD = UserDataToModify();
if (pUD != NULL)
{
bDetached = pTarget->DetachUserData(pUD);
if (bDetached)
{
//Notify clients that a modification was made and that the UserData pointer has changed
OnModification();
}
delete pUD;
}
return bDetached ? true : false;
}
if(bWasCloned)
{
if(m_bObjectInDatabase)
{
delete m_pClone;
}
m_pClone = NULL;
}
return false;
}
template <class T>
bool CLBPRhObjectWrapper<T>::UserDataPresent() const
{
return (NULL == UserData(lbprh_dont_use_defaults) ? false : true);
}
template <class T>
bool CLBPRhObjectWrapper<T>::CopyToObject(const UUID& uuid) const
{
CRhinoDoc* pDoc = Document();
if (pDoc)
{
return CopyToObject(pDoc->LookupObject(uuid));
}
return false;
}
template <class T>
bool CLBPRhObjectWrapper<T>::CopyToObject(const CRhinoObject* pObject) const
{
if(!UserDataPresent()) return false;
CLBPRhObjectWrapper<T> target(pObject, m_type);
return NULL == target.AttachData(UserData()) ? false : true;
}
template <class T>
bool CLBPRhObjectWrapper<T>::CopyFromObject(const UUID& uuid)
{
const CRhinoDoc* pDoc = Document();
if (pDoc)
{
return CopyFromObject(pDoc->LookupObject(uuid));
}
return false;
}
template <class T>
bool CLBPRhObjectWrapper<T>::CopyFromObject(const CRhinoObject* pObject)
{
CLBPRhObjectWrapper<T> source(pObject, m_type);
if(!source.UserDataPresent()) return false;
return NULL == AttachData(source.UserData()) ? false : true;
}
template <class T>
const T* CLBPRhObjectWrapper<T>::UserData(eLBPRhWrapper_DefaultUse du) const
{
ASSERT(NULL != m_pObject);
const ON_Object* pTarget = GetTarget();
if(!pTarget) return NULL;
const T* pUD = T::Cast(pTarget->GetUserData(g_ud.m_userdata_uuid));
if(pUD) return pUD;
if(lbprh_use_defaults == du)
return &g_ud;
return NULL;
}
template <class T>
CRhinoObject* CLBPRhObjectWrapper<T>::ModifiedObject() const
{
if(CRhinoObject::Cast(m_pClone)) return static_cast<CRhinoObject*>(m_pClone);
return NULL;
}
template <class T>
CRhinoObjectAttributes* CLBPRhObjectWrapper<T>::ModifiedAttributes() const
{
if(CRhinoObjectAttributes::Cast(m_pClone))
return static_cast<CRhinoObjectAttributes*>(m_pClone);
if(CRhinoObject::Cast(m_pClone))
return const_cast<CRhinoObjectAttributes*>(&static_cast<CRhinoObject*>(m_pClone)->Attributes());
return NULL;
}
template <class T>
ON_Geometry* CLBPRhObjectWrapper<T>::ModifiedGeometry() const
{
if(CRhinoObject::Cast(m_pClone))
return const_cast<ON_Geometry*>(static_cast<CRhinoObject*>(m_pClone)->Geometry());
return NULL;
}
template <class T>
CRhinoObject* CLBPRhObjectWrapper<T>::ModifyObject(void)
{
//If we're not already in a cloned state, get cloned.
if(!ModificationsPending())
{
Modify();
}
//We may already have a cloned full object. If so, return that.
if(CRhinoObject::Cast(m_pClone)) return static_cast<CRhinoObject*>(m_pClone);
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
//We currently have a modification which didn't result in a cloned object
//Should never get here - but this is a placeholder for stuff which really can't happen
//line for viewport, material and layer wrappers.
if(m_type != lwt_attributes) return NULL;
#ifdef _DEBUG
if(NULL == CRhinoObjectAttributes::Cast(m_pClone))
{
CLBPRhRunTimeException re(L"Wrong original Object type");
throw re;
}
#endif
const CRhinoDoc* pDoc = Document();
const CRhinoObject* pOriginalObject = pDoc ? pDoc->LookupObject(OriginalUuid()) : NULL;
if(!pOriginalObject) return NULL;
CRhinoObjectAttributes* pAttributesClone = static_cast<CRhinoObjectAttributes*>(m_pClone);
CRhinoObject* pObject = pOriginalObject->Duplicate();
if(!pObject) return NULL;
//Copy the changes across to the newly duplicated object
const_cast<CRhinoObjectAttributes&>(pObject->Attributes()) = *pAttributesClone;
//Delete the old attributes
delete m_pClone;
//Set the new clone to an actual object.
m_pClone = pObject;
return pObject;
#else
return NULL;
#endif
}
template <class T>
T* CLBPRhObjectWrapper<T>::UserDataToModify(void)
{
if (!m_pObject) return NULL;
T* pExistingData = NULL;
if(m_pClone)
{
//We know the data is coming from the clone, so we can
//safely cast away the const-ness to enable us to reuse
//the UserData function
pExistingData = const_cast<T*>(UserData());
if(pExistingData)
{
return pExistingData;
}
}
return AttachData();
}
template <class T>
void CLBPRhObjectWrapper<T>::RenewOriginalObject(const UUID& uuid)
{
const CRhinoDoc* pDoc = Document();
if (NULL == pDoc)
{
m_pObject = NULL;
return;
}
//THe underlying object has changed - renew the pointer
if(OriginalObjectIsCRhinoObject())
{
m_pObject = pDoc->LookupObject(uuid);
}
else
{
#if RHWRAP_VER == 4
//This happens in the case of attributes
m_pObject = &pDoc->LookupObject(uuid)->Attributes();
#else
m_pObject = NULL;
#endif
}
}
template <class T>
void CLBPRhObjectWrapper<T>::ReleaseObject()
{
CommitChanges();
}
template <class T>
void CLBPRhObjectWrapper<T>::RecaptureObject()
{
RenewOriginalObject(OriginalUuid());
}
template <class T>
bool CLBPRhObjectWrapper<T>::TransformObject(const ON_Xform& xform)
{
CRhinoDoc* pDoc = Document();
LBPRhAssert(NULL != pDoc);
if(NULL == pDoc)
return false;
//Suspend ownership of the object
Suspend o(*this);
const UUID uuid = o.Uuid();
CRhinoObjRef ref(uuid);
CRhinoObject* pNewObject = pDoc->TransformObject(ref, xform);
#if RHWRAP_VER == 4
LBPRhAssert(NULL != pNewObject);
#endif
if(NULL == pNewObject)
return false;
return true;
}
template <class T>
bool CLBPRhObjectWrapper<T>::CommitChanges()
{
if (NULL == m_pClone)
return false;
if (!m_bObjectInDatabase)
{
//In this case, the changes are already committed
m_pClone = NULL;
return true;
}
CRhinoDoc* pDoc = Document();
if(NULL == pDoc)
return false;
//Attributes does not change the object pointer, so this isn't needed
//ON_UUID uuidOriginalId = OriginalUuid();
bool bSucceeded = false;
switch(m_type)
{
case lwt_geometry:
bSucceeded = pDoc->ReplaceObject(CRhinoObjRef(OriginalRhinoObject()),
static_cast<CRhinoObject*>(m_pClone)
);
break;
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
case lwt_object:
bSucceeded = pDoc->ReplaceObject(CRhinoObjRef(OriginalRhinoObject()),
static_cast<CRhinoObject*>(m_pClone)
);
break;
case lwt_attributes:
{
//Attribute user data requires that the copy count is incremented before
//ModifyObjectAttributes is called
T* pUD = UserDataPresent() ? UserDataToModify() : NULL;
if(NULL != pUD)
{
pUD->m_userdata_copycount++;
}
if(CRhinoObjectAttributes::Cast(m_pClone))
{
//The clone is an attributes object
ON_UUID uuidObject = OriginalUuid();
const CRhinoObject* pObject = pDoc ? pDoc->LookupObject(uuidObject) : NULL;
if(pObject)
{
bSucceeded = pDoc->ModifyObjectAttributes( CRhinoObjRef(pObject),
*(static_cast<CRhinoObjectAttributes*>(m_pClone))
);
//ModifyObjectAttributes does not involve a change of ownership on the close, unlike
//the geometry or object attach sites. So at this point, we have to delete the clone if
//successful to avoid a memory leak.
if (bSucceeded)
{
delete m_pClone;
m_pClone = NULL;
}
}
}
else
{
//Copy the attributes
CRhinoObjectAttributes attr = static_cast<CRhinoObject*>(m_pClone)->Attributes();
//First replace the object
bSucceeded = pDoc->ReplaceObject(CRhinoObjRef(pDoc->RuntimeSerialNumber(), OriginalUuid()),
static_cast<CRhinoObject*>(m_pClone)
);
//Then modify the attributes
if(bSucceeded)
{
bSucceeded = pDoc->ModifyObjectAttributes(CRhinoObjRef(pDoc->RuntimeSerialNumber(), OriginalUuid()), attr);
}
}
}
break;
#endif
//END RHINO 4+ SPECIFIC CODE
default:
//TODO: Implement other types
break;
}
if(!bSucceeded)
{
//The process did not succeed - we basically have to get rid of the modifications
if(m_bObjectInDatabase)
{
delete m_pClone;
}
}
else
{
//THe underlying object has changed - renew the pointer
RenewOriginalObject(OriginalUuid());
}
m_pClone = NULL;
return bSucceeded;
}
template <class T>
bool CLBPRhObjectWrapper<T>::OriginalObjectIsCRhinoObject() const
{
//BEGIN RHINO 4+ SPECIFIC CODE
#if RHWRAP_VER == 4
return m_type != lwt_attributes;
#else
return true;
#endif
//END RHINO 4+ SPECIFIC CODE
}
template <class T>
T* CLBPRhObjectWrapper<T>::AttachData(const T* pData)
{
bool bWasCloned = false;
if (!m_pClone)
{
Clone();
bWasCloned = true;
}
if (!m_pClone) return NULL;
// We need non const user data
ON_Object* pTarget = GetTarget();//
if(NULL == pTarget) return NULL;
T* pCloneUserData = T::Cast(pTarget->GetUserData(g_ud.m_userdata_uuid));
if(pCloneUserData)
{
if(pData)
{
*pCloneUserData = *pData;
}
if(pData || bWasCloned)
{
OnModification();
}
return pCloneUserData;
}
//There's no data on the original source - we have to attach new data.
//Either we're cloning for modications
//or we're attaching new data from another source
//We are now sure we can add the user data.
pCloneUserData = new T;
if(pData)
{
*pCloneUserData = *pData;
}
if(!pTarget->AttachUserData(pCloneUserData))
{
delete pCloneUserData;
pCloneUserData = NULL;
//There is a critical bug - we should never reach this point
ASSERT(false);
}
if(NULL == pData)
{
pCloneUserData->SetToDefaultsImpl(lbprh_wt_object);
}
OnModification();
return pCloneUserData;
}
template <class T>
const CRhinoObject* CLBPRhObjectWrapper<T>::OriginalRhinoObject() const
{
if(OriginalObjectIsCRhinoObject())
{
return static_cast<const CRhinoObject*>(m_pObject);
}
else
{
const CRhinoDoc* pDoc = Document();
return pDoc ? pDoc->LookupObject(OriginalUuid()) : NULL;
}
}
template <class T>
const CRhinoObjectAttributes* CLBPRhObjectWrapper<T>::OriginalAttributes() const
{
#if RHWRAP_VER == 4
LBPRhAssert(!OriginalObjectIsCRhinoObject());
return static_cast<const CRhinoObjectAttributes*>(m_pObject);
#else
return NULL;
#endif
}
template<class TWrapper> class ILBPRhWrapperMap
{
public:
virtual ~ILBPRhWrapperMap() {};
//AddWrapperRef passes ownership to the map implementation
virtual bool AddWrapper(TWrapper* wrapper) = 0;
//This should delete the wrapper from the map if the reference count is 0
virtual bool RemoveWrapper(const UUID& uuid) = 0;
//Returns the one wrapper with the UUID in question
virtual TWrapper* Wrapper(const UUID& uuid) const = 0;
};
template<class TWrapper> class CLBPRhWrapperMap : public ILBPRhWrapperMap<TWrapper>
{
public:
virtual bool AddWrapper(TWrapper* wrapper)
{
m_map.SetAt(wrapper->OriginalUuid(), wrapper);
return true;
}
virtual bool RemoveWrapper(const UUID& uuid)
{
delete Wrapper(uuid);
return m_map.Remove(uuid) ? true : false;
}
virtual TWrapper* Wrapper(const UUID& uuid) const
{
TWrapper* p = NULL;
if (!m_map.Lookup(uuid, p))
return NULL;
return p;
}
private:
ON_SimpleUuidMap<TWrapper*> m_map;
};
template<class TWrapper> class CLBPRhObjectWrapperRef
{
public:
CLBPRhObjectWrapperRef(ILBPRhWrapperMap<TWrapper>& map,
const CRhinoDoc& doc,
const UUID& uuidObject,
lbprh_wrapper_eType type = lwt_geometry);
CLBPRhObjectWrapperRef(ILBPRhWrapperMap<TWrapper>& map,
const CRhinoObject * pRhinoObject,
lbprh_wrapper_eType type = lwt_geometry);
CLBPRhObjectWrapperRef(ILBPRhWrapperMap<TWrapper>& map,
const CRhinoDoc& doc,
const CRhinoObjectAttributes* pAttributes);
~CLBPRhObjectWrapperRef();
public:
TWrapper& Wrapper();
TWrapper* operator->() { return &Wrapper(); }
private:
void CommonCtor(const UUID& uuid, ILBPRhWrapperMap<TWrapper>* pMap);
UUID m_uuid;
ILBPRhWrapperMap<TWrapper>* m_pMap;
TWrapper* m_pWrapper;
CCriticalSection m_cs;
};
template <class TWrapper>
TWrapper& CLBPRhObjectWrapperRef<TWrapper>::Wrapper()
{
LBPRhAssert(NULL != m_pMap);
LBPRhAssert(NULL != m_pMap->Wrapper(m_uuid));
return *m_pWrapper;
}
template <class TWrapper>
void CLBPRhObjectWrapperRef<TWrapper>::CommonCtor(const UUID& uuid, ILBPRhWrapperMap<TWrapper>* pMap)
{
m_pMap = pMap;
m_uuid = uuid;
LBPRhAssert(NULL != m_pWrapper);
m_pWrapper->AddRef();
}
template <class TWrapper>
CLBPRhObjectWrapperRef<TWrapper>::CLBPRhObjectWrapperRef(ILBPRhWrapperMap<TWrapper>& map,
const CRhinoDoc& doc,
const UUID& uuidObject,
lbprh_wrapper_eType type)
{
m_pWrapper = map.Wrapper(uuidObject);
if(NULL == m_pWrapper)
{
m_pWrapper = new TWrapper(uuidObject, type);
bool bRet = map.AddWrapper(doc, m_pWrapper);
LBPRhAssert(bRet);
}
CommonCtor(uuidObject, &map);
}
template <class TWrapper>
CLBPRhObjectWrapperRef<TWrapper>::CLBPRhObjectWrapperRef(ILBPRhWrapperMap<TWrapper>& map,