Xcos MVC: implement state / dstate / blocktype 65/14965/6
Paul Bignier [Thu, 31 Jul 2014 16:05:20 +0000 (18:05 +0200)]
Also, laid the grounds for the odstate property.

Change-Id: I9826ad8d799b8546af109ff91bec882c06294d6c

scilab/modules/scicos/includes/utilities.hxx
scilab/modules/scicos/src/cpp/LoggerView.cpp
scilab/modules/scicos/src/cpp/Model_getObjectProperties.cpp
scilab/modules/scicos/src/cpp/Model_setObjectProperties.cpp
scilab/modules/scicos/src/cpp/model/Block.hxx
scilab/modules/scicos/src/cpp/view_scilab/ModelAdapter.cpp
scilab/modules/scicos/src/cpp/view_scilab/ports_management.hxx

index 963349d..a755397 100644 (file)
@@ -61,12 +61,16 @@ enum object_properties_t
     SIM_FUNCTION_NAME,  //!< model::Descriptor::functionName value (stored into model::Block::sim)
     SIM_FUNCTION_API,   //!< model::Descriptor::functionApi value (stored into model::Block::sim)
     SIM_SCHEDULE,       //!< model::Descriptor::schedulingProperties value (stored into model::Block::sim)
+    SIM_BLOCKTYPE,      //!< model::Descriptor::blocktype value (stored into model::Block::sim)
     ANGLE,              //!< model::Block::flip and theta values
     EXPRS,              //!< model::Block::exprs value
     INPUTS,             //!< model::Block::in value
     OUTPUTS,            //!< model::Block::out value
     EVENT_INPUTS,       //!< model::Block::ein value
     EVENT_OUTPUTS,      //!< model::Block::eout value
+    STATE,              //!< model::Block::state value
+    DSTATE,             //!< model::Block::dstate value
+    ODSTATE,            //!< model::Block::odstate value
     PARENT_BLOCK,       //!< model::Block::parentBlock value
     CHILDREN,           //!< model::Block::children for superblocks or model::Diagram::children value
     PORT_REFERENCE,     //!< model::Block::portReference value
index 87024c5..dc5c138 100644 (file)
@@ -101,6 +101,9 @@ std::ostream& operator<<(std::ostream& os, object_properties_t p)
         case SIM_SCHEDULE:
             os << "SIM_SCHEDULE";
             break;
+        case SIM_BLOCKTYPE:
+            os << "SIM_BLOCKTYPE";
+            break;
         case ANGLE:
             os << "ANGLE";
             break;
@@ -119,6 +122,15 @@ std::ostream& operator<<(std::ostream& os, object_properties_t p)
         case EVENT_OUTPUTS:
             os << "EVENT_OUTPUTS";
             break;
+        case STATE:
+            os << "STATE";
+            break;
+        case DSTATE:
+            os << "DSTATE";
+            break;
+        case ODSTATE:
+            os << "ODSTATE";
+            break;
         case PARENT_BLOCK:
             os << "PARENT_BLOCK";
             break;
@@ -146,6 +158,15 @@ std::ostream& operator<<(std::ostream& os, object_properties_t p)
         case DATATYPE:
             os << "DATATYPE";
             break;
+        case DATATYPE_ROWS:
+            os << "DATATYPE_ROWS";
+            break;
+        case DATATYPE_COLS:
+            os << "DATATYPE_COLS";
+            break;
+        case DATATYPE_TYPE:
+            os << "DATATYPE_TYPE";
+            break;
         case SOURCE_BLOCK:
             os << "SOURCE_BLOCK";
             break;
@@ -158,15 +179,6 @@ std::ostream& operator<<(std::ostream& os, object_properties_t p)
         case PORT_NUMBER:
             os << "PORT_NUMBER";
             break;
-        case DATATYPE_ROWS:
-            os << "DATATYPE_ROWS";
-            break;
-        case DATATYPE_COLS:
-            os << "DATATYPE_COLS";
-            break;
-        case DATATYPE_TYPE:
-            os << "DATATYPE_TYPE";
-            break;
         case CONNECTED_SIGNALS:
             os << "CONNECTED_SIGNALS";
             break;
index 307c203..0ef0b3c 100644 (file)
@@ -97,6 +97,9 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, int
             case SIM_FUNCTION_API:
                 o->getSimFunctionApi(v);
                 return true;
+            case SIM_BLOCKTYPE:
+                o->getSimBlocktype(v);
+                return true;
             default:
                 break;
         }
@@ -348,6 +351,12 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, std
             case ANGLE:
                 o->getAngle(v);
                 return true;
+            case STATE:
+                o->getState(v);
+                return true;
+            case DSTATE:
+                o->getDState(v);
+                return true;
             default:
                 break;
         }
index 73dcbe2..774b7aa 100644 (file)
@@ -92,9 +92,9 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
         switch (p)
         {
             case SIM_FUNCTION_API:
-            {
                 return o->setSimFunctionApi(v);
-            }
+            case SIM_BLOCKTYPE:
+                return o->setSimBlocktype(v);
             default:
                 break;
         }
@@ -319,6 +319,10 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
                 return o->setGeometry(v);
             case ANGLE:
                 return o->setAngle(v);
+            case STATE:
+                return o->setState(v);
+            case DSTATE:
+                return o->setDState(v);
             default:
                 break;
         }
index 04689a2..acf7f73 100644 (file)
@@ -50,7 +50,7 @@ struct Parameter
 struct State
 {
     std::vector<double> state;
-    std::vector<int> dstate;
+    std::vector<double> dstate;
     list_t odstate;
 };
 
@@ -381,6 +381,67 @@ private:
         return SUCCESS;
     }
 
+    void getSimBlocktype(int& data) const
+    {
+        data = sim.blocktype;
+    }
+
+    update_status_t setSimBlocktype(const int data)
+    {
+        if (data == sim.blocktype)
+        {
+            return NO_CHANGES;
+        }
+
+        switch (data)
+        {
+            case BLOCKTYPE_C:
+            case BLOCKTYPE_D:
+            case BLOCKTYPE_H:
+            case BLOCKTYPE_L:
+            case BLOCKTYPE_M:
+            case BLOCKTYPE_X:
+            case BLOCKTYPE_Z:
+                sim.blocktype = data;
+                break;
+            default:
+                return FAIL;
+        }
+        return SUCCESS;
+    }
+
+    void getState(std::vector<double>& data) const
+    {
+        data = state.state;
+    }
+
+    update_status_t setState(const std::vector<double>& data)
+    {
+        if (data == state.state)
+        {
+            return NO_CHANGES;
+        }
+
+        state.state = data;
+        return SUCCESS;
+    }
+
+    void getDState(std::vector<double>& data) const
+    {
+        data = state.dstate;
+    }
+
+    update_status_t setDState(const std::vector<double>& data)
+    {
+        if (data == state.dstate)
+        {
+            return NO_CHANGES;
+        }
+
+        state.dstate = data;
+        return SUCCESS;
+    }
+
 private:
     ScicosID parentDiagram;
     std::string interfaceFunction;
index da0c599..a3c73b7 100644 (file)
  *
  */
 
+#include <string>
+#include <vector>
+#include <algorithm>
+
 #include "list.hxx"
 #include "double.hxx"
 #include "string.hxx"
@@ -46,7 +50,7 @@ struct sim
         }
         else
         {
-            types::Double* Api = new types::Double(1, 1, (double) api);
+            types::Double* Api = new types::Double(1, 1, static_cast<double>(api));
             types::List* o = new types::List();
             o->set(0, Name);
             o->set(1, Api);
@@ -108,7 +112,7 @@ struct sim
             {
                 return false;
             }
-            int api_int = (int) api;
+            int api_int = static_cast<int>(api);
 
             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api_int);
@@ -121,7 +125,6 @@ struct sim
     }
 };
 
-
 struct in
 {
 
@@ -164,7 +167,6 @@ struct intyp
     }
 };
 
-
 struct out
 {
 
@@ -235,6 +237,169 @@ struct evtout
     }
 };
 
+struct state
+{
+
+    static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
+    {
+        model::Block* adaptee = adaptor.getAdaptee();
+
+        std::vector<double> state;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), STATE, state);
+
+        double* data;
+        types::Double* o = new types::Double(state.size(), 1, &data);
+
+        std::copy(state.begin(), state.end(), data);
+
+        return o;
+    }
+
+    static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+
+        if (v->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+
+        types::Double* current = v->getAs<types::Double>();
+        if (current->getCols() != 0 && current->getCols() != 1)
+        {
+            return false;
+        }
+
+        model::Block* adaptee = adaptor.getAdaptee();
+
+        std::vector<double> state;
+        std::copy(current->getReal(), current->getReal() + current->getSize(), state.begin());
+
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), STATE, state);
+        return true;
+    }
+};
+
+struct dstate
+{
+
+    static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
+    {
+        model::Block* adaptee = adaptor.getAdaptee();
+
+        std::vector<double> dstate;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), DSTATE, dstate);
+
+        double* data;
+        types::Double* o = new types::Double(dstate.size(), 1, &data);
+
+        std::copy(dstate.begin(), dstate.end(), data);
+
+        return o;
+    }
+
+    static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+
+        if (v->getType() != types::InternalType::ScilabDouble)
+        {
+            return false;
+        }
+
+        types::Double* current = v->getAs<types::Double>();
+        if (current->getCols() != 0 && current->getCols() != 1)
+        {
+            return false;
+        }
+
+        model::Block* adaptee = adaptor.getAdaptee();
+
+        std::vector<double> dstate;
+        std::copy(current->getReal(), current->getReal() + current->getSize(), dstate.begin());
+
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), DSTATE, dstate);
+        return true;
+    }
+};
+
+struct odstate
+{
+
+    static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
+    {
+        // FIXME: get odstate
+        return 0;
+    }
+
+    static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        model::Block* adaptee = adaptor.getAdaptee();
+
+        if (v->getType() != types::InternalType::ScilabList)
+        {
+            return false;
+        }
+
+        types::List* current = v->getAs<types::List>();
+
+        if (current->getSize() == 0)
+        {
+            return true;
+        }
+        else
+        {
+            // FIXME: get the input list and store it in the odstate field
+            return false;
+        }
+    }
+};
+
+struct blocktype
+{
+
+    static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
+    {
+        model::Block* adaptee = adaptor.getAdaptee();
+
+        int type;
+        controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_BLOCKTYPE, type);
+
+        wchar_t Type = type;
+        types::String* o = new types::String(&Type);
+
+        return o;
+    }
+
+    static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
+    {
+        model::Block* adaptee = adaptor.getAdaptee();
+
+        if (v->getType() != types::InternalType::ScilabString)
+        {
+            return false;
+        }
+
+        types::String* current = v->getAs<types::String>();
+        if (current->getSize() != 1)
+        {
+            return false;
+        }
+        // The input must be a character
+        if (current->get(0)[0] == '\0')
+        {
+            return false;
+        }
+        if (current->get(0)[1] != '\0')
+        {
+            return false;
+        }
+
+        int type = current->get(0)[0];
+
+        controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_BLOCKTYPE, type);
+        return true;
+    }
+};
+
 template<> property<ModelAdapter>::props_t property<ModelAdapter>::fields = property<ModelAdapter>::props_t();
 
 ModelAdapter::ModelAdapter(const ModelAdapter& o) :
@@ -245,7 +410,7 @@ ModelAdapter::ModelAdapter(org_scilab_modules_scicos::model::Block* o) :
 {
     if (property<ModelAdapter>::properties_has_not_been_set())
     {
-        property<ModelAdapter>::fields.reserve(7);
+        property<ModelAdapter>::fields.reserve(13);
         property<ModelAdapter>::add_property(L"sim", &sim::get, &sim::set);
         property<ModelAdapter>::add_property(L"in", &in::get, &in::set);
         property<ModelAdapter>::add_property(L"in2", &in2::get, &in2::set);
@@ -255,6 +420,10 @@ ModelAdapter::ModelAdapter(org_scilab_modules_scicos::model::Block* o) :
         property<ModelAdapter>::add_property(L"outtyp", &outtyp::get, &outtyp::set);
         property<ModelAdapter>::add_property(L"evtin", &evtin::get, &evtin::set);
         property<ModelAdapter>::add_property(L"evtout", &evtout::get, &evtout::set);
+        property<ModelAdapter>::add_property(L"state", &state::get, &state::set);
+        property<ModelAdapter>::add_property(L"dstate", &dstate::get, &dstate::set);
+        property<ModelAdapter>::add_property(L"odstate", &odstate::get, &odstate::set);
+        property<ModelAdapter>::add_property(L"blocktype", &blocktype::get, &blocktype::set);
     }
 }
 
index dbe9e90..8a549a4 100644 (file)
@@ -270,7 +270,7 @@ bool fillNewPorts(std::vector<int>& newPorts, const std::vector<ScicosID>& child
             }
         } // no check is performed for other properties as newPorts will contains value not index
 
-        *it = (int) (*d);
+        *it = static_cast<int>(*d);
     }
     return true;
 }