Menu

Templates support

Help
2015-04-24
2015-05-02
  • Semenyakin Vladimir

    Hi. I'm trying to use CppHeaderParser for automated convertion of C++ libraries interfaces to deployment representation without private methods, fields, e.t.c.

    All was good, until I added template-dependened contexts (class and methods) to parse list. I tried to find "template" word in documentation - there was no such word. I found "template" several times in source, but "not implemented" mentioned near found places... So, templates currently not supported?

     
  • Jashua Cloutier

    Jashua Cloutier - 2015-04-24

    Please include a sample .h file and tell me what data you are after programmatically. I will need this to answer your question.

    You can also look at the regression tests in the repo to get an idea.

    See the following example header file CppHeaderParser/test/TestSampleClass.h

    Also, see the following example code that looks for different items in it
    CppHeaderParser/test/test_CppHeaderParser.py

     
  • Semenyakin Vladimir

    Thank you for fast answer!

    I attached python covertion code, input and output headers. There are inner classes in output, but, I think, it's not big problem. The problem is that I can't find API to get template argumetns of template classes and methods.

    In test cases I found test "class Lizzard_TestCase(unittest.TestCase):", where template key is used. But it's just flag there, not some structure, where detail template data is stored.

    P.S.: Sorry for my English.
    P.P.S.: Sorry for my Python. It's not my native programing language

     
  • Jashua Cloutier

    Jashua Cloutier - 2015-04-27

    First, if I understand correctly, your goal is to publish a .h file that has everything private stripped. If this is the case, remember that if you have a compiled library using the private .h representation of the class, and someone else compiles code with the public version of the .h file an links against your compiled code, the program will have 2 different understandings of the size of your object and things could end badly.

    Looking at your code, I still dont understand exactly what you are having trouble with finding. Are you having trouble accessing DVirtualDeviceRAM::HeapStorage? Please specify what data you want to get pragmatically and how then I can help further

     
  • Semenyakin Vladimir

    Yeah, thanks for your focusing on static libraries linkage issues, I found this article with pimpl idiom description, that may help to fix such problems.

    About API... Let's I show on example:


    template<typename T_ObjectType="">
    HeapStorage<T_ObjectType> createStorage() {
    return HeapStorage<T_ObjectType>(heapAllocator());
    }

    Return value of this class is specification of HeapStorage template class. It dependes from template argument with name T_ObjectType. So, for correct header convertion, I need some API to get template argument names - to create "template<typename T_ObjectType="">" line (where template argument name is presented). Something like this:

    inCppClass["methods"]["public"]["createStorage"]["templateArgs"][0]

     

    Last edit: Semenyakin Vladimir 2015-04-27
  • Jashua Cloutier

    Jashua Cloutier - 2015-05-02

    I see, you are after "template<typename T_ObjectType="">" or a parsed version of it. If you look around line 2071 you will see that I actually strip out this part of the code

            # Strip out template declarations
            templateSectionsToSliceOut = []
            try:
                for m in re.finditer("template[\t ]*<[^>]*>", headerFileStr):
                    start = m.start()
                    # Search for the final '>' which may or may not be caught in the case of nexted <>'s
                    for i in range(start, len(headerFileStr)):
                        if headerFileStr[i] == '<':
                            firstBracket = i
                            break
                    ltgtStackCount = 1
                    #Now look for fianl '>'
                    for i in range(firstBracket + 1, len(headerFileStr)):
                        if headerFileStr[i] == '<':
                            ltgtStackCount += 1
                        elif headerFileStr[i] == '>':
                            ltgtStackCount -= 1
                        if ltgtStackCount == 0:
                            end = i
                            break
                    templateSectionsToSliceOut.append((start, end))
    
                # Now strip out all instances of the template
                templateSectionsToSliceOut.reverse()
                for tslice in templateSectionsToSliceOut:
                    newlines = headerFileStr[tslice[0]: tslice[1]].count("\n") * "\n" #Keep line numbers the same
                    headerFileStr = headerFileStr[:tslice[0]] + newlines  + headerFileStr[tslice[1] + 1:]
            except:
                pass
    

    I do this because dealing with that broke other parts of the code and there wasnt a need for it. I will consider adding this as something that gets parsed in a future release (God help me).

     

    Last edit: Jashua Cloutier 2015-05-02

Log in to post a comment.