Nasal/CppBind: Difference between revisions

Jump to navigation Jump to search
2,633 bytes added ,  13 November 2013
m
Line 192: Line 192:
# print it again
# print it again
print( obj.value );
print( obj.value );
</syntaxhighlight>
== Exposing HLA to Nasal ==
Here's another stub, this time exposing the simgear::HLAFederate class and a handful of its methods to Nasal space as a Nasal Ghost. Note that the following snippet assumes, that:
* You built & installed OpenRTI (see [[HLA]]
* you built & installed SimGear with -DENABLE_RTI=ON
* you built FlightGear with -DENABLE_RTI=ON
In and of itself, this snippet isn't yet particularly useful, especially because we don't want HLA federates to be running inside the main process, but the same HLA bindings could obviously also be used by a standalone Nasal interpreter at some point. So this just serves as an example.
<syntaxhighlight lang="cpp">
// $FG_SRC/Scripting/NasalHLA.cxx
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include <Main/globals.hxx>
#include <Main/util.hxx>
#include <simgear/nasal/cppbind/from_nasal.hxx>
#include <simgear/nasal/cppbind/to_nasal.hxx>
#include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/nasal/cppbind/Ghost.hxx>
#include <simgear/hla/HLAFederate.hxx>
typedef boost::shared_ptr<simgear::HLAFederate> HLAFederate_ptr;
typedef nasal::Ghost< HLAFederate_ptr > NasalHLAFederate;
naRef to_nasal_helper(naContext c, simgear::HLAFederate* obj)
{
HLAFederate_ptr ptr(obj);
return NasalHLAFederate::create(c, ptr);
}
simgear::HLAFederate*
from_nasal_helper(naContext c, naRef ref, const simgear::HLAFederate*)
{
return (simgear::HLAFederate*) naGhost_ptr(ref);
}
typedef boost::shared_ptr<simgear::HLAObjectClass> HLAObjectClass_ptr;
typedef nasal::Ghost< HLAObjectClass_ptr > NasalHLAObjectClass;
naRef to_nasal_helper(naContext c, simgear::HLAObjectClass* obj)
{
HLAObjectClass_ptr ptr(obj);
return NasalHLAObjectClass::create(c, ptr);
}
simgear::HLAObjectClass*
from_nasal_helper(naContext c, naRef ref, const simgear::HLAObjectClass*)
{
return (simgear::HLAObjectClass*) naGhost_ptr(ref);
}
static naRef f_new_federate(const nasal::CallContext& ctx)
{
 
  return ctx.to_nasal( new  simgear::HLAFederate );
}
naRef initNasalDemo(naRef globals, naContext c)
{
    nasal::Hash globals_module(globals, c);
  using namespace simgear;
  NasalHLAFederate::init("hla.federate")
.method("init", &HLAFederate::init)
.method("update", &HLAFederate::update)
.method("shutdown", &HLAFederate::shutdown)
.method("createObjectClass", &HLAFederate::createObjectClass)
.method("setFederateType", &HLAFederate::setFederateType)
.method("setFederationExecutionName", &HLAFederate::setFederationExecutionName);
  nasal::Hash hla = globals_module.createHash("hla");
  hla.set("new", &f_new_federate);
  return naNil();
}


</syntaxhighlight>
</syntaxhighlight>
Line 205: Line 289:
* popFront()
* popFront()
* popBack()
* popBack()


== Exposing C++ Classes ==
== Exposing C++ Classes ==

Navigation menu