JSBFunction.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #pragma once
  8. #include <Atomic/IO/Log.h>
  9. #include "JSBClass.h"
  10. #include "JSBType.h"
  11. #include "JSBSymbol.h"
  12. using namespace Atomic;
  13. namespace ToolCore
  14. {
  15. class JSBFunctionType
  16. {
  17. public:
  18. JSBFunctionType(JSBType* type) : type_(type)
  19. {
  20. isSharedPtr_ = false;
  21. isPointer_ = false;
  22. isReference_ = false;
  23. isTemplate_ = false;
  24. isConst_ = false;
  25. }
  26. bool isSharedPtr_;
  27. bool isPointer_;
  28. bool isReference_;
  29. bool isTemplate_;
  30. bool isConst_;
  31. String name_;
  32. String initializer_;
  33. JSBType* type_;
  34. String ToString()
  35. {
  36. String tstring = type_->ToString();
  37. if (isPointer_)
  38. tstring += "*";
  39. if (isReference_)
  40. tstring += "&";
  41. if (name_.Length())
  42. {
  43. tstring += " " + name_;
  44. }
  45. return tstring;
  46. }
  47. String ToArgString(int index)
  48. {
  49. String tstring = type_->ToString();
  50. if (isPointer_)
  51. tstring += "*";
  52. if (isReference_ && tstring != "String")
  53. tstring += "&";
  54. if (name_.Length())
  55. {
  56. tstring.AppendWithFormat(" __arg%i", index);
  57. }
  58. return tstring;
  59. }
  60. };
  61. class JSBFunction : public JSBSymbol
  62. {
  63. friend class JSFunctionWriter;
  64. friend class CSFunctionWriter;
  65. public:
  66. JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
  67. isConstructor_(false), isDestructor_(false),
  68. isGetter_(false), isSetter_(false),
  69. isOverload_(false), skip_(false), isVirtual_(false)
  70. {
  71. }
  72. const String& GetName() { return name_; }
  73. bool IsConstructor() { return isConstructor_; }
  74. bool IsDestructor() { return isDestructor_; }
  75. bool IsSetter() { return isSetter_; }
  76. bool IsGetter() { return isGetter_; }
  77. bool IsOverload() { return isOverload_; }
  78. bool IsVirtual() { return isVirtual_; }
  79. bool Skip() { return skip_; }
  80. JSBClass* GetClass() { return class_; }
  81. const String& GetPropertyName() { return propertyName_; }
  82. JSBFunctionType* GetReturnType() { return returnType_; }
  83. /// Get class return type or null
  84. JSBClass* GetReturnClass();
  85. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  86. const String& GetDocString() { return docString_; }
  87. void SetName(const String& name) { name_ = name; }
  88. void SetConstructor(bool value = true) { isConstructor_ = value; }
  89. void SetDestructor(bool value = true) { isDestructor_ = value; }
  90. void SetSetter(bool value = true) { isSetter_ = value; }
  91. void SetGetter(bool value = true) { isGetter_ = value; }
  92. void SetOverload(bool value = true) { isOverload_ = value; }
  93. void SetVirtual(bool value = true) { isVirtual_ = value; }
  94. void SetSkip(bool value) { skip_ = value; }
  95. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  96. void SetDocString(const String& docString) { docString_ = docString; }
  97. int FirstDefaultParameter()
  98. {
  99. for (unsigned i = 0; i < parameters_.Size(); i++)
  100. {
  101. if (parameters_[i]->initializer_.Length())
  102. return i;
  103. }
  104. return -1;
  105. }
  106. void AddParameter(JSBFunctionType* parm)
  107. {
  108. parameters_.Push(parm);
  109. }
  110. void Process();
  111. void WriteParameterMarshal(String& source);
  112. void WriteFunction(String& source);
  113. void WriteConstructor(String& source);
  114. void Write(String& source);
  115. void Dump()
  116. {
  117. String sig;
  118. if (!returnType_)
  119. sig += "void ";
  120. else
  121. sig += returnType_->ToString() + " ";
  122. sig += name_;
  123. sig += "(";
  124. for (unsigned i = 0; i < parameters_.Size(); i++)
  125. {
  126. sig += parameters_.At(i)->ToString();
  127. if (i != parameters_.Size() - 1)
  128. sig += ", ";
  129. }
  130. sig += ");";
  131. LOGINFOF(" %s", sig.CString());
  132. }
  133. private:
  134. SharedPtr<JSBClass> class_;
  135. String name_;
  136. String propertyName_;
  137. JSBFunctionType* returnType_;
  138. Vector<JSBFunctionType*> parameters_;
  139. String docString_;
  140. bool isConstructor_;
  141. bool isDestructor_;
  142. bool isGetter_;
  143. bool isSetter_;
  144. bool isOverload_;
  145. bool isVirtual_;
  146. bool skip_;
  147. };
  148. }