#include "list.hxx"
#include "tlist.hxx"
#include "mlist.hxx"
+#include "struct.hxx"
extern "C"
{
// An empty list input will return [22; 0], a tlist [23; 0] and an mlist [24; 0]
}
+static void encode(types::Struct* input, std::vector<double> &ret)
+{
+ int iElements = 0;
+ types::String* fields;
+ if (input->getSize() > 0)
+ {
+ fields = input->get(0)->getFieldNames();
+ iElements = fields->getSize();
+ }
+ int totalSize = 2;
+
+ std::vector<std::vector<double>> fieldsContent (1 + iElements);
+ if (input->getSize() > 0)
+ {
+ // Call var2vec on the struct's fields to extract a header
+ var2vec(fields, fieldsContent[0]);
+ totalSize += static_cast<int>(fieldsContent[0].size());
+ // Now extract the fields' content
+ for (int i = 1; i < iElements + 1; ++i)
+ {
+ // Recursively call var2vec on each element and extract the obtained results
+ var2vec(input->get(0)->get(fields->get(i - 1)), fieldsContent[i]);
+ totalSize += static_cast<int>(fieldsContent[i].size());
+ }
+ }
+ // Allocation for type + fields names + fields content
+ ret.resize(totalSize);
+
+ ret[0] = input->getType();
+ ret[1] = iElements;
+ int offset = 0;
+ for (int i = 0; i < iElements + 1; ++i)
+ {
+ memcpy(&ret[2 + offset], &fieldsContent[i][0], fieldsContent[i].size() * sizeof(double));
+ offset += static_cast<int>(fieldsContent[i].size());
+ }
+ // An empty struct input will return [26; 0]
+}
+
bool var2vec(types::InternalType* in, std::vector<double> &out)
{
switch (in->getType())
{
- // Reuse scicos model encoding for 'model.opar' and 'model.odstate' fields
+ // Reuse scicos model encoding for 'model.opar' and 'model.odstate' fields
case types::InternalType::ScilabDouble :
encode(in->getAs<types::Double>(), out);
break;
encode(in->getAs<types::List>(), out);
break;
+ case types::InternalType::ScilabStruct :
+ encode(in->getAs<types::Struct>(), out);
+ break;
+
default :
- Scierror(999, _("%s: Wrong type for input argument #%d: %s, %s, %s, %s or %s type.\n"), var2vecName.c_str(), 1, "Double", "Integer", "Boolean", "String", "List");
+ Scierror(999, _("%s: Wrong type for input argument #%d: %s, %s, %s, %s, %s or %s type.\n"), var2vecName.c_str(), 1, "Double", "Integer", "Boolean", "String", "List", "Struct");
return false;
}
#include "types.hxx"
#include "internal.hxx"
-#include "double.hxx"
bool var2vec(types::InternalType* in, std::vector<double> &out);
#include "list.hxx"
#include "tlist.hxx"
#include "mlist.hxx"
+#include "struct.hxx"
+
+extern "C"
+{
+#include "Scierror.h"
+#include "localization.h"
+}
static const std::string vec2varName = "vec2var";
break;
}
+ case types::InternalType::ScilabStruct :
+ {
+ if (inputRows < 2)
+ {
+ Scierror(999, _("%s: Wrong size for input argument #%d: At least %dx%d expected.\n"), vec2varName.c_str(), 1, offset + 2, 1);
+ return false;
+ }
+
+ if (iDims <= 0)
+ {
+ res = new types::Struct();
+ offset += 2;
+ break;
+ }
+
+ types::Struct* pStruct = new types::Struct(1, 1);
+
+ offset += 2;
+ // Read the header...
+ int elementType = static_cast<int>(*(input + offset));
+ if (elementType != types::InternalType::ScilabString)
+ {
+ Scierror(999, _("%s: Wrong value for input argument #%d: %d (String) expected.\n"), vec2varName.c_str(), 1, 11);
+ return false;
+ }
+ int elementDims = static_cast<int>(*(input + offset + 1));
+ types::InternalType* element;
+ if (!readElement(input + offset, elementType, elementDims, inputRows - offset, offset, element))
+ {
+ return false;
+ }
+ types::String* header = element->getAs<types::String>();
+ // ... and copy it in 'pStruct'
+ for (int i = 0; i < header->getSize(); ++i)
+ {
+ pStruct->get(0)->addField(header->get(i));
+ }
+
+ for (int i = 1; i < iDims + 1; ++i)
+ {
+ if (inputRows < 2 + offset)
+ {
+ Scierror(999, _("%s: Wrong size for input argument #%d: At least %dx%d expected.\n"), vec2varName.c_str(), 1, offset + 2, 1);
+ return false;
+ }
+ // Extract the fields content infos and recursively call readElement
+ elementType = static_cast<int>(*(input + offset));
+ elementDims = static_cast<int>(*(input + offset + 1));
+ if (!readElement(input + offset, elementType, elementDims, inputRows - offset, offset, element))
+ {
+ return false;
+ }
+ pStruct->get(0)->set(header->get(i - 1), element);
+ }
+ res = pStruct;
+ break;
+ }
+
default :
Scierror(999, _("%s: Wrong value for element #%d of input argument #%d: Unknown type.\n"), vec2varName.c_str(), offset + 1, 1);
return false;
#include "types.hxx"
#include "internal.hxx"
-extern "C"
-{
-#include "Scierror.h"
-#include "localization.h"
-}
-
bool vec2var(const std::vector<double> in, types::InternalType* &out);
#endif /* VEC2VAR_HXX_ */
return true;
}
- if (v->getType() != types::InternalType::ScilabList)
- {
- return false;
- }
-
std::vector<double> prop_content;
- if (!var2vec(v->getAs<types::List>(), prop_content))
+ if (!var2vec(v, prop_content))
{
return false;
}
return true;
}
- if (v->getType() != types::InternalType::ScilabList)
- {
- return false;
- }
-
std::vector<double> prop_content;
- if (!var2vec(v->getAs<types::List>(), prop_content))
+ if (!var2vec(v, prop_content))
{
return false;
}
propertyUpdated( 29 , BLOCK , NMODE ) : NO_CHANGES
propertyUpdated( 29 , BLOCK , UID ) : NO_CHANGES
assert_checkequal(o.model.opar, listRef);
+// With tlist
+tlistRef = tlist(["tl" "f1" "f2" "f3"], [], testHM, []);
+o.model.opar = tlistRef;
+propertyUpdated( 29 , BLOCK , SIM_FUNCTION_NAME ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , SIM_FUNCTION_API ) : NO_CHANGES
+propertyUpdated( 30 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 31 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 30 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 31 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 30 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 31 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 32 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 32 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 32 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 33 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , STATE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , DSTATE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , ODSTATE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , RPAR ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , IPAR ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , OPAR ) : SUCCESS
+propertyUpdated( 29 , BLOCK , SIM_BLOCKTYPE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , SIM_DEP_UT ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , LABEL ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , NZCROSS ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , NMODE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , UID ) : NO_CHANGES
+assert_checkequal(o.model.opar, tlistRef);
+// With struct
+structRef = struct("f1", [], "f2", testHM, "f3", []);
+o.model.opar = structRef;
+propertyUpdated( 29 , BLOCK , SIM_FUNCTION_NAME ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , SIM_FUNCTION_API ) : NO_CHANGES
+propertyUpdated( 30 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 31 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 30 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 31 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 30 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 31 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 32 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 32 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 32 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 33 , PORT , DATATYPE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , STATE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , DSTATE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , ODSTATE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , RPAR ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , IPAR ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , OPAR ) : SUCCESS
+propertyUpdated( 29 , BLOCK , SIM_BLOCKTYPE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , SIM_DEP_UT ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , LABEL ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , NZCROSS ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , NMODE ) : NO_CHANGES
+propertyUpdated( 29 , BLOCK , UID ) : NO_CHANGES
+assert_checkequal(o.model.opar, structRef);
// Test 'equations'
o = VsourceAC("define")
objectCreated( 34 , BLOCK )
o.model.opar = listRef;
assert_checkequal(o.model.opar, listRef);
+// With tlist
+tlistRef = tlist(["tl" "f1" "f2" "f3"], [], testHM, []);
+o.model.opar = tlistRef;
+assert_checkequal(o.model.opar, tlistRef);
+// With struct
+structRef = struct("f1", [], "f2", testHM, "f3", []);
+o.model.opar = structRef;
+assert_checkequal(o.model.opar, structRef);
+
// Test 'equations'
o = VsourceAC("define")
o.model.equations
assert_checkequal(vec2var(var2vec(mlEmpties)), mlEmpties);
mlMixed = mlist(["Type" "f1" "f2" "f3" "f4" "f5", "f6"], [], Mat, HyperMat, int32(Mat), string(Mat), Mat>15);
assert_checkequal(vec2var(var2vec(mlMixed)), mlMixed);
+// Struct
+st = struct();
+assert_checkequal(vec2var(var2vec(st)), st);
+stFull = struct("f1", [], "f2", Mat, "f3", HyperMat, "f4", int32(Mat), "f5", string(Mat), "f6", Mat>15);
+assert_checkequal(vec2var(var2vec(stFull)), stFull);
assert_checkequal(vec2var(var2vec(mlEmpties)), mlEmpties);
mlMixed = mlist(["Type" "f1" "f2" "f3" "f4" "f5", "f6"], [], Mat, HyperMat, int32(Mat), string(Mat), Mat>15);
assert_checkequal(vec2var(var2vec(mlMixed)), mlMixed);
+
+// Struct
+st = struct();
+assert_checkequal(vec2var(var2vec(st)), st);
+stFull = struct("f1", [], "f2", Mat, "f3", HyperMat, "f4", int32(Mat), "f5", string(Mat), "f6", Mat>15);
+assert_checkequal(vec2var(var2vec(stFull)), stFull);