Search code examples
c++vectorautosar

Is it possible to breakdown a c++ statement as shown?


The statement is taken from a source file that is developed using vector autosar adaptive davinci APIs/functions etc.

adaptive::communication::ServiceHandle find_a_service = services::nameofservice::proxy::serviceproxy::StartSearchService(
    [this](const adaptive::communication::ContainerServiceHandles<services::nameofservice::proxy::serviceproxy::HandleTypes>& handles) 
    { 
        SearchServiceHandler(handles);
    }, adaptive::core::InstanceSpec{ kInstance });

I have renamed/obscured names but kept the format intact in the source code. Can anyone help in breaking down this complex statement to several simple ones?

Thanks.


Solution

  • While aliasing every name I can, this is rather simple statement, formatted for readability:

    ServiceHandler find_a_service = 
                StartSearchService ( [this](const HandleContainer<Htypes>& handles ) {
                        SearchServiceHandler(handles);
                }, 
                InstanceSpec{kInstance} );
    

    It declares some variable find_a_service, intialized by some expression. It's unknown that StartSearchService is a type or a function, or a name of callable object.

    First argument of StartSearchService argument is a lambda expression, which captures this, takes a const reference to HandleContainer<Htypes> as a parameter, and calls some callable with it as argument.

    Second argument of StartSearchService is a temporal value, resulting from an expression of type InstanceSpec, initialized by list {kInstance}, whatever these are.

    Now you can carefully create aliases for each name. You can create type alisases or you can use using declaration.Carefully because you do not want conflicts.

    //type alias
    using ServiceHandler  = adaptive::communication::ServiceHandle;
    using HandleContainer = adaptive::communication::ContainerServiceHandles;
    
    // etc.
    

    Using declarion would cut fully qualified name to short one:

    // in function scope:
    using adaptive::communication::ContainerServiceHandles;
    // you can write ContainerServiceHandles now, but any samely named entity is ambigous now.
    

    Finally, fundamental using-namespace (valid ONLY if communication is a namespace):

    using namespace adaptive::communication;
    // all names in namespace adaptive::communication 
    // are now valid in local. Ambiguity is possible.
    

    The approach isn't without flaws. It may not be even possible to use in such form in template code or with forward declarations. Koenig's name lookup and necessity of namespace to be declared before arise their ugly heads.

    You may write lambda separate in most cases, but its ill-advised, because you would create a copy of that and original would live until end of scope, which would a) harm optimization b) can have side-effects, everything about those entities is unknown.

    auto searchPredicate = 
            [this](const HandleContainer<Htypes>& handles ) {
                SearchServiceHandler(handles);
            };