Xcos MVC: replace 'set' with 'append' when possible and fix 'is_valid' 21/15721/2
Paul Bignier [Thu, 18 Dec 2014 13:38:37 +0000 (14:38 +0100)]
Change-Id: I5a6df097f58aed2f59b77ea20645a2596bc6900f

scilab/modules/scicos/src/cpp/view_scilab/BaseAdapter.hxx
scilab/modules/scicos/src/cpp/view_scilab/BlockAdapter.cpp
scilab/modules/scicos/src/cpp/view_scilab/DiagramAdapter.cpp
scilab/modules/scicos/src/cpp/view_scilab/LinkAdapter.cpp
scilab/modules/scicos/src/cpp/view_scilab/ParamsAdapter.cpp
scilab/modules/scicos/src/cpp/view_scilab/TextAdapter.cpp

index 2fa983c..4cc2a12 100644 (file)
@@ -165,13 +165,12 @@ public:
         {
             header->set(index, it->name.c_str());
         }
-        tlist->set(0, header);
+        tlist->append(header);
 
         // set the tlist field value
-        index = 1;
-        for (typename property<Adaptor>::props_t_it it = properties.begin(); it != properties.end(); ++it, ++index)
+        for (typename property<Adaptor>::props_t_it it = properties.begin(); it != properties.end(); ++it)
         {
-            tlist->set(index, it->get(*static_cast<Adaptor*>(this), controller));
+            tlist->append(it->get(*static_cast<Adaptor*>(this), controller));
         }
 
         tlist->IncreaseRef();
index 8398be4..4d86ac7 100644 (file)
@@ -185,8 +185,8 @@ BlockAdapter::BlockAdapter(const BlockAdapter& adapter) :
             {
                 types::MList* deletedObject = new types::MList();
                 types::String* header = new types::String(Deleted.data());
-                deletedObject->set(0, header);
-                List_objects->set(i, deletedObject);
+                deletedObject->append(header);
+                List_objects->append(deletedObject);
                 continue;
             }
 
@@ -198,7 +198,7 @@ BlockAdapter::BlockAdapter(const BlockAdapter& adapter) :
                     std::shared_ptr<org_scilab_modules_scicos::model::Annotation> annotation = std::static_pointer_cast<org_scilab_modules_scicos::model::Annotation>(item);
                     TextAdapter* localAdaptor = new TextAdapter(annotation);
 
-                    List_objects->set(i, localAdaptor);
+                    List_objects->append(localAdaptor);
                     continue;
                 }
                 case BLOCK:
@@ -218,7 +218,7 @@ BlockAdapter::BlockAdapter(const BlockAdapter& adapter) :
                         localAdaptor->setDiagram(oldBlockDiagram);
                     }
 
-                    List_objects->set(i, localAdaptor);
+                    List_objects->append(localAdaptor);
                     continue;
                 }
                 case LINK:
@@ -229,7 +229,7 @@ BlockAdapter::BlockAdapter(const BlockAdapter& adapter) :
                     // Do the model linking in the next loop, in case the Link points to a Block that has not been added yet
                     linkListView.push_back(localAdaptor);
 
-                    List_objects->set(i, localAdaptor);
+                    List_objects->append(localAdaptor);
                     continue;
                 }
                 default:
index fce0a10..a0225d8 100644 (file)
@@ -88,8 +88,8 @@ struct objs
             {
                 types::MList* deletedObject = new types::MList();
                 types::String* header = new types::String(Deleted.data());
-                deletedObject->set(0, header);
-                o->set(i, deletedObject);
+                deletedObject->append(header);
+                o->append(deletedObject);
                 continue;
             }
 
@@ -101,7 +101,7 @@ struct objs
                 {
                     std::shared_ptr<model::Annotation> annotation = std::static_pointer_cast<model::Annotation>(item);
                     TextAdapter* localAdaptor = new TextAdapter(annotation);
-                    o->set(i, localAdaptor);
+                    o->append(localAdaptor);
                     continue;
                 }
                 case BLOCK:
@@ -123,7 +123,7 @@ struct objs
                         localAdaptor->setDocContent(oldDoc);
                     }
 
-                    o->set(i, localAdaptor);
+                    o->append(localAdaptor);
                     continue;
                 }
                 case LINK:
@@ -139,7 +139,7 @@ struct objs
                         localAdaptor->setTo(to[link_number]);
                         link_number++;
                     }
-                    o->set(i, localAdaptor);
+                    o->append(localAdaptor);
                     continue;
                 }
                 default:
@@ -408,8 +408,8 @@ DiagramAdapter::DiagramAdapter(const DiagramAdapter& adapter) :
         {
             types::MList* deletedObject = new types::MList();
             types::String* header = new types::String(Deleted.data());
-            deletedObject->set(0, header);
-            List_objects->set(i, deletedObject);
+            deletedObject->append(header);
+            List_objects->append(deletedObject);
             continue;
         }
 
@@ -421,7 +421,7 @@ DiagramAdapter::DiagramAdapter(const DiagramAdapter& adapter) :
                 std::shared_ptr<model::Annotation> annotation = std::static_pointer_cast<model::Annotation>(item);
                 TextAdapter* localAdaptor = new TextAdapter(annotation);
 
-                List_objects->set(i, localAdaptor);
+                List_objects->append(localAdaptor);
                 continue;
             }
             case BLOCK:
@@ -442,7 +442,7 @@ DiagramAdapter::DiagramAdapter(const DiagramAdapter& adapter) :
                     localAdaptor->setDiagram(oldBlockDiagram);
                 }
 
-                List_objects->set(i, localAdaptor);
+                List_objects->append(localAdaptor);
                 continue;
             }
             case LINK:
@@ -453,7 +453,7 @@ DiagramAdapter::DiagramAdapter(const DiagramAdapter& adapter) :
                 // Do the model linking in the next loop, in case the Link points to a Block that has not been added yet
                 linkListView.push_back(localAdaptor);
 
-                List_objects->set(i, localAdaptor);
+                List_objects->append(localAdaptor);
                 continue;
             }
             default:
index 66a765f..aeea500 100644 (file)
@@ -660,22 +660,43 @@ void setLinkEnd(const ScicosID id, Controller& controller, const object_properti
 }
 
 // Check if the Link is valid
-bool is_valid(const link_t& linkt)
+bool is_valid(types::Double* o)
 {
-    if (floor(linkt.block) != linkt.block ||
-            floor(linkt.port) != linkt.port ||
-            floor(linkt.kind) != linkt.kind)
+    if (o->getSize() == 0)
     {
-        return false; // Block, Port and Kind must be integer values
+        return true;
     }
-    if (linkt.port < 0 || linkt.kind < 0)
+
+    if (o->getSize() == 2 || o->getSize() == 3)
     {
-        return false; // Port and Kind must be positive
+        if (floor(o->get(0)) != o->get(0) || floor(o->get(1)) != o->get(1))
+        {
+            return false; // Block and Port numbers must be integer values
+        }
+        if (o->get(1) < 0)
+        {
+            return false; // Port number must be positive
+        }
+
+        if (o->getSize() == 3)
+        {
+            if (floor(o->get(2)) != o->get(2))
+            {
+                return false; // Kind must be an integer value
+            }
+            if (o->get(2) < 0)
+            {
+                return false; // Kind must be positive
+            }
+        }
+
+        return true;
     }
 
-    return true;
+    return false;
 }
 
+
 struct from
 {
 
@@ -701,7 +722,7 @@ struct from
 
         types::Double* current = v->getAs<types::Double>();
 
-        if (current->getSize() != 0 && current->getSize() != 2 && current->getSize() != 3)
+        if (!is_valid(current))
         {
             return false;
         }
@@ -711,16 +732,12 @@ struct from
         {
             from_content.block = current->get(0);
             from_content.port = current->get(1);
-            // By default, kind designates an output (set to 0)
-        }
-        if (current->getSize() == 3)
-        {
-            from_content.kind = (current->get(2) == 0.) ? Start : End;
-        }
+            // By default, 'kind' designates an output (set to 0)
 
-        if (!is_valid(from_content))
-        {
-            return false;
+            if (current->getSize() == 3)
+            {
+                from_content.kind = (current->get(2) == 0.) ? Start : End;
+            }
         }
 
         adaptor.setFromInModel(from_content, controller);
@@ -758,21 +775,22 @@ struct to
             return false;
         }
 
+        if (!is_valid(current))
+        {
+            return false;
+        }
+
         link_t to_content {0, 0, End};
         if (current->getSize() >= 2)
         {
             to_content.block = current->get(0);
             to_content.port = current->get(1);
-            // By default, kind designates an input (set to 1)
-        }
-        if (current->getSize() == 3)
-        {
-            to_content.kind = (current->get(2) == 0.) ? Start : End;
-        }
+            // By default, 'kind' designates an input (set to 1)
 
-        if (!is_valid(to_content))
-        {
-            return false;
+            if (current->getSize() == 3)
+            {
+                to_content.kind = (current->get(2) == 0.) ? Start : End;
+            }
         }
 
         adaptor.setToInModel(to_content, controller);
index d56df8b..77a7c9c 100644 (file)
@@ -276,7 +276,7 @@ struct options
         types::String* header = new types::String(scsopt.c_str());
 
         types::TList* Scsopt = new types::TList();
-        Scsopt->set(0, header);
+        Scsopt->append(header);
         return Scsopt;
     }
 
index 31d9364..256813c 100644 (file)
@@ -51,7 +51,7 @@ struct graphics
         MListFields->set(1, orig.c_str());
         MListFields->set(2, sz.c_str());
         MListFields->set(3, exprs.c_str());
-        o->set(0, MListFields);
+        o->append(MListFields);
 
         // orig and sz
         std::vector<double> geom;
@@ -65,8 +65,8 @@ struct graphics
         dataOrig[1] = geom[1];
         dataSz[0]   = geom[2];
         dataSz[1]   = geom[3];
-        o->set(1, origField);
-        o->set(2, szField);
+        o->append(origField);
+        o->append(szField);
 
         // exprs
         std::vector<std::string> Exprs (3);
@@ -79,7 +79,7 @@ struct graphics
         {
             exprsField->set(i, Exprs[i].data());
         }
-        o->set(3, exprsField);
+        o->append(exprsField);
 
         return o;
     }