JVector.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.yahoo.jute.compiler;
  19. /**
  20. *
  21. * @author Milind Bhandarkar
  22. */
  23. public class JVector extends JCompType {
  24. static private int level = 0;
  25. static private String getId(String id) { return id+getLevel(); }
  26. static private String getLevel() { return Integer.toString(level); }
  27. static private void incrLevel() { level++; }
  28. static private void decrLevel() { level--; }
  29. private JType mElement;
  30. /** Creates a new instance of JVector */
  31. public JVector(JType t) {
  32. super("struct " + extractVectorName(t), " ::std::vector<"+t.getCppType()+">", "java.util.List<" + t.getJavaType() + ">", "Vector", "java.util.ArrayList<" + t.getJavaType() + ">");
  33. mElement = t;
  34. }
  35. public String getSignature() {
  36. return "[" + mElement.getSignature() + "]";
  37. }
  38. public String genJavaCompareTo(String fname) {
  39. return "";
  40. }
  41. public String genJavaReadWrapper(String fname, String tag, boolean decl) {
  42. StringBuffer ret = new StringBuffer("");
  43. if (decl) {
  44. ret.append(" java.util.List "+fname+";\n");
  45. }
  46. ret.append(" {\n");
  47. incrLevel();
  48. ret.append(" Index "+getId("vidx")+" = a_.startVector(\""+tag+"\");\n");
  49. ret.append(" if ("+getId("vidx")+"!= null) {");
  50. ret.append(" "+fname+"=new java.util.ArrayList<"+ mElement.getJavaType() + ">();\n");
  51. ret.append(" for (; !"+getId("vidx")+".done(); "+getId("vidx")+".incr()) {\n");
  52. ret.append(mElement.genJavaReadWrapper(getId("e"), getId("e"), true));
  53. ret.append(" "+fname+".add("+getId("e")+");\n");
  54. ret.append(" }\n");
  55. ret.append(" }\n");
  56. ret.append(" a_.endVector(\""+tag+"\");\n");
  57. decrLevel();
  58. ret.append(" }\n");
  59. return ret.toString();
  60. }
  61. public String genJavaReadMethod(String fname, String tag) {
  62. return genJavaReadWrapper(fname, tag, false);
  63. }
  64. public String genJavaWriteWrapper(String fname, String tag) {
  65. StringBuffer ret = new StringBuffer(" {\n");
  66. incrLevel();
  67. ret.append(" a_.startVector("+fname+",\""+tag+"\");\n");
  68. ret.append(" if ("+fname+"!= null) {");
  69. ret.append(" int "+getId("len")+" = "+fname+".size();\n");
  70. ret.append(" for(int "+getId("vidx")+" = 0; "+getId("vidx")+"<"+getId("len")+"; "+getId("vidx")+"++) {\n");
  71. ret.append(" "+mElement.getJavaWrapperType()+" "+getId("e")+" = ("+mElement.getJavaWrapperType()+") "+fname+".get("+getId("vidx")+");\n");
  72. ret.append(mElement.genJavaWriteWrapper(getId("e"), getId("e")));
  73. ret.append(" }\n");
  74. ret.append(" }\n");
  75. ret.append(" a_.endVector("+fname+",\""+tag+"\");\n");
  76. ret.append(" }\n");
  77. decrLevel();
  78. return ret.toString();
  79. }
  80. public String genJavaWriteMethod(String fname, String tag) {
  81. return genJavaWriteWrapper(fname, tag);
  82. }
  83. public JType getElementType() {
  84. return mElement;
  85. }
  86. static public String extractVectorName(JType jvType) {
  87. return JRecord.extractMethodSuffix(jvType)+"_vector";
  88. }
  89. }