Topic
  • 8 replies
  • Latest Post - ‏2012-04-18T01:36:48Z by mendell
BruceGlassford
BruceGlassford
71 Posts

Pinned topic Generic C++ operators and recursive types

‏2012-04-15T23:31:51Z |
Got a port with the following type: <rstring f1, list<tuple<rstring f2, rstring f3>f4 >>

I can get to the attributes by using $model->getOutputPortAt(0)->getAttributes()

That gives me f1, f4 neatly.

I can get the SPL type for f4 using SPL::CodeGen::Type::getElementType($splType); where $splType is the getSPLType() for the attribute being iterated.

How do I turn that attribute back to a tuple/port so I can do the same decomposition for f4?
  • mendell
    mendell
    219 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-16T00:50:43Z  
    I am not sure what you are trying to do. The whole tuple arrives on the port as one 'object'. You can access this in C++
    as:
    
    
    
    const IPort0Type& t = (
    
    const IPort0Type&) tuple;   
    // Assumes tuple is on port 0, and tuple name is 'tuple' in prototype 
    
    const IPort0Type::f4_type& listOfTuples = t.get_f4(); 
    // Do something with listOfTuples 
    
    const IPort0Type::f4_type::value_type& first_value& = listOfTuples[0]; 
    
    const IPort0Type::f4_type::value_type::f2_type& 
    /* rstring */ = first_value.get_f2(); 
    // Note that typedefes help here :-)
    


    If you have an OutputAttribute, you can use $attr->getCppType() to get the type of the attribute. There isn't too much support for going deeper into the type, but you can use type::value_type to get to the element type of a list (comes from std::vector). There is also key_type for sets, key_type and value_type for maps, etc.

    Mark
  • BruceGlassford
    BruceGlassford
    71 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-16T01:03:27Z  
    • mendell
    • ‏2012-04-16T00:50:43Z
    I am not sure what you are trying to do. The whole tuple arrives on the port as one 'object'. You can access this in C++
    as:
    <pre class="jive-pre"> const IPort0Type& t = ( const IPort0Type&) tuple; // Assumes tuple is on port 0, and tuple name is 'tuple' in prototype const IPort0Type::f4_type& listOfTuples = t.get_f4(); // Do something with listOfTuples const IPort0Type::f4_type::value_type& first_value& = listOfTuples[0]; const IPort0Type::f4_type::value_type::f2_type& /* rstring */ = first_value.get_f2(); // Note that typedefes help here :-) </pre>

    If you have an OutputAttribute, you can use $attr->getCppType() to get the type of the attribute. There isn't too much support for going deeper into the type, but you can use type::value_type to get to the element type of a list (comes from std::vector). There is also key_type for sets, key_type and value_type for maps, etc.

    Mark
    What I'm trying to do is parse data from the input tuple - basically a JSON string in one of the input tuple attributes. If there is a corresponding output port attribute to one of the JSON tags, populate it. I've got it working for scalars, but can't figure out how to get one level deeper in the perl preprocessor to handle tuples (or lists of tuples) to determine the attribute names and process them.

    I can get the element types for lists if they're scalars - the nested tuples are what's thwarting me at present.

    Doing it from C seems to be a problem - I'd have to know the attribute names in advance from what I can figure out so far, so have to go to the Perl to get them, but I can't get to the attribute names within a tuple inside of a list inside of a tuple....

    ... Bruce
  • mendell
    mendell
    219 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-16T02:01:06Z  
    What I'm trying to do is parse data from the input tuple - basically a JSON string in one of the input tuple attributes. If there is a corresponding output port attribute to one of the JSON tags, populate it. I've got it working for scalars, but can't figure out how to get one level deeper in the perl preprocessor to handle tuples (or lists of tuples) to determine the attribute names and process them.

    I can get the element types for lists if they're scalars - the nested tuples are what's thwarting me at present.

    Doing it from C seems to be a problem - I'd have to know the attribute names in advance from what I can figure out so far, so have to go to the Perl to get them, but I can't get to the attribute names within a tuple inside of a list inside of a tuple....

    ... Bruce
    From the SPL::CodeGen::Type documentation:
    
    # This is the SPL Operator Code Generation Type API.  This API is used to extract information about # SPL types.  These types are representations of the SPL types, and are often extracted using # SPL::Operator::Instance::Expression::getSPLType() from expressions and parameters.  The SPL type # is the same as written in SPL, with whitespace removed, except between an attribute
    's type and associated # name. # For example, the SPL type 
    
    for the output port of 
    
    this operator: # stream<rstring a, int32 b> A = Beacon() 
    {
    } # is the string: # 
    "tuple<rstring a,int32 b>" # # The functions in 
    
    this 
    
    class can be used to test the kind of the type, and pull apart types into # their component parts. # # For the above example, SPL::CodeGen::Type::isTuple($type) will 
    
    return 1. # SPL::CodeGen::Type::getAttributeTypes($type will 
    
    return [
    "rstring", 
    "int32"] # SPL::CodeGen::Type::getAttributeNames($type) will 
    
    return [
    "a", 
    "b"]
    

    You can pull apart f4's SPL type into the attribute names and SPL types. That should give you enough information to make this work
  • BruceGlassford
    BruceGlassford
    71 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-16T17:58:05Z  
    Gives me enough to work with. Will post some of the logic when I get finished rewriting everything from scratch later.
  • BruceGlassford
    BruceGlassford
    71 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-17T21:28:31Z  
    Gives me enough to work with. Will post some of the logic when I get finished rewriting everything from scratch later.
    Nope, didn't work - I can get the structure without issue, but initializing a new inner tuple appears to be impossible. I'd need some way to get the type name of the inner tuple from what I can see. Attacking it from a different angle for now - will revisit it when I get some time - going to use output functions to get the inner data instead of just being able to use the structure of the output tuple.

    Which means that the flip side function (taking a tuple and building the string representation) is also going to be interesting for arbitrary tuple layouts.
  • BruceGlassford
    BruceGlassford
    71 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-17T21:32:44Z  
    Nope, didn't work - I can get the structure without issue, but initializing a new inner tuple appears to be impossible. I'd need some way to get the type name of the inner tuple from what I can see. Attacking it from a different angle for now - will revisit it when I get some time - going to use output functions to get the inner data instead of just being able to use the structure of the output tuple.

    Which means that the flip side function (taking a tuple and building the string representation) is also going to be interesting for arbitrary tuple layouts.
    Just re-read the first one - I missed seeing the IPort0Type::f4_type - that is EXACTLY what I need, I think. Thanks again, Mark - going to go off and read that very carefully and see if I can make it work. Where would I look to see the documentation on what gets built in the classes for a tuple? (i.e. where I would see the f4_type etc)?
  • mendell
    mendell
    219 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-18T01:33:08Z  
    Just re-read the first one - I missed seeing the IPort0Type::f4_type - that is EXACTLY what I need, I think. Thanks again, Mark - going to go off and read that very carefully and see if I can make it work. Where would I look to see the documentation on what gets built in the classes for a tuple? (i.e. where I would see the f4_type etc)?
    See the C++ section in Toolkit Developer Reference for details. There is even an example with nested tuples.

    Mark
  • mendell
    mendell
    219 Posts

    Re: Generic C++ operators and recursive types

    ‏2012-04-18T01:36:48Z  
    • mendell
    • ‏2012-04-18T01:33:08Z
    See the C++ section in Toolkit Developer Reference for details. There is even an example with nested tuples.

    Mark
    If you are really interested in the guts of *tuple*s, you can look at output/src/type/WEIRD_NAME.{h,cpp} for the right tuple type. Note that information in that file is subject to change, and only the documented API interface is guarenteed to remain the same.

    Mark