[Suggestion] Unique ID for dpConnect and other variants

Discussion about recent product features & solutions!
8 posts • Page 1 of 1
ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

[Suggestion] Unique ID for dpConnect and other variants

Post by ozangor »

Hi all,

Currently, it is a must to use the dpConnect and dpDisconnect in the same script. Even if you have a global object that connects to datapoints and needs to disconnect, you have to call the methods in the same script.

Consider having an instance of a class in ScopeLib, and you have two different buttons on the same panel which calls dpConnect and dpDisconnect.

solution: It would be great to have a unique identifier for a dpConnect so that it can be used anywhere to do a dpDisconnect.


Simplest example:

ScopeLib:
int id;

button 1 clicked:
id = dpConnect("work", "dp1.");

button 2 clicked.
dpDisconnect(id);

This results in a problem that you need to have it stored somewhere, but I still think it would be more comfortable to use. In addition, I think that it is even more important to have with the CTRL++

I don't know of any real solution (rather than a workaround) for this problem. If there is already a real solution please inform me :)

RudiKreiner
Posts:198
Joined: Mon May 16, 2011 2:10 pm

Re: [Suggestion] Unique ID for dpConnect and other variants

Post by RudiKreiner »

Have you had a look at dpConnectUserData().
I think you can solve your problem with that.

ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

Re: [Suggestion] Unique ID for dpConnect and other variants

Post by ozangor »

You still need to be in the same script if you need to disconnect from dpconnectuserdata, unfortunately.

kilianvp
Posts:443
Joined: Fri Jan 16, 2015 10:29 am

Re: [Suggestion] Unique ID for dpConnect and other variants

Post by kilianvp »

A ID would be cool. Right now we can't do something like

Code: Select all

if (isConnected(ID))
{
  // do something
}
or:

Code: Select all

if (isConnectedWithDp(DP))
{
  // do something
}
I needed that in the past so many times

ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

Re: [Suggestion] Unique ID for dpConnect and other variants

Post by ozangor »

With CTRL++ you'll probably need it even more.

Think of associating a dptype with a class. Two separate methods could connect and disconnect to/from elements. This is not possible at the moment. An ID will give more flexibility to regular CTRL scripts. However, giving the possibility to handle this in an object would be sufficient for CTRL++ as well.

Another problem is that you cannot access object members from the callback function. Because the callback function is considered as it was called from the UI element and not in the scope of the object anymore.

You are able to pass any user data with dpConnectUserData but if the member changes when the dp is already connected, callback cannot get the current value (even if you pass a shared_ptr, or maybe I could not manage to make it work). It feels like it creates a copy object of the user data in the callback even if it is a pointer.


So suggestion number two would be:

If the callback function is the member of the class, it should be able to reach its other members, or passing the complete object pointer to the dpConnectUserData should be possible.

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: [Suggestion] Unique ID for dpConnect and other variants

Post by mkoller »

With our next version 3.16 you will be able to connect to class member functions and the started callback function then has the this pointer to its own instance.

Regarding your shared_ptr issue: See the following script how to do it:

Code: Select all

struct Test
{
  string print() { DebugN("object in Test::print", this, value); }
  int value;
};

main()
{
  shared_ptr t = new Test;
  DebugN("object in main", t, t.value);
  dpConnectUserData("callback", t, "ExampleDP_Arg1.");
  delay(2);
  t.value = 123;
}

void callback(shared_ptr ptr, string dpe, float value)
{
  DebugN("object in callback", ptr, ptr.value);
  ptr.print();
}


ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

Re: [Suggestion] Unique ID for dpConnect and other variants

Post by ozangor »

Wow, actually I remember trying exactly the same example, which did not work for me. Good to see that it works.

ozangor
Posts:44
Joined: Thu Sep 22, 2011 2:57 pm

Re: [Suggestion] Unique ID for dpConnect and other variants

Post by ozangor »

With the help of DpConnectHelper class, it is possible to pass complete object to the dpconnect. There may be some side effects for this. However, I think this can be used as a workaround until we get the feature in 3.16.

Code: Select all

//Interface for classes that do dpconnect
class Connectable
{
  public int doConnect(string callback, shared_ptr thijs, dyn_string dpList)
  {
  }
  
  public int doDisconnect(string callback, shared_ptr thijs, dyn_string dp)
  {
  }

};

class ConnectTest : Connectable
{
  public string str;
  public int doConnect(string callback, shared_ptr thijs, dyn_string dp)
  {
    return dpConnectUserData(callback, thijs, dp);
  }
  
  public int doDisconnect(string callback, shared_ptr thijs, dyn_string dp)
  {
    return dpDisconnectUserData(callback, thijs, dp);
  }
};



struct DpConnectHelper
{ 
  static doConnect(string callback, shared_ptr obj, dyn_string dpList)
  {
    obj.doConnect(callback, obj, dpList);
  }
  static doDisconnect(string callback, shared_ptr obj, dyn_string dpList)
  {
    obj.doDisconnect(callback, obj, dpList);
  }
};

8 posts • Page 1 of 1