/** Returns the function call to unpack a given type. */ private String getUnpackFuncJava(Type t, Object off, Object len) { String prefix = (record != packetType) ? "unpack" : "unpackPayload"; if (t == BOOLEAN) return prefix + "Boolean(" + off + ")"; if (t == BOOLNULL) return prefix + "Boolean(" + off + ")"; if (t == INETADDR) return prefix + "InetAddr(" + off + ")"; if (t == UUID) return prefix + "UUID(" + off + ")"; if (t == BYTE) return prefix + "Byte(" + off + ")"; if (t == SHORT) return prefix + "Short(" + off + ")"; if (t == INT) return prefix + "Int(" + off + ")"; if (t == LONG) return prefix + "Long(" + off + ")"; if (t == FLOAT) return prefix + "Float(" + off + ")"; if (t == DOUBLE) return prefix + "Double(" + off + ")"; if (t == UTCTIME) return prefix + "TimeStamp(" + off + ", nxm.vrt.lib.TimeStamp.IntegerMode.UTC)"; if (t == GPSTIME) return prefix + "TimeStamp(" + off + ", nxm.vrt.lib.TimeStamp.IntegerMode.GPS)"; if (t.isAsciiType()) return prefix + "Ascii(" + off + ", " + len + ")"; if (t.isUTF8Type()) return prefix + "UTF8(" + off + ", " + len + ")"; if (t.isAsciiTypeStr()) return prefix + "Ascii(" + off + ", " + len + ")"; if (t.isUTF8TypeStr()) return prefix + "UTF8(" + off + ", " + len + ")"; if (t.isMetadataType()) return prefix + "Metadata(" + off + ", " + len + ")"; if (t instanceof Record) return prefix + "Record(" + off + ", new " + t.getTypeJavaGet() + "())"; if (t instanceof Enumeration) return "" + t.getTypeJavaGet() + ".forValue(" + prefix + "Byte(" + off + "))"; throw new RuntimeException("Unsupported type " + t.getTypeName()); }
/** Converts Java type to object (for example "int" to "Integer"). */ private static CharSequence getTypeJavaClass(Type t) { if (t.isArrayType()) { Type tt = t.getArrayType(); if (tt == BOOLEAN) return "boolean[].class"; if (tt == BYTE) return "byte[].class"; if (tt == SHORT) return "short[].class"; if (tt == INT) return "int[].class"; if (tt == LONG) return "long[].class"; if (tt == FLOAT) return "float[].class"; if (tt == DOUBLE) return "double[].class"; return tt.getTypeJavaGet() + "[].class"; } else { if (t == BOOLEAN) return "Boolean.TYPE"; if (t == BYTE) return "Byte.TYPE"; if (t == SHORT) return "Short.TYPE"; if (t == INT) return "Integer.TYPE"; if (t == LONG) return "Long.TYPE"; if (t == FLOAT) return "Float.TYPE"; if (t == DOUBLE) return "Double.TYPE"; return t.getTypeJavaGet() + ".class"; } }
/** Write the set method. */ private void writeJavaSet( PrintStream out, String permission, String html, String size, String limit, String len, String indent) { html = html.replace("\n", "\n * "); Type t = getType(); int b = t.getTypeBytes(); // === DOCS === out.println(indent + " "); out.println(indent + " /** Set " + html + " "); if ((units != null) && (units.length() > 0)) { out.println(indent + " * <br> Units: <i>" + units + "</i>"); } if ((range != null) && (range.length() > 0)) { out.println(indent + " * <br> Range: <tt>" + range + "</tt>"); } if (t.isArrayType() || t.isStringType()) { out.println(indent + " * <br> Size: <tt>" + size + "</tt>"); } if (t.isStringType()) { out.println(indent + " * @param val The new value to write to the packet."); if (t.isAsciiType()) { out.println( indent + " * Any non-ASCII characters in the string (numeric value >0x7F) or ASCII"); out.println( indent + " * control characters (numeric value <0x20, 0x7F) will be replaced with \'?\'."); } if (b >= 0) { out.println( indent + " * If the string is longer than the size allotted, it will be"); out.println(indent + " * truncated without warning. "); } } else { out.println(indent + " * @param val The new value to write to the packet."); } if (t.isArrayType()) { out.println( indent + " * @throws java.lang.IllegalArgumentException If the length of <tt>val</tt> is invalid"); } out.println(indent + " */"); // === CODE === if (!t.isArrayType()) { out.println( indent + " " + permission + " void set" + methodName + " (" + t.getTypeJavaSet() + " val) {"); if (b < 0) { if (limit != null) { out.println(indent + " int limit = " + limit + ";"); out.println(indent + " int length = " + len + ";"); out.println(indent + " int len = (length > limit)? ((limit + 3) & 0x7FFFFFFC)"); out.println( indent + " : ((length + 3) & 0x7FFFFFFC); // length to next multiple of 4"); out.println(indent + " setPayloadLength(" + offset + " + len);"); } else { out.println( indent + " int len = (" + len + " + 3) & 0x7FFFFFFC; // length to next multiple of 4"); out.println(indent + " setPayloadLength(" + offset + " + len);"); } len = "len"; } out.println(indent + " " + getPackFuncJava(t, offset, len) + ";"); out.println(indent + " }"); } else { Type arrayType = t.getArrayType(); int count = t.getArrayLength(); int bytes = arrayType.getTypeBytes(); String singleName = notPlural(methodName); String getCount = "get" + singleName + "Count()"; out.println( indent + " " + permission + " void setAll" + methodName + " (" + arrayType.getTypeJavaGet() + " ... val) {"); if (t.getArrayType() == BYTE) { out.println(indent + " // Use fast method for packing an array of bytes"); out.println(indent + " packPayloadBytes(" + offset + ", val, " + getCount + ");"); } else { if (count >= 0) { out.println(indent + " if (val.length != " + b + ") {"); out.println( indent + " throw new java.lang.IllegalArgumentException(\"Invalid array length \"+val.length+\" for " + name + "\");"); out.println(indent + " }"); } else { out.println(indent + " set" + singleName + "Count(val.length);"); } out.println(indent + " for (int i = 0; i < val.length; i++) {"); out.println(indent + " set" + singleName + "(i, val[i]);"); out.println(indent + " }"); } out.println(indent + " }"); out.println(indent + " "); out.println(indent + " /** Set item in " + html + " "); out.println(indent + " * @param i The index."); out.println(indent + " * @param val The new value."); out.println( indent + " * @throws java.lang.IllegalArgumentException if <tt>i</tt> is greater than or equal to " + getCount + ""); out.println(indent + " */"); out.println( indent + " " + permission + " void set" + singleName + " (int i, " + arrayType.getTypeJavaSet() + " val) {"); out.println(indent + " if ((i < 0) || (i > " + getCount + ")) {"); out.println( indent + " throw new java.lang.IllegalArgumentException(\"Invalid index \"+i+\" into " + name + "\");"); out.println(indent + " }"); out.println(indent + " int off = " + offset + " + (" + bytes + " * i);"); out.println(indent + " " + getPackFuncJava(arrayType, "off", bytes) + ";"); out.println(indent + " }"); if (count < 0) { out.println(indent + " "); out.println(indent + " /** Set length of " + html + " "); out.println(indent + " * @param length The number of entries."); out.println( indent + " * @throws java.lang.IllegalArgumentException if <tt>length</tt> is negative"); out.println(indent + " */"); out.println(indent + " " + permission + " void set" + singleName + "Count (int length) {"); out.println(indent + " if (length < 0) {"); out.println( indent + " throw new java.lang.IllegalArgumentException(\"Illegal length \"+length+\" for " + name + "\");"); out.println(indent + " }"); out.println( indent + " int len = ((length * " + bytes + ") + 3) & 0x7FFFFFFC; // length to next multiple of 4"); out.println(indent + " setPayloadLength(" + offset + " + len);"); out.println(indent + " }"); } } }
/** Write the get method. */ private void writeJavaGet( PrintStream out, String permission, String html, String size, String limit, String len, String indent) { html = html.replace("\n", "\n * "); Type t = getType(); // === DOCS === out.println(indent + " "); out.println(indent + " /** Get " + html + " "); if ((units != null) && (units.length() > 0)) { out.println(indent + " * <br> Units: <i>" + units + "</i>"); } if ((range != null) && (range.length() > 0)) { out.println(indent + " * <br> Range: <tt>" + range + "</tt>"); } if (t.isStringType() || t.isArrayType()) { out.println(indent + " * <br> Size: <tt>" + size + "</tt>"); } out.println(indent + " * @return The value read from the packet."); out.println(indent + " */"); // === CODE === if (!t.isArrayType()) { out.println( indent + " " + permission + " " + t.getTypeJavaGet() + " get" + methodName + " () {"); out.println(indent + " return " + getUnpackFuncJava(t, offset, len) + ";"); out.println(indent + " }"); } else { Type arrayType = t.getArrayType(); int count = t.getArrayLength(); int bytes = arrayType.getTypeBytes(); String singleName = notPlural(methodName); String getCount = "get" + singleName + "Count()"; out.println( indent + " " + permission + " " + arrayType.getTypeJavaGet() + "[] getAll" + methodName + " () {"); if (t.getArrayType() == BYTE) { out.println(indent + " // Use fast method for unpacking an array of bytes"); out.println(indent + " return unpackPayloadBytes(" + offset + ", " + getCount + ");"); } else { out.println(indent + " int len = " + getCount + ";"); out.println( indent + " " + arrayType.getTypeJavaGet() + "[] all = new " + arrayType.getTypeJavaGet() + "[len];"); out.println(indent + " for (int i = 0; i < len; i++) {"); out.println(indent + " all[i] = get" + singleName + "(i);"); out.println(indent + " }"); out.println(indent + " return all;"); } out.println(indent + " }"); out.println(indent + " "); out.println(indent + " /** Get item in " + html + " "); out.println(indent + " * @param i The index."); out.println(indent + " * @return The value read from the packet."); out.println( indent + " * @throws java.lang.IllegalArgumentException if <tt>i</tt> is greater than or equal to " + getCount + ""); out.println(indent + " */"); out.println( indent + " " + permission + " " + arrayType.getTypeJavaGet() + " get" + singleName + " (int i) {"); out.println(indent + " if ((i < 0) || (i > " + getCount + ")) {"); out.println( indent + " throw new java.lang.IllegalArgumentException(\"Invalid index \"+i+\" into " + name + "\");"); out.println(indent + " }"); out.println(indent + " int off = " + offset + " + (" + bytes + " * i);"); out.println(indent + " return " + getUnpackFuncJava(arrayType, "off", bytes) + ";"); out.println(indent + " }"); out.println(indent + " "); out.println(indent + " /** Get length of " + html + " "); if (count >= 0) { out.println(indent + " * @return The number of entries, which is always " + count + "."); } else { out.println(indent + " * @return The number of entries."); } out.println(indent + " */"); out.println(indent + " " + permission + " int get" + singleName + "Count () {"); if (count >= 0) { out.println(indent + " return " + len + ";"); } else { out.println(indent + " return (getPayloadLength()-" + offset + ")/" + bytes + ";"); } out.println(indent + " }"); } }
/** Write the get method. */ private void writeCPPGet( PrintStream hdr, PrintStream cpp, String permission, String html, String size, String limit, String len, String indent) { html = html.replace("\n", "\n * "); Type t = getType(); // === DOCS === hdr.println(indent + " "); hdr.println(indent + " /** Get " + html + " "); if ((units != null) && (units.length() > 0)) { hdr.println(indent + " * <br> Units: <i>" + units + "</i>"); } if ((range != null) && (range.length() > 0)) { hdr.println(indent + " * <br> Range: <tt>" + range + "</tt>"); } if (t.isStringType() || t.isArrayType()) { hdr.println(indent + " * <br> Size: <tt>" + size + "</tt>"); } hdr.println(indent + " * @return The value read from the packet."); hdr.println(indent + " */"); // === HEADER === if (!t.isArrayType()) { hdr.println( indent + " " + permission + ": inline " + t.getTypeCPPGet() + " get" + methodName + " () const {"); if (t instanceof Record) { hdr.println(indent + " " + t.getTypeJavaGet() + " r;"); hdr.println(indent + " " + getUnpackFuncCPP(t, offset, len) + ";"); hdr.println(indent + " return r;"); } else { hdr.println(indent + " return " + getUnpackFuncCPP(t, offset, len) + ";"); } hdr.println(indent + " }"); } else { Type arrayType = t.getArrayType(); int count = t.getArrayLength(); int bytes = arrayType.getTypeBytes(); String singleName = notPlural(methodName); String getCount = "get" + singleName + "Count()"; hdr.println( indent + " " + permission + ": vector<" + arrayType.getTypeCPPGet() + "> getAll" + methodName + " () const;"); hdr.println(indent + " "); hdr.println(indent + " /** Get method used by the HasFields functions. */"); if (!isCPPNativeType(t)) { hdr.println( indent + " private: vector<VRTObject*>* _getAll" + methodName + " () const;"); } else { hdr.println( indent + " private: vector<" + arrayType.getTypeCPPGet() + ">* _getAll" + methodName + " () const;"); } hdr.println(indent + " "); hdr.println(indent + " /** Get item in " + html + " "); hdr.println(indent + " * @param i The index."); hdr.println(indent + " * @return The value read from the packet."); hdr.println( indent + " * @throws VRTException if <tt>i</tt> is greater than or equal to " + getCount + ""); hdr.println(indent + " */"); hdr.println( indent + " " + permission + ": " + arrayType.getTypeCPPGet() + " get" + singleName + " (int32_t i) const;"); hdr.println(indent + " "); hdr.println(indent + " /** Get length of " + html + " "); if (count >= 0) { hdr.println( indent + " * @return The number of entries, which is always " + count + "."); } else { hdr.println(indent + " * @return The number of entries."); } hdr.println(indent + " */"); hdr.println( indent + " " + permission + ": inline int32_t get" + singleName + "Count () const {"); if (count >= 0) { hdr.println(indent + " return " + len + ";"); } else { hdr.println(indent + " return (getPayloadLength()-" + offset + ")/" + bytes + ";"); } hdr.println(indent + " }"); // === CODE === cpp.println( indent + "vector<" + arrayType.getTypeCPPGet() + "> " + record.name + "::getAll" + methodName + " () const {"); if (t.getArrayType() == BYTE) { cpp.println(indent + " // Use fast method for unpacking an array of bytes"); cpp.println(indent + " return unpackPayloadBytes(" + offset + ", " + getCount + ");"); } else { cpp.println(indent + " int32_t len = " + getCount + ";"); cpp.println(indent + " vector<" + arrayType.getTypeCPPGet() + "> all(len);"); cpp.println(indent + " for (int32_t i = 0; i < len; i++) {"); cpp.println(indent + " all[i] = get" + singleName + "(i);"); cpp.println(indent + " }"); cpp.println(indent + " return all;"); } cpp.println(indent + "}"); cpp.println(indent + ""); cpp.println( indent + "" + arrayType.getTypeCPPGet() + " " + record.name + "::get" + singleName + " (int32_t i) const {"); cpp.println(indent + " if ((i < 0) || (i > " + getCount + ")) {"); cpp.println(indent + " throw VRTException(\"Invalid index %d into " + name + "\", i);"); cpp.println(indent + " }"); cpp.println(indent + " int32_t off = " + offset + " + (" + bytes + " * i);"); if (arrayType instanceof Record) { cpp.println(indent + " " + arrayType.getTypeJavaGet() + " r;"); cpp.println(indent + " " + getUnpackFuncCPP(arrayType, "off", bytes) + ";"); cpp.println(indent + " return r;"); } else { cpp.println(indent + " return " + getUnpackFuncCPP(arrayType, "off", bytes) + ";"); } cpp.println(indent + "}"); cpp.println(indent + ""); if (!isCPPNativeType(t)) { cpp.println( indent + "vector<VRTObject*>* " + record.name + "::_getAll" + methodName + " () const {"); cpp.println(indent + " int32_t len = " + getCount + ";"); cpp.println(indent + " vector<VRTObject*>* all = new vector<VRTObject*>;"); cpp.println(indent + " for (int32_t i = 0; i < len; i++) {"); cpp.println( indent + " all->push_back(new " + arrayType.getTypeCPPGet() + "(get" + singleName + "(i)));"); cpp.println(indent + " }"); cpp.println(indent + " return all;"); cpp.println(indent + "}"); cpp.println(indent + ""); } else { cpp.println( indent + "vector<" + arrayType.getTypeCPPGet() + ">* " + record.name + "::_getAll" + methodName + " () const {"); cpp.println(indent + " int32_t len = " + getCount + ";"); cpp.println( indent + " vector<" + arrayType.getTypeCPPGet() + ">* all = new vector<" + arrayType.getTypeCPPGet() + ">;"); cpp.println(indent + " for (int32_t i = 0; i < len; i++) {"); cpp.println(indent + " all.push_back(get" + singleName + "(i));"); cpp.println(indent + " }"); cpp.println(indent + " return all;"); cpp.println(indent + "}"); cpp.println(indent + ""); } } }