📄 main.cxx
字号:
} else numFields = 0; extendable = extend; if (ext != NULL) { ext->DisallowDeleteObjects(); for (PINDEX i = 0; i < ext->GetSize(); i++) fields.Append(ext->GetAt(i)); delete ext; }}void SequenceType::PrintOn(ostream & strm) const{ PrintStart(strm); strm << '\n'; PINDEX i; for (i = 0; i < numFields; i++) strm << fields[i]; if (extendable) { strm << indent() << "...\n"; for (; i < fields.GetSize(); i++) strm << fields[i]; } PrintFinish(strm);}void SequenceType::FlattenUsedTypes(){ for (PINDEX i = 0; i < fields.GetSize(); i++) fields.SetAt(i, fields[i].FlattenThisType(*this));}TypeBase * SequenceType::FlattenThisType(const TypeBase & parent){ return new DefinedType(this, parent);}BOOL SequenceType::IsPrimitiveType() const{ return FALSE;}void SequenceType::GenerateCplusplus(ostream & hdr, ostream & cxx){ PINDEX i; BeginGenerateCplusplus(hdr, cxx); PINDEX baseOptions = 0; for (i = 0; i < fields.GetSize(); i++) { if (i < numFields && fields[i].IsOptional()) baseOptions++; } // Complete ancestor constructor parameters cxx << ", " << baseOptions << ", " << (extendable ? "TRUE" : "FALSE") << ", " << fields.GetSize() - numFields << ')'; // Output enum for optional parameters BOOL outputEnum = FALSE; for (i = 0; i < fields.GetSize(); i++) { if (i >= numFields || fields[i].IsOptional()) { if (outputEnum) hdr << ",\n"; else { hdr << " enum OptionalFields {\n"; outputEnum = TRUE; } hdr << " e_" << fields[i].GetIdentifier(); } } if (outputEnum) hdr << "\n" " };\n" "\n"; // Output the declarations and constructors for member variables for (i = 0; i < fields.GetSize(); i++) { PString varname = "m_" + fields[i].GetIdentifier(); hdr << " " << fields[i].GetTypeName() << ' ' << varname << ";\n"; if (fields[i].HasNonStandardTag()) { cxx << ",\n" " " << varname; fields[i].GenerateCplusplusConstructor(hdr, cxx); } } // Output declarations for generated functions hdr << "\n" " PINDEX GetDataLength() const;\n" " BOOL Decode(PASN_Stream & strm);\n" " void Encode(PASN_Stream & strm) const;\n" "#ifndef PASN_NOPRINTON\n" " void PrintOn(ostream & strm) const;\n" "#endif\n"; if (numFields > 0) hdr << " Comparison Compare(const PObject & obj) const;\n"; cxx << "\n" "{\n"; GenerateCplusplusConstraints(PString(), hdr, cxx); for (i = 0; i < fields.GetSize(); i++) { PString ident = fields[i].GetIdentifier(); fields[i].GenerateCplusplusConstraints("m_" + ident + ".", hdr, cxx); if (i >= numFields && !fields[i].IsOptional()) cxx << " IncludeOptionalField(e_" << ident << ");\n"; } cxx << "}\n" "\n" "\n" "#ifndef PASN_NOPRINTON\n" << GetTemplatePrefix() << "void " << GetClassNameString() << "::PrintOn(ostream & strm) const\n" "{\n" " int indent = strm.precision() + 2;\n" " strm << \"{\\n\";\n"; for (i = 0; i < fields.GetSize(); i++) { PString id = fields[i].GetIdentifier(); if (i >= numFields || fields[i].IsOptional()) cxx << " if (HasOptionalField(e_" << id << "))\n" " "; cxx << " strm << setw(indent+" << id.GetLength()+3 << ") << \"" << id << " = \" << setprecision(indent) << m_" << id << " << '\\n';\n"; } cxx << " strm << setw(indent-1) << \"}\";\n" "}\n" "#endif\n" "\n" "\n"; if (numFields > 0) { cxx << GetTemplatePrefix() << "PObject::Comparison " << GetClassNameString() << "::Compare(const PObject & obj) const\n" "{\n" "#ifndef PASN_LEANANDMEAN\n" " PAssert(IsDescendant(" << GetClassNameString() << "::Class()), PInvalidCast);\n" "#endif\n" " const " << GetClassNameString() << " & other = (const " << GetClassNameString() << " &)obj;\n" "\n" " Comparison result;\n" "\n"; for (i = 0; i < numFields; i++) { PString identifier = fields[i].GetIdentifier(); cxx << " if ((result = m_" << identifier << ".Compare(other.m_" << identifier << ")) != EqualTo)\n" " return result;\n"; } cxx << "\n" " return PASN_Sequence::Compare(other);\n" "}\n" "\n" "\n"; } cxx << GetTemplatePrefix() << "PINDEX " << GetClassNameString() << "::GetDataLength() const\n" "{\n" " return "; outputEnum = FALSE; for (i = 0; i < numFields; i++) { if (outputEnum) cxx << " +\n" " "; else outputEnum = TRUE; cxx << "m_" << fields[i].GetIdentifier() << ".GetObjectLength()"; } if (!outputEnum) cxx << '0'; cxx << ";\n" "}\n" "\n" "\n" << GetTemplatePrefix() << "BOOL " << GetClassNameString() << "::Decode(PASN_Stream & strm)\n" "{\n" " if (!PreambleDecode(strm))\n" " return FALSE;\n\n"; for (i = 0; i < numFields; i++) { cxx << " if ("; if (fields[i].IsOptional()) cxx << "HasOptionalField(e_" << fields[i].GetIdentifier() << ") && "; cxx << "!m_" << fields[i].GetIdentifier() << ".Decode(strm))\n" " return FALSE;\n"; } for (; i < fields.GetSize(); i++) cxx << " if (!KnownExtensionDecode(strm, e_" << fields[i].GetIdentifier() << ", m_" << fields[i].GetIdentifier() << "))\n" " return FALSE;\n"; cxx << "\n" " return UnknownExtensionsDecode(strm);\n" "}\n" "\n" "\n" << GetTemplatePrefix() << "void " << GetClassNameString() << "::Encode(PASN_Stream & strm) const\n" "{\n" " PreambleEncode(strm);\n\n"; for (i = 0; i < numFields; i++) { if (fields[i].IsOptional()) cxx << " if (HasOptionalField(e_" << fields[i].GetIdentifier() << "))\n" " "; cxx << " m_" << fields[i].GetIdentifier() << ".Encode(strm);\n"; } for (; i < fields.GetSize(); i++) cxx << " KnownExtensionEncode(strm, e_" << fields[i].GetIdentifier() << ", m_" << fields[i].GetIdentifier() << ");\n"; cxx << "\n" " UnknownExtensionsEncode(strm);\n"; EndGenerateCplusplus(hdr, cxx);}const char * SequenceType::GetAncestorClass() const{ return "PASN_Sequence";}BOOL SequenceType::CanReferenceType() const{ return TRUE;}BOOL SequenceType::ReferencesType(const TypeBase & type){ for (PINDEX i = 0; i < fields.GetSize(); i++) if (fields[i].ReferencesType(type)) return TRUE; return FALSE;}/////////////////////////////////////////////////////////SequenceOfType::SequenceOfType(TypeBase * base, Constraint * constraint, unsigned tag) : TypeBase(tag){ baseType = base; if (constraint != NULL) AddConstraint(constraint);}SequenceOfType::~SequenceOfType(){ delete baseType;}void SequenceOfType::PrintOn(ostream & strm) const{ PrintStart(strm); if (baseType == NULL) strm << "!!Null Type!!\n"; else strm << *baseType << '\n'; PrintFinish(strm);}void SequenceOfType::FlattenUsedTypes(){ baseType = baseType->FlattenThisType(*this);}TypeBase * SequenceOfType::FlattenThisType(const TypeBase & parent){ if (!baseType->IsPrimitiveType() || baseType->HasConstraints()) return new DefinedType(this, parent); // Search for an existing sequence of type PString seqName = "ArrayOf_" + baseType->GetTypeName(); TypeBase * existingType = Module->FindType(seqName); if (existingType != NULL) return new DefinedType(this, existingType); return new DefinedType(this, seqName);}BOOL SequenceOfType::IsPrimitiveType() const{ return FALSE;}void SequenceOfType::GenerateCplusplus(ostream & hdr, ostream & cxx){ BeginGenerateCplusplus(hdr, cxx); cxx << ")\n" "{\n"; GenerateCplusplusConstraints(PString(), hdr, cxx); cxx << "}\n" "\n" "\n"; PString baseTypeName = baseType->GetTypeName(); // Generate declarations for generated functions hdr << " PASN_Object * CreateObject() const;\n" " " << baseTypeName << " & operator[](PINDEX i) const"; if (Module->UsingInlines()) hdr << " { return (" << baseTypeName << " &)array[i]; }\n"; else hdr << ";\n"; // Generate implementation for functions cxx << GetTemplatePrefix() << "PASN_Object * " << GetClassNameString() << "::CreateObject() const\n" "{\n"; if (baseType->HasConstraints()) { cxx << " " << baseTypeName << " * obj = new " << baseTypeName << ";\n"; baseType->GenerateCplusplusConstraints("obj->", hdr, cxx); cxx << " return obj;\n"; } else cxx << " return new " << baseTypeName << ";\n"; if (!Module->UsingInlines()) cxx << "}\n" "\n" "\n" << GetTemplatePrefix() << baseTypeName << " & " << GetClassNameString() << "::operator[](PINDEX i) const\n" "{\n" " return (" << baseTypeName << " &)array[i];\n"; EndGenerateCplusplus(hdr, cxx);}void SequenceOfType::GenerateForwardDecls(ostream & hdr){ if (baseType->IsParameterizedType()) return; PString baseTypeName = baseType->GetTypeName(); PStringSet typesOutput(PARRAYSIZE(StandardClasses), StandardClasses); typesOutput += GetIdentifier(); if (!typesOutput.Contains(baseTypeName)) hdr << "class " << baseTypeName << ";\n\n";}const char * SequenceOfType::GetAncestorClass() const{ return "PASN_Array";}BOOL SequenceOfType::CanReferenceType() const{ return TRUE;}BOOL SequenceOfType::ReferencesType(const TypeBase & type){ return baseType->ReferencesType(type) && baseType->IsParameterizedType();}/////////////////////////////////////////////////////////SetType::SetType() : SequenceType(NULL, FALSE, NULL, Tag::UniversalSet){}SetType::SetType(SequenceType * seq) : SequenceType(*seq){ tag.number = Tag::UniversalSet;}const char * SetType::GetAncestorClass() const{ return "PASN_Set";}/////////////////////////////////////////////////////////SetOfType::SetOfType(TypeBase * base, Constraint * constraint) : SequenceOfType(base, constraint, Tag::UniversalSet){}/////////////////////////////////////////////////////////ChoiceType::ChoiceType(TypesList * stnd, BOOL extendable, TypesList * extensions) : SequenceType(stnd, extendable, extensions, Tag::IllegalUniversalTag){}void ChoiceType::GenerateCplusplus(ostream & hdr, ostream & cxx){ PINDEX i; BeginGenerateCplusplus(hdr, cxx); // Complete the ancestor constructor parameters cxx << ", " << numFields << ", " << (extendable ? "TRUE" : "FALSE"); // Generate the enum's for each choice discriminator, and include strings for // PrintOn() debug output into acncestor constructor BOOL outputEnum = FALSE; int prevNum = -1; for (i = 0; i < fields.GetSize(); i++) { const Tag & fieldTag = fields[i].GetTag(); if (fieldTag.mode == Tag::Automatic || !fields[i].IsChoice()) { if (outputEnum) { hdr << ",\n"; cxx << " \""; } else { hdr << " enum Choices {\n"; cxx << "\n" "#ifndef PASN_NOPRINTON\n" " , \""; outputEnum = TRUE; } hdr << " e_" << fields[i].GetIdentifier(); cxx << fields[i].GetIdentifier(); if (fieldTag.mode != Tag::Automatic && fieldTag.number != (unsigned)(prevNum+1)) { hdr << " = " << fieldTag.number; cxx << '=' << fieldTag.number; } prevNum = fieldTag.number; cxx << " \"\n"; } } if (outputEnum) { hdr << "\n" " };\n" "\n"; cxx << "#endif\n" " "; } cxx << ")\n" "{\n"; GenerateCplusplusConstraints(PString(), hdr, cxx); cxx << "}\n" "\n" "\n"; // Generate code for type safe cast operators of selected choice object BOOL needExtraLine = FALSE; PStringSet typesOutput(PARRAYSIZE(StandardClasses), StandardClasses); typesOutput += GetIdentifier(); for (i = 0; i < fields.GetSize(); i++) { PString type = fields[i].GetTypeName(); if (!typesOutput.Contains(type)) { if (Module->UsingInlines()) { hdr << "#if defined(__GNUC__) && __GNUC__ <= 2 && __GNUC_MINOR__ < 9\n" " operator " << type << " &() const { return *(" << type << " *)choice; }\n" "#else\n" " operator " << type << " &() { return *(" << type << " *)choice; }\n" " operator const " << type << " &() const { return *(const " << type << " *)choice; }\n" "#endif\n"; } else { hdr << "#if defined(__GNUC__) && __GNUC__ <= 2 && __GNUC_MINOR__ < 9\n" " operator " << type << " &() const;\n" "#else\n" " operator " << type << " &();\n" " operator const " << type << " &() const;\n" "#endif\n"; cxx << "#if defined(__GNUC__) && __GNUC__ <= 2 && __GNUC_MINOR__ < 9\n" << GetTemplatePrefix() << GetClassNameString() << "::operator " << type << " &() const\n" "#else\n" << GetTemplatePrefix() << GetClassNameString() << "::operator " << type << " &()\n" "{\n" "#ifndef PASN_LEANANDMEAN\n" " PAssert(PAssertNULL(choice)->IsDescendant(" << type << "::Class()), PInvalidCast);\n" "#endif\n" " return *(" << type << " *)choice;\n" "}\n" "\n" "\n" << GetTemplatePrefix() << GetClassNameString() << "::operator const " << type << " &() const\n" "#endif\n" "{\n" "#ifndef PASN_LEANANDMEAN\n" " PAssert(PAssertNULL(choice)->IsDescendant(" << type << "::Class()), PInvalidCast);\n" "#endif\n" " return *(" << type << " *)choice;\n" "}\n" "\n" "\n"; } typesOutput += type; needExtraLine = TRUE; } } if (needExtraLine) hdr << '\n'; // Generate virtual function to create chosen object based on discriminator hdr << " BOOL CreateObject();\n"; cxx << GetTemplatePrefix() << "BOOL " << GetClassNameString() << "::CreateObject()\n" "{\n"; // declare an array of flags indicating whether the tag has been output or not PBYTEArray flags(fields.GetSize()); for (i = 0; i < fields.GetSize(); i++) flags[i] = 0; // keep outputEnum = FALSE; for (i = 0; i < fields.GetSize(); i++) { if (fields[i].GetTag().mode == Tag::Automatic || !fields[i].IsChoice()) { // ignore this tag if output previously if (flags[i] != 0) continue; if (!outputEnum) { cxx << " switch (tag) {\n"; outputEnum = TRUE; } // if the field has constraints, then output it alone // otherwise, look for all fields with the same type PString name = fields[i].GetTypeName(); if (fields[i].HasConstraints()) { cxx << " case e_" << fields[i].GetIdentifier() << " :\n"; flags[i] = 1; } else { PINDEX j; for (j = i; j < fields.GetSize(); j++) { if (fields[j].GetTypeName() == name) { cxx << " case e_" << fields[j].GetIdentifier() << " :\n"; flags[j] = 1; } } } cxx << " choice = new " << name; fields[i].GenerateCplusplusConstructor(hdr, cxx); cxx << ";\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -