001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.openwire.tool;
018    
019    import java.io.PrintWriter;
020    import java.util.Iterator;
021    import java.util.List;
022    
023    import org.codehaus.jam.JClass;
024    import org.codehaus.jam.JProperty;
025    
026    /**
027     * 
028     */
029    public class CppHeadersGenerator extends CppClassesGenerator {
030    
031        protected String getFilePostFix() {
032            return ".hpp";
033        }
034    
035        protected void generateFile(PrintWriter out) {
036            generateLicence(out);
037    
038            out.println("#ifndef ActiveMQ_" + className + "_hpp_");
039            out.println("#define ActiveMQ_" + className + "_hpp_");
040            out.println("");
041            out.println("// Turn off warning message for ignored exception specification");
042            out.println("#ifdef _MSC_VER");
043            out.println("#pragma warning( disable : 4290 )");
044            out.println("#endif");
045            out.println("");
046            out.println("#include <string>");
047            out.println("#include \"activemq/command/" + baseClass + ".hpp\"");
048    
049            List properties = getProperties();
050            for (Iterator iter = properties.iterator(); iter.hasNext();) {
051                JProperty property = (JProperty)iter.next();
052                if (!property.getType().isPrimitiveType() && !property.getType().getSimpleName().equals("String") && !property.getType().getSimpleName().equals("ByteSequence")) {
053                    String includeName = toCppType(property.getType());
054                    if (property.getType().isArrayType()) {
055                        JClass arrayType = property.getType().getArrayComponentType();
056                        if (arrayType.isPrimitiveType()) {
057                            continue;
058                        }
059                    }
060                    if (includeName.startsWith("array<")) {
061                        includeName = includeName.substring(6, includeName.length() - 1);
062                    } else if (includeName.startsWith("p<")) {
063                        includeName = includeName.substring(2, includeName.length() - 1);
064                    }
065                    if (includeName.equals("IDataStructure")) {
066                        out.println("#include \"activemq/" + includeName + ".hpp\"");
067                    } else {
068                        out.println("#include \"activemq/command/" + includeName + ".hpp\"");
069                    }
070                }
071            }
072            out.println("");
073            out.println("#include \"activemq/protocol/IMarshaller.hpp\"");
074            out.println("#include \"ppr/io/IOutputStream.hpp\"");
075            out.println("#include \"ppr/io/IInputStream.hpp\"");
076            out.println("#include \"ppr/io/IOException.hpp\"");
077            out.println("#include \"ppr/util/ifr/array\"");
078            out.println("#include \"ppr/util/ifr/p\"");
079            out.println("");
080            out.println("namespace apache");
081            out.println("{");
082            out.println("  namespace activemq");
083            out.println("  {");
084            out.println("    namespace command");
085            out.println("    {");
086            out.println("      using namespace ifr;");
087            out.println("      using namespace std;");
088            out.println("      using namespace apache::activemq;");
089            out.println("      using namespace apache::activemq::protocol;");
090            out.println("      using namespace apache::ppr::io;");
091            out.println("");
092            out.println("/*");
093            out.println(" *");
094            out.println(" *  Command and marshalling code for OpenWire format for " + className + "");
095            out.println(" *");
096            out.println(" *");
097            out.println(" *  NOTE!: This file is autogenerated - do not modify!");
098            out.println(" *         if you need to make a change, please see the Groovy scripts in the");
099            out.println(" *         activemq-core module");
100            out.println(" *");
101            out.println(" */");
102            out.println("class " + className + " : public " + baseClass + "");
103            out.println("{");
104            out.println("protected:");
105    
106            for (Iterator iter = properties.iterator(); iter.hasNext();) {
107                JProperty property = (JProperty)iter.next();
108                String type = toCppType(property.getType());
109                String name = decapitalize(property.getSimpleName());
110                out.println("    " + type + " " + name + " ;");
111            }
112            out.println("");
113            out.println("public:");
114            out.println("    const static unsigned char TYPE = " + getOpenWireOpCode(jclass) + ";");
115            out.println("");
116            out.println("public:");
117            out.println("    " + className + "() ;");
118            out.println("    virtual ~" + className + "() ;");
119            out.println("");
120            out.println("    virtual unsigned char getDataStructureType() ;");
121    
122            for (Iterator iter = properties.iterator(); iter.hasNext();) {
123                JProperty property = (JProperty)iter.next();
124                String type = toCppType(property.getType());
125                String propertyName = property.getSimpleName();
126                String parameterName = decapitalize(propertyName);
127                out.println("");
128                out.println("    virtual " + type + " get" + propertyName + "() ;");
129                out.println("    virtual void set" + propertyName + "(" + type + " " + parameterName + ") ;");
130            }
131            out.println("");
132            out.println("    virtual int marshal(p<IMarshaller> marshaller, int mode, p<IOutputStream> ostream) throw (IOException) ;");
133            out.println("    virtual void unmarshal(p<IMarshaller> marshaller, int mode, p<IInputStream> istream) throw (IOException) ;");
134            out.println("} ;");
135            out.println("");
136            out.println("/* namespace */");
137            out.println("    }");
138            out.println("  }");
139            out.println("}");
140            out.println("");
141            out.println("#endif /*ActiveMQ_" + className + "_hpp_*/");
142        }
143    
144    }