﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
4678	Allow passing external objects as parameters to a class	Bernhard Thiele	somebody	"There are cases there passing external objects as parameters to a class is very handy. An example is in the Modelica_DeviceDrivers library where an external object holding the handle to a Comedi-device is used as a parameter for ADC and DAC device blocks. The MLS 3.4 doesn't allow this construction, but it is supported by some other tools and I think the MLS 3.4 is overly restrictive in this respect.

OpenModelica seems to almost support it. The two examples below show the principle.

1. `Test_EO` creates an external object named `eo` and uses it directly. This (standard conform) model '''works'''.
2. `Test_BlockWithEOParameter` is almost the same except that the logic is outsourced in a block which is instantiated and the block receives the external object as an argument. This (non standard conform) model '''doesn't work'''.

OpenModelica translates example 2. to C code without error, but during C code compilation one gets the error:

{{{
EOsAsParameter.Test_BlockWithEOParameter.c:90:48: error: use of undeclared identifier '$PblockEO$Peo'
    omc_EOsAsParameter_doSthWithEO(threadData, $PblockEO$Peo)
}}}

`blockEO.eo` is just an alias for `eo`. It seems that not much is missing to have it working and I think it would be a fine enhancement if one could actually make it work.

{{{#!modelica
package EOsAsParameter ""Using external objects as parameter""

  model Test_EO
    EO eo=EO(""handlepath"");
  equation
    when sample(0,0.2) then
      doSthWithEO(eo);
    end when;
  end Test_EO;

  model Test_BlockWithEOParameter
    EO eo=EO(""handlepath"");
    BlockWithEOParameter blockEO(eo=eo);
  end Test_BlockWithEOParameter;

  block BlockWithEOParameter ""Block with ExternalObject as parameter""
    parameter EO eo;
  equation
    when sample(0,0.2) then
      doSthWithEO(eo);
    end when;
  end BlockWithEOParameter;

  class EO ""External object""
  extends ExternalObject;

    function constructor ""Open device""
      input String devicename = ""/dev/comedi0"" ""Device name"";
      output EO eo ""Handle to external object"";
      external ""C"" eo = my_eo_create(devicename)
      annotation (Include=""
        void* my_eo_create(const char* devicename) {
          int dummy = 0;

          return (void*)&dummy;
        }
      "");
    end constructor;

    function destructor ""Close device""
      input EO eo ""Handle to external object"";
      external ""C"" my_eo_free(eo)
      annotation (Include=""void my_eo_free(void* eo) {}"");
    end destructor;
  end EO;

  function doSthWithEO
    input EO eo;
    external ""C"" doSthWithEO(eo)
    annotation (Include=""
      #include <ModelicaUtilities.h>
      void doSthWithEO(void* eo) {
        ModelicaFormatMessage(\""Called doSthWithEO\\n\"");
      }
    "");

  end doSthWithEO;
end EOsAsParameter;
}}}"	enhancement	new	high	Future	*unknown*	v1.13.0-dev-nightly			Martin Sjölund Volker Waurich
