Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SofaDefaultType] Extend TypeInfo to handle containers that are not flat array. #3851

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

damienmarchal
Copy link
Contributor

Quick test on what could be a direction to fix issue: sofa-framework/SofaPython3#346

I'm now wonder:

  • how to generalize that find more comprehensible names for all that.
  • is there room for small refactoring in TypeInfo related to the exisitng containers.

If some of you have suggestion, it is more than welcome.


By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

The underlying idea is to extend the abstract API of the sofa type info system to implement
the proper behavior for specific datastructures.

TODO: find better name and more systematic approche for this.

Signed-off-by: Damien Marchal <damien.marchal@univ-lille1.fr>
@damienmarchal damienmarchal added the pr: status wip Development in the pull-request is still in progress label May 11, 2023
@damienmarchal
Copy link
Contributor Author

@alxbilger, @fredroy, @hugtalbot I welcome some nameing & refactoring suggestion here.

Copy link
Contributor

@alxbilger alxbilger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the code is still obscure to me. I need to better understand for a better review.

@@ -97,34 +96,27 @@ struct DefaultDataTypeInfo
enum { Text = 0 /**< 1 if this type uses text values*/ };
enum { CopyOnWrite = 0 /**< 1 if this type uses copy-on-write. The memory is shared with its source Data while only the source is changing (and the source modifications are then visible in the current Data). As soon as modifications are applied to the current Data, it will allocate its own value, and no longer shares memory with the source.*/ };
enum { Container = 0 /**< 1 if this type is a container*/ };
enum { UniqueKeyContainer = 0 /**< 1 if this type is a container*/ };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the right comment

@damienmarchal
Copy link
Contributor Author

damienmarchal commented May 12, 2023

@alxbilger

Thanks for your relevant question, actually this is why the PR is in wip.

So more details.
Fundamentally the TypeInfo system is a strange beast compose of two things. One is a type-traits API designed to be used in compile time code, one is dynamic interface API. AbstractTypeInfo is the dynamic interface (with the virtual function). The AbstractTypeInfoDynamicWrapper is a façade to "wrap" the compile time TypeInfo type-traits into the virtual one.

The TypeInfo type-traits API has several consistancy issues (probably dating back to pre-c++17 area) mixing a lot of different behavior into a single dynamic API... making things very fuzzy.

In the current status of the PR I just wanted to draft something to have a gimplse of the look and feel of a more modern code architecture (without breaking everything).

Regarding your question about insertion in a vector... the problem is that insert is not a push_back... it means inserting "somewhere" which could of course be implemented on vector. But it would requires a serious work on the AbstractTypeInfo API to make all that consistent.

  set map vector fixed_array
clear() X X X  
insert(A) X      
insert(std::pair<key, B>)   X Y  
size() X X X X
resize()     X  
empty() X X X X

So in short:
! IsFixedSizeContainer => clear(), resize()
isContainer => empty(), size()
isSet => insert(A) and !insert(std::pair<>)

I drafted the general implementation, to illustrate what the current TypeInfo is really doing and how to refactor it: https://godbolt.org/z/srsq3ExPq
extract:

template<class Info>
class AbstractTypeInfoDynamicWrapper : public BaseAbstractTypeInfo
{
public:
    bool size() override 
    { 
        if constexpr(
            Info::IsContainer && 
            Info::Properties::IsContainer)
            return Info::clear();
        throw std::runtime_error("Invalid operation");
    }

    bool empty() override 
    { 
        if constexpr(
            Info::IsContainer && 
            Info::Properties::IsContainer)
            return Info::empty();
        throw std::runtime_error("Invalid operation");
    }

    void setValueInteger(int index, int value) override 
    { 
        if constexpr(
            !Info::Properties::IsSetContainer && 
            Info::Properties::Type == DataType::Integer)
                return Info::setAt(index, value);
        throw std::runtime_error("Invalid operation");
    }
    /// ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: status wip Development in the pull-request is still in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants