Advertisement
vladislavbelov

D2136 Possible Solution

Jul 31st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. // Usual non-virtual method of IGUIObject, or you may
  2. // split it out and pass GUI* as an argument.
  3. template<typename InterfaceType, typename ObjectType>
  4. JS::PersistentRootedObject IGUIObject::CreateJSObject(
  5.     ObjectType* object, const char* name)
  6. {
  7.     JSContext* cx = GetGUI()->GetScriptInterface()->GetContext();
  8.     JSAutoRequest rq(cx);
  9.     JS::PersistentRootedObject JSObject;
  10.  
  11.     if (!JSObject.initialized())
  12.     {
  13.         JSObject.init(
  14.             cx, GetGUI()->GetScriptInterface()->CreateCustomObject(name));
  15.         InterfaceType<ObjectType>::RegisterScriptFunctions(cx, JSObject);
  16.         JS_SetPrivate(JSObject.get(), object);
  17.     }
  18.  
  19.     return JSObject;
  20. }
  21.  
  22. // IGUIObject interface object
  23. template<typename ObjectType>
  24. class IGUIObjectInterface
  25. {
  26. public:
  27.     static void RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj)
  28.     {
  29.         JS_DefineFunctions(cx, obj, Methods);
  30.         // ...
  31.     }
  32.  
  33.     static bool JSI_IGUIObject::toString(
  34.         JSContext* cx, uint UNUSED(argc), JS::Value* vp)
  35.     {
  36.         JSAutoRequest rq(cx);
  37.         JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
  38.  
  39.         JS::RootedObject thisObj(cx, JS_THIS_OBJECT(cx, vp));
  40.  
  41.         // Here we have a derived/final object type. That means
  42.         // we don't have to do downcast, only upcast. Which is simple.
  43.         ObjectType* object = reinterpret_cast<ObjectType*>(
  44.             JS_GetInstancePrivate(
  45.                 cx, thisObj, &JSI_IGUIObject::JSI_class, NULL));
  46.         if (!object)
  47.             return false;
  48.  
  49.         ScriptInterface::ToJSVal(
  50.                 cx, rec.rval(), "[GUIObject: " + object->GetName() + "]");
  51.         return true;
  52.     }
  53.  
  54. private:
  55.     static JSFunctionSpec Methods;
  56. };
  57.  
  58. template<typename ObjectType>
  59. JSFunctionSpec IGUIObjectInterface<ObjectType>::Methods = {
  60.     JS_FN("toString", IGUIObjectInterface<ObjectType>::ToString, 0, 0),
  61.     JS_FS_END
  62. };
  63.  
  64. // Another interface object, it can derive or not, it doesn't matter,
  65. // it also allows to use virtual inheritance.
  66. template<typename ObjectType>
  67. class IGUITextOwnerInterface : public IGUIObjectInterface<ObjectType>
  68. {
  69. public:
  70.     static void RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj)
  71.     {
  72.         IGUIObjectInterface<ObjectType>::RegisterScriptFunctions(cx, obj);
  73.         // ...
  74.     }
  75.  
  76. private:
  77.     static JSFunctionSpec Methods;
  78. };
  79.  
  80. // All this objects can be a part of origin GUI objects.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement