engineAPI.h 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _ENGINEAPI_H_
  23. #define _ENGINEAPI_H_
  24. #include <tuple>
  25. #include <utility>
  26. #ifndef _FIXEDTUPLE_H_
  27. #include "fixedTuple.h"
  28. #endif
  29. #ifndef _CONSOLETYPES_H_
  30. #include "console/consoleTypes.h"
  31. #endif
  32. #ifndef _CONSOLE_H_
  33. #include "console/console.h"
  34. #endif
  35. #ifndef _STRINGFUNCTIONS_H_
  36. #include "core/strings/stringFunctions.h"
  37. #endif
  38. #ifndef _SIMOBJECT_H_
  39. #include "console/simObject.h"
  40. #endif
  41. #ifndef _ENGINEFUNCTIONS_H_
  42. #include "console/engineFunctions.h"
  43. #endif
  44. // Whatever types are used in API definitions, their DECLAREs must be visible to the
  45. // macros. We include the basic primitive and struct types here.
  46. #ifndef _ENGINEPRIMITIVES_H_
  47. #include "console/enginePrimitives.h"
  48. #endif
  49. #ifndef _ENGINESTRUCTS_H_
  50. #include "console/engineStructs.h"
  51. #endif
  52. // Needed for the executef macros. Blame GCC.
  53. #ifndef _SIMEVENTS_H_
  54. #include "console/simEvents.h"
  55. #endif
  56. /// @file
  57. /// Definitions for exposing engine functionality to the control layer.
  58. ///
  59. /// This file provides a convenience layer around the underlying engine interop system (which at
  60. /// the moment still includes the legacy TorqueScript interop a.k.a. "console system"). The
  61. /// macros exposed here will automatically take care of all marshalling, value type constraints,
  62. /// reflection info instancing, etc. involved in defining engine API call-ins and call-outs.
  63. ///
  64. /// @note At the moment, this file supplies both the legacy TorqueScript console system as well
  65. /// as the new engine export system with the structures and information they need. In the
  66. /// near future, the console-based parts will get purged. This will not result in visible
  67. /// changes to users of the functionality here except for the string-based marshalling
  68. /// functions currently exposed (which will also disappear).
  69. //TODO: Disable warning for extern "C" functions returning UDTs for now; need to take a closer look at this
  70. #pragma warning( disable : 4190 )
  71. // Disable some VC warnings that are irrelevant to us.
  72. #pragma warning( push )
  73. #pragma warning( disable : 4510 ) // default constructor could not be generated; all the Args structures are never constructed by us
  74. #pragma warning( disable : 4610 ) // can never be instantiated; again Args is never constructed by us
  75. namespace engineAPI {
  76. /// Flag for enabling legacy console behavior in the interop system while
  77. /// we still have it around. Will disappear along with console.
  78. extern bool gUseConsoleInterop;
  79. /// Flag to allow engine functions to detect whether the engine had been
  80. /// initialized or shut down.
  81. extern bool gIsInitialized;
  82. }
  83. /// @name Marshalling
  84. ///
  85. /// Functions for converting to/from string-based data representations.
  86. ///
  87. /// @note This functionality is specific to the console interop.
  88. /// @{
  89. /// Marshal a single piece of data from native into client form.
  90. template< typename T >
  91. inline const char* EngineMarshallData( const T& value )
  92. {
  93. return castConsoleTypeToString( value );
  94. }
  95. inline const char* EngineMarshallData( bool value )
  96. {
  97. if( value )
  98. return "1";
  99. else
  100. return "0";
  101. }
  102. inline const char* EngineMarshallData( const char* str )
  103. {
  104. // The API assumes that if you pass a plain "const char*" through it, then you are referring
  105. // to string storage with non-local lifetime that can be safely passed to the control layer.
  106. return str;
  107. }
  108. template< typename T >
  109. inline const char* EngineMarshallData( T* object )
  110. {
  111. return ( object ? object->getIdString() : "0" );
  112. }
  113. template< typename T >
  114. inline const char* EngineMarshallData( const T* object )
  115. {
  116. return ( object ? object->getIdString() : "0" );
  117. }
  118. inline const char* EngineMarshallData( U32 value )
  119. {
  120. return EngineMarshallData( S32( value ) );
  121. }
  122. /// Marshal data from native into client form stored directly in
  123. /// client function invocation vector.
  124. template< typename T >
  125. inline void EngineMarshallData( const T& arg, S32& argc, ConsoleValue *argv )
  126. {
  127. const char* str = castConsoleTypeToString(arg);;
  128. argv[ argc++ ].setString(str);
  129. }
  130. inline void EngineMarshallData( bool arg, S32& argc, ConsoleValue *argv )
  131. {
  132. argv[ argc++ ].setBool(arg);
  133. }
  134. inline void EngineMarshallData( S32 arg, S32& argc, ConsoleValue *argv )
  135. {
  136. argv[ argc++ ].setInt(arg);
  137. }
  138. inline void EngineMarshallData( U32 arg, S32& argc, ConsoleValue *argv )
  139. {
  140. EngineMarshallData( S32( arg ), argc, argv );
  141. }
  142. inline void EngineMarshallData( F32 arg, S32& argc, ConsoleValue *argv )
  143. {
  144. argv[ argc++ ].setFloat(arg);
  145. }
  146. inline void EngineMarshallData( const char* arg, S32& argc, ConsoleValue *argv )
  147. {
  148. argv[ argc++ ].setString(arg);
  149. }
  150. inline void EngineMarshallData( char* arg, S32& argc, ConsoleValue *argv )
  151. {
  152. argv[ argc++ ].setString(arg);
  153. }
  154. template< typename T >
  155. inline void EngineMarshallData( T* object, S32& argc, ConsoleValue *argv )
  156. {
  157. argv[ argc++ ].setInt(object ? object->getId() : 0);
  158. }
  159. template< typename T >
  160. inline void EngineMarshallData( const T* object, S32& argc, ConsoleValue *argv )
  161. {
  162. argv[ argc++ ].setInt(object ? object->getId() : 0);
  163. }
  164. /// Unmarshal data from client form to engine form.
  165. ///
  166. /// This is wrapped in an a struct as partial specializations on function
  167. /// templates are not allowed in C++.
  168. template< typename T >
  169. struct EngineUnmarshallData
  170. {
  171. T operator()( const char* str ) const
  172. {
  173. T value;
  174. castConsoleTypeFromString( value, str );
  175. return value;
  176. }
  177. };
  178. template<>
  179. struct EngineUnmarshallData< S32 >
  180. {
  181. S32 operator()( ConsoleValue &ref ) const
  182. {
  183. return (S32)ref.getInt();
  184. }
  185. S32 operator()( const char* str ) const
  186. {
  187. return dAtoi( str );
  188. }
  189. };
  190. template<>
  191. struct EngineUnmarshallData< U32 >
  192. {
  193. U32 operator()( ConsoleValue &ref ) const
  194. {
  195. return (U32)ref.getInt();
  196. }
  197. U32 operator()( const char* str ) const
  198. {
  199. return dAtoui( str );
  200. }
  201. };
  202. template<>
  203. struct EngineUnmarshallData< F32 >
  204. {
  205. F32 operator()( ConsoleValue &ref ) const
  206. {
  207. return (F32)ref.getFloat();
  208. }
  209. F32 operator()( const char* str ) const
  210. {
  211. return dAtof( str );
  212. }
  213. };
  214. template<>
  215. struct EngineUnmarshallData< U8 >
  216. {
  217. U8 operator()( ConsoleValue &ref ) const
  218. {
  219. return (U8)((S32)ref.getInt());
  220. }
  221. U8 operator()( const char* str ) const
  222. {
  223. return dAtoui( str );
  224. }
  225. };
  226. template<>
  227. struct EngineUnmarshallData< const char* >
  228. {
  229. const char* operator()( ConsoleValue &ref ) const
  230. {
  231. return ref.getString();
  232. }
  233. const char* operator()( const char* str ) const
  234. {
  235. return str;
  236. }
  237. };
  238. template< typename T >
  239. struct EngineUnmarshallData< T* >
  240. {
  241. T* operator()( ConsoleValue &ref ) const
  242. {
  243. return dynamic_cast< T* >( Sim::findObject( ref ) );
  244. }
  245. T* operator()( const char* str ) const
  246. {
  247. return dynamic_cast< T* >( Sim::findObject( str ) );
  248. }
  249. };
  250. template<>
  251. struct EngineUnmarshallData< void >
  252. {
  253. void operator()( ConsoleValue& ) const {}
  254. void operator()( const char* ) const {}
  255. };
  256. template<>
  257. struct EngineUnmarshallData< ConsoleValue >
  258. {
  259. ConsoleValue operator()( ConsoleValue ref ) const
  260. {
  261. return std::move(ref);
  262. }
  263. };
  264. /// @}
  265. /// @name C to C++ Trampolines
  266. ///
  267. /// The trampolines serve two purposes:
  268. ///
  269. /// For one, they ensure that no matter what argument types are specified by users of the engine API macros, the correct
  270. /// argument value types are enforced on the functions exported by the engine. Let's say, for example, the user writes
  271. /// a function that takes a "Point3F direction" argument, then the template machinery here will automatically expose an
  272. /// API function that takes a "Point3F& direction" argument.
  273. ///
  274. /// Secondly, the templates jump the incoming calls from extern "C" space into C++ space. This is mostly relevant for
  275. /// methods only as they will need an implicit object type argument.
  276. ///
  277. /// @{
  278. // Helper type to factor out commonalities between function and method trampolines.
  279. template<typename T> struct _EngineTrampoline {
  280. struct Args {};
  281. };
  282. template< typename R, typename ...ArgTs >
  283. struct _EngineTrampoline< R( ArgTs ... ) >
  284. {
  285. template<typename T> using AVT = typename EngineTypeTraits<T>::ArgumentValueType;
  286. typedef fixed_tuple<AVT<ArgTs> ...> Args;
  287. Args argT;
  288. };
  289. template< typename T >
  290. struct _EngineFunctionTrampolineBase : public _EngineTrampoline< T >
  291. {
  292. typedef T FunctionType;
  293. };
  294. // Trampolines for any call-ins that aren't methods.
  295. template< typename T >
  296. struct _EngineFunctionTrampoline {};
  297. template< typename R, typename ...ArgTs >
  298. struct _EngineFunctionTrampoline< R(ArgTs...) > : public _EngineFunctionTrampolineBase< R(ArgTs...) >
  299. {
  300. private:
  301. using Super = _EngineFunctionTrampolineBase< R(ArgTs...) >;
  302. using SelfType = _EngineFunctionTrampoline< R(ArgTs...) >;
  303. using ArgsType = typename _EngineFunctionTrampolineBase< R(ArgTs ...) >::Args;
  304. template<size_t ...> struct Seq {};
  305. template<size_t N, size_t ...S> struct Gens : Gens<N-1, N-1, S...> {};
  306. template<size_t ...I> struct Gens<0, I...>{ typedef Seq<I...> type; };
  307. template<size_t I>
  308. static typename fixed_tuple_element<I, fixed_tuple<ArgTs...>>::type getAndToType(const ArgsType& args) {
  309. return EngineTypeTraits<typename fixed_tuple_element<I, fixed_tuple<ArgTs...>>::type>::ArgumentToValue(fixed_tuple_accessor<I>::get(args));
  310. }
  311. template<size_t ...I>
  312. static R dispatchHelper(typename Super::FunctionType fn, const ArgsType& args, Seq<I...>) {
  313. return R( fn(SelfType::template getAndToType<I>(args) ...) );
  314. }
  315. using SeqType = typename Gens<sizeof...(ArgTs)>::type;
  316. public:
  317. static R jmp(typename Super::FunctionType fn, const ArgsType& args )
  318. {
  319. return dispatchHelper(fn, args, SeqType());
  320. }
  321. };
  322. // Trampolines for engine methods
  323. template< typename T >
  324. struct _EngineMethodTrampolineBase : public _EngineTrampoline< T > {};
  325. template< typename Frame, typename T >
  326. struct _EngineMethodTrampoline {};
  327. template< typename Frame, typename R, typename ...ArgTs >
  328. struct _EngineMethodTrampoline< Frame, R(ArgTs ...) > : public _EngineMethodTrampolineBase< R(ArgTs ...) >
  329. {
  330. using FunctionType = R( typename Frame::ObjectType*, ArgTs ...);
  331. private:
  332. using Super = _EngineMethodTrampolineBase< R(ArgTs ...) >;
  333. using SelfType = _EngineMethodTrampoline< Frame, R(ArgTs ...) >;
  334. using ArgsType = typename _EngineMethodTrampolineBase< R(ArgTs ...) >::Args;
  335. template<size_t ...> struct Seq {};
  336. template<size_t N, size_t ...S> struct Gens : Gens<N-1, N-1, S...> {};
  337. template<size_t ...I> struct Gens<0, I...>{ typedef Seq<I...> type; };
  338. template<size_t I>
  339. static typename fixed_tuple_element<I, fixed_tuple<ArgTs...>>::type getAndToType(const ArgsType& args) {
  340. return EngineTypeTraits<typename fixed_tuple_element<I, fixed_tuple<ArgTs...>>::type>::ArgumentToValue(fixed_tuple_accessor<I>::get(args));
  341. }
  342. template<size_t ...I>
  343. static R dispatchHelper(Frame f, const ArgsType& args, Seq<I...>) {
  344. return R(f._exec(SelfType::template getAndToType<I>(args) ...));
  345. }
  346. using SeqType = typename Gens<sizeof...(ArgTs)>::type;
  347. public:
  348. static R jmp( typename Frame::ObjectType* object, const ArgsType& args )
  349. {
  350. Frame f;
  351. f.object = object;
  352. return dispatchHelper(f, args, SeqType());
  353. }
  354. };
  355. /// @}
  356. /// @name Thunking
  357. ///
  358. /// Internal functionality for thunks placed between TorqueScript calls of engine functions and their native
  359. /// implementations.
  360. ///
  361. /// @note The functionality in this group is specific to the console interop system.
  362. /// @{
  363. // Helper function to return data from a thunk.
  364. template< typename T >
  365. inline const char* _EngineConsoleThunkReturnValue( const T& value )
  366. {
  367. return EngineMarshallData( value );
  368. }
  369. inline bool _EngineConsoleThunkReturnValue( bool value )
  370. {
  371. return value;
  372. }
  373. inline S32 _EngineConsoleThunkReturnValue( S32 value )
  374. {
  375. return value;
  376. }
  377. inline F32 _EngineConsoleThunkReturnValue( F32 value )
  378. {
  379. return value;
  380. }
  381. inline const char* _EngineConsoleThunkReturnValue( const String& str )
  382. {
  383. return Con::getReturnBuffer( str );
  384. }
  385. inline const char* _EngineConsoleThunkReturnValue( const char* value )
  386. {
  387. return EngineMarshallData( value );
  388. }
  389. template< typename T >
  390. inline const char* _EngineConsoleThunkReturnValue( T* value )
  391. {
  392. return ( value ? value->getIdString() : "" );
  393. }
  394. template< typename T >
  395. inline const char* _EngineConsoleThunkReturnValue( const T* value )
  396. {
  397. return ( value ? value->getIdString() : "" );
  398. }
  399. // Helper class to determine the type of callback registered with the console system.
  400. template< typename R >
  401. struct _EngineConsoleThunkType
  402. {
  403. typedef const char* ReturnType;
  404. typedef StringCallback CallbackType;
  405. };
  406. template<>
  407. struct _EngineConsoleThunkType< S32 >
  408. {
  409. typedef S32 ReturnType;
  410. typedef IntCallback CallbackType;
  411. };
  412. template<>
  413. struct _EngineConsoleThunkType< U32 >
  414. {
  415. typedef U32 ReturnType;
  416. typedef IntCallback CallbackType;
  417. };
  418. template<>
  419. struct _EngineConsoleThunkType< F32 >
  420. {
  421. typedef F32 ReturnType;
  422. typedef FloatCallback CallbackType;
  423. };
  424. template<>
  425. struct _EngineConsoleThunkType< bool >
  426. {
  427. typedef bool ReturnType;
  428. typedef BoolCallback CallbackType;
  429. };
  430. template<>
  431. struct _EngineConsoleThunkType< void >
  432. {
  433. typedef void ReturnType;
  434. typedef VoidCallback CallbackType;
  435. };
  436. // Helper struct to count the number of parameters in a function list.
  437. // The setup through operator () allows omitting the the argument list entirely.
  438. struct _EngineConsoleThunkCountArgs
  439. {
  440. template<typename ...ArgTs> U32 operator()(ArgTs... args){
  441. return sizeof...(ArgTs);
  442. }
  443. operator U32() const{ // FIXME: WHAT IS THIS?? I'm pretty sure it's incorrect, and it's the version that is invoked by all the macros
  444. return 0;
  445. }
  446. };
  447. // Encapsulation of a legacy console function invocation.
  448. namespace engineAPI{
  449. namespace detail{
  450. template<S32 startArgc, typename R, typename ...ArgTs>
  451. struct ThunkHelpers {
  452. using SelfType = ThunkHelpers<startArgc, R, ArgTs...>;
  453. using FunctionType = R(*)(ArgTs...);
  454. template<typename Frame> using MethodType = R(Frame::*)(ArgTs ...) const;
  455. template<size_t I> using IthArgType = typename std::tuple_element<I, std::tuple<ArgTs ...> >::type;
  456. template<size_t ...> struct Seq {};
  457. template<size_t N, size_t ...S> struct Gens : Gens<N-1, N-1, S...> {};
  458. template<size_t ...I> struct Gens<0, I...>{ typedef Seq<I...> type; };
  459. typedef typename _EngineConsoleThunkType< R >::ReturnType ReturnType;
  460. static const S32 NUM_ARGS = sizeof...(ArgTs) + startArgc;
  461. template<size_t index, size_t method_offset = 0, typename ...RealArgTs>
  462. static IthArgType<index> getRealArgValue(S32 argc, ConsoleValue *argv, const _EngineFunctionDefaultArguments< void(RealArgTs...) >& defaultArgs)
  463. {
  464. if((startArgc + index) < argc)
  465. {
  466. return EngineUnmarshallData< IthArgType<index> >()( argv[ startArgc + index ] );
  467. } else {
  468. return fixed_tuple_accessor<index + method_offset>::get(defaultArgs.mArgs);
  469. }
  470. }
  471. template<size_t ...I>
  472. static R dispatchHelper(S32 argc, ConsoleValue *argv, FunctionType fn, const _EngineFunctionDefaultArguments< void(ArgTs...) >& defaultArgs, Seq<I...>){
  473. return fn(SelfType::getRealArgValue<I>(argc, argv, defaultArgs) ...);
  474. }
  475. template<typename Frame, size_t ...I>
  476. static R dispatchHelper(S32 argc, ConsoleValue *argv, MethodType<Frame> fn, Frame* frame, const _EngineFunctionDefaultArguments< void( typename Frame::ObjectType*, ArgTs...) >& defaultArgs, Seq<I...>){
  477. return (frame->*fn)(SelfType::getRealArgValue<I,1>(argc, argv, defaultArgs) ...);
  478. }
  479. using SeqType = typename Gens<sizeof...(ArgTs)>::type;
  480. };
  481. template<typename ArgVT> struct MarshallHelpers {
  482. template<typename ...ArgTs> static void marshallEach(S32 &argc, ArgVT *argv, const ArgTs& ...args){}
  483. template<typename H, typename ...Tail> static void marshallEach(S32 &argc, ArgVT *argv, const H& head, const Tail& ...tail){
  484. argv[argc++] = EngineMarshallData(head);
  485. marshallEach(argc, argv, tail...);
  486. }
  487. };
  488. template<> struct MarshallHelpers<ConsoleValue> {
  489. template<typename ...ArgTs> static void marshallEach(S32 &argc, ConsoleValue *argv, const ArgTs& ...args){}
  490. template<typename H, typename ...Tail> static void marshallEach(S32 &argc, ConsoleValue *argv, const H& head, const Tail& ...tail){
  491. EngineMarshallData(head, argc, argv);
  492. marshallEach(argc, argv, tail...);
  493. }
  494. };
  495. }
  496. }
  497. template< S32 startArgc, typename T >
  498. struct _EngineConsoleThunk {};
  499. template< S32 startArgc, typename R, typename ...ArgTs >
  500. struct _EngineConsoleThunk< startArgc, R(ArgTs...) >
  501. {
  502. private:
  503. using Helper = engineAPI::detail::ThunkHelpers<startArgc, R, ArgTs...>;
  504. using SeqType = typename Helper::SeqType;
  505. public:
  506. typedef typename Helper::FunctionType FunctionType;
  507. typedef typename Helper::ReturnType ReturnType;
  508. template<typename Frame> using MethodType = typename Helper::template MethodType<Frame>;
  509. static const S32 NUM_ARGS = Helper::NUM_ARGS;
  510. static ReturnType thunk( S32 argc, ConsoleValue *argv, FunctionType fn, const _EngineFunctionDefaultArguments< void(ArgTs...) >& defaultArgs)
  511. {
  512. return _EngineConsoleThunkReturnValue( Helper::dispatchHelper(argc, argv, fn, defaultArgs, SeqType()));
  513. }
  514. template< typename Frame >
  515. static ReturnType thunk( S32 argc, ConsoleValue *argv, MethodType<Frame> fn, Frame* frame, const _EngineFunctionDefaultArguments< void( typename Frame::ObjectType*, ArgTs...) >& defaultArgs)
  516. {
  517. return _EngineConsoleThunkReturnValue( Helper::dispatchHelper(argc, argv, fn, frame, defaultArgs, SeqType()));
  518. }
  519. };
  520. // Have to do a partial specialization for void-returning functions :(
  521. template<S32 startArgc, typename ...ArgTs>
  522. struct _EngineConsoleThunk<startArgc, void(ArgTs...)> {
  523. private:
  524. using Helper = engineAPI::detail::ThunkHelpers<startArgc, void, ArgTs...>;
  525. using SeqType = typename Helper::SeqType;
  526. public:
  527. typedef typename Helper::FunctionType FunctionType;
  528. typedef typename Helper::ReturnType ReturnType;
  529. template<typename Frame> using MethodType = typename Helper::template MethodType<Frame>;
  530. static const S32 NUM_ARGS = Helper::NUM_ARGS;
  531. static void thunk( S32 argc, ConsoleValue *argv, FunctionType fn, const _EngineFunctionDefaultArguments< void(ArgTs...) >& defaultArgs)
  532. {
  533. Helper::dispatchHelper(argc, argv, fn, defaultArgs, SeqType());
  534. }
  535. template< typename Frame >
  536. static void thunk( S32 argc, ConsoleValue *argv, MethodType<Frame> fn, Frame* frame, const _EngineFunctionDefaultArguments< void( typename Frame::ObjectType*, ArgTs...) >& defaultArgs)
  537. {
  538. Helper::dispatchHelper(argc, argv, fn, frame, defaultArgs, SeqType());
  539. }
  540. };
  541. /// @}
  542. /// @name API Definition Macros
  543. ///
  544. /// The macros in this group allow to create engine API functions that work both with the
  545. /// legacy console system as well as with the new engine export system. As such, they only
  546. /// support those function features that are available in both systems. This means that for
  547. /// console-style variadic functions, the ConsoleXXX must be used and that for overloaded
  548. /// and/or C-style variadic functions as well as for placing functions in export scopes,
  549. /// DEFINE_CALLIN must be used directly.
  550. ///
  551. /// When the console system is removed, the console thunking functionality will be removed
  552. /// from these macros but otherwise they will remain unchanged and in place.
  553. ///
  554. /// @{
  555. // Helpers to implement initialization checks. Pulled out into separate macros so this can be deactivated easily.
  556. // Especially important for the initialize() function itself.
  557. #define _CHECK_ENGINE_INITIALIZED_IMPL( fnName, returnType ) \
  558. if( !engineAPI::gIsInitialized ) \
  559. { \
  560. Con::errorf( "EngineAPI: Engine not initialized when calling " #fnName ); \
  561. return EngineTypeTraits< returnType >::ReturnValue( EngineTypeTraits< returnType >::ReturnValueType() ); \
  562. }
  563. #define _CHECK_ENGINE_INITIALIZED( fnName, returnType ) _CHECK_ENGINE_INITIALIZED_IMPL( fnName, returnType )
  564. /// Define a call-in point for calling into the engine.
  565. ///
  566. /// @param name The name of the function as it should be seen by the control layer.
  567. /// @param returnType The value type returned to the control layer.
  568. /// @param args The argument list as it would appear on the function definition
  569. /// @param defaultArgs The list of default argument values.
  570. /// @param usage The usage doc string for the engine API reference.
  571. ///
  572. /// @code
  573. /// DefineEngineFunction( myFunction, int, ( float f, const String& s ), ( "value for s" ), "This is my function." )
  574. /// {
  575. /// return int( f ) + dAtoi( s );
  576. /// }
  577. /// @endcode
  578. #define DefineEngineFunction( name, returnType, args, defaultArgs, usage ) \
  579. static inline returnType _fn ## name ## impl args; \
  580. TORQUE_API EngineTypeTraits< returnType >::ReturnValueType fn ## name \
  581. ( _EngineFunctionTrampoline< returnType args >::Args a ) \
  582. { \
  583. _CHECK_ENGINE_INITIALIZED( name, returnType ); \
  584. return EngineTypeTraits< returnType >::ReturnValue( \
  585. _EngineFunctionTrampoline< returnType args >::jmp( _fn ## name ## impl, a ) \
  586. ); \
  587. } \
  588. static _EngineFunctionDefaultArguments< void args > _fn ## name ## DefaultArgs defaultArgs; \
  589. static EngineFunctionInfo _fn ## name ## FunctionInfo( \
  590. #name, \
  591. &_SCOPE<>()(), \
  592. usage, \
  593. #returnType " " #name #args, \
  594. "fn" #name, \
  595. TYPE< returnType args >(), \
  596. &_fn ## name ## DefaultArgs, \
  597. ( void* ) &fn ## name, \
  598. 0 \
  599. ); \
  600. static _EngineConsoleThunkType< returnType >::ReturnType _ ## name ## caster( SimObject*, S32 argc, ConsoleValue *argv ) \
  601. { \
  602. return _EngineConsoleThunkType< returnType >::ReturnType( _EngineConsoleThunk< 1, returnType args >::thunk( \
  603. argc, argv, &_fn ## name ## impl, _fn ## name ## DefaultArgs \
  604. ) ); \
  605. } \
  606. static ConsoleFunctionHeader _ ## name ## header \
  607. ( #returnType, #args, #defaultArgs ); \
  608. static ConsoleConstructor \
  609. _ ## name ## obj( NULL, #name, _EngineConsoleThunkType< returnType >::CallbackType( _ ## name ## caster ), usage, \
  610. _EngineConsoleThunk< 1, returnType args >::NUM_ARGS - _EngineConsoleThunkCountArgs() defaultArgs, \
  611. _EngineConsoleThunk< 1, returnType args >::NUM_ARGS, \
  612. false, &_ ## name ## header \
  613. ); \
  614. static inline returnType _fn ## name ## impl args
  615. // The next thing is a bit tricky. DefineEngineMethod allows to make the 'object' (=this) argument to the function
  616. // implicit which presents quite an obstacle for the macro internals as the engine export system requires the
  617. // name of a DLL symbol that represents an extern "C" function with an explicit first object pointer argument.
  618. //
  619. // Even if we ignored the fact that we don't have a guarantee how the various C++ compilers implement implicit 'this' arguments,
  620. // we could still not just use a C++ method for this as then we would have to get past the C++ compiler's mangling to
  621. // get to the function symbol name (let alone the fact that typing this method correctly would be tricky).
  622. //
  623. // So, the trick employed here is to package all but the implicit 'this' argument in a structure and then define an
  624. // extern "C" function that takes the object pointer as a first argument and the struct type as the second argument.
  625. // This will result in a function with an identical stack call frame layout to the function we want.
  626. //
  627. // Unfortunately, that still requires that function to chain on to the real user-defined function. To do this
  628. // cleanly and portably, _EngineMethodTrampoline is used to unpack and jump the call from extern "C" into C++ space.
  629. // In optimized builds, the compiler should be smart enough to pretty much optimize all our trickery here away.
  630. #define _DefineMethodTrampoline( className, name, returnType, args ) \
  631. TORQUE_API EngineTypeTraits< returnType >::ReturnValueType \
  632. fn ## className ## _ ## name ( className* object, _EngineMethodTrampoline< _ ## className ## name ## frame, returnType args >::Args a )\
  633. { \
  634. _CHECK_ENGINE_INITIALIZED( className::name, returnType ); \
  635. return EngineTypeTraits< returnType >::ReturnValue( \
  636. _EngineMethodTrampoline< _ ## className ## name ## frame, returnType args >::jmp( object, a ) \
  637. ); \
  638. }
  639. /// Define a call-in point for calling a method on an engine object.
  640. ///
  641. /// @param name The name of the C++ class.
  642. /// @param name The name of the method as it should be seen by the control layer.
  643. /// @param returnType The value type returned to the control layer.
  644. /// @param args The argument list as it would appear on the function definition
  645. /// @param defaultArgs The list of default argument values.
  646. /// @param usage The usage doc string for the engine API reference.
  647. ///
  648. /// @code
  649. /// DefineEngineMethod( MyClass, myMethod, int, ( float f, const String& s ), ( "value for s" ), "This is my method." )
  650. /// {
  651. /// return object->someMethod( f, s );
  652. /// }
  653. /// @endcode
  654. #define DefineEngineMethod( className, name, returnType, args, defaultArgs, usage ) \
  655. struct _ ## className ## name ## frame \
  656. { \
  657. typedef className ObjectType; \
  658. className* object; \
  659. inline returnType _exec args const; \
  660. }; \
  661. _DefineMethodTrampoline( className, name, returnType, args ); \
  662. static _EngineFunctionDefaultArguments< _EngineMethodTrampoline< _ ## className ## name ## frame, void args >::FunctionType > \
  663. _fn ## className ## name ## DefaultArgs defaultArgs; \
  664. static EngineFunctionInfo _fn ## className ## name ## FunctionInfo( \
  665. #name, \
  666. &_SCOPE< className >()(), \
  667. usage, \
  668. "virtual " #returnType " " #name #args, \
  669. "fn" #className "_" #name, \
  670. TYPE< _EngineMethodTrampoline< _ ## className ## name ## frame, returnType args >::FunctionType >(), \
  671. &_fn ## className ## name ## DefaultArgs, \
  672. ( void* ) &fn ## className ## _ ## name, \
  673. 0 \
  674. ); \
  675. static _EngineConsoleThunkType< returnType >::ReturnType _ ## className ## name ## caster( SimObject* object, S32 argc, ConsoleValue *argv ) \
  676. { \
  677. _ ## className ## name ## frame frame; \
  678. frame.object = static_cast< className* >( object ); \
  679. return _EngineConsoleThunkType< returnType >::ReturnType( _EngineConsoleThunk< 2, returnType args >::thunk( \
  680. argc, argv, &_ ## className ## name ## frame::_exec, &frame, _fn ## className ## name ## DefaultArgs \
  681. ) ); \
  682. } \
  683. static ConsoleFunctionHeader _ ## className ## name ## header \
  684. ( #returnType, #args, #defaultArgs ); \
  685. static ConsoleConstructor \
  686. className ## name ## obj( #className, #name, \
  687. _EngineConsoleThunkType< returnType >::CallbackType( _ ## className ## name ## caster ), usage, \
  688. _EngineConsoleThunk< 2, returnType args >::NUM_ARGS - _EngineConsoleThunkCountArgs() defaultArgs, \
  689. _EngineConsoleThunk< 2, returnType args >::NUM_ARGS, \
  690. false, &_ ## className ## name ## header \
  691. ); \
  692. returnType _ ## className ## name ## frame::_exec args const
  693. /// Define a call-in point for calling into the engine. Unlike with DefineEngineFunction, the statically
  694. /// callable function will be confined to the namespace of the given class.
  695. ///
  696. /// @param classname The name of the C++ class (or a registered export scope).
  697. /// @param name The name of the method as it should be seen by the control layer.
  698. /// @param returnType The value type returned to the control layer.
  699. /// @param args The argument list as it would appear on the function definition
  700. /// @param defaultArgs The list of default argument values.
  701. /// @param usage The usage doc string for the engine API reference.
  702. ///
  703. /// @code
  704. /// DefineEngineStaticMethod( MyClass, myMethod, int, ( float f, string s ), ( "value for s" ), "This is my method." )
  705. /// {
  706. /// }
  707. /// @endcode
  708. #define DefineEngineStaticMethod( className, name, returnType, args, defaultArgs, usage ) \
  709. static inline returnType _fn ## className ## name ## impl args; \
  710. TORQUE_API EngineTypeTraits< returnType >::ReturnValueType fn ## className ## _ ## name \
  711. ( _EngineFunctionTrampoline< returnType args >::Args a ) \
  712. { \
  713. _CHECK_ENGINE_INITIALIZED( className::name, returnType ); \
  714. return EngineTypeTraits< returnType >::ReturnValue( \
  715. _EngineFunctionTrampoline< returnType args >::jmp( _fn ## className ## name ## impl, a ) \
  716. ); \
  717. } \
  718. static _EngineFunctionDefaultArguments< void args > _fn ## className ## name ## DefaultArgs defaultArgs; \
  719. static EngineFunctionInfo _fn ## name ## FunctionInfo( \
  720. #name, \
  721. &_SCOPE< className >()(), \
  722. usage, \
  723. #returnType " " #name #args, \
  724. "fn" #className "_" #name, \
  725. TYPE< returnType args >(), \
  726. &_fn ## className ## name ## DefaultArgs, \
  727. ( void* ) &fn ## className ## _ ## name, \
  728. 0 \
  729. ); \
  730. static _EngineConsoleThunkType< returnType >::ReturnType _ ## className ## name ## caster( SimObject*, S32 argc, ConsoleValue *argv )\
  731. { \
  732. return _EngineConsoleThunkType< returnType >::ReturnType( _EngineConsoleThunk< 1, returnType args >::thunk( \
  733. argc, argv, &_fn ## className ## name ## impl, _fn ## className ## name ## DefaultArgs \
  734. ) ); \
  735. } \
  736. static ConsoleFunctionHeader _ ## className ## name ## header \
  737. ( #returnType, #args, #defaultArgs, true ); \
  738. static ConsoleConstructor \
  739. _ ## className ## name ## obj( #className, #name, _EngineConsoleThunkType< returnType >::CallbackType( _ ## className ## name ## caster ), usage, \
  740. _EngineConsoleThunk< 1, returnType args >::NUM_ARGS - _EngineConsoleThunkCountArgs() defaultArgs, \
  741. _EngineConsoleThunk< 1, returnType args >::NUM_ARGS, \
  742. false, &_ ## className ## name ## header \
  743. ); \
  744. static inline returnType _fn ## className ## name ## impl args
  745. # define DefineEngineStringlyVariadicFunction(name,returnType,minArgs,maxArgs,usage) \
  746. static inline returnType _fn ## name ## impl (SimObject *, S32 argc, ConsoleValue *argv); \
  747. TORQUE_API EngineTypeTraits< returnType >::ReturnValueType fn ## name \
  748. (Vector<const char*>* vec) \
  749. { \
  750. _CHECK_ENGINE_INITIALIZED( name, returnType ); \
  751. StringArrayToConsoleValueWrapper args(vec->size(), vec->address()); \
  752. return EngineTypeTraits< returnType >::ReturnValue( \
  753. _fn ## name ## impl(NULL, args.count(), args) \
  754. ); \
  755. } \
  756. static _EngineFunctionDefaultArguments< void (Vector<const char*>* vec) > _fn ## name ## DefaultArgs; \
  757. static EngineFunctionInfo _fn ## name ## FunctionInfo( \
  758. #name, \
  759. &_SCOPE<>()(), \
  760. usage, \
  761. #returnType " " #name "(Vector<String> args)", \
  762. "fn" #name, \
  763. TYPE< returnType (Vector<const char*>* vec) >(), \
  764. &_fn ## name ## DefaultArgs, \
  765. ( void* ) &fn ## name, \
  766. 0 \
  767. ); \
  768. ConsoleConstructor cc_##name##_obj(NULL,#name,_fn ## name ## impl,usage,minArgs,maxArgs); \
  769. returnType _fn ## name ## impl(SimObject *, S32 argc, ConsoleValue *argv)
  770. # define DefineEngineStringlyVariadicMethod(className, name,returnType,minArgs,maxArgs,usage) \
  771. struct _ ## className ## name ## frame \
  772. { \
  773. typedef className ObjectType; \
  774. className* object; \
  775. inline returnType _exec (S32 argc, ConsoleValue* argv) const; \
  776. }; \
  777. TORQUE_API EngineTypeTraits< returnType >::ReturnValueType fn ## className ## _ ## name \
  778. (className* object, Vector<const char*>* vec) \
  779. { \
  780. _CHECK_ENGINE_INITIALIZED( name, returnType ); \
  781. StringArrayToConsoleValueWrapper args(vec->size(), vec->address()); \
  782. _ ## className ## name ## frame frame {}; \
  783. frame.object = static_cast< className* >( object ); \
  784. return EngineTypeTraits< returnType >::ReturnValue( \
  785. frame._exec(args.count(), args) \
  786. ); \
  787. } \
  788. static _EngineFunctionDefaultArguments< void (className* object, S32 argc, const char** argv) > \
  789. _fn ## className ## name ## DefaultArgs; \
  790. static EngineFunctionInfo _fn ## className ## name ## FunctionInfo( \
  791. #name, \
  792. &_SCOPE< className >()(), \
  793. usage, \
  794. "virtual " #returnType " " #name "(Vector<String> args)", \
  795. "fn" #className "_" #name, \
  796. TYPE< _EngineMethodTrampoline< _ ## className ## name ## frame, returnType (Vector<const char*> vec) >::FunctionType >(), \
  797. &_fn ## className ## name ## DefaultArgs, \
  798. ( void* ) &fn ## className ## _ ## name, \
  799. 0 \
  800. ); \
  801. returnType cm_##className##_##name##_caster(SimObject* object, S32 argc, ConsoleValue* argv) { \
  802. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  803. _ ## className ## name ## frame frame {}; \
  804. frame.object = static_cast< className* >( object ); \
  805. conmethod_return_##returnType ) frame._exec(argc,argv); \
  806. }; \
  807. ConsoleConstructor cc_##className##_##name##_obj(#className,#name,cm_##className##_##name##_caster,usage,minArgs,maxArgs); \
  808. inline returnType _ ## className ## name ## frame::_exec(S32 argc, ConsoleValue *argv) const
  809. // The following three macros are only temporary. They allow to define engineAPI functions using the framework
  810. // here in this file while being visible only in the new API. When the console interop is removed, these macros
  811. // can be removed and all their uses be replaced with their corresponding versions that now still include support
  812. // for the console (e.g. DefineNewEngineFunction should become DefineEngineFunction).
  813. #define DefineNewEngineFunction( name, returnType, args, defaultArgs, usage ) \
  814. static inline returnType _fn ## name ## impl args; \
  815. TORQUE_API EngineTypeTraits< returnType >::ReturnValueType fn ## name \
  816. ( _EngineFunctionTrampoline< returnType args >::Args a ) \
  817. { \
  818. _CHECK_ENGINE_INITIALIZED( name, returnType ); \
  819. return EngineTypeTraits< returnType >::ReturnValue( \
  820. _EngineFunctionTrampoline< returnType args >::jmp( _fn ## name ## impl, a ) \
  821. ); \
  822. } \
  823. static _EngineFunctionDefaultArguments< void args > _fn ## name ## DefaultArgs defaultArgs; \
  824. static EngineFunctionInfo _fn ## name ## FunctionInfo( \
  825. #name, \
  826. &_SCOPE<>()(), \
  827. usage, \
  828. #returnType " " #name #args, \
  829. "fn" #name, \
  830. TYPE< returnType args >(), \
  831. &_fn ## name ## DefaultArgs, \
  832. ( void* ) &fn ## name, \
  833. 0 \
  834. ); \
  835. static inline returnType _fn ## name ## impl args
  836. #define DefineNewEngineMethod( className, name, returnType, args, defaultArgs, usage ) \
  837. struct _ ## className ## name ## frame \
  838. { \
  839. typedef className ObjectType; \
  840. className* object; \
  841. inline returnType _exec args const; \
  842. }; \
  843. _DefineMethodTrampoline( className, name, returnType, args ); \
  844. static _EngineFunctionDefaultArguments< _EngineMethodTrampoline< _ ## className ## name ## frame, void args >::FunctionType > \
  845. _fn ## className ## name ## DefaultArgs defaultArgs; \
  846. static EngineFunctionInfo _fn ## className ## name ## FunctionInfo( \
  847. #name, \
  848. &_SCOPE< className >()(), \
  849. usage, \
  850. "virtual " #returnType " " #name #args, \
  851. "fn" #className "_" #name, \
  852. TYPE< _EngineMethodTrampoline< _ ## className ## name ## frame, returnType args >::FunctionType >(), \
  853. &_fn ## className ## name ## DefaultArgs, \
  854. ( void* ) &fn ## className ## _ ## name, \
  855. 0 \
  856. ); \
  857. returnType _ ## className ## name ## frame::_exec args const
  858. #define DefineNewEngineStaticMethod( className, name, returnType, args, defaultArgs, usage ) \
  859. static inline returnType _fn ## className ## name ## impl args; \
  860. TORQUE_API EngineTypeTraits< returnType >::ReturnValueType fn ## className ## _ ## name \
  861. ( _EngineFunctionTrampoline< returnType args >::Args a ) \
  862. { \
  863. _CHECK_ENGINE_INITIALIZED( className::name, returnType ); \
  864. return EngineTypeTraits< returnType >::ReturnValue( \
  865. _EngineFunctionTrampoline< returnType args >::jmp( _fn ## className ## name ## impl, a ) \
  866. ); \
  867. } \
  868. static _EngineFunctionDefaultArguments< void args > _fn ## className ## name ## DefaultArgs defaultArgs; \
  869. static EngineFunctionInfo _fn ## name ## FunctionInfo( \
  870. #name, \
  871. &_SCOPE< className >()(), \
  872. usage, \
  873. #returnType " " #name #args, \
  874. "fn" #className "_" #name, \
  875. TYPE< returnType args >(), \
  876. &_fn ## className ## name ## DefaultArgs, \
  877. ( void* ) &fn ## className ## _ ## name, \
  878. 0 \
  879. ); \
  880. static inline returnType _fn ## className ## name ## impl args
  881. /// @}
  882. //=============================================================================
  883. // Callbacks.
  884. //=============================================================================
  885. /// Matching implement for DECLARE_CALLBACK.
  886. ///
  887. ///
  888. /// @warn With the new interop system, method-style callbacks <em>must not</em> be triggered on object
  889. /// that are being created! This is because the control layer will likely not yet have a fully valid wrapper
  890. /// object in place for the EngineObject under construction.
  891. #define IMPLEMENT_CALLBACK( class, name, returnType, args, argNames, usageString ) \
  892. struct _ ## class ## name ## frame { typedef class ObjectType; }; \
  893. TORQUE_API _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType* cb ## class ## _ ## name; \
  894. TORQUE_API void set_cb ## class ## _ ## name( \
  895. _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType fn ) \
  896. { cb ## class ## _ ## name = fn; } \
  897. _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType* cb ## class ## _ ## name; \
  898. namespace { \
  899. ::EngineFunctionInfo _cb ## class ## name( \
  900. #name, \
  901. &::_SCOPE< class >()(), \
  902. usageString, \
  903. "virtual " #returnType " " #name #args, \
  904. "cb" #class "_" #name, \
  905. ::TYPE< _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType >(), \
  906. NULL, \
  907. ( void* ) &cb ## class ## _ ## name, \
  908. EngineFunctionCallout \
  909. ); \
  910. } \
  911. returnType class::name ## _callback args \
  912. { \
  913. if( cb ## class ## _ ## name ) { \
  914. _EngineCallbackHelper cbh( this, reinterpret_cast< const void* >( cb ## class ## _ ## name ) ); \
  915. return returnType( cbh.call< returnType > argNames ); \
  916. } \
  917. if( engineAPI::gUseConsoleInterop ) \
  918. { \
  919. static StringTableEntry sName = StringTable->insert( #name ); \
  920. _EngineConsoleCallbackHelper cbh( sName, this ); \
  921. return returnType( cbh.call< returnType > argNames ); \
  922. } \
  923. return returnType(); \
  924. } \
  925. namespace { \
  926. ConsoleFunctionHeader _ ## class ## name ## header( \
  927. #returnType, #args, "" ); \
  928. ConsoleConstructor _ ## class ## name ## obj( #class, #name, usageString, &_ ## class ## name ## header ); \
  929. }
  930. /// Used to define global callbacks not associated with
  931. /// any particular class or namespace.
  932. #define IMPLEMENT_GLOBAL_CALLBACK( name, returnType, args, argNames, usageString ) \
  933. DEFINE_CALLOUT( cb ## name, name,, returnType, args, 0, usageString ); \
  934. returnType name ## _callback args \
  935. { \
  936. if( cb ## name ) \
  937. return returnType( cb ## name argNames ); \
  938. if( engineAPI::gUseConsoleInterop ) \
  939. { \
  940. static StringTableEntry sName = StringTable->insert( #name ); \
  941. _EngineConsoleCallbackHelper cbh( sName, NULL ); \
  942. return returnType( cbh.call< returnType > argNames ); \
  943. } \
  944. return returnType(); \
  945. } \
  946. namespace { \
  947. ConsoleFunctionHeader _ ## name ## header( \
  948. #returnType, #args, "" ); \
  949. ConsoleConstructor _ ## name ## obj( NULL, #name, usageString, &_ ## name ## header ); \
  950. }
  951. // Again, temporary macros to allow splicing the API while we still have the console interop around.
  952. #define IMPLEMENT_CONSOLE_CALLBACK( class, name, returnType, args, argNames, usageString ) \
  953. returnType class::name ## _callback args \
  954. { \
  955. if( engineAPI::gUseConsoleInterop ) \
  956. { \
  957. static StringTableEntry sName = StringTable->insert( #name ); \
  958. _EngineConsoleCallbackHelper cbh( sName, this ); \
  959. return returnType( cbh.call< returnType > argNames ); \
  960. } \
  961. return returnType(); \
  962. } \
  963. namespace { \
  964. ConsoleFunctionHeader _ ## class ## name ## header( \
  965. #returnType, #args, "" ); \
  966. ConsoleConstructor _ ## class ## name ## obj( #class, #name, usageString, &_ ## class ## name ## header ); \
  967. }
  968. #define IMPLEMENT_NEW_CALLBACK( class, name, returnType, args, argNames, usageString ) \
  969. struct _ ## class ## name ## frame { typedef class ObjectType; }; \
  970. TORQUE_API _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType* cb ## class ## _ ## name; \
  971. TORQUE_API void set_cb ## class ## _ ## name( \
  972. _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType fn ) \
  973. { cb ## class ## _ ## name = fn; } \
  974. _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType* cb ## class ## _ ## name; \
  975. namespace { \
  976. ::EngineFunctionInfo _cb ## class ## name( \
  977. #name, \
  978. &::_SCOPE< class >()(), \
  979. usageString, \
  980. "virtual " #returnType " " #name #args, \
  981. "cb" #class "_" #name, \
  982. ::TYPE< _EngineMethodTrampoline< _ ## class ## name ## frame, returnType args >::FunctionType >(), \
  983. NULL, \
  984. &cb ## class ## _ ## name, \
  985. EngineFunctionCallout \
  986. ); \
  987. } \
  988. returnType class::name ## _callback args \
  989. { \
  990. if( cb ## class ## _ ## name ) { \
  991. _EngineCallbackHelper cbh( this, reinterpret_cast< const void* >( cb ## class ## _ ## name ) ); \
  992. return returnType( cbh.call< returnType > argNames ); \
  993. } \
  994. return returnType(); \
  995. }
  996. // Internal helper class for doing call-outs in the new interop.
  997. struct _EngineCallbackHelper
  998. {
  999. protected:
  1000. EngineObject* mThis;
  1001. const void* mFn;
  1002. public:
  1003. _EngineCallbackHelper( EngineObject* pThis, const void* fn )
  1004. : mThis( pThis ),
  1005. mFn( fn ) {}
  1006. template< typename R, typename ...ArgTs >
  1007. R call(ArgTs ...args) const
  1008. {
  1009. typedef R( FunctionType )( EngineObject*, ArgTs... );
  1010. return R( reinterpret_cast< FunctionType* >( const_cast<void*>(mFn) )( mThis, args... ) );
  1011. }
  1012. };
  1013. #include "console/stringStack.h"
  1014. // Internal helper for callback support in legacy console system.
  1015. struct _BaseEngineConsoleCallbackHelper
  1016. {
  1017. public:
  1018. /// Matches up to storeArgs.
  1019. static const U32 MAX_ARGUMENTS = 11;
  1020. SimObject* mThis;
  1021. S32 mInitialArgc;
  1022. S32 mArgc;
  1023. StringTableEntry mCallbackName;
  1024. ConsoleValue mArgv[ MAX_ARGUMENTS + 2 ];
  1025. ConsoleValue _exec();
  1026. ConsoleValue _execLater(SimConsoleThreadExecEvent *evt);
  1027. _BaseEngineConsoleCallbackHelper(): mThis(NULL), mInitialArgc(0), mArgc(0), mCallbackName(StringTable->EmptyString()){;}
  1028. };
  1029. // Base helper for console callbacks
  1030. struct _EngineConsoleCallbackHelper : public _BaseEngineConsoleCallbackHelper
  1031. {
  1032. private:
  1033. using Helper = engineAPI::detail::MarshallHelpers<ConsoleValue>;
  1034. public:
  1035. _EngineConsoleCallbackHelper( StringTableEntry callbackName, SimObject* pThis )
  1036. {
  1037. mThis = pThis;
  1038. mArgc = mInitialArgc = pThis ? 2 : 1 ;
  1039. mCallbackName = callbackName;
  1040. }
  1041. template< typename R, typename ...ArgTs >
  1042. R call(ArgTs ...args)
  1043. {
  1044. if (Con::isMainThread())
  1045. {
  1046. ConsoleStackFrameSaver sav; sav.save();
  1047. mArgv[ 0 ].setStringTableEntry(mCallbackName);
  1048. Helper::marshallEach(mArgc, mArgv, args...);
  1049. return R( EngineUnmarshallData< R >()( _exec() ) );
  1050. }
  1051. else
  1052. {
  1053. SimConsoleThreadExecCallback cb;
  1054. SimConsoleThreadExecEvent *evt = new SimConsoleThreadExecEvent(mArgc + sizeof...(ArgTs), NULL, false, &cb);
  1055. evt->populateArgs(mArgv);
  1056. mArgv[ 0 ].setStringTableEntry(mCallbackName);
  1057. Helper::marshallEach(mArgc, mArgv, args...);
  1058. Sim::postEvent((SimObject*)Sim::getRootGroup(), evt, Sim::getCurrentTime());
  1059. return R( EngineUnmarshallData< R >()( cb.waitForResult() ) );
  1060. }
  1061. }
  1062. };
  1063. // Override for when first parameter is presumably a SimObject*, in which case A will be absorbed as the callback
  1064. template<typename P1> struct _EngineConsoleExecCallbackHelper : public _BaseEngineConsoleCallbackHelper
  1065. {
  1066. private:
  1067. using Helper = engineAPI::detail::MarshallHelpers<ConsoleValue>;
  1068. public:
  1069. _EngineConsoleExecCallbackHelper( SimObject* pThis )
  1070. {
  1071. mThis = pThis;
  1072. mArgc = mInitialArgc = 2;
  1073. mCallbackName = NULL;
  1074. }
  1075. template< typename R, typename SCB, typename ...ArgTs >
  1076. R call( SCB simCB , ArgTs ...args )
  1077. {
  1078. if (Con::isMainThread())
  1079. {
  1080. ConsoleStackFrameSaver sav; sav.save();
  1081. mArgv[ 0 ].setString(simCB);
  1082. Helper::marshallEach(mArgc, mArgv, args...);
  1083. return R( EngineUnmarshallData< R >()( _exec() ) );
  1084. }
  1085. else
  1086. {
  1087. SimConsoleThreadExecCallback cb;
  1088. SimConsoleThreadExecEvent *evt = new SimConsoleThreadExecEvent(mArgc+sizeof...(ArgTs), NULL, true, &cb);
  1089. evt->populateArgs(mArgv);
  1090. mArgv[ 0 ].setString(simCB);
  1091. Helper::marshallEach(mArgc, mArgv, args...);
  1092. Sim::postEvent(mThis, evt, Sim::getCurrentTime());
  1093. return R( EngineUnmarshallData< R >()( cb.waitForResult() ) );
  1094. }
  1095. }
  1096. };
  1097. // Override for when first parameter is const char*
  1098. template<> struct _EngineConsoleExecCallbackHelper<const char*> : public _BaseEngineConsoleCallbackHelper
  1099. {
  1100. private:
  1101. using Helper = engineAPI::detail::MarshallHelpers<ConsoleValue>;
  1102. public:
  1103. _EngineConsoleExecCallbackHelper( const char *callbackName )
  1104. {
  1105. mThis = NULL;
  1106. mArgc = mInitialArgc = 1;
  1107. mCallbackName = StringTable->insert(callbackName);
  1108. }
  1109. template< typename R, typename ...ArgTs >
  1110. R call(ArgTs ...args)
  1111. {
  1112. if (Con::isMainThread())
  1113. {
  1114. ConsoleStackFrameSaver sav; sav.save();
  1115. mArgv[ 0 ].setStringTableEntry(mCallbackName);
  1116. Helper::marshallEach(mArgc, mArgv, args...);
  1117. return R( EngineUnmarshallData< R >()( _exec() ) );
  1118. }
  1119. else
  1120. {
  1121. SimConsoleThreadExecCallback cb;
  1122. SimConsoleThreadExecEvent *evt = new SimConsoleThreadExecEvent(mArgc+sizeof...(ArgTs), NULL, false, &cb);
  1123. evt->populateArgs(mArgv);
  1124. mArgv[ 0 ].setStringTableEntry(mCallbackName);
  1125. Helper::marshallEach(mArgc, mArgv, args...);
  1126. Sim::postEvent((SimObject*)Sim::getRootGroup(), evt, Sim::getCurrentTime());
  1127. return R( EngineUnmarshallData< R >()( cb.waitForResult() ) );
  1128. }
  1129. }
  1130. };
  1131. // Re-enable some VC warnings we disabled for this file.
  1132. #pragma warning( pop ) // 4510 and 4610
  1133. #endif // !_ENGINEAPI_H_