PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/farsounder/protobuf/compiler/matlab/matlab_generator.h

https://code.google.com/
C Header | 156 lines | 96 code | 20 blank | 40 comment | 0 complexity | 46e54c29987b29ded554b25a24a0b68c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // protobuf-matlab - FarSounder's Protocol Buffer support for Matlab
  2. // Copyright (c) 2008, FarSounder Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf-matlab/
  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. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. //
  11. // * Redistributions in binary form must reproduce the above copyright
  12. // notice, this list of conditions and the following disclaimer in the
  13. // documentation and/or other materials provided with the distribution.
  14. //
  15. // * Neither the name of the FarSounder Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from this
  17. // software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. // POSSIBILITY OF SUCH DAMAGE.
  30. // Author: fedor.labounko@gmail.com (Fedor Labounko)
  31. // Based on Google's C++ Protobuf compiler.
  32. //
  33. // Generates Matlab code for a given .proto file.
  34. #ifndef FARSOUNDER_PROTOBUF_COMPILER_MATLAB_GENERATOR_H__
  35. #define FARSOUNDER_PROTOBUF_COMPILER_MATLAB_GENERATOR_H__
  36. #include <string>
  37. #include <google/protobuf/compiler/code_generator.h>
  38. #include <google/protobuf/descriptor.h>
  39. #include <google/protobuf/stubs/common.h>
  40. namespace google {
  41. namespace protobuf {
  42. class Descriptor;
  43. class FileDescriptor;
  44. namespace io {
  45. class Printer;
  46. } // namespace io
  47. } // namespace protobuf
  48. } // namespace google
  49. namespace farsounder {
  50. namespace protobuf {
  51. namespace compiler {
  52. namespace matlab {
  53. // CodeGenerator implementation which generates a C++ source file and
  54. // header. If you create your own protocol compiler binary and you want
  55. // it to support C++ output, you can do so by registering an instance of this
  56. // CodeGenerator with the CommandLineInterface in your main() function.
  57. class LIBPROTOC_EXPORT MatlabGenerator :
  58. public ::google::protobuf::compiler::CodeGenerator {
  59. public:
  60. MatlabGenerator();
  61. ~MatlabGenerator();
  62. // implements CodeGenerator ----------------------------------------
  63. bool Generate(const ::google::protobuf::FileDescriptor* file,
  64. const ::std::string& parameter,
  65. ::google::protobuf::compiler::GeneratorContext* output_directory,
  66. ::std::string* error) const;
  67. enum MatlabType {
  68. MATLABTYPE_INT32 = 1, // TYPE_SINT32, TYPE_INT32, TYPE_SFIXED32
  69. MATLABTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
  70. MATLABTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32, TYPE_BOOL
  71. MATLABTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64
  72. MATLABTYPE_DOUBLE = 5, // TYPE_DOUBLE
  73. MATLABTYPE_SINGLE = 6, // TYPE_FLOAT
  74. MATLABTYPE_STRING = 7, // TYPE_STRING
  75. MATLABTYPE_BYTES = 8, // TYPE_BYTES
  76. MATLABTYPE_MESSAGE = 9, // TYPE_MESSAGE, TYPE_GROUP
  77. MATLABTYPE_ENUM = 10, // TYPE_ENUM
  78. MAX_MATLABTYPE = 10
  79. };
  80. static const MatlabType kTypeToMatlabTypeMap[
  81. ::google::protobuf::FieldDescriptor::MAX_TYPE + 1];
  82. static const ::std::string kMatlabTypeToString[MAX_MATLABTYPE + 1];
  83. private:
  84. void PrintMessageFunctions() const;
  85. void PrintDescriptorFunction(
  86. const ::google::protobuf::Descriptor & descriptor) const;
  87. void PrintDescriptorHeader(
  88. ::google::protobuf::io::Printer & printer,
  89. const ::google::protobuf::Descriptor & descriptor) const;
  90. void PrintDescriptorComment(
  91. ::google::protobuf::io::Printer & printer,
  92. const ::google::protobuf::Descriptor & descriptor) const;
  93. void PrintDescriptorBody(
  94. ::google::protobuf::io::Printer & printer,
  95. const ::google::protobuf::Descriptor & descriptor) const;
  96. void PrintFieldDescriptors(
  97. ::google::protobuf::io::Printer & printer,
  98. const ::google::protobuf::Descriptor & descriptor) const;
  99. void PrintFieldIndecesByNumber(
  100. ::google::protobuf::io::Printer & printer,
  101. const ::google::protobuf::Descriptor & descriptor) const;
  102. void PrintReadFunction(
  103. const ::google::protobuf::Descriptor & descriptor) const;
  104. void PrintReadHeader(
  105. ::google::protobuf::io::Printer & printer,
  106. const ::google::protobuf::Descriptor & descriptor) const;
  107. void PrintReadComment(
  108. ::google::protobuf::io::Printer & printer,
  109. const ::google::protobuf::Descriptor & descriptor) const;
  110. void PrintReadBody(
  111. ::google::protobuf::io::Printer & printer,
  112. const ::google::protobuf::Descriptor & descriptor) const;
  113. ::std::string DefaultValueToString(
  114. const ::google::protobuf::FieldDescriptor & field) const;
  115. ::std::string MakeReadFunctionHandle(
  116. const ::google::protobuf::FieldDescriptor & field) const;
  117. ::std::string MakeWriteFunctionHandle(
  118. const ::google::protobuf::FieldDescriptor & field) const;
  119. ::std::string DescriptorFunctionName(
  120. const ::google::protobuf::Descriptor & descriptor) const;
  121. ::std::string ReadFunctionName(
  122. const ::google::protobuf::Descriptor & descriptor) const;
  123. // Very coarse-grained lock to ensure that Generate() is reentrant.
  124. // Guards file_ and printer_.
  125. mutable ::google::protobuf::internal::Mutex mutex_;
  126. mutable const ::google::protobuf::FileDescriptor*
  127. file_; // Set in Generate(). Under mutex_.
  128. mutable ::google::protobuf::compiler::GeneratorContext*
  129. output_directory_; // Set in Generate().
  130. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MatlabGenerator);
  131. };
  132. } // namespace cpp
  133. } // namespace compiler
  134. } // namespace protobuf
  135. } // namespace farsounder
  136. #endif // FARSOUNDER_PROTOBUF_COMPILER_MATLAB_GENERATOR_H__