2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
30 #include "Controller.hxx"
31 #include "Adapters.hxx"
32 #include "ModelAdapter.hxx"
33 #include "DiagramAdapter.hxx"
34 #include "ports_management.hxx"
35 #include "utilities.hxx"
38 #include "sci_malloc.h"
39 #include "charEncoding.h"
42 namespace org_scilab_modules_scicos
49 const std::string input ("input");
50 const std::string output ("output");
51 const std::string inimpl ("inimpl");
52 const std::string outimpl ("outimpl");
54 const std::wstring modelica (L"modelica");
55 const std::wstring model (L"model");
56 const std::wstring inputs (L"inputs");
57 const std::wstring outputs (L"outputs");
58 const std::wstring parameters (L"parameters");
63 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
65 ScicosID adaptee = adaptor.getAdaptee()->id();
67 // First, extract the function Name
69 controller.getObjectProperty(adaptee, BLOCK, SIM_FUNCTION_NAME, name);
70 types::String* Name = new types::String(1, 1);
71 Name->set(0, name.data());
73 // Then the Api. If it is zero, then just return the Name. Otherwise, return a list containing both.
75 controller.getObjectProperty(adaptee, BLOCK, SIM_FUNCTION_API, api);
83 types::Double* Api = new types::Double(static_cast<double>(api));
84 types::List* o = new types::List();
91 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
93 ScicosID adaptee = adaptor.getAdaptee()->id();
95 if (v->getType() == types::InternalType::ScilabString)
97 types::String* current = v->getAs<types::String>();
98 if (current->getSize() != 1)
103 char* c_str = wide_string_to_UTF8(current->get(0));
104 std::string name(c_str);
107 // If the input is a scalar string, then the functionApi is 0.
110 controller.setObjectProperty(adaptee, BLOCK, SIM_FUNCTION_NAME, name);
111 controller.setObjectProperty(adaptee, BLOCK, SIM_FUNCTION_API, api);
113 else if (v->getType() == types::InternalType::ScilabList)
115 // If the input is a 2-sized list, then it must be string and positive integer.
116 types::List* current = v->getAs<types::List>();
117 if (current->getSize() != 2)
121 if (current->get(0)->getType() != types::InternalType::ScilabString || current->get(1)->getType() != types::InternalType::ScilabDouble)
126 types::String* Name = current->get(0)->getAs<types::String>();
127 if (Name->getSize() != 1)
131 char* c_str = wide_string_to_UTF8(Name->get(0));
132 std::string name(c_str);
135 types::Double* Api = current->get(1)->getAs<types::Double>();
136 if (Api->getSize() != 1)
140 double api = Api->get(0);
141 if (floor(api) != api)
145 int api_int = static_cast<int>(api);
147 controller.setObjectProperty(adaptee, BLOCK, SIM_FUNCTION_NAME, name);
148 controller.setObjectProperty(adaptee, BLOCK, SIM_FUNCTION_API, api_int);
161 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
163 return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, INPUTS, controller);
166 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
168 return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, INPUTS, controller, v);
175 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
177 return get_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, INPUTS, controller);
180 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
182 return set_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, INPUTS, controller, v);
189 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
191 return get_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, INPUTS, controller);
194 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
196 return set_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, INPUTS, controller, v);
203 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
205 return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, OUTPUTS, controller);
208 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
210 return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, OUTPUTS, controller, v);
217 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
219 return get_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, OUTPUTS, controller);
222 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
224 return set_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, OUTPUTS, controller, v);
231 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
233 return get_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, OUTPUTS, controller);
236 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
238 return set_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, OUTPUTS, controller, v);
245 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
247 return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_INPUTS, controller);
250 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
252 return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_INPUTS, controller, v);
259 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
261 return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_OUTPUTS, controller);
264 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
266 return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_OUTPUTS, controller, v);
273 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
275 ScicosID adaptee = adaptor.getAdaptee()->id();
277 std::vector<double> state;
278 controller.getObjectProperty(adaptee, BLOCK, STATE, state);
281 types::Double* o = new types::Double((int)state.size(), 1, &data);
284 std::copy(state.begin(), state.end(), stdext::checked_array_iterator<double*>(data, state.size()));
286 std::copy(state.begin(), state.end(), data);
291 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
294 if (v->getType() != types::InternalType::ScilabDouble)
299 types::Double* current = v->getAs<types::Double>();
300 if (current->getCols() != 0 && current->getCols() != 1)
305 ScicosID adaptee = adaptor.getAdaptee()->id();
307 std::vector<double> state (current->getSize());
308 std::copy(current->getReal(), current->getReal() + current->getSize(), state.begin());
310 controller.setObjectProperty(adaptee, BLOCK, STATE, state);
318 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
320 ScicosID adaptee = adaptor.getAdaptee()->id();
322 std::vector<double> dstate;
323 controller.getObjectProperty(adaptee, BLOCK, DSTATE, dstate);
326 types::Double* o = new types::Double((int)dstate.size(), 1, &data);
329 std::copy(dstate.begin(), dstate.end(), stdext::checked_array_iterator<double*>(data, dstate.size()));
331 std::copy(dstate.begin(), dstate.end(), data);
336 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
338 ScicosID adaptee = adaptor.getAdaptee()->id();
340 if (v->getType() == types::InternalType::ScilabString)
342 types::String* current = v->getAs<types::String>();
343 if (current->getSize() != 1)
348 std::vector<double> dstate;
349 controller.setObjectProperty(adaptee, BLOCK, DSTATE, dstate);
353 if (v->getType() != types::InternalType::ScilabDouble)
357 types::Double* current = v->getAs<types::Double>();
358 if (current->getCols() != 0 && current->getCols() != 1)
363 std::vector<double> dstate (current->getSize());
364 std::copy(current->getReal(), current->getReal() + current->getSize(), dstate.begin());
366 controller.setObjectProperty(adaptee, BLOCK, DSTATE, dstate);
371 types::InternalType* getPropList(const ModelAdapter& adaptor, const Controller& controller, const object_properties_t prop)
373 ScicosID adaptee = adaptor.getAdaptee()->id();
375 std::vector<int> prop_content;
376 controller.getObjectProperty(adaptee, BLOCK, prop, prop_content);
378 if (prop_content.empty())
380 return types::Double::Empty();
383 types::List* list = new types::List();
385 int index = 1; // Index to each element of the returned list
387 for (int i = 0; i < prop_content[0]; ++i) // 'list' must have exactly 'prop_content[0]' elements
392 int numberOfIntNeeded = 0;
393 switch (prop_content[index])
395 case types::InternalType::ScilabDouble:
397 iDims = prop_content[index + 1];
398 pDims = new int[iDims];
399 for (int j = 0; j < iDims; ++j)
401 pDims[j] = prop_content[index + 2 + j];
402 iElements *= pDims[j];
406 numberOfIntNeeded = 1; // Only mind the complex flag
407 list->set(i, types::Double::Empty());
410 int isComplex = prop_content[index + 2 + iDims];
412 types::Double* pDouble;
416 pDouble = new types::Double(iDims, pDims, false);
417 numberOfIntNeeded = 2 * iElements + 1;
418 memcpy(pDouble->get(), &prop_content[index + 2 + iDims + 1], iElements * sizeof(double));
422 pDouble = new types::Double(iDims, pDims, true);
423 numberOfIntNeeded = 4 * iElements + 1;
424 memcpy(pDouble->get(), &prop_content[index + 2 + iDims + 1], iElements * sizeof(double));
425 memcpy(pDouble->getImg(), &prop_content[index + 2 + iDims + 1 + 2 * iElements], iElements * sizeof(double));
429 list->set(i, pDouble);
432 case types::InternalType::ScilabInt8:
434 iDims = prop_content[index + 1];
435 pDims = new int[iDims];
436 for (int j = 0; j < iDims; ++j)
438 pDims[j] = prop_content[index + 2 + j];
439 iElements *= pDims[j];
441 numberOfIntNeeded = ((iElements - 1) / 4) + 1;
443 types::Int8* pInt8 = new types::Int8(iDims, pDims);
446 // Use a buffer to prevent copying only parts of integers
447 char* buffer = new char[numberOfIntNeeded * 4];
448 memcpy(buffer, &prop_content[index + 2 + iDims], numberOfIntNeeded * sizeof(int));
449 memcpy(pInt8->get(), buffer, iElements * sizeof(char));
455 case types::InternalType::ScilabUInt8:
457 iDims = prop_content[index + 1];
458 pDims = new int[iDims];
459 for (int j = 0; j < iDims; ++j)
461 pDims[j] = prop_content[index + 2 + j];
462 iElements *= pDims[j];
464 numberOfIntNeeded = ((iElements - 1) / 4) + 1;
466 types::UInt8* pUInt8 = new types::UInt8(iDims, pDims);
469 // Use a buffer to prevent copying only parts of integers
470 unsigned char* buffer = new unsigned char[numberOfIntNeeded * 4];
471 memcpy(buffer, &prop_content[index + 2 + iDims], numberOfIntNeeded * sizeof(int));
472 memcpy(pUInt8->get(), buffer, iElements * sizeof(char));
475 list->set(i, pUInt8);
478 case types::InternalType::ScilabInt16:
480 iDims = prop_content[index + 1];
481 pDims = new int[iDims];
482 for (int j = 0; j < iDims; ++j)
484 pDims[j] = prop_content[index + 2 + j];
485 iElements *= pDims[j];
487 numberOfIntNeeded = ((iElements - 1) / 2) + 1;
489 types::Int16* pInt16 = new types::Int16(iDims, pDims);
492 // Use a buffer to prevent copying only parts of integers
493 short int* buffer = new short int[numberOfIntNeeded * 2];
494 memcpy(buffer, &prop_content[index + 2 + iDims], numberOfIntNeeded * sizeof(int));
495 memcpy(pInt16->get(), buffer, iElements * sizeof(short int));
498 list->set(i, pInt16);
501 case types::InternalType::ScilabUInt16:
503 iDims = prop_content[index + 1];
504 pDims = new int[iDims];
505 for (int j = 0; j < iDims; ++j)
507 pDims[j] = prop_content[index + 2 + j];
508 iElements *= pDims[j];
510 numberOfIntNeeded = ((iElements - 1) / 2) + 1;
512 types::UInt16* pUInt16 = new types::UInt16(iDims, pDims);
515 // Use a buffer to prevent copying only parts of integers
516 unsigned short int* buffer = new unsigned short int[numberOfIntNeeded * 2];
517 memcpy(buffer, &prop_content[index + 2 + iDims], numberOfIntNeeded * sizeof(int));
518 memcpy(pUInt16->get(), buffer, iElements * sizeof(unsigned short int));
521 list->set(i, pUInt16);
524 case types::InternalType::ScilabInt32:
526 iDims = prop_content[index + 1];
527 pDims = new int[iDims];
528 for (int j = 0; j < iDims; ++j)
530 pDims[j] = prop_content[index + 2 + j];
531 iElements *= pDims[j];
533 numberOfIntNeeded = iElements;
535 types::Int32* pInt32 = new types::Int32(iDims, pDims);
538 memcpy(pInt32->get(), &prop_content[index + 2 + iDims], iElements * sizeof(int));
540 list->set(i, pInt32);
543 case types::InternalType::ScilabUInt32:
545 iDims = prop_content[index + 1];
546 pDims = new int[iDims];
547 for (int j = 0; j < iDims; ++j)
549 pDims[j] = prop_content[index + 2 + j];
550 iElements *= pDims[j];
552 numberOfIntNeeded = iElements;
554 types::UInt32* pUInt32 = new types::UInt32(iDims, pDims);
557 memcpy(pUInt32->get(), &prop_content[index + 2 + iDims], iElements * sizeof(unsigned int));
559 list->set(i, pUInt32);
562 case types::InternalType::ScilabString:
564 iDims = prop_content[index + 1];
565 pDims = new int[iDims];
566 for (int j = 0; j < iDims; ++j)
568 pDims[j] = prop_content[index + 2 + j];
569 iElements *= pDims[j];
572 types::String* pString = new types::String(iDims, pDims);
575 for (int j = 0; j < iElements; ++j)
577 int strLen = prop_content[index + 2 + iDims + numberOfIntNeeded];
579 wchar_t* str = new wchar_t[strLen + 1];
580 memcpy(str, &prop_content[index + 2 + iDims + numberOfIntNeeded + 1], strLen * sizeof(wchar_t));
582 pString->set(j, str);
585 numberOfIntNeeded += 1 + strLen;
587 list->set(i, pString);
590 case types::InternalType::ScilabBool:
592 iDims = prop_content[index + 1];
593 pDims = new int[iDims];
594 for (int j = 0; j < iDims; ++j)
596 pDims[j] = prop_content[index + 2 + j];
597 iElements *= pDims[j];
599 numberOfIntNeeded = iElements;
601 types::Bool* pBool = new types::Bool(iDims, pDims);
604 memcpy(pBool->get(), &prop_content[index + 2 + iDims], iElements * sizeof(int));
612 index += 2 + iDims + numberOfIntNeeded;
618 bool setPropList(ModelAdapter& adaptor, Controller& controller, const object_properties_t prop, types::InternalType* v)
620 ScicosID adaptee = adaptor.getAdaptee()->id();
622 if (v->getType() == types::InternalType::ScilabDouble)
624 types::Double* current = v->getAs<types::Double>();
625 if (current->getSize() != 0)
630 std::vector<int> prop_content;
631 controller.setObjectProperty(adaptee, BLOCK, prop, prop_content);
635 if (v->getType() != types::InternalType::ScilabList)
640 types::List* list = v->getAs<types::List>();
642 // 'prop_content' will be a buffer containing the elements of the list, copied into 'int' type by bits
643 std::vector<int> prop_content (1, list->getSize()); // Save the number of list elements in the first element
644 int index = 1; // Index to point at every new list element
646 for (int i = 0; i < list->getSize(); ++i)
648 // Save the variable type
649 prop_content.resize(prop_content.size() + 1);
650 prop_content[index] = list->get(i)->getType();
651 // The two previous lines could be simplified to 'prop_content.push_back(list->get(i)->getType());' but they explicit 'index' role
656 int numberOfIntNeeded = 0;
657 switch (list->get(i)->getType())
659 case types::InternalType::ScilabDouble:
661 types::Double* pDouble = list->get(i)->getAs<types::Double>();
662 iDims = pDouble->getDims();
663 pDims = pDouble->getDimsArray();
664 for (int j = 0; j < iDims; ++j)
666 iElements *= pDims[j];
669 if (!pDouble->isComplex())
671 // It takes 2 int (4 bytes) to save 1 real (1 double: 8 bytes)
672 // So reserve '2*iElements', '1 + iDims' integers for the matrix dimensions and 1 for the complexity
673 numberOfIntNeeded = 1 + 2 * iElements;
674 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
675 prop_content[index + 2 + iDims] = 0; // Flag for real
677 // Using contiguity of the memory, we save the input into 'prop_content'
678 memcpy(&prop_content[index + 2 + iDims + 1], pDouble->getReal(), iElements * sizeof(double));
682 // It takes 4 int (4 bytes) to save 1 complex (2 double: 16 bytes)
683 // So reserve '4*iElements', '1 + iDims' integers for the matrix dimensions and 1 for the complexity
684 numberOfIntNeeded = 1 + 4 * iElements;
685 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
686 prop_content[index + 2 + iDims] = 1; // Flag for complex
688 // Contiguously save the real and complex parts
689 memcpy(&prop_content[index + 2 + iDims + 1], pDouble->getReal(), iElements * sizeof(double));
690 memcpy(&prop_content[index + 2 + iDims + 1 + 2 * iElements], pDouble->getImg(), iElements * sizeof(double));
694 case types::InternalType::ScilabInt8:
696 types::Int8* pInt8 = list->get(i)->getAs<types::Int8>();
697 iDims = pInt8->getDims();
698 pDims = pInt8->getDimsArray();
699 for (int j = 0; j < iDims; ++j)
701 iElements *= pDims[j];
704 // It takes 1 int (4 bytes) to save 4 char (1 byte)
705 // So reserve 'iElements/4' and '1+iDims' integers for the matrix dimensions
706 numberOfIntNeeded = ((iElements - 1) / 4) + 1;
707 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
709 // Using contiguity of the memory, we save the input into 'prop_content'
710 // Use a buffer to fill the entirety of 'prop_content'
711 char* buffer = new char[numberOfIntNeeded * 4];
712 memcpy(buffer, pInt8->get(), iElements * sizeof(char));
713 memcpy(&prop_content[index + 2 + iDims], buffer, numberOfIntNeeded * sizeof(int));
717 case types::InternalType::ScilabUInt8:
719 types::UInt8* pUInt8 = list->get(i)->getAs<types::UInt8>();
720 iDims = pUInt8->getDims();
721 pDims = pUInt8->getDimsArray();
722 for (int j = 0; j < iDims; ++j)
724 iElements *= pDims[j];
727 // It takes 1 int (4 bytes) to save 4 unsigned char (1 byte)
728 // So reserve 'iElements/4' and '1+iDims' integers for the matrix dimensions
729 numberOfIntNeeded = ((iElements - 1) / 4) + 1;
730 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
732 // Using contiguity of the memory, we save the input into 'prop_content'
733 // Use a buffer to fill the entirety of 'prop_content'
734 unsigned char* buffer = new unsigned char[numberOfIntNeeded * 4];
735 memcpy(buffer, pUInt8->get(), iElements * sizeof(unsigned char));
736 memcpy(&prop_content[index + 2 + iDims], buffer, numberOfIntNeeded * sizeof(int));
740 case types::InternalType::ScilabInt16:
742 types::Int16* pInt16 = list->get(i)->getAs<types::Int16>();
743 iDims = pInt16->getDims();
744 pDims = pInt16->getDimsArray();
745 for (int j = 0; j < iDims; ++j)
747 iElements *= pDims[j];
750 // It takes 1 int (4 bytes) to save 2 short int (2 bytes)
751 // So reserve 'iElements/2' and '1+iDims' integers for the matrix dimensions
752 numberOfIntNeeded = ((iElements - 1) / 2) + 1;
753 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
755 // Using contiguity of the memory, we save the input into 'prop_content'
756 // Use a buffer to fill the entirety of 'prop_content'
757 short int* buffer = new short int[numberOfIntNeeded * 2];
758 memcpy(buffer, pInt16->get(), iElements * sizeof(short int));
759 memcpy(&prop_content[index + 2 + iDims], buffer, numberOfIntNeeded * sizeof(int));
763 case types::InternalType::ScilabUInt16:
765 types::UInt16* pUInt16 = list->get(i)->getAs<types::UInt16>();
766 iDims = pUInt16->getDims();
767 pDims = pUInt16->getDimsArray();
768 for (int j = 0; j < iDims; ++j)
770 iElements *= pDims[j];
773 // It takes 1 int (4 bytes) to save 2 unsigned short int (2 bytes)
774 // So reserve 'iElements/2' and '1+iDims' integers for the matrix dimensions
775 numberOfIntNeeded = ((iElements - 1) / 2) + 1;
776 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
778 // Using contiguity of the memory, we save the input into prop_content
779 // Use a buffer to fill the entirety of 'prop_content'
780 unsigned short int* buffer = new unsigned short int[numberOfIntNeeded * 2];
781 memcpy(buffer, pUInt16->get(), iElements * sizeof(unsigned short int));
782 memcpy(&prop_content[index + 2 + iDims], buffer, numberOfIntNeeded * sizeof(int));
786 case types::InternalType::ScilabInt32:
788 types::Int32* pInt32 = list->get(i)->getAs<types::Int32>();
789 iDims = pInt32->getDims();
790 pDims = pInt32->getDimsArray();
791 for (int j = 0; j < iDims; ++j)
793 iElements *= pDims[j];
796 // It takes 1 int (4 bytes) to save 1 int (4 bytes)
797 // So reserve 'iElements' and '1+iDims' integers for the matrix dimensions
798 numberOfIntNeeded = iElements;
799 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
801 // Using contiguity of the memory, we save the input into 'prop_content'
802 memcpy(&prop_content[index + 2 + iDims], pInt32->get(), iElements * sizeof(int));
805 case types::InternalType::ScilabUInt32:
807 types::UInt32* pUInt32 = list->get(i)->getAs<types::UInt32>();
808 iDims = pUInt32->getDims();
809 pDims = pUInt32->getDimsArray();
810 for (int j = 0; j < iDims; ++j)
812 iElements *= pDims[j];
815 // It takes 1 int (4 bytes) to save 1 unsigned int (4 bytes)
816 // So reserve 'iElements' and '1+iDims' integers for the matrix dimensions
817 numberOfIntNeeded = iElements;
818 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
820 // Using contiguity of the memory, we save the input into 'prop_content'
821 memcpy(&prop_content[index + 2 + iDims], pUInt32->get(), iElements * sizeof(unsigned int));
824 case types::InternalType::ScilabInt64:
825 case types::InternalType::ScilabUInt64:
826 // int64 and uint64 are not treated yet
828 case types::InternalType::ScilabString:
830 types::String* pString = list->get(i)->getAs<types::String>();
831 iDims = pString->getDims();
832 pDims = pString->getDimsArray();
833 for (int j = 0; j < iDims; ++j)
835 iElements *= pDims[j];
838 // For the moment, we don't know how many characters each string is long, so only reserve the matrix size
839 prop_content.resize(prop_content.size() + 1 + iDims);
841 for (int j = 0; j < iElements; ++j)
843 // Extract the input string length and reserve as many characters in 'prop_content'
844 int strLen = static_cast<int>(wcslen(pString->get(j)));
845 prop_content.resize(prop_content.size() + 1 + strLen);
846 prop_content[index + 2 + iDims + numberOfIntNeeded] = strLen;
848 memcpy(&prop_content[index + 2 + iDims + numberOfIntNeeded + 1], pString->get(j), strLen * sizeof(wchar_t));
849 numberOfIntNeeded += 1 + strLen;
853 case types::InternalType::ScilabBool:
855 types::Bool* pBool = list->get(i)->getAs<types::Bool>();
856 iDims = pBool->getDims();
857 pDims = pBool->getDimsArray();
858 for (int j = 0; j < iDims; ++j)
860 iElements *= pDims[j];
863 // It takes 1 int (4 bytes) to save 1 bool (1 int: 4 bytes)
864 // So reserve 'iElements' and '1+iDims' integers for the matrix dimensions
865 numberOfIntNeeded = iElements;
866 prop_content.resize(prop_content.size() + 1 + iDims + numberOfIntNeeded);
868 // Using contiguity of the memory, we save the input into 'prop_content'
869 memcpy(&prop_content[index + 2 + iDims], pBool->get(), iElements * sizeof(int));
875 // Save the matrix (/hypermatrix) dimensions in 'prop_content' and increment index to match the next list element
876 prop_content[index + 1] = iDims;
877 for (int j = 0; j < iDims; ++j)
879 prop_content[index + 2 + j] = pDims[j];
881 index += 2 + iDims + numberOfIntNeeded;
884 controller.setObjectProperty(adaptee, BLOCK, prop, prop_content);
891 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
893 return getPropList(adaptor, controller, ODSTATE);
896 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
898 return setPropList(adaptor, controller, ODSTATE, v);
903 * When setting a diagram in 'rpar', the Superblock's ports must be consistent with the "port blocks" inside it.
904 * By "port blocks", we mean IN_f, OUT_f, CLKIN_f, CLKOUT_f, CLKINV_f, CLKOUTV_f, INIMPL_f and OUTIMPL_f.
906 bool setInnerBlocksRefs(ModelAdapter& adaptor, const std::vector<ScicosID>& children, Controller& controller)
908 ScicosID adaptee = adaptor.getAdaptee()->id();
910 for (std::vector<ScicosID>::const_iterator it = children.begin(); it != children.end(); ++it)
912 if (controller.getObject(*it)->kind() == BLOCK) // Rule out Annotations and Links
915 controller.getObjectProperty(*it, BLOCK, SIM_FUNCTION_NAME, name);
917 // Find the "port blocks"
918 if (name == input || name == inimpl || name == output || name == outimpl)
920 std::vector<int> ipar;
921 controller.getObjectProperty(*it, BLOCK, IPAR, ipar);
922 if (ipar.size() != 1)
926 int portIndex = ipar[0];
928 // "name" is not enough to tell the event and data ports apart, so check the block's port.
929 object_properties_t kind;
930 std::vector<ScicosID> innerPort;
931 if (name == input || name == inimpl)
933 controller.getObjectProperty(*it, BLOCK, OUTPUTS, innerPort);
934 if (!innerPort.empty())
945 controller.getObjectProperty(*it, BLOCK, INPUTS, innerPort);
946 if (!innerPort.empty())
952 kind = EVENT_OUTPUTS;
956 std::vector<ScicosID> superPorts;
957 controller.getObjectProperty(adaptee, BLOCK, kind, superPorts);
958 if (static_cast<int>(superPorts.size()) < portIndex)
963 ScicosID port = superPorts[portIndex - 1];
965 // Check consistency of the implicitness between the inner and outer ports
967 controller.getObjectProperty(port, PORT, IMPLICIT, isImplicit);
968 if (name == input || name == output)
983 controller.setObjectProperty(*it, BLOCK, PORT_REFERENCE, port);
986 // Regardless of the ports, use the loop to set each Block's 'parent_block' property
987 controller.setObjectProperty(*it, BLOCK, PARENT_BLOCK, adaptee);
996 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
998 ScicosID adaptee = adaptor.getAdaptee()->id();
1000 std::vector<ScicosID> diagramChild;
1001 controller.getObjectProperty(adaptee, BLOCK, CHILDREN, diagramChild);
1003 if (diagramChild.empty())
1005 std::vector<double> rpar;
1006 controller.getObjectProperty(adaptee, BLOCK, RPAR, rpar);
1009 types::Double* o = new types::Double((int)rpar.size(), 1, &data);
1011 std::copy(rpar.begin(), rpar.end(), stdext::checked_array_iterator<double*>(data, rpar.size()));
1013 std::copy(rpar.begin(), rpar.end(), data);
1017 else // SuperBlock, return the contained diagram
1019 std::shared_ptr<model::Diagram> super = std::static_pointer_cast<model::Diagram>(controller.getObject(diagramChild[0]));
1020 DiagramAdapter* localAdaptor = new DiagramAdapter(super);
1022 DiagramAdapter* diagram = adaptor.getDiagram();
1023 localAdaptor->setListObjects(diagram->getListObjects());
1024 localAdaptor->setFrom(diagram->getFrom());
1025 localAdaptor->setTo(diagram->getTo());
1026 return localAdaptor;
1030 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1032 ScicosID adaptee = adaptor.getAdaptee()->id();
1034 if (v->getType() == types::InternalType::ScilabDouble)
1036 types::Double* current = v->getAs<types::Double>();
1038 std::vector<double> rpar (current->getSize());
1039 for (int i = 0; i < current->getSize(); ++i)
1041 rpar[i] = current->get(i);
1044 controller.setObjectProperty(adaptee, BLOCK, RPAR, rpar);
1047 else if (v->getType() == types::InternalType::ScilabString)
1049 // Allow Text blocks to define strings in rpar
1052 else if (v->getType() == types::InternalType::ScilabUserType)
1054 // Make sure the input describes a Diagram
1055 const Adapters::adapters_index_t adapter_index = Adapters::instance().lookup_by_typename(v->getShortTypeStr());
1056 if (adapter_index != Adapters::DIAGRAM_ADAPTER)
1061 // Translate 'v' to an DiagramAdapter, save it and update the Block's children list
1062 DiagramAdapter* diagram = v->getAs<DiagramAdapter>();
1064 adaptor.setDiagram(diagram);
1066 // Save the child diagram's ID so it is deleted on 'clear'
1067 std::vector<ScicosID> diagramChild (1, diagram->getAdaptee()->id());
1068 controller.setObjectProperty(adaptee, BLOCK, CHILDREN, diagramChild);
1070 // Link the Superblock ports to their inner "port blocks"
1071 std::vector<ScicosID> diagramChildren;
1072 controller.getObjectProperty(diagram->getAdaptee()->id(), DIAGRAM, CHILDREN, diagramChildren);
1073 return setInnerBlocksRefs(adaptor, diagramChildren, controller);
1082 double toDouble(const int a)
1084 return static_cast<double>(a);
1090 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1092 ScicosID adaptee = adaptor.getAdaptee()->id();
1094 std::vector<int> ipar;
1095 controller.getObjectProperty(adaptee, BLOCK, IPAR, ipar);
1098 types::Double* o = new types::Double((int)ipar.size(), 1, &data);
1101 std::transform(ipar.begin(), ipar.end(), stdext::checked_array_iterator<double*>(data, ipar.size()), toDouble);
1103 std::transform(ipar.begin(), ipar.end(), data, toDouble);
1108 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1110 ScicosID adaptee = adaptor.getAdaptee()->id();
1112 if (v->getType() == types::InternalType::ScilabList)
1114 std::vector<int> ipar;
1115 controller.setObjectProperty(adaptee, BLOCK, IPAR, ipar);
1119 if (v->getType() != types::InternalType::ScilabDouble)
1124 types::Double* current = v->getAs<types::Double>();
1125 if (current->getCols() != 0 && current->getCols() != 1)
1130 std::vector<int> ipar (current->getSize());
1131 for (int i = 0; i < current->getSize(); ++i)
1133 if (floor(current->get(i)) != current->get(i))
1137 ipar[i] = static_cast<int>(current->get(i));
1140 controller.setObjectProperty(adaptee, BLOCK, IPAR, ipar);
1148 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1150 return getPropList(adaptor, controller, OPAR);
1153 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1155 return setPropList(adaptor, controller, OPAR, v);
1162 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1164 ScicosID adaptee = adaptor.getAdaptee()->id();
1167 controller.getObjectProperty(adaptee, BLOCK, SIM_BLOCKTYPE, type);
1169 types::String* o = new types::String(type.c_str());
1173 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1175 ScicosID adaptee = adaptor.getAdaptee()->id();
1177 if (v->getType() != types::InternalType::ScilabString)
1182 types::String* current = v->getAs<types::String>();
1183 if (current->getSize() != 1)
1188 char* c_str = wide_string_to_UTF8(current->get(0));
1189 std::string type (c_str);
1192 // the value validation is performed on the model
1193 return controller.setObjectProperty(adaptee, BLOCK, SIM_BLOCKTYPE, type) != FAIL;
1200 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1202 return get_ports_property<ModelAdapter, FIRING>(adaptor, EVENT_OUTPUTS, controller);
1205 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1207 return set_ports_property<ModelAdapter, FIRING>(adaptor, EVENT_OUTPUTS, controller, v);
1214 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1216 ScicosID adaptee = adaptor.getAdaptee()->id();
1218 std::vector<int> dep_ut;
1219 controller.getObjectProperty(adaptee, BLOCK, SIM_DEP_UT, dep_ut);
1222 types::Bool* o = new types::Bool(1, 2, &dep);
1230 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1232 ScicosID adaptee = adaptor.getAdaptee()->id();
1234 if (v->getType() != types::InternalType::ScilabBool)
1239 types::Bool* current = v->getAs<types::Bool>();
1240 if (current->getRows() != 1 || current->getCols() != 2)
1245 std::vector<int> dep_ut (2);
1246 dep_ut[0] = current->get(0);
1247 dep_ut[1] = current->get(1);
1249 controller.setObjectProperty(adaptee, BLOCK, SIM_DEP_UT, dep_ut);
1257 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1259 ScicosID adaptee = adaptor.getAdaptee()->id();
1262 controller.getObjectProperty(adaptee, BLOCK, LABEL, label);
1264 types::String* o = new types::String(1, 1);
1265 o->set(0, label.data());
1270 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1272 if (v->getType() != types::InternalType::ScilabString)
1277 types::String* current = v->getAs<types::String>();
1278 if (current->getSize() != 1)
1283 ScicosID adaptee = adaptor.getAdaptee()->id();
1285 char* c_str = wide_string_to_UTF8(current->get(0));
1286 std::string label(c_str);
1289 controller.setObjectProperty(adaptee, BLOCK, LABEL, label);
1297 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1299 ScicosID adaptee = adaptor.getAdaptee()->id();
1301 std::vector<int> nzcross;
1302 controller.getObjectProperty(adaptee, BLOCK, NZCROSS, nzcross);
1305 types::Double* o = new types::Double((int)nzcross.size(), 1, &data);
1308 std::transform(nzcross.begin(), nzcross.end(), stdext::checked_array_iterator<double*>(data, nzcross.size()), toDouble);
1310 std::transform(nzcross.begin(), nzcross.end(), data, toDouble);
1315 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1317 ScicosID adaptee = adaptor.getAdaptee()->id();
1319 if (v->getType() != types::InternalType::ScilabDouble)
1324 types::Double* current = v->getAs<types::Double>();
1325 if (current->getCols() != 0 && current->getCols() != 1)
1330 std::vector<int> nzcross (current->getSize());
1331 for (int i = 0; i < current->getSize(); ++i)
1333 if (floor(current->get(i)) != current->get(i))
1337 nzcross[i] = static_cast<int>(current->get(i));
1340 controller.setObjectProperty(adaptee, BLOCK, NZCROSS, nzcross);
1348 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1350 ScicosID adaptee = adaptor.getAdaptee()->id();
1352 std::vector<int> nmode;
1353 controller.getObjectProperty(adaptee, BLOCK, NMODE, nmode);
1356 types::Double* o = new types::Double((int)nmode.size(), 1, &data);
1359 std::transform(nmode.begin(), nmode.end(), stdext::checked_array_iterator<double*>(data, nmode.size()), toDouble);
1361 std::transform(nmode.begin(), nmode.end(), data, toDouble);
1366 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1368 ScicosID adaptee = adaptor.getAdaptee()->id();
1370 if (v->getType() != types::InternalType::ScilabDouble)
1375 types::Double* current = v->getAs<types::Double>();
1376 if (current->getCols() != 0 && current->getCols() != 1)
1381 std::vector<int> nmode (current->getSize());
1382 for (int i = 0; i < current->getSize(); ++i)
1384 if (floor(current->get(i)) != current->get(i))
1388 nmode[i] = static_cast<int>(current->get(i));
1391 controller.setObjectProperty(adaptee, BLOCK, NMODE, nmode);
1399 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1401 ScicosID adaptee = adaptor.getAdaptee()->id();
1403 std::vector<std::string> equations;
1404 controller.getObjectProperty(adaptee, BLOCK, EQUATIONS, equations);
1406 if (equations.size() == 0)
1408 return new types::List();
1411 types::TList* o = new types::TList();
1413 // Header, starting with "modelica"
1414 types::String* header = new types::String(1, 5);
1415 header->set(0, modelica.c_str());
1416 header->set(1, model.c_str());
1417 header->set(2, inputs.c_str());
1418 header->set(3, outputs.c_str());
1419 header->set(4, parameters.c_str());
1423 if (equations[0].c_str() == std::string())
1425 o->set(1, types::Double::Empty());
1429 types::String* modelField = new types::String(1, 1);
1430 modelField->set(0, equations[0].c_str());
1431 o->set(1, modelField);
1435 std::istringstream inputsSizeStr (equations[1]);
1437 inputsSizeStr >> inputsSize;
1438 if (inputsSize == 0)
1440 types::Double* inputsField = types::Double::Empty();
1441 o->set(2, inputsField);
1445 types::String* inputsField = new types::String(inputsSize, 1);
1446 for (int i = 0; i < inputsSize; ++i)
1448 inputsField->set(i, equations[i + 2].c_str());
1450 o->set(2, inputsField);
1454 std::istringstream outputsSizeStr (equations[2 + inputsSize]);
1456 outputsSizeStr >> outputsSize;
1457 if (outputsSize == 0)
1459 types::Double* outputsField = types::Double::Empty();
1460 o->set(3, outputsField);
1464 types::String* outputsField = new types::String(outputsSize, 1);
1465 for (int i = 0; i < outputsSize; ++i)
1467 outputsField->set(i, equations[i + 3 + inputsSize].c_str());
1469 o->set(3, outputsField);
1473 types::List* parametersField = new types::List();
1475 // 'parameters' names
1476 std::istringstream parametersSizeStr (equations[3 + inputsSize + outputsSize]);
1478 parametersSizeStr >> parametersSize;
1479 if (parametersSize == 0)
1481 types::Double* parametersNames = types::Double::Empty();
1482 parametersField->set(0, parametersNames);
1486 types::String* parametersNames = new types::String(parametersSize, 1);
1487 for (int i = 0; i < parametersSize; ++i)
1489 parametersNames->set(i, equations[i + 4 + inputsSize + outputsSize].c_str());
1491 parametersField->set(0, parametersNames);
1494 // 'parameters' values
1495 types::List* parametersValues = new types::List();
1496 for (int i = 0; i < parametersSize; ++i)
1498 std::istringstream parametersValueStr (equations[i + 4 + inputsSize + outputsSize + parametersSize]);
1499 double parametersVal;
1500 parametersValueStr >> parametersVal;
1501 types::Double* parametersValue = new types::Double(parametersVal);
1502 parametersValues->set(i, parametersValue);
1504 parametersField->set(1, parametersValues);
1506 o->set(4, parametersField);
1511 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1513 ScicosID adaptee = adaptor.getAdaptee()->id();
1515 if (v->getType() == types::InternalType::ScilabList)
1517 types::List* current = v->getAs<types::List>();
1518 if (current->getSize() != 0)
1525 if (v->getType() != types::InternalType::ScilabTList)
1530 types::TList* current = v->getAs<types::TList>();
1533 types::String* header = current->getFieldNames();
1534 if (header->getSize() != 5)
1538 if (header->get(0) != modelica)
1542 if (header->get(1) != model)
1546 if (header->get(2) != inputs)
1550 if (header->get(3) != outputs)
1554 if (header->get(4) != parameters)
1559 char* c_str; // Temporary buffer used for conversions
1562 std::vector<std::string> equations;
1563 if (current->get(1)->getType() == types::InternalType::ScilabString)
1565 types::String* modelField = current->get(1)->getAs<types::String>();
1566 if (modelField->getSize() != 1)
1571 c_str = wide_string_to_UTF8(modelField->get(0));
1572 std::string modelFieldStored(c_str);
1574 equations.push_back(modelFieldStored);
1576 else if (current->get(1)->getType() == types::InternalType::ScilabDouble)
1578 types::Double* modelFieldDouble = current->get(1)->getAs<types::Double>();
1579 if (modelFieldDouble->getSize() != 0)
1584 // An empty matrix stores an empty string, which will later be translated back to an empty matrix
1585 equations.push_back(std::string());
1594 if (current->get(2)->getType() == types::InternalType::ScilabDouble)
1596 types::Double* inputsField = current->get(2)->getAs<types::Double>();
1597 if (inputsField->getSize() != 0)
1603 std::ostringstream strInputs;
1604 strInputs << inputsSize;
1605 std::string inputsSizeStr = strInputs.str();
1606 equations.push_back(inputsSizeStr); // When 'inputs'=[], just insert "0" in 'equations'
1610 if (current->get(2)->getType() != types::InternalType::ScilabString)
1615 types::String* inputsField = current->get(2)->getAs<types::String>();
1616 inputsSize = inputsField->getSize();
1617 equations.resize(equations.size() + 1 + inputsSize);
1618 std::ostringstream strInputs;
1619 strInputs << inputsSize;
1620 std::string inputsSizeStr = strInputs.str();
1621 equations[1] = inputsSizeStr; // Saving the size of the 'inputs' field'
1622 for (size_t i = 0; i < inputsSize; ++i)
1624 c_str = wide_string_to_UTF8(inputsField->get(static_cast<int>(i)));
1625 std::string inputsFieldStored(c_str);
1627 equations[i + 2] = inputsFieldStored;
1633 if (current->get(3)->getType() == types::InternalType::ScilabDouble)
1635 types::Double* outputsField = current->get(3)->getAs<types::Double>();
1636 if (outputsField->getSize() != 0)
1642 std::ostringstream strOutputs;
1643 strOutputs << outputsSize;
1644 std::string outputsSizeStr = strOutputs.str();
1645 equations.push_back(outputsSizeStr); // When 'outputs'=[], just insert "0" in 'equations'
1649 if (current->get(3)->getType() != types::InternalType::ScilabString)
1654 types::String* outputsField = current->get(3)->getAs<types::String>();
1655 outputsSize = outputsField->getSize();
1656 equations.resize(equations.size() + 1 + outputsSize);
1657 std::ostringstream strOutputs;
1658 strOutputs << outputsSize;
1659 std::string outputsSizeStr = strOutputs.str();
1660 equations[2 + inputsSize] = outputsSizeStr; // Saving the size of the 'outputs' field'
1661 for (size_t i = 0; i < outputsSize; ++i)
1663 c_str = wide_string_to_UTF8(outputsField->get(static_cast<int>(i)));
1664 std::string outputsFieldStored(c_str);
1666 equations[i + 3 + inputsSize] = outputsFieldStored;
1671 int parametersIndex = 4;
1672 if (current->get(parametersIndex)->getType() == types::InternalType::ScilabDouble)
1674 // For backward compatibility sake, allow the presence of an empty matrix here
1675 types::Double* emptyMatrix = current->get(parametersIndex)->getAs<types::Double>();
1676 if (emptyMatrix->getSize() != 0)
1684 if (current->get(parametersIndex)->getType() != types::InternalType::ScilabList)
1689 types::List* list = current->get(parametersIndex)->getAs<types::List>();
1690 if (list->getSize() < 1)
1695 // 'parameters' names
1696 size_t parametersSize;
1697 if (list->get(0)->getType() == types::InternalType::ScilabDouble)
1699 types::Double* parametersNames = list->get(0)->getAs<types::Double>();
1700 if (parametersNames->getSize() != 0)
1705 // When 'parameters(1)'=[], just insert "0" in 'equations', set in the model and return
1707 std::ostringstream strParameters;
1708 strParameters << parametersSize;
1709 std::string parametersSizeStr = strParameters.str();
1710 equations.push_back(parametersSizeStr);
1712 controller.setObjectProperty(adaptee, BLOCK, EQUATIONS, equations);
1717 if (list->get(0)->getType() != types::InternalType::ScilabString)
1722 types::String* parametersNames = list->get(0)->getAs<types::String>();
1723 parametersSize = parametersNames->getSize();
1724 equations.resize(equations.size() + 1 + parametersSize);
1725 std::ostringstream strParameters;
1726 strParameters << parametersSize;
1727 std::string parametersSizeStr = strParameters.str();
1728 equations[3 + inputsSize + outputsSize] = parametersSizeStr; // Saving the size of the 'parameters' field'
1729 for (size_t i = 0; i < parametersSize; ++i)
1731 c_str = wide_string_to_UTF8(parametersNames->get(static_cast<int>(i)));
1732 std::string parametersName(c_str);
1734 equations[i + 4 + inputsSize + outputsSize] = parametersName;
1738 // 'parameters' values
1739 if (list->get(1)->getType() == types::InternalType::ScilabDouble)
1741 types::Double* parameterVal = list->get(1)->getAs<types::Double>();
1742 if (parameterVal->getSize() != static_cast<int>(parametersSize))
1747 for (size_t i = 0; i < parametersSize; ++i)
1749 std::ostringstream strParameterVal;
1750 strParameterVal << parameterVal->get(static_cast<int>(i));
1751 std::string parameterValStr = strParameterVal.str();
1752 equations.push_back(parameterValStr);
1757 if (list->get(1)->getType() != types::InternalType::ScilabList)
1762 types::List* list2 = list->get(1)->getAs<types::List>();
1763 if (list2->getSize() != static_cast<int>(parametersSize))
1768 equations.resize(equations.size() + parametersSize);
1769 for (size_t i = 0; i < parametersSize; ++i)
1771 if (list2->get(static_cast<int>(i))->getType() != types::InternalType::ScilabDouble)
1776 types::Double* parametersVal = list2->get(static_cast<int>(i))->getAs<types::Double>();
1777 if (parametersVal->getSize() != 1)
1782 std::ostringstream strParametersVal;
1783 strParametersVal << parametersVal->get(0);
1784 std::string parametersValStr = strParametersVal.str();
1785 equations[i + 4 + inputsSize + outputsSize + parametersSize] = parametersValStr;
1789 controller.setObjectProperty(adaptee, BLOCK, EQUATIONS, equations);
1797 static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
1799 ScicosID adaptee = adaptor.getAdaptee()->id();
1802 controller.getObjectProperty(adaptee, BLOCK, UID, uid);
1804 types::String* o = new types::String(1, 1);
1805 o->set(0, uid.data());
1810 static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
1812 if (v->getType() != types::InternalType::ScilabString)
1817 types::String* current = v->getAs<types::String>();
1818 if (current->getSize() != 1)
1823 ScicosID adaptee = adaptor.getAdaptee()->id();
1825 char* c_str = wide_string_to_UTF8(current->get(0));
1826 std::string uid(c_str);
1829 controller.setObjectProperty(adaptee, BLOCK, UID, uid);
1836 template<> property<ModelAdapter>::props_t property<ModelAdapter>::fields = property<ModelAdapter>::props_t();
1838 ModelAdapter::ModelAdapter(std::shared_ptr<model::Block> adaptee) :
1839 BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(adaptee),
1840 diagramAdapter(nullptr)
1842 if (property<ModelAdapter>::properties_have_not_been_set())
1844 property<ModelAdapter>::fields.reserve(23);
1845 property<ModelAdapter>::add_property(L"sim", &sim::get, &sim::set);
1846 property<ModelAdapter>::add_property(L"in", &in::get, &in::set);
1847 property<ModelAdapter>::add_property(L"in2", &in2::get, &in2::set);
1848 property<ModelAdapter>::add_property(L"intyp", &intyp::get, &intyp::set);
1849 property<ModelAdapter>::add_property(L"out", &out::get, &out::set);
1850 property<ModelAdapter>::add_property(L"out2", &out2::get, &out2::set);
1851 property<ModelAdapter>::add_property(L"outtyp", &outtyp::get, &outtyp::set);
1852 property<ModelAdapter>::add_property(L"evtin", &evtin::get, &evtin::set);
1853 property<ModelAdapter>::add_property(L"evtout", &evtout::get, &evtout::set);
1854 property<ModelAdapter>::add_property(L"state", &state::get, &state::set);
1855 property<ModelAdapter>::add_property(L"dstate", &dstate::get, &dstate::set);
1856 property<ModelAdapter>::add_property(L"odstate", &odstate::get, &odstate::set);
1857 property<ModelAdapter>::add_property(L"rpar", &rpar::get, &rpar::set);
1858 property<ModelAdapter>::add_property(L"ipar", &ipar::get, &ipar::set);
1859 property<ModelAdapter>::add_property(L"opar", &opar::get, &opar::set);
1860 property<ModelAdapter>::add_property(L"blocktype", &blocktype::get, &blocktype::set);
1861 property<ModelAdapter>::add_property(L"firing", &firing::get, &firing::set);
1862 property<ModelAdapter>::add_property(L"dep_ut", &dep_ut::get, &dep_ut::set);
1863 property<ModelAdapter>::add_property(L"label", &label::get, &label::set);
1864 property<ModelAdapter>::add_property(L"nzcross", &nzcross::get, &nzcross::set);
1865 property<ModelAdapter>::add_property(L"nmode", &nmode::get, &nmode::set);
1866 property<ModelAdapter>::add_property(L"equations", &equations::get, &equations::set);
1867 property<ModelAdapter>::add_property(L"uid", &uid::get, &uid::set);
1871 ModelAdapter::ModelAdapter(const ModelAdapter& adapter) :
1872 BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(adapter),
1873 diagramAdapter(adapter.getDiagram())
1877 ModelAdapter::~ModelAdapter()
1879 if (diagramAdapter != nullptr)
1881 diagramAdapter->DecreaseRef();
1882 diagramAdapter->killMe();
1886 std::wstring ModelAdapter::getTypeStr()
1888 return getSharedTypeStr();
1891 std::wstring ModelAdapter::getShortTypeStr()
1893 return getSharedTypeStr();
1896 DiagramAdapter* ModelAdapter::getDiagram() const
1898 if (diagramAdapter != nullptr)
1900 diagramAdapter->IncreaseRef();
1902 return diagramAdapter;
1905 void ModelAdapter::setDiagram(DiagramAdapter* newDiagram)
1907 if (newDiagram != nullptr)
1909 // The old 'diagramAdapter' needs to be freed after setting it to 'v'
1910 DiagramAdapter* temp = diagramAdapter;
1912 newDiagram->IncreaseRef();
1913 diagramAdapter = newDiagram;
1915 if (temp != nullptr)
1917 temp->DecreaseRef();
1923 } /* namespace view_scilab */
1924 } /* namespace org_scilab_modules_scicos */