Skip to content

Commit

Permalink
* Add Info.immutable to disable generating setters for public data…
Browse files Browse the repository at this point in the history
… members (pull #461)
  • Loading branch information
equeim authored Mar 1, 2021
1 parent 5c976fc commit 7ad5b66
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add `Info.immutable` to disable generating setters for public data members ([pull #461](https://github.com/bytedeco/javacpp/pull/461))
* Map `String` to `char*` with `Charset.forName(STRING_BYTES_CHARSET)` when that macro is defined ([pull #460](https://github.com/bytedeco/javacpp/pull/460))
* Fix `Loader.ClassProperties` not always getting overridden correctly when defined multiple times
* Allow `Loader.load()` to also rename executables on extraction to output filenames specified with the `#` character
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Context {
javaName = c.javaName;
constName = c.constName;
constBaseName = c.constBaseName;
immutable = c.immutable;
inaccessible = c.inaccessible;
objectify = c.objectify;
virtualize = c.virtualize;
Expand All @@ -59,6 +60,7 @@ class Context {
String javaName = null;
String constName = null;
String constBaseName = null;
boolean immutable = false;
boolean inaccessible = false;
boolean objectify = false;
boolean virtualize = false;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public Info(Info i) {
/** Outputs declarations for this class into their subclasses as well.
* Also adds methods for explicit casting, as done for multiple inheritance by default. */
boolean flatten = false;
/** Disables generation of setters for public data members of a class */
boolean immutable = false;
/** Map global functions to instance methods, without {@code static} modifier, to implement an interface, etc. */
boolean objectify = false;
/** Attempts to translate naively the statements of variable-like macros to Java. */
Expand Down Expand Up @@ -130,6 +132,8 @@ public Info(Info i) {
public Info enumerate(boolean enumerate) { this.enumerate = enumerate; return this; }
public Info flatten() { this.flatten = true; return this; }
public Info flatten(boolean flatten) { this.flatten = flatten; return this; }
public Info immutable() { this.immutable = true; return this; }
public Info immutable(boolean immutable) { this.immutable = immutable; return this; }
public Info objectify() { this.objectify = true; return this; }
public Info objectify(boolean objectify) { this.objectify = objectify; return this; }
public Info translate() { this.translate = true; return this; }
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2439,12 +2439,13 @@ boolean variable(Context context, DeclarationList declList) throws ParserExcepti
dcl.type.annotations = dcl.type.annotations.replaceAll("@Name\\(.*\\) ", "");
javaName = metadcl.javaName + "_" + shortName;
}
if ((dcl.type.constValue && dcl.indirections == 0) || dcl.constPointer || dcl.type.constExpr) {
final boolean hasSetter = !(dcl.type.constValue && dcl.indirections == 0) && !dcl.constPointer && !dcl.type.constExpr && !context.immutable;
if (!hasSetter) {
decl.text += "@MemberGetter ";
}
decl.text += modifiers + dcl.type.annotations.replace("@ByVal ", "@ByRef ")
+ dcl.type.javaName + " " + javaName + "(" + indices + ");";
if (!(dcl.type.constValue && dcl.indirections == 0) && !dcl.constPointer && !dcl.type.constExpr) {
if (hasSetter) {
if (indices.length() > 0) {
indices += ", ";
}
Expand Down Expand Up @@ -2484,12 +2485,13 @@ boolean variable(Context context, DeclarationList declList) throws ParserExcepti
}
tokens.index = backIndex;
Declarator dcl2 = declarator(context, null, -1, false, n, false, false);
if (dcl2.type.constValue || dcl2.constPointer || dcl2.indirections < 2 || dcl2.type.constExpr) {
final boolean hasSetter = !dcl.type.constValue && !dcl.constPointer && !(dcl2.indirections < 2) && !dcl2.type.constExpr && !context.immutable;
if (!hasSetter) {
decl.text += "@MemberGetter ";
}
decl.text += modifiers + dcl.type.annotations.replace("@ByVal ", "@ByRef ")
+ dcl.type.javaName + " " + javaName + "(" + indices + ");";
if (!dcl.type.constValue && !dcl.constPointer && !(dcl2.indirections < 2) && !dcl2.type.constExpr) {
if (hasSetter) {
if (indices.length() > 0) {
indices += ", ";
}
Expand Down Expand Up @@ -3170,8 +3172,11 @@ boolean group(Context context, DeclarationList declList) throws ParserException
// used for setter methods
ctx.javaName = type.javaName;
}
if (info != null && info.virtualize) {
ctx.virtualize = true;
if (info != null) {
if (info.virtualize)
ctx.virtualize = true;
if (info.immutable)
ctx.immutable = true;
}
ctx.baseType = base.cppName;

Expand Down

0 comments on commit 7ad5b66

Please sign in to comment.