out.println(""+className+"::"+className+"()");
out.println("{");
List properties = getProperties();
for (Iterator iter = properties.iterator(); iter.hasNext();) {
JProperty property = (JProperty) iter.next();
String type = toCppType(property.getType());
String value = toCppDefaultValue(property.getType());
String propertyName = property.getSimpleName();
String parameterName = decapitalize(propertyName);
if( !type.startsWith("std::vector") ) {
out.println(" this->"+parameterName+" = "+value+";");
}
}
out.println("}");
out.println("");
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println(""+className+"::~"+className+"()");
out.println("{");
for( Iterator iter = properties.iterator(); iter.hasNext(); ) {
JProperty property = (JProperty) iter.next();
String type = toCppType(property.getType());
String propertyName = property.getSimpleName();
String parameterName = decapitalize(propertyName);
if( property.getType().isPrimitiveType() ||
property.getType().getSimpleName().equals("String") ) {
continue;
}
if( !type.startsWith("std::vector" ) ) {
out.println(" delete this->" + parameterName + ";");
} else if( type.contains( "*" ) ) {
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < " + parameterName + ".size(); ++i" + parameterName + " ) {");
out.println(" delete " + parameterName + "[i" + parameterName + "];");
out.println(" }");
}
}
out.println("}");
out.println("");
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println(className+"* "+className+"::cloneDataStructure() const {");
String newInstance = decapitalize( className );
out.println(" "+className+"* "+newInstance+" = new "+className+"();");
out.println("");
out.println(" // Copy the data from the base class or classes");
out.println(" "+newInstance+"->copyDataStructure( this );");
out.println("");
out.println(" return "+newInstance+";");
out.println("}");
out.println("");
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println("void "+className+"::copyDataStructure( const DataStructure* src ) {");
out.println("");
if( baseClass != null ) {
out.println(" // Copy the data of the base class or classes");
out.println(" "+getProperBaseClassName( className, baseClass )+"::copyDataStructure( src );");
out.println("");
}
out.println(" const "+className+"* srcPtr = dynamic_cast<const "+className+"*>( src );");
out.println("");
out.println(" if( srcPtr == NULL || src == NULL ) {");
out.println("");
out.println(" throw decaf::lang::exceptions::NullPointerException(");
out.println(" __FILE__, __LINE__,");
out.println(" \""+className+"::copyDataStructure - src is NULL or invalid\" );");
out.println(" }");
for( Iterator iter = properties.iterator(); iter.hasNext(); ) {
JProperty property = (JProperty) iter.next();
String type = toCppType(property.getType());
String propertyName = property.getSimpleName();
String parameterName = decapitalize(propertyName);
String constNess = "";
String getter = property.getGetter().getSimpleName();
String setter = property.getSetter().getSimpleName();
if( property.getType().isPrimitiveType() ||
type.equals("std::string") ||
property.getType().getSimpleName().equals("ByteSequence") ){
out.println(" this->"+setter+"( srcPtr->"+getter+"() );");
} else if( property.getType().isArrayType() &&
!property.getType().getArrayComponentType().isPrimitiveType() ) {
String arrayType = property.getType().getArrayComponentType().getSimpleName();
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < srcPtr->"+getter+"().size(); ++i" + parameterName + " ) {");
out.println(" if( srcPtr->"+getter+"()[i"+parameterName+"] != NULL ) {");
out.println(" this->"+getter+"().push_back(");
out.println(" dynamic_cast<"+arrayType+"*>(");
out.println(" srcPtr->"+getter+"()[i"+parameterName+"]->cloneDataStructure() ) );");
out.println(" } else {");
out.println(" this->"+getter+"().push_back( NULL );");
out.println(" }");
out.println(" }");
} else if( property.getType().isArrayType() &&
property.getType().getArrayComponentType().isPrimitiveType() ) {
out.println(" this->"+setter+"( srcPtr->"+getter+"() );");
} else {
out.println(" if( srcPtr->"+getter+"() != NULL ) {");
out.println(" this->"+setter+"(");
out.println(" dynamic_cast<"+type+"*>(");
out.println(" srcPtr->"+getter+"()->cloneDataStructure() ) );");
out.println(" }");
}
}
out.println("}");
// getDataStructureType
out.println("");
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println("unsigned char "+className+"::getDataStructureType() const {");
out.println(" return "+className+"::ID_" + className.toUpperCase() + ";");
out.println("}");
// toString
out.println("");
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println("std::string "+className+"::toString() const {");
out.println("");
out.println(" ostringstream stream;" );
out.println("");
out.println(" stream << \"Begin Class = "+className+"\" << std::endl;" );
out.println(" stream << \" Value of "+className+"::ID_" + className.toUpperCase() + " = "+getOpenWireOpCode(jclass)+"\" << std::endl;");
for( Iterator iter = properties.iterator(); iter.hasNext(); ) {
JProperty property = (JProperty) iter.next();
String type = toCppType(property.getType());
String propertyName = property.getSimpleName();
String parameterName = decapitalize(propertyName);
String constNess = "";
String getter = property.getGetter().getSimpleName();
String setter = property.getSetter().getSimpleName();
if( property.getType().getSimpleName().equals("ByteSequence") ) {
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < this->"+getter+"().size(); ++i" + parameterName + " ) {");
out.println(" stream << \" Value of "+propertyName+"[\" << i" + parameterName+" << \"] = \" << this->"+getter+"()[i"+parameterName+"] << std::endl;" );
out.println(" }" );
} else if( type.equals("unsigned char") ){
out.println(" stream << \" Value of "+propertyName+" = \" << (int)this->"+getter+"() << std::endl;");
} else if( property.getType().isPrimitiveType() ||
type.equals("std::string") ){
out.println(" stream << \" Value of "+propertyName+" = \" << this->"+getter+"() << std::endl;");
} else if( property.getType().isArrayType() &&
!property.getType().getArrayComponentType().isPrimitiveType() ) {
String arrayType = property.getType().getArrayComponentType().getSimpleName();
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < this->"+getter+"().size(); ++i" + parameterName + " ) {");
out.println(" stream << \" Value of "+propertyName+"[\" << i" + parameterName+" << \"] is Below:\" << std::endl;" );
out.println(" if( this->"+getter+"()[i"+parameterName+"] != NULL ) {");
out.println(" stream << this->"+getter+"()[i"+parameterName+"]->toString() << std::endl;");
out.println(" } else {");
out.println(" stream << \" Object is NULL\" << std::endl;");
out.println(" }");
out.println(" }");
} else if( property.getType().isArrayType() &&
property.getType().getArrayComponentType().isPrimitiveType() ) {
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < this->"+getter+"().size(); ++i" + parameterName + " ) {");
out.println(" stream << \" Value of "+propertyName+"[\" << i"+parameterName+" << \"] = \" << this->"+getter+"()[i"+parameterName+"] << std::endl;");
out.println(" }");
} else {
out.println(" stream << \" Value of "+propertyName+" is Below:\" << std::endl;" );
out.println(" if( this->"+getter+"() != NULL ) {");
out.println(" stream << this->"+getter+"()->toString() << std::endl;");
out.println(" } else {");
out.println(" stream << \" Object is NULL\" << std::endl;");
out.println(" }");
}
}
if( baseClass != null ) {
out.println(" stream << "+getProperBaseClassName( className, baseClass )+"::toString();");
}
out.println(" stream << \"End Class = "+className+"\" << std::endl;" );
out.println("");
out.println(" return stream.str();");
out.println("}");
// equals
out.println("");
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println("bool "+className+"::equals( const DataStructure* value ) const {");
out.println(" const "+className+"* valuePtr = dynamic_cast<const "+className+"*>( value );");
out.println("");
out.println(" if( valuePtr == NULL || value == NULL ) {");
out.println(" return false;");
out.println(" }");
for( Iterator iter = properties.iterator(); iter.hasNext(); ) {
JProperty property = (JProperty) iter.next();
String type = toCppType(property.getType());
String propertyName = property.getSimpleName();
String parameterName = decapitalize(propertyName);
String constNess = "";
String getter = property.getGetter().getSimpleName();
String setter = property.getSetter().getSimpleName();
if( property.getType().getSimpleName().equals("ByteSequence") ) {
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < this->"+getter+"().size(); ++i" + parameterName + " ) {");
out.println(" if( this->"+getter+"()[i" + parameterName+"] != valuePtr->"+getter+"()[i"+parameterName+"] ) {" );
out.println(" return false;" );
out.println(" }" );
out.println(" }" );
} else if( property.getType().isPrimitiveType() ||
type.equals("std::string") ){
out.println(" if( this->"+getter+"() != valuePtr->"+getter+"() ) {");
out.println(" return false;" );
out.println(" }" );
} else if( property.getType().isArrayType() &&
!property.getType().getArrayComponentType().isPrimitiveType() ) {
String arrayType = property.getType().getArrayComponentType().getSimpleName();
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < this->"+getter+"().size(); ++i" + parameterName + " ) {");
out.println(" if( this->"+getter+"()[i"+parameterName+"] != NULL ) {" );
out.println(" if( !this->"+getter+"()[i"+parameterName+"]->equals( valuePtr->"+getter+"()[i"+parameterName+"] ) ) {" );
out.println(" return false;");
out.println(" }");
out.println(" } else if( valuePtr->"+getter+"()[i"+parameterName+"] != NULL ) {");
out.println(" return false;");
out.println(" }");
out.println(" }");
} else if( property.getType().isArrayType() &&
property.getType().getArrayComponentType().isPrimitiveType() ) {
out.println(" for( size_t i" + parameterName + " = 0; i" + parameterName + " < this->"+getter+"().size(); ++i" + parameterName + " ) {");
out.println(" if( this->"+getter+"()[i"+parameterName+"] != valuePtr->"+getter+"()[i"+parameterName+"] ) {");
out.println(" return false;");
out.println(" }");
out.println(" }");
} else {
out.println(" if( this->"+getter+"() != NULL ) {");
out.println(" if( !this->"+getter+"()->equals( valuePtr->"+getter+"() ) ) {" );
out.println(" return false;");
out.println(" }");
out.println(" } else if( valuePtr->"+getter+"() != NULL ) {");
out.println(" return false;");
out.println(" }");
}
}
if( baseClass != null ) {
out.println(" if( !"+getProperBaseClassName( className, baseClass )+"::equals( value ) ) {");
out.println(" return false;");
out.println(" }");
}
out.println(" return true;" );
out.println("}");
if( className.equals( "Message" ) ) {
out.println("");
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println("unsigned int "+className+"::getSize() const {");
out.println("");
out.println(" unsigned int size = DEFAULT_MESSAGE_SIZE;");
out.println("");
out.println(" size += (unsigned int)this->getContent().size();");
out.println(" size += (unsigned int)this->getMarshalledProperties().size();");
out.println("");
out.println(" return size;");
out.println("}");
}
for( Iterator iter = properties.iterator(); iter.hasNext(); ) {
JProperty property = (JProperty) iter.next();
String type = toCppType(property.getType());
String propertyName = property.getSimpleName();
String parameterName = decapitalize(propertyName);
String getter = property.getGetter().getSimpleName();
String setter = property.getSetter().getSimpleName();
String constNess = "";
if( !property.getType().isPrimitiveType() &&
!property.getType().getSimpleName().equals("ByteSequence") &&
!property.getType().getSimpleName().equals("String") &&
!type.startsWith("std::vector") ) {
type = type + "*";
} else if( property.getType().getSimpleName().equals("String") ||
type.startsWith( "std::vector") ) {
type = type + "&";
constNess = "const ";
}
out.println("");
if( property.getType().isPrimitiveType() ) {
out.println("////////////////////////////////////////////////////////////////////////////////");
out.println(type+" "+className+"::"+getter+"() const {");
out.println(" return "+parameterName+";");
out.println("}");