Giter VIP home page Giter VIP logo

Comments (8)

timyhac avatar timyhac commented on June 17, 2024

@jkoplo - I'd be keen to get your view on this.

from libplctag.net.

jkoplo avatar jkoplo commented on June 17, 2024

from libplctag.net.

timyhac avatar timyhac commented on June 17, 2024

What about something like this:

The consumer provides the preferred C# they want to use, and they provide the method by which the tag is encoded/decoded.

public class Tag<T, U> where T : IDataType<U>, new()
{

    T dataType = new T();

    Tag tag;

    public void Read(TimeSpan timeout)
    {
        tag.Read(timeout);
        Value = dataType.Decode(tag);
    }

    public void Write(TimeSpan timeout)
    {
        dataType.Encode(tag, Value);
        tag.Write(timeout);
    }

    public U Value { get; set; }
}
public interface IDataType<T>
{
    int ElementSize { get; }
    int CipCode { get; }
    void Encode(Tag tag, T t);
    T Decode(Tag tag);
}

Our library could implement the DataType for the basic types but if they wanted to extend this (PID tags, other UDTs, etc) they could just implement IDataType.

This is probably better than the abstract classes I did last year for my own projects in my opinion. The main thing missing is array indexers.

from libplctag.net.

jkoplo avatar jkoplo commented on June 17, 2024

That's really nice and just about what I had in mind. I particularly like the interface. You kinda did the work already, but I'm hoping I'll have time to integrate this tonight.

from libplctag.net.

timyhac avatar timyhac commented on June 17, 2024

Cool, nice - I haven't compiled/tried it but seems roughly right. Do you know if there is a way to make classes be arrays in a generic way:
i.e. a single class can have 0, 1, 2 or 3 dimensions?

from libplctag.net.

kyle-github avatar kyle-github commented on June 17, 2024

Nice!

Perhaps you could call something like your DataType.Decode(), but it would take two arguments: the tag and an offset. The offset would be the byte offset to that element of the array in the tag's data. After Read you would calculate the element offsets, and then call Decode() on one element at a time?

In C++ this is painful. Easier to have a class that overloads the array operator and pretends to be an array than to use the normal notation for array declarations. I think that there is some template magic to make this a bit more palatable.

Minor nit: in your example code, if the timeout is too short or zero, Read() will return before there is any data and perhaps throw an exception.

from libplctag.net.

timyhac avatar timyhac commented on June 17, 2024

@kyle-github I think we're thinking along the same lines: maybe we should only support 0D and 1D access because that is what libplctag does. I know that AB have 2d and 3d arrays, but not sure about other systems.
If users want to inherit this class and provide 2d or 3d indexers and different constructors (with array sizes) they could do so. I don't know how this would work in C++but in C# it is fairly straightforward to provide 2D indexers.

So the base type would provide access via a Value property, and 1d access via a 1D indexer.

What do you think?

public class Tag<T, U> where T : IDataType<U>, new()
{

    // .....

    public U Value { get; set; } // check that element_count == 1 and throw error if not
    public U this[int index] { get; set; } // Of course do checks on index bounds
}

Hmmmm... it doesn't feel right, maybe only the indexer? hmmmm...

from libplctag.net.

timyhac avatar timyhac commented on June 17, 2024

This is long done

from libplctag.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.