JSBClass.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #pragma once
  5. #include <Atomic/IO/Log.h>
  6. #include <Atomic/Container/Str.h>
  7. #include <Atomic/Container/Vector.h>
  8. using namespace Atomic;
  9. class JSBModule;
  10. class JSBFunction;
  11. class JSBHeader;
  12. class JSBType;
  13. // chosen function overrides
  14. class JSBFunctionOverride
  15. {
  16. public:
  17. JSBFunctionOverride(const String& name, const Vector<String>& sig);
  18. String name_;
  19. Vector<String> sig_;
  20. Vector<JSBType*> types_;
  21. void Parse() ;
  22. bool parsed_;
  23. };
  24. class JSBEnum
  25. {
  26. public:
  27. JSBHeader* header_;
  28. String name_;
  29. Vector<String> values_;
  30. };
  31. class JSBProperty
  32. {
  33. public:
  34. String name_;
  35. JSBFunction* getter_;
  36. JSBFunction* setter_;
  37. JSBProperty() : getter_(0), setter_(0)
  38. {
  39. }
  40. };
  41. class JSBClass
  42. {
  43. // class name, possibly renamed for bindings
  44. String name_;
  45. // original classname
  46. String className_;
  47. bool isObject_;
  48. bool isAbstract_;
  49. bool hasProperties_;
  50. HashMap<String, JSBProperty*> properties_;
  51. // Vector3, Color, etc can be marshalled via arrays
  52. int numberArrayElements_;
  53. String arrayElementType_;
  54. JSBHeader* header_;
  55. JSBModule* module_;
  56. Vector<JSBClass*> baseClasses_;
  57. static Vector<JSBClass*> allClasses_;
  58. Vector<JSBFunction*> functions_;
  59. Vector<JSBFunctionOverride*> overrides_;
  60. static void RecursiveAddBaseClass(JSBClass* klass, Vector<JSBClass*>& baseClasses)
  61. {
  62. for (unsigned i = 0; i < baseClasses.Size(); i++)
  63. {
  64. JSBClass* base = baseClasses.At(i);
  65. if (!klass->baseClasses_.Contains(base))
  66. klass->baseClasses_.Push(base);
  67. RecursiveAddBaseClass(klass, base->baseClasses_);
  68. }
  69. }
  70. static void WriteProtoTypeRecursive(String& source, JSBClass* klass, Vector<JSBClass *> &written);
  71. public:
  72. JSBClass(const String& name, const String& className = String::EMPTY) :
  73. isObject_(false), isAbstract_(false) , hasProperties_(false), numberArrayElements_(0), arrayElementType_("float"),
  74. header_(0), module_(0)
  75. {
  76. name_ = name;
  77. if (className.Length())
  78. className_ = className;
  79. else
  80. className_ = name_;
  81. if (name_ == "Color")
  82. numberArrayElements_ = 4;
  83. else if (name_ == "Vector2")
  84. numberArrayElements_ = 2;
  85. else if (name_ == "Vector3")
  86. numberArrayElements_ = 3;
  87. else if (name_ == "Vector4")
  88. numberArrayElements_ = 4;
  89. else if (name_ == "Quaternion")
  90. numberArrayElements_ = 4;
  91. else if (name_ == "BoundingBox")
  92. numberArrayElements_ = 6;
  93. else if (name_ == "Rect")
  94. numberArrayElements_ = 4;
  95. else if (name_ == "IntRect")
  96. {
  97. numberArrayElements_ = 4;
  98. arrayElementType_ = "int";
  99. }
  100. else if (name_ == "IntVector2")
  101. {
  102. numberArrayElements_ = 2;
  103. arrayElementType_ = "int";
  104. }
  105. allClasses_.Push(this);
  106. }
  107. JSBClass* GetBaseClass() { return baseClasses_.Size() ? baseClasses_[0] : NULL; }
  108. void AddBaseClass(JSBClass* base)
  109. {
  110. baseClasses_.Push(base);
  111. }
  112. void SetSkipFunction(const String& name, bool skip = true);
  113. JSBFunction* GetFunction(const String& name);
  114. void GetPropertyNames(Vector<String>& names) { names = properties_.Keys(); }
  115. JSBProperty* GetProperty(const String& name)
  116. {
  117. if (!properties_.Contains(name))
  118. return 0;
  119. return properties_[name];
  120. }
  121. void AddFunction(JSBFunction* function);
  122. void AddFunctionOverride(JSBFunctionOverride* functionOverride);
  123. void SetModule(JSBModule* module) {module_ = module;}
  124. JSBModule* GetModule() {return module_;}
  125. void SetHeader(JSBHeader* header) {header_ = header;}
  126. JSBHeader* GetHeader() {return header_;}
  127. void Dump();
  128. static void Preprocess();
  129. static void Process();
  130. void AddPropertyFunction(JSBFunction* function);
  131. unsigned GetFunctionCount() {return functions_.Size(); }
  132. JSBFunction* GetFunction(unsigned idx) {return functions_.At(idx); }
  133. // get constructor if defined and not skipped
  134. JSBFunction* GetConstructor();
  135. static void DumpAllClasses()
  136. {
  137. for (unsigned i = 0; i < allClasses_.Size(); i++)
  138. {
  139. allClasses_[i]->Dump();
  140. }
  141. }
  142. bool isAbstract() { return isAbstract_;}
  143. void setAbstract(bool value) { isAbstract_ = value;}
  144. bool isObject() { return isObject_;}
  145. void setObject(bool value) { isObject_ = value;}
  146. bool hasProperties() { return hasProperties_; }
  147. bool isNumberArray() { return numberArrayElements_ != 0; }
  148. int GetNumberArrayElements() { return numberArrayElements_;}
  149. const String& GetArrayElementType() const { return arrayElementType_; }
  150. const String& GetName() { return name_; }
  151. const String& GetClassName() { return className_; }
  152. void Write(String& source);
  153. void WriteFunctions(String& source);
  154. void WriteDefinition(String& source);
  155. void WriteForwardDeclarations(String& source);
  156. static void WriteProtoTypeSetup(String& source);
  157. };