engineAPI.h 77 KB

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