engineAPI.h 77 KB

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