JBuffer.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 JBuffer extends JCompType {
  24. /** Creates a new instance of JBuffer */
  25. public JBuffer() {
  26. super("struct buffer", " ::std::string", "byte[]", "Buffer", "byte[]");
  27. }
  28. public String genCppGetSet(String fname, int fIdx) {
  29. String cgetFunc = " virtual const "+getCppType()+"& get"+fname+"() const {\n";
  30. cgetFunc += " return m"+fname+";\n";
  31. cgetFunc += " }\n";
  32. String getFunc = " virtual "+getCppType()+"& get"+fname+"() {\n";
  33. getFunc += " bs_.set("+fIdx+");return m"+fname+";\n";
  34. getFunc += " }\n";
  35. return cgetFunc + getFunc;
  36. }
  37. public String getSignature() {
  38. return "B";
  39. }
  40. public String genJavaReadWrapper(String fname, String tag, boolean decl) {
  41. String ret = "";
  42. if (decl) {
  43. ret = " byte[] "+fname+";\n";
  44. }
  45. return ret + " "+fname+"=a_.readBuffer(\""+tag+"\");\n";
  46. }
  47. public String genJavaWriteWrapper(String fname, String tag) {
  48. return " a_.writeBuffer("+fname+",\""+tag+"\");\n";
  49. }
  50. public String genJavaCompareTo(String fname, String other) {
  51. StringBuffer sb = new StringBuffer();
  52. sb.append(" {\n");
  53. sb.append(" byte[] my = "+fname+";\n");
  54. sb.append(" byte[] ur = "+other+";\n");
  55. sb.append(" ret = com.yahoo.jute.Utils.compareBytes(my,0,my.length,ur,0,ur.length);\n");
  56. sb.append(" }\n");
  57. return sb.toString();
  58. }
  59. public String genJavaCompareTo(String fname) {
  60. return genJavaCompareTo(fname, "peer."+fname);
  61. }
  62. public String genJavaCompareToWrapper(String fname, String other) {
  63. return " "+genJavaCompareTo(fname, other);
  64. }
  65. public String genJavaEquals(String fname, String peer) {
  66. return " ret = com.yahoo.jute.Utils.bufEquals("+fname+","+peer+");\n";
  67. }
  68. public String genJavaHashCode(String fname) {
  69. return " ret = "+fname+".toString().hashCode();\n";
  70. }
  71. public String genJavaSlurpBytes(String b, String s, String l) {
  72. StringBuffer sb = new StringBuffer();
  73. sb.append(" {\n");
  74. sb.append(" int i = com.yahoo.jute.Utils.readVInt("+b+", "+s+");\n");
  75. sb.append(" int z = WritableUtils.getVIntSize(i);\n");
  76. sb.append(" "+s+" += z+i; "+l+" -= (z+i);\n");
  77. sb.append(" }\n");
  78. return sb.toString();
  79. }
  80. public String genJavaCompareBytes() {
  81. StringBuffer sb = new StringBuffer();
  82. sb.append(" {\n");
  83. sb.append(" int i1 = com.yahoo.jute.Utils.readVInt(b1, s1);\n");
  84. sb.append(" int i2 = com.yahoo.jute.Utils.readVInt(b2, s2);\n");
  85. sb.append(" int z1 = WritableUtils.getVIntSize(i1);\n");
  86. sb.append(" int z2 = WritableUtils.getVIntSize(i2);\n");
  87. sb.append(" s1+=z1; s2+=z2; l1-=z1; l2-=z2;\n");
  88. sb.append(" int r1 = com.yahoo.jute.Utils.compareBytes(b1,s1,l1,b2,s2,l2);\n");
  89. sb.append(" if (r1 != 0) { return (r1<0)?-1:0; }\n");
  90. sb.append(" s1+=i1; s2+=i2; l1-=i1; l1-=i2;\n");
  91. sb.append(" }\n");
  92. return sb.toString();
  93. }
  94. }