JSBClass.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/Core/Object.h>
  9. #include "JSBHeader.h"
  10. #include "JSBModule.h"
  11. using namespace Atomic;
  12. namespace ToolCore
  13. {
  14. class JSBPackage;
  15. class JSBFunction;
  16. class JSBFunctionType;
  17. class JSBType;
  18. // chosen function overrides
  19. class JSBFunctionSignature
  20. {
  21. public:
  22. JSBFunctionSignature(const String& name, const Vector<String>& sig);
  23. String name_;
  24. Vector<String> sig_;
  25. Vector<JSBType*> types_;
  26. bool Match(JSBFunction* function);
  27. void Parse() ;
  28. bool parsed_;
  29. };
  30. class JSBProperty
  31. {
  32. public:
  33. String name_;
  34. JSBFunction* getter_;
  35. JSBFunction* setter_;
  36. JSBProperty() : getter_(0), setter_(0)
  37. {
  38. }
  39. // returns proper case for property name
  40. // based on whether the Getter/Setter is all caps
  41. // GetMyValue -> myValue;
  42. // GetGUID -> guid
  43. String GetCasePropertyName()
  44. {
  45. if (!name_.Length())
  46. return name_;
  47. bool allUpper = true;
  48. for (unsigned k = 0; k < name_.Length(); k++)
  49. {
  50. if (!isupper(name_[k]))
  51. {
  52. allUpper = false;
  53. break;
  54. }
  55. }
  56. if (allUpper)
  57. {
  58. return name_.ToLower();
  59. }
  60. String name = name_;
  61. name[0] = tolower(name[0]);
  62. return name;
  63. }
  64. };
  65. class JSBClass : public Object
  66. {
  67. friend class JSClassWriter;
  68. friend class CSClassWriter;
  69. OBJECT(JSBClass)
  70. public:
  71. JSBClass(Context* context, JSBModule* module, const String& name, const String& nativeName);
  72. virtual ~JSBClass();
  73. const String& GetName() { return name_; }
  74. const String& GetNativeName() { return nativeName_; }
  75. JSBClass* GetBaseClass();
  76. PODVector<JSBClass*>& GetBaseClasses() {return baseClasses_; }
  77. PODVector<JSBFunction*>& GetFunctions() { return functions_; }
  78. bool IsAbstract() { return isAbstract_; }
  79. /// Note that if we at some point want to generate bindings for JSBClass
  80. /// this override will need to be addressed, as we'll need to know that JSBClass is
  81. /// itself an object
  82. bool IsObject() { return isObject_; }
  83. bool HasProperties() { return hasProperties_; }
  84. void GetPropertyNames(Vector<String>& names) { names = properties_.Keys(); }
  85. JSBProperty* GetProperty(const String& name)
  86. {
  87. if (!properties_.Contains(name))
  88. return 0;
  89. return properties_[name];
  90. }
  91. JSBFunction* MatchFunction(JSBFunction* function, bool includeBases = false);
  92. bool MatchProperty(JSBProperty* property, bool includeBases = false);
  93. JSBHeader* GetHeader() { return header_; }
  94. JSBModule* GetModule() { return module_; }
  95. JSBPackage* GetPackage() { return module_->GetPackage(); }
  96. bool IsNumberArray() { return numberArrayElements_ != 0; }
  97. int GetNumberArrayElements() { return numberArrayElements_;}
  98. const String& GetArrayElementType() const { return arrayElementType_; }
  99. JSBFunction* GetConstructor();
  100. void SetAbstract(bool value = true) { isAbstract_ = value; }
  101. void SetObject(bool value = true) { isObject_ = value; }
  102. void SetHeader(JSBHeader* header) { header_ = header; }
  103. void SetBaseClass(JSBClass* baseClass);
  104. void SetSkipFunction(const String& name, bool skip = true);
  105. void AddFunction(JSBFunction* function);
  106. void AddFunctionOverride(JSBFunctionSignature* override) { overrides_.Push(override); }
  107. void AddFunctionExclude(JSBFunctionSignature* exclude) { excludes_.Push(exclude); }
  108. void AddPropertyFunction(JSBFunction* function);
  109. void AddTypeScriptDecl(const String& decl) { typeScriptDecls_.Push(decl); }
  110. unsigned GetNumTypeScriptDecl() { return typeScriptDecls_.Size(); }
  111. const String& GetTypeScriptDecl(unsigned idx) { return typeScriptDecls_[idx]; }
  112. void AddHaxeDecl(const String& decl) { haxeDecls_.Push(decl); }
  113. unsigned GetNumHaxeDecl() { return haxeDecls_.Size(); }
  114. const String& GetHaxeDecl(unsigned idx) { return haxeDecls_[idx]; }
  115. void Preprocess();
  116. void Process();
  117. void PostProcess();
  118. void Dump();
  119. private:
  120. void RecursiveAddBaseClass(PODVector<JSBClass *> &baseClasses);
  121. String name_;
  122. String nativeName_;
  123. SharedPtr<JSBHeader> header_;
  124. SharedPtr<JSBModule> module_;
  125. PODVector<JSBFunction*> functions_;
  126. PODVector<JSBClass*> baseClasses_;
  127. PODVector<JSBFunctionSignature*> overrides_;
  128. PODVector<JSBFunctionSignature*> excludes_;
  129. Vector<String> typeScriptDecls_;
  130. Vector<String> haxeDecls_;
  131. bool isAbstract_;
  132. bool isObject_;
  133. // Vector3, Color, etc are marshalled via arrays
  134. int numberArrayElements_;
  135. String arrayElementType_;
  136. bool hasProperties_;
  137. HashMap<String, JSBProperty*> properties_;
  138. };
  139. }