engineAPI.h 78 KB

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