Author: toshok
Date: 2007-08-30 19:09:59 -0400 (Thu, 30 Aug 2007)
New Revision: 85083

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/animation.cpp
   trunk/moon/src/animation.h
   trunk/moon/src/collection.cpp
   trunk/moon/src/collection.h
Log:
        * collection.h, collection.cpp: change the prototype of
        Collection::Add, ::Insert, and ::SetVal to return bool.  Make the
        subclasses check the return value before doing their type specific
        handling of the child.  Fixes the crash from invalid xaml in
        alan's testcase.

        * animation.h, animation.cpp: track collection api change.

2007-08-30  Chris Toshok  <[EMAIL PROTECTED]>



Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-08-30 23:07:23 UTC (rev 85082)
+++ trunk/moon/src/ChangeLog    2007-08-30 23:09:59 UTC (rev 85083)
@@ -1,5 +1,15 @@
 2007-08-30  Chris Toshok  <[EMAIL PROTECTED]>
 
+       * collection.h, collection.cpp: change the prototype of
+       Collection::Add, ::Insert, and ::SetVal to return bool.  Make the
+       subclasses check the return value before doing their type specific
+       handling of the child.  Fixes the crash from invalid xaml in
+       alan's testcase.
+
+       * animation.h, animation.cpp: track collection api change.
+
+2007-08-30  Chris Toshok  <[EMAIL PROTECTED]>
+
        * collection.cpp (Collection::Add): make the warning a lot more
        useful if the child isn't of the right type.
 

Modified: trunk/moon/src/animation.cpp
===================================================================
--- trunk/moon/src/animation.cpp        2007-08-30 23:07:23 UTC (rev 85082)
+++ trunk/moon/src/animation.cpp        2007-08-30 23:09:59 UTC (rev 85083)
@@ -712,32 +712,30 @@
        delete sorted_list;
 }
 
-void
+bool
 KeyFrameCollection::Add (DependencyObject *data)
 {
-       KeyFrameNode *kfn = new KeyFrameNode ((KeyFrame *) data);
-
-       resolved = false;
-       Collection::Add (data);
+       bool b = Collection::Add (data);
+       if (b)
+               resolved = false;
+       return b;
 }
 
-void
+bool
 KeyFrameCollection::Insert (int index, DependencyObject *data)
 {
-       KeyFrameNode *kfn = new KeyFrameNode ((KeyFrame *) data);
-
-       resolved = false;
-       Collection::Insert (index, data);
+       bool b = Collection::Insert (index, data);
+       if (b)
+               resolved = false;
+       return b;
 }
 
 bool
 KeyFrameCollection::Remove (DependencyObject *data)
 {
-       KeyFrame *kf = (KeyFrame *) data;
-       
-       resolved = false;
-
-       return Collection::Remove (kf);
+       bool b = Collection::Remove (data);
+       if (b)
+               resolved = false;
 }
 
 void

Modified: trunk/moon/src/animation.h
===================================================================
--- trunk/moon/src/animation.h  2007-08-30 23:07:23 UTC (rev 85082)
+++ trunk/moon/src/animation.h  2007-08-30 23:09:59 UTC (rev 85083)
@@ -277,9 +277,9 @@
        virtual Type::Kind GetObjectType() { return Type::KEYFRAME_COLLECTION; }
        virtual Type::Kind GetElementType() { return Type::KEYFRAME; }
 
-       virtual void Add (DependencyObject *obj);
+       virtual bool Add (DependencyObject *obj);
        virtual bool Remove (DependencyObject *obj);
-       virtual void Insert (int index, DependencyObject *data);
+       virtual bool Insert (int index, DependencyObject *data);
        virtual void Clear ();
 
        List *sorted_list;

Modified: trunk/moon/src/collection.cpp
===================================================================
--- trunk/moon/src/collection.cpp       2007-08-30 23:07:23 UTC (rev 85082)
+++ trunk/moon/src/collection.cpp       2007-08-30 23:09:59 UTC (rev 85083)
@@ -57,13 +57,13 @@
        delete list;
 }
 
-void
+bool
 Collection::Add (DependencyObject *data)
 {
        if (!Type::Find(data->GetObjectType())->IsSubclassOf(GetElementType())) 
{
                g_warning ("Cannot add children of type `%s' to a collection of 
type `%s'.  Its children must be subclasses of `%s'.",
                           data->GetTypeName(), GetTypeName(), Type::Find 
(GetElementType())->name);
-               return;
+               return false;
        }
        
        generation++;
@@ -90,12 +90,18 @@
 
                closure->OnCollectionChanged (this, 
CollectionChangeTypeItemAdded, data, NULL);
        }
+
+       return true;
 }
 
-void
+bool
 Collection::Insert (int index, DependencyObject *data)
 {
-       g_return_if_fail 
(Type::Find(data->GetObjectType())->IsSubclassOf(GetElementType()));
+       if (!Type::Find(data->GetObjectType())->IsSubclassOf(GetElementType())) 
{
+               g_warning ("Cannot add children of type `%s' to a collection of 
type `%s'.  Its children must be subclasses of `%s'.",
+                          data->GetTypeName(), GetTypeName(), Type::Find 
(GetElementType())->name);
+               return false;
+       }
        
        generation++;
        list->Insert (new Collection::Node (data, this), index);
@@ -112,6 +118,8 @@
 
                closure->OnCollectionChanged (this, 
CollectionChangeTypeItemAdded, data, NULL);
        }
+
+       return true;
 }
 
 void
@@ -127,7 +135,11 @@
 DependencyObject *
 Collection::SetVal (int index, DependencyObject *data)
 {
-       g_return_val_if_fail 
(Type::Find(data->GetObjectType())->IsSubclassOf(GetElementType()), NULL);
+       if (!Type::Find(data->GetObjectType())->IsSubclassOf(GetElementType())) 
{
+               g_warning ("Cannot add children of type `%s' to a collection of 
type `%s'.  Its children must be subclasses of `%s'.",
+                          data->GetTypeName(), GetTypeName(), Type::Find 
(GetElementType())->name);
+               return NULL;
+       }
 
        generation++;
        Node *old = (Collection::Node *) list->Replace (new Collection::Node 
(data, this), index);
@@ -189,10 +201,11 @@
 {
        Collection::Node *n = (Collection::Node *) list->Index (index);
        
-       generation++;
        if (n == NULL)
                return false;
        
+       generation++;
+
        n->Unlink ();
 
        n->obj->Detach (NULL, this);
@@ -236,16 +249,17 @@
 }
 
 DependencyProperty *Collection::CountProperty;
+
 void
 collection_init (void)
 {
        Collection::CountProperty = DependencyObject::Register 
(Type::COLLECTION, "Count", Type::INT32);
 }
 
-void
+bool
 collection_add (Collection *collection, DependencyObject *data)
 {
-       collection->Add (data);
+       return collection->Add (data);
 }
 
 int
@@ -293,10 +307,10 @@
        return true;
 }
 
-void
+bool
 collection_insert (Collection *collection, int index, DependencyObject *data)
 {
-       collection->Insert (index, data);
+       return collection->Insert (index, data);
 }
 
 //
@@ -439,22 +453,25 @@
        panel->UpdateBounds ();
 }
 
-void
+bool
 VisualCollection::Add (DependencyObject *data)
 {
        UIElement *item = (UIElement *) data;
        
-       Collection::Add (item);
+       bool b = Collection::Add (item);
+
+       if (b) {
+               g_ptr_array_insert_sorted (z_sorted, UIElementZIndexComparer, 
item);
        
-       g_ptr_array_insert_sorted (z_sorted, UIElementZIndexComparer, item);
-       
-       if (closure && ((UIElement*)closure)->flags & UIElement::IS_LOADED) {
-               /* emit loaded events on the new item if the tree
-                  we're adding it to has already been "loaded" */
-               item->OnLoaded ();
+               if (closure && ((UIElement*)closure)->flags & 
UIElement::IS_LOADED) {
+                       /* emit loaded events on the new item if the tree
+                          we're adding it to has already been "loaded" */
+                       item->OnLoaded ();
+               }
+
+               VisualUpdate (data);
        }
-
-       VisualUpdate (data);
+       return b;
 }
 
 DependencyObject *
@@ -464,37 +481,43 @@
        
        UIElement *old = (UIElement *) Collection::SetVal (index, item);
        
-       g_ptr_array_remove (z_sorted, old);
+       if (old) {
+               g_ptr_array_remove (z_sorted, old);
        
-       g_ptr_array_insert_sorted (z_sorted, UIElementZIndexComparer, item);
+               g_ptr_array_insert_sorted (z_sorted, UIElementZIndexComparer, 
item);
        
-       if (closure && ((UIElement*)closure)->flags & UIElement::IS_LOADED) {
-               /* emit loaded events on the new item if the tree
-                  we're adding it to has already been "loaded" */
-               item->OnLoaded ();
+               if (closure && ((UIElement*)closure)->flags & 
UIElement::IS_LOADED) {
+                       /* emit loaded events on the new item if the tree
+                          we're adding it to has already been "loaded" */
+                       item->OnLoaded ();
+               }
+
+               VisualUpdate (data);
        }
 
-       VisualUpdate (data);
-
        return old;
 }
 
-void
+bool
 VisualCollection::Insert (int index, DependencyObject *data)
 {
        UIElement *item = (UIElement *) data;
        
-       Collection::Insert (index, item);
+       bool b = Collection::Insert (index, item);
+
+       if (b) {
+               g_ptr_array_insert_sorted (z_sorted, UIElementZIndexComparer, 
item);
        
-       g_ptr_array_insert_sorted (z_sorted, UIElementZIndexComparer, item);
-       
-       if (closure && ((UIElement*)closure)->flags & UIElement::IS_LOADED) {
-               /* emit loaded events on the new item if the tree
-                  we're adding it to has already been "loaded" */
-               item->OnLoaded ();
+               if (closure && ((UIElement*)closure)->flags & 
UIElement::IS_LOADED) {
+                       /* emit loaded events on the new item if the tree
+                          we're adding it to has already been "loaded" */
+                       item->OnLoaded ();
+               }
+
+               VisualUpdate (data);
        }
 
-       VisualUpdate (data);
+       return b;
 }
 
 bool
@@ -506,10 +529,12 @@
        item->Invalidate ();
        bool b = Collection::Remove (item);
        
-       g_ptr_array_remove (z_sorted, item);
+       if (b) {
+               g_ptr_array_remove (z_sorted, item);
        
-       if (panel)
-               panel->UpdateBounds ();
+               if (panel)
+                       panel->UpdateBounds ();
+       }
 
        return b;
 }
@@ -527,11 +552,13 @@
 
        item->Invalidate ();
        bool b = Collection::RemoveAt (index);
+
+       if (b) {
+               g_ptr_array_remove (z_sorted, item);
        
-       g_ptr_array_remove (z_sorted, item);
-       
-       if (panel)
-               panel->UpdateBounds ();
+               if (panel)
+                       panel->UpdateBounds ();
+       }
 
        return b;
 }
@@ -555,75 +582,82 @@
 }
 
 
-void
+bool
 TriggerCollection::Add (DependencyObject *data)
 {
-       FrameworkElement *fwe = (FrameworkElement *) closure;
-       
-       printf ("Adding %p\n", data);
-       EventTrigger *trigger = (EventTrigger *) data;
+       bool b = Collection::Add (data);
 
-       Collection::Add (trigger);
+       if (b) {
+               FrameworkElement *fwe = (FrameworkElement *) closure;
+               EventTrigger *trigger = (EventTrigger *) data;
 
-       trigger->SetTarget (fwe);
+               trigger->SetTarget (fwe);
+       }
+
+       return b;
 }
 
 DependencyObject *
 TriggerCollection::SetVal (int index, DependencyObject *data)
 {
-       FrameworkElement *fwe = (FrameworkElement *) closure;
-       
-       printf ("Inserting %p\n", data);
-       EventTrigger *trigger = (EventTrigger *) data;
+       FrameworkElement *old = (FrameworkElement *) Collection::SetVal (index, 
data);
 
-       FrameworkElement *old = (FrameworkElement *) Collection::SetVal (index, 
trigger);
+       if (old) {
+               FrameworkElement *fwe = (FrameworkElement *) closure;
+               EventTrigger *trigger = (EventTrigger *) data;
 
-       trigger->SetTarget (fwe);
+               trigger->SetTarget (fwe);
 
-       trigger->RemoveTarget (old);
+               trigger->RemoveTarget (old);
+       }
 
        return old;
 }
 
-void
+bool
 TriggerCollection::Insert (int index, DependencyObject *data)
 {
-       FrameworkElement *fwe = (FrameworkElement *) closure;
-       
-       printf ("Inserting %p\n", data);
-       EventTrigger *trigger = (EventTrigger *) data;
+       bool b = Collection::Insert (index, data);
+       if (b) {
+               FrameworkElement *fwe = (FrameworkElement *) closure;
+               EventTrigger *trigger = (EventTrigger *) data;
 
-       Collection::Insert (index, trigger);
+               trigger->SetTarget (fwe);
+       }
 
-       trigger->SetTarget (fwe);
+       return b;
 }
 
 bool
 TriggerCollection::Remove (DependencyObject *data)
 {
-       FrameworkElement *fwe = (FrameworkElement *) closure;
-       
-       EventTrigger *trigger = (EventTrigger *) data;
+       bool b = Collection::Remove (data);
 
-       bool b = Collection::Remove (trigger);
+       if (b) {
+               FrameworkElement *fwe = (FrameworkElement *) closure;
+               EventTrigger *trigger = (EventTrigger *) data;
 
-       trigger->RemoveTarget (fwe);
+               trigger->RemoveTarget (fwe);
+       }
+
        return b;
 }
 
 bool
 TriggerCollection::RemoveAt (int index)
 {
-       FrameworkElement *fwe = (FrameworkElement *) closure;
        Collection::Node *n = (Collection::Node *) list->Index (index);
        if (n == NULL)
                return false;
 
-       EventTrigger *trigger = (EventTrigger *) n->obj;
-
        bool b = Collection::RemoveAt (index);
 
-       trigger->RemoveTarget (fwe);
+       if (b) {
+               FrameworkElement *fwe = (FrameworkElement *) closure;
+               EventTrigger *trigger = (EventTrigger *) n->obj;
+               trigger->RemoveTarget (fwe);
+       }
+
        return b;
 }
 

Modified: trunk/moon/src/collection.h
===================================================================
--- trunk/moon/src/collection.h 2007-08-30 23:07:23 UTC (rev 85082)
+++ trunk/moon/src/collection.h 2007-08-30 23:09:59 UTC (rev 85083)
@@ -39,10 +39,10 @@
        virtual Type::Kind GetObjectType () { return Type::COLLECTION; };       
        virtual Type::Kind GetElementType () { return Type::DEPENDENCY_OBJECT; }
 
-       virtual void Add    (DependencyObject *data);
+       virtual bool Add    (DependencyObject *data);
        virtual bool Remove (DependencyObject *data);
        virtual bool RemoveAt (int index);
-       virtual void Insert (int index, DependencyObject *data);
+       virtual bool Insert (int index, DependencyObject *data);
        virtual void Clear  ();
 
        //
@@ -54,9 +54,6 @@
        virtual void OnSubPropertyChanged (DependencyProperty *prop, 
DependencyProperty *subprop);
 
        static DependencyProperty *CountProperty;
-       
- private:
-       void SharedAdd (DependencyObject *data);
 };
 
 bool CollectionNodeFinder (List::Node *n, void *data);
@@ -83,10 +80,10 @@
        virtual Type::Kind GetObjectType () { return Type::VISUAL_COLLECTION; }
        virtual Type::Kind GetElementType () { return Type::VISUAL; }
 
-       virtual void Add    (DependencyObject *data);
+       virtual bool Add    (DependencyObject *data);
        virtual bool Remove (DependencyObject *data);
        virtual bool RemoveAt (int index);
-       virtual void Insert (int index, DependencyObject *data);
+       virtual bool Insert (int index, DependencyObject *data);
        virtual void Clear  ();
        virtual DependencyObject *SetVal (int index, DependencyObject *data);
 
@@ -103,10 +100,10 @@
        virtual Type::Kind GetObjectType () { return Type::TRIGGER_COLLECTION; }
        virtual Type::Kind GetElementType () { return Type::EVENTTRIGGER; }
 
-       virtual void Add    (DependencyObject *data);
+       virtual bool Add    (DependencyObject *data);
        virtual bool Remove (DependencyObject *data);
        virtual bool RemoveAt (int index);
-       virtual void Insert (int index, DependencyObject *data);
+       virtual bool Insert (int index, DependencyObject *data);
        virtual DependencyObject *SetVal (int index, DependencyObject *data);
 };
 
@@ -162,10 +159,10 @@
 
 G_BEGIN_DECLS
 
-void collection_add    (Collection *collection, DependencyObject *data);
+bool collection_add    (Collection *collection, DependencyObject *data);
 bool collection_remove (Collection *collection, DependencyObject *data);
 bool collection_remove_at (Collection *collection, int index);
-void collection_insert (Collection *collection, int index, DependencyObject 
*data);
+bool collection_insert (Collection *collection, int index, DependencyObject 
*data);
 void collection_clear  (Collection *collection);
 int  collection_count  (Collection *collection);
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to