Removing Templates from Code - Best Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    Removing Templates from Code - Best Method

    Hi All,

    This is a weird one but I am hoping someone can help or has some
    pointers, a recipe how to do the following:

    I have to move some code from c++ to objective-c and to do this I must
    remove all defined templates. I am not really a c++ guy so I have not
    worked with templates all that much (not really at all) and as such I
    don't have a good idea where to start.

    In a nutshell what is the best way to take code with templates and
    turn it into code without templates?

    Thanks in advance,

    Chris
  • =?ISO-8859-9?B?YmFy/f4gY2FuIGth/v1r5/0=?=

    #2
    Re: Removing Templates from Code - Best Method

    On Aug 6, 8:00 am, Chris <christopherri. ..@gmail.comwro te:
    Hi All,
    >
    This is a weird one but I am hoping someone can help or has some
    pointers, a recipe how to do the following:
    >
    I have to move some code from c++ to objective-c and to do this I must
    remove all defined templates.  I am not really a c++ guy so I have not
    worked with templates all that much (not really at all) and as such I
    don't have a good idea where to start.
    >
    In a nutshell what is the best way to take code with templates and
    turn it into code without templates?
    >
    Thanks in advance,
    >
    Chris
    Hi Chris,

    Templates are skeletons for classes (speaking of class templates).
    They need to be instantiated with type information to actually become
    a class as we understand. Therefore if I were you I would check
    template instantiations in the code, if they are multiple (the same
    skeleton instantiated with and integer and a float in class template
    with single type information for example), then you will experience a
    problem. You might need to rewrite several parts of the code depending
    on what kind of information relevant to these types are used. If size
    information is used in members to do computations and instantiation
    types for the template have different sizes then you will need to
    introduce some logic to determine type (or use run time type
    information techniques, if you can in your case). If this does not
    work you may need to inevitably write separate classes for different
    type instantiations.
    Same arguments apply to function templates. A quick way to get rid of
    type dependency in function arguments is to replace the template
    one(s) with a void* and modify the caller places. Although beware
    again that you may need to introduce size related arguments to your
    original function, if that is needed inside the function.

    Have a nice day
    Baris

    Comment

    • Chris

      #3
      Re: Removing Templates from Code - Best Method

      On Aug 5, 10:54 pm, barýþ can kaþýkçý <bariskasi...@g mail.comwrote:
      On Aug 6, 8:00 am, Chris <christopherri. ..@gmail.comwro te:
      >
      >
      >
      Hi All,
      >
      This is a weird one but I am hoping someone can help or has some
      pointers, a recipe how to do the following:
      >
      I have to move some code from c++ to objective-c and to do this I must
      remove all defined templates.  I am not really a c++ guy so I have not
      worked with templates all that much (not really at all) and as such I
      don't have a good idea where to start.
      >
      In a nutshell what is the best way to take code with templates and
      turn it into code without templates?
      >
      Thanks in advance,
      >
      Chris
      >
      Hi Chris,
      >
      Templates are skeletons for classes (speaking of class templates).
      They need to be instantiated with type information to actually become
      a class as we understand. Therefore if I were you I would check
      template instantiations in the code, if they are multiple (the same
      skeleton instantiated with and integer and a float in class template
      with single type information for example), then you will experience a
      problem. You might need to rewrite several parts of the code depending
      on what kind of information relevant to these types are used. If size
      information is used in members to do computations and instantiation
      types for the template have different sizes then you will need to
      introduce some logic to determine type (or use run time type
      information techniques, if you can in your case). If this does not
      work you may need to inevitably write separate classes for different
      type instantiations.
      Same arguments apply to function templates. A quick way to get rid of
      type dependency in function arguments is to replace the template
      one(s) with a void* and modify the caller places. Although beware
      again that you may need to introduce size related arguments to your
      original function, if that is needed inside the function.
      >
      Have a nice day
      Baris
      Thank You Very Much Baris, this gives me something very concrete to go
      on... I think I'm going to have to end up rewriting some code but the
      majority I may get lucky and be able to use void*.

      Again Great Description Most Appreciated,

      Chris :)

      Comment

      • M.T

        #4
        Re: Removing Templates from Code - Best Method

        Chris <christopherrin es@gmail.comwri tes:


        I think you can write a script to do such works. using regex,you can
        match string like this
        >template[\n\t]*<*>*\(*\)(cons t)?{something}
        And then delete it.Surely the regex expression above will not work.
        maybe you can learn to using regex expressions to solve problems.


        Hi All,
        >
        This is a weird one but I am hoping someone can help or has some
        pointers, a recipe how to do the following:
        >
        I have to move some code from c++ to objective-c and to do this I must
        remove all defined templates. I am not really a c++ guy so I have not
        worked with templates all that much (not really at all) and as such I
        don't have a good idea where to start.
        >
        In a nutshell what is the best way to take code with templates and
        turn it into code without templates?
        >
        Thanks in advance,
        >
        Chris

        Comment

        • Pascal J. Bourguignon

          #5
          Re: Removing Templates from Code - Best Method

          Chris <christopherrin es@gmail.comwri tes:
          This is a weird one but I am hoping someone can help or has some
          pointers, a recipe how to do the following:
          >
          I have to move some code from c++ to objective-c and to do this I must
          remove all defined templates. I am not really a c++ guy so I have not
          worked with templates all that much (not really at all) and as such I
          don't have a good idea where to start.
          >
          In a nutshell what is the best way to take code with templates and
          turn it into code without templates?
          That would depend on the template.
          I think we can identify different kinds of templates.

          Some are purely meta-programming (probably the hardest to "translate" ).

          Some implement syntactic abstraction; they could possibly be
          translated as macros.

          Some are used for true generic programming. These ones will be the
          most agreable to translate into Objective-C. You will be able to
          implement them as Objective-C classes using objects ids instead of
          template parameters, and sending our beloved dynamic messages instead
          of generic ->method calls.

          And of course, you will have to distinguish class templates from
          function template, from member templates inside class (templates or
          not). Class and member templates you will probably be able to
          translate as normal Objective-C classes and methods (with the template
          parameters taken as normal generic parameters such as id or SEL). For
          function templates, you could implement them as a methods in a
          category to some class up in the hierarchy (perhaps NSObject), or as
          plain C function (with generic parameters).

          --
          __Pascal Bourguignon__

          Comment

          • Colander

            #6
            Re: Removing Templates from Code - Best Method

            On Aug 6, 7:00 am, Chris <christopherri. ..@gmail.comwro te:
            Hi All,
            >
            This is a weird one but I am hoping someone can help or has some
            pointers, a recipe how to do the following:
            >
            I have to move some code from c++ to objective-c and to do this I must
            remove all defined templates.  I am not really a c++ guy so I have not
            worked with templates all that much (not really at all) and as such I
            don't have a good idea where to start.
            Did you know that if you ended your objective-c file with .mm instead
            of .m
            you can use C++ constructions.. .. Don't know if it solves your
            problem, but
            it might be the easy way :)

            Good luck,
            Bas

            Comment

            • aman.c++

              #7
              Re: Removing Templates from Code - Best Method

              On Aug 6, 11:18 am, Chris <christopherri. ..@gmail.comwro te:
              I have to move some code from c++ to objective-c and to do this I must
              remove all defined templates.
              If it helps you, I successfully tested some basic template code using
              Objective-C++ over 1.5 years ago. It worked just fine though I faintly
              remember that the manual mentioned certain restrictions regarding the C
              ++ idioms you can/can't use in objective-C++. Please refer to your
              manual and you might find a better way than translating code.

              regards,
              Aman Angrish.


              Comment

              • Pavel

                #8
                Re: Removing Templates from Code - Best Method

                Chris wrote:
                Hi All,
                >
                This is a weird one but I am hoping someone can help or has some
                pointers, a recipe how to do the following:
                >
                I have to move some code from c++ to objective-c and to do this I must
                remove all defined templates. I am not really a c++ guy so I have not
                worked with templates all that much (not really at all) and as such I
                don't have a good idea where to start.
                >
                In a nutshell what is the best way to take code with templates and
                turn it into code without templates?
                >
                Thanks in advance,
                >
                Chris
                See this discusssion:


                They say, Comeau has some facility to translate C++ to C (which then
                will probably be not difficult to compile with Objective C). Cfront used
                to be the guy, but I just found out it has been discontinued long ago so
                it probably will not translate your C++ code if it is more or less new.
                Otherwise, you can check out
                http://www.softwarepreservation.org/...us_plus/cfront.

                Hope this will help
                -Pavel

                Comment

                Working...