B4Alpha-Aft3r0mega / javacpp

Automatically exported from code.google.com/p/javacpp
GNU General Public License v2.0
0 stars 0 forks source link

What's the correct way to implement enums? #40

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Write a C++ function that takes an enum as a parameter, and another that 
returns an enum. The enum in Java needs to be written.

What is the expected output? What do you see instead?
I don't know what to use for a superclass of my Java enum. There's nothing like 
an EnumPointer class or anything of the sort.

What version of the product are you using? On what operating system?
JavaCPP 0.6. OSX but building for Android.

Please provide any additional information below.
Please provide guidance for how to do something like this. Please add it to the 
tutorial on the main page.

Original issue reported on code.google.com by proulxem...@gmail.com on 18 Nov 2013 at 2:48

GoogleCodeExporter commented 8 years ago
An `enum` in C++ is basically an `int` value so we can map it that way, along 
with a `@Cast`. Mapping it to a Java `enum` isn't exactly straightforward, or 
even desirable, because we lose the ability to use arbitrary `int`, as we can 
from C++. If you have a good idea, though, I'm all ears!

There is a lot of `enum` samples in the Presets here:
https://code.google.com/p/javacpp/source/browse/?repo=presets
Do we really need more documentation for that?

Original comment by samuel.a...@gmail.com on 22 Nov 2013 at 11:39

GoogleCodeExporter commented 8 years ago
We now have documentation for this, in a way: The new `Parser` feature produces 
the desired output by default. So, by following the steps detailed here, for 
example:
    https://github.com/bytedeco/javacpp-presets/wiki/Create-New-Presets

with a config file like this:
    import org.bytedeco.javacpp.*;
    import org.bytedeco.javacpp.annotation.*;
    import org.bytedeco.javacpp.tools.*;

    @Properties(value=@Platform(include="Stuff.h"), target="Stuff")
    public class StuffConfig implements InfoMapper {
        public void map(InfoMap infoMap) {
        }
    }

and a `Stuff.h` header file like this:
    enum Stuff {
        ONE, TWO, THREE
    };

JavaCPP outputs the following interface:
    import java.nio.*;
    import org.bytedeco.javacpp.*;
    import org.bytedeco.javacpp.annotation.*;

    public class Stuff extends StuffConfig {
        static { Loader.load(); }

    // Parsed from Stuff.h

    /** enum Stuff */
    public static final int
        ONE = 0, TWO = 1, THREE = 2;
    }

BTW, if you have any other issues, please post about them on GitHub, if 
possible. Thank you for reporting!

Original comment by samuel.a...@gmail.com on 29 Dec 2014 at 3:32