PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace.Pinch/Interlace.Pinch/Languages/Cpp/CppLanguage.cs

https://bitbucket.org/VahidN/interlace
C# | 166 lines | 117 code | 24 blank | 25 comment | 10 complexity | f3fdff61f05422922438fd9fdfd231ef MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using Interlace.Pinch.Dom;
  32. using Interlace.Pinch.Generation;
  33. using Interlace.Utilities;
  34. using Interlace.PropertyLists;
  35. using Interlace.Pinch.Languages.Cpp;
  36. #endregion
  37. namespace Interlace.Pinch.Languages
  38. {
  39. public class CppLanguage : Language
  40. {
  41. static Dictionary<Pair<IntrinsicType, FieldModifier>, CppType> _intrinsics;
  42. static CppLanguage()
  43. {
  44. _intrinsics = new Dictionary<Pair<IntrinsicType, FieldModifier>, CppType>();
  45. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Float32, FieldModifier.Required), new CppType("float", "float", "0.0f"));
  46. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Float64, FieldModifier.Required), new CppType("double", "double", "0.0"));
  47. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int8, FieldModifier.Required), new CppType("unsigned char", "unsigned char", "0"));
  48. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int16, FieldModifier.Required), new CppType("short", "short", "0"));
  49. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int32, FieldModifier.Required), new CppType("int", "int", "0"));
  50. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int64, FieldModifier.Required), new CppType("long long", "long long", "0"));
  51. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Bool, FieldModifier.Required), new CppType("bool", "bool", "false"));
  52. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.String, FieldModifier.Required), new CppType("CString", "const CString &"));
  53. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Bytes, FieldModifier.Required), new CppType("CBlob", "const CBlob &"));
  54. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Float32, FieldModifier.Optional), new CppType("CNullable<float>", "CNullable<float>"));
  55. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Float64, FieldModifier.Optional), new CppType("CNullable<double>", "CNullable<double>"));
  56. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int8, FieldModifier.Optional), new CppType("CNullable<unsigned char>", "CNullable<unsigned char>"));
  57. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int16, FieldModifier.Optional), new CppType("CNullable<short>", "CNullable<short>"));
  58. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int32, FieldModifier.Optional), new CppType("CNullable<int>", "CNullable<int>"));
  59. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Int64, FieldModifier.Optional), new CppType("CNullable<long long>", "CNullable<long long>"));
  60. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Bool, FieldModifier.Optional), new CppType("CNullable<bool>", "CNullable<bool>"));
  61. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.String, FieldModifier.Optional), new CppType("CNullable<CString>", "const CNullable<CString> &"));
  62. _intrinsics.Add(new Pair<IntrinsicType, FieldModifier>(IntrinsicType.Bytes, FieldModifier.Optional), new CppType("CNullable<CBlob>", "const CNullable<CBlob> &"));
  63. }
  64. public CppLanguage()
  65. : base("cpp", "C++")
  66. {
  67. }
  68. public static string IdentifierToClassName(string identifier)
  69. {
  70. return string.Format("C{0}", identifier);
  71. }
  72. public override object CreateProtocolImplementationHelper(Protocol protocol, PropertyDictionary options)
  73. {
  74. return new CppProtocol(options);
  75. }
  76. public override object CreateStructureMemberImplementationHelper(StructureMember member)
  77. {
  78. if (member.FieldTypeReference is IntrinsicTypeReference)
  79. {
  80. IntrinsicTypeReference reference = member.FieldTypeReference as IntrinsicTypeReference;
  81. return new CppStructureMember(member, _intrinsics[new Pair<IntrinsicType, FieldModifier>(reference.Type, member.Modifier)]);
  82. }
  83. else
  84. {
  85. DeclarationTypeReference reference = member.FieldTypeReference as DeclarationTypeReference;
  86. bool isInProtocolNamespace =
  87. reference.Declaration.QualifiedName.ContainingName.Equals(member.Parent.Parent.Name);
  88. CppType type;
  89. string referenceClassName = IdentifierToClassName(reference.Declaration.Identifier);
  90. if (reference.Declaration is Enumeration)
  91. {
  92. string typeName = string.Format("{0}::Enumeration", referenceClassName);
  93. if (member.Modifier == FieldModifier.Optional) typeName = string.Format("CNullable<{0}>", typeName);
  94. if (isInProtocolNamespace)
  95. {
  96. type = new CppType(typeName, typeName);
  97. }
  98. else
  99. {
  100. type = new CppType(typeName, typeName);
  101. }
  102. }
  103. else if (reference.Declaration.Implementation is CppStructure)
  104. {
  105. CppStructure structure = (CppStructure)reference.Declaration.Implementation;
  106. if (!structure.IsSurrogate)
  107. {
  108. type = new CppType(
  109. string.Format("boost::shared_ptr<{0}>", referenceClassName),
  110. string.Format("const boost::shared_ptr<{0}>", referenceClassName),
  111. null, referenceClassName, structure);
  112. }
  113. else
  114. {
  115. type = new CppType(
  116. member.Modifier == FieldModifier.Required ? structure.SurrogateValueType : structure.SurrogateNullableValueType,
  117. member.Modifier == FieldModifier.Required ? structure.SurrogateReferenceType : structure.SurrogateNullableReferenceType,
  118. null, referenceClassName, structure);
  119. }
  120. }
  121. else
  122. {
  123. throw new InvalidOperationException();
  124. }
  125. return new CppStructureMember(member, type);
  126. }
  127. }
  128. public override object CreateStructureImplementationHelper(Structure structure, PropertyDictionary options)
  129. {
  130. return new CppStructure(structure, options);
  131. }
  132. public override void GenerateFiles(Generator generator, Document document)
  133. {
  134. generator.GenerateFile(
  135. Path.Combine(generator.DestinationPath, generator.BaseName + ".h"),
  136. Templates.CppTemplate, "h", "Document", document);
  137. generator.GenerateFile(
  138. Path.Combine(generator.DestinationPath, generator.BaseName + ".cpp"),
  139. Templates.CppTemplate, "cpp", "Document", document);
  140. }
  141. }
  142. }