Google Groups Home
Help | Sign in
Porting C++ Template to Java Generic
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Aadain  
View profile
 More options May 16, 7:25 pm
Newsgroups: comp.lang.java.programmer
From: Aadain <aad...@gmail.com>
Date: Fri, 16 May 2008 16:25:42 -0700 (PDT)
Local: Fri, May 16 2008 7:25 pm
Subject: Porting C++ Template to Java Generic
I've run into a road block with porting some C++ code that makes use
of templates to Java.  In it, there is a main class I'll call A that
is a template class:

// C++
template <class T> class A
public:
    virtual T Evaluate() const = 0;

};

Other classes extend this class:

// C++
template <class T> class B : public A<T> {
public:
    B(const T &v) { value = v; }
    T Evaluate() const { return value; }
private:
    T value;

};

Now all of this is easy to implement in Java using Generic
programing.  The issue I'm having is when I try to implement code like
this:

template <class T1, class T2> class C : public A<T2> {
public:
    C(A<T1> a, A<T2> b) { t1 = a; t2 = b; }
    T2 Evaluate() {
        return t1->Evaluate() * t2->Evaluate();
    }
private:
    A<T1> t1;
    A<T2> t2;

};

Java complains about about the * operator being undefined for
arguments T1, T2;  Since Java doesn't have operator overloading, I
can't really see how to implement the same functionality.  Has anyone
ever encountered this type of problem, or can anyone see a way around
this issue?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chronic Philharmonic  
View profile
 More options May 17, 3:19 pm
Newsgroups: comp.lang.java.programmer
From: "Chronic Philharmonic" <karl.uppi...@verizon.net>
Date: Sat, 17 May 2008 19:19:16 GMT
Local: Sat, May 17 2008 3:19 pm
Subject: Re: Porting C++ Template to Java Generic

"Stefan Ram" <r...@zedat.fu-berlin.de> wrote in message

news:multiply-20080517013612@ram.dialup.fu-berlin.de...

> Aadain <aad...@gmail.com> writes:
>>Has anyone ever encountered this type of problem,
>>or can anyone see a way around this issue?

>  In Java, »*« only applies to primitive types, which cannot be
>  type arguments.

>  On this low level you would need to define an implementation
>  of »multiply« for each relevant type and use »a.multiply(b)«
>  or »multiply(a,b)« or so depending on the type of dispatch
>  (static or non-static) wanted for each argument.

>  Often, it will be better to re-design and re-implement in Java.

I inherited a "Java project" that was ill-advisedly ported from both C and
C++. The Java code had in/out parameters, HVALUE type return types that you
had to check (without the benefit of the Win32 "cracker" macros --  instead
of throwing/catching exceptions), and monstrosities such as "MutableInt". It
was not Java, it was Win32 and/or C and/or C++ that you could compile down
to Java byte codes and run in a JVM.

The sad thing was, when the project was ported to Java, it severed its ties
to the original native code base, so the only argument for the port was
slightly faster time to market, or slightly less development cost than
starting over in Java. I do not believe those arguments were validated, and
even if they were, any gains were more than offset by the very high cost of
maintenance over the years of the product's useful life.

Port the design, not the code.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lew  
View profile
 More options May 17, 7:21 pm
Newsgroups: comp.lang.java.programmer
From: Lew <l...@lewscanon.com>
Date: Sat, 17 May 2008 19:21:15 -0400
Local: Sat, May 17 2008 7:21 pm
Subject: Re: Porting C++ Template to Java Generic

Stefan Ram wrote:
> "Chronic Philharmonic" <karl.uppi...@verizon.net> writes:
>> Port the design, not the code.

>   If the code was already ported, it might still be improved by
>   refactoring - which includes writing unit test before any
>   modifications are made.

>   But this also takes some time to do. If you are not granted
>   pay to do this, then it can't be done; but each other paid
>   change to be done will take longer and be more error-prone.

Sometimes the productivity gain from doing things right, even in the short
term, is so great that it is profitable to do it right even when you aren't
explicitly paid to do it right.  You just don't tell your boss or client (same
thing) all the things you did to meet the objectives that they did state.

--
Lew


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aadain  
View profile
 More options May 19, 1:05 pm
Newsgroups: comp.lang.java.programmer
From: Aadain <aad...@gmail.com>
Date: Mon, 19 May 2008 10:05:30 -0700 (PDT)
Local: Mon, May 19 2008 1:05 pm
Subject: Re: Porting C++ Template to Java Generic
On May 17, 12:19 pm, "Chronic Philharmonic" <karl.uppi...@verizon.net>
wrote:

> Port the design, not the code.

This is exactly what I am attempting to do.  I've porting the
functionality of the program, which itself is pretty efficient and
mostly language neutral.  There were plenty of operations that took
advantage of C++ functionality, such as the original problem I
demonstrated in the original post.  After doing some digging and some
thinking on my own, I'm beginning to think that the solution is create
a simple master class that implements basic math for all possible
types that the generic types will take (there are only a handful) and
requiring the generic class to extend that master class.  Then instead
of the command

return t1.Evaluate() * t2.Evaluate();

It would look like

return t1.Evaluate().mult(t2.Evaluate());

Has anyone else done something similar with generics before?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Pitts  
View profile
 More options May 19, 5:59 pm
Newsgroups: comp.lang.java.programmer
From: Daniel Pitts <newsgroup.spamfil...@virtualinfinity.net>
Date: Mon, 19 May 2008 14:59:18 -0700
Local: Mon, May 19 2008 5:59 pm
Subject: Re: Porting C++ Template to Java Generic

Since Java doesn't support operator overloading, that does make sense.

> Has anyone else done something similar with generics before?

You don't even *need* generics for this, you *can* use a simple
interface, although it could help.

interface Addible<R,T> {
    R add(T t);

}

class MyInt extends Addible<MyInt, MyInt> {
    private final int value;
    public MyInt(int value) {
      this.value = value;
    }
    public MyInt add(MyInt other) {
      return new MyInt(value + other.value);
    }

}

public class DoesSomeWork {
   public static <T extends Addible<T,T>> T sumOf(Collection<T> addable){
     Iterator<T> it = addible.iterator();
     if (!it.hasNext()) {
        return null;
     }

     T current = it.next();
     while (it.hasNext() {
        current = current.add(it.next());
     }
     return current;
   }

}

MyInt total = DoesSomeWork.sumOf(myIntList);

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arved Sandstrom  
View profile
 More options May 19, 6:19 pm
Newsgroups: comp.lang.java.programmer
From: "Arved Sandstrom" <asandst...@accesswave.ca>
Date: Mon, 19 May 2008 22:19:25 GMT
Local: Mon, May 19 2008 6:19 pm
Subject: Re: Porting C++ Template to Java Generic
"Aadain" <aad...@gmail.com> wrote in message

news:66cb2495-8458-4f15-95d3-da9d90007779@d77g2000hsb.googlegroups.com...

I may be missing something in what you are trying to do, but this doesn't
seem to be a problem for generics. If you only have a small number of
classes, each supporting a certain set of methods (like mult()), then there
are two main choices for implementation - inheritance or interfaces. I don't
know enough about the classes in question to suggest which one would be
best, but I'm pretty sure you don't need generics.

AHS


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google