[Smtk-developers] Proposed Change to Attribute and Attribute Group Item Classes

David Thompson david.thompson at kitware.com
Tue Jul 21 08:48:24 EDT 2015


Hi Bob,

> Some time ago a set of findXXX methods were add to the Attribute class in order to make accessing items easier.  For example findDouble(“a") would return a DoubleItemPtr whose name is a.  There is also a templated method called findAs which does pretty much the same thing findAs<DoubleItem>(“a”)

The idea is that the templated methods are for use in other templated code while the non-templated methods are more terse and thus easier to type and to read.

The non-templated methods can also be used in debuggers without issue, while the templated methods cannot always be invoked (at least on OS X, it depends on whether the templated method is directly invoked somewhere in the same execution unit as the stack frame you are debugging when you try to call the method).

> When this was done, the developer failed to notice that GroupItems also behave like Attributes in that they also contain Items and as a result it only has the original find() method.

Oops. Sorry.

> I figured the reason that we have the explicit findXXX methods was to provide Python wrappings since the original findAs method could not be wrapped directly.  However, I discovered that there are Python  findAs methods being generated explicitly in the typesystem.xml (findAsDouble for example).

At the time I wrote findDouble, I had not yet figured out how to add methods to typesystem.xml that would return items of the proper type.

> As a result we have findXXX and findAs style methods being used in both C++ and Python code.  I would like there to be only one style for each.
> 
> What I propose is that the findXXX methods be removed from the Attribute class and just leave the findAs template (and add it to GroupItem).  Also to add the findAs methods in the GroupItem Python wrapping.
> 
> Any comments?

I would prefer to fix GroupItem but keep the findXXX methods as convenience methods; I've really found the shorter, untemplated methods useful.

The findAsXXX() methods in typesystem.xml are defined on the Operator class. I am unhappy with the API consistency there, but there does not seem to be a simple fix. One problem with injecting methods via typesystem.xml is that subclasses do not inherit them; for example, if we add a Python findAsFoo method to class A then Python objects of some type B that inherits A will not have a findAsFoo method. To call findAsFoo, one must ask Python for the same object cast as type A.

    class A {
      Foo* findAsFoo(const std::string&) const;
    };
    class B : public A {
    };

    <object-type name="A">
      <add-function signature="findAsFoo(const std::string&) const" .../>
    </object-type>
    <object-type name="B"/>

    import smtk
    instanceA = smtk.A()
    instanceB = smtk.B()
    instanceA.findAsFoo("x") # this will work
    instanceB.findAsFoo("x") # this will not work
    smtk.A(instanceB).findAsFoo("x") # will work if smtk.A(instanceB) exists

This means that injecting findAsXXX() methods would involve a lot of copied code in typesystem for IntItem, DoubleItem, StringItem, GroupItem, Operator (which inherits Attribute), and possibly others.

	David


More information about the Smtk-developers mailing list