X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FScripting%2FNasalPositioned_cppbind.cxx;h=7ef63d02c5f7aea29c6241d094cb131c87e8e57c;hb=9c63b7713307eb4db9f6df7959fe83977f561c42;hp=cc3568c4b67f8ee16984e675774f12ea3b15da28;hpb=881df711ba5be8a8c0bc65f2126a0dcb63865df9;p=flightgear.git diff --git a/src/Scripting/NasalPositioned_cppbind.cxx b/src/Scripting/NasalPositioned_cppbind.cxx index cc3568c4b..7ef63d02c 100644 --- a/src/Scripting/NasalPositioned_cppbind.cxx +++ b/src/Scripting/NasalPositioned_cppbind.cxx @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -77,7 +78,7 @@ naRef to_nasal_helper(naContext c, flightgear::Approach* iap) } //------------------------------------------------------------------------------ -static naRef f_navaid_course(naContext, FGNavRecord& nav) +static naRef f_navaid_course(FGNavRecord& nav, naContext) { if( !( nav.type() == FGPositioned::ILS || nav.type() == FGPositioned::LOC @@ -130,12 +131,12 @@ static naRef f_airport_comms(FGAirport& apt, const nasal::CallContext& ctx) } //------------------------------------------------------------------------------ -FGRunway* runwayFromNasalArg( const FGAirport& apt, +FGRunwayRef runwayFromNasalArg( const FGAirport& apt, const nasal::CallContext& ctx, size_t index = 0 ) { if( index >= ctx.argc ) - return NULL; + return FGRunwayRef(); try { @@ -144,7 +145,7 @@ FGRunway* runwayFromNasalArg( const FGAirport& apt, { if( !apt.hasRunwayWithIdent(ident) ) // TODO warning/exception? - return NULL; + return FGRunwayRef(); return apt.getRunwayByIdent(ident); } @@ -153,7 +154,7 @@ FGRunway* runwayFromNasalArg( const FGAirport& apt, {} // TODO warn/error if no runway? - return NasalRunway::fromNasal(ctx.c, ctx.args[index]); + return ctx.from_nasal(ctx.args[index]); } //------------------------------------------------------------------------------ @@ -244,8 +245,8 @@ static bool extractGeod(nasal::CallContext& ctx, SGGeod& result) if( ctx.isGhost(0) ) { - FGPositioned* pos = - NasalPositioned::fromNasal(ctx.c, ctx.requireArg(0)); + FGPositionedRef pos = + ctx.from_nasal(ctx.requireArg(0)); if( pos ) { @@ -398,7 +399,7 @@ static naRef f_findWithinRange(nasal::CallContext ctx) std::string typeSpec = ctx.getArg(1); FGPositioned::TypeFilter filter(FGPositioned::TypeFilter::fromString(typeSpec)); - + FGPositionedList items = FGPositioned::findWithinRange(pos, range_nm, &filter); FGPositioned::sortByRange(items, pos); return ctx.to_nasal(items); @@ -456,11 +457,42 @@ static naRef f_sortByRange(nasal::CallContext ctx) return ctx.to_nasal(items); } +//------------------------------------------------------------------------------ +// Get difference between two lists of positioned objects. +// +// For every element in old_list not in new_list the callback cb_remove is +// called with the removed element as single argument. For every element in +// new_list not in old_list cb_add is called. +// +// diff(old_list, new_list, cb_add[, cb_remove]) +// +// example: +// # Print all fixes within a distance of 320 to 640 miles +// diff( findWithinRange(320, "fix"), +// findWithinRange(640, "fix"), +// func(p) print('found fix: ', p.id) ); +static naRef f_diff(nasal::CallContext ctx) +{ + typedef simgear::ListDiff Diff; + Diff::List old_list = ctx.requireArg(0), + new_list = ctx.requireArg(1); + Diff::Callback cb_add = ctx.requireArg(2), + cb_rm = ctx.getArg(3); + + // Note that FGPositionedRef instances are only compared for pointer equality. + // As the NavCache caches every queried positioned instance it is guaranteed + // that only one instance of every positioned object can exist. Therefore we + // can make the comparison faster by just comparing pointers and not also the + // guid. + // (On my machine the difference is 0.27s vs 0.17s) + Diff::inplace(old_list, new_list, cb_add, cb_rm); + + return naNil(); +} + //------------------------------------------------------------------------------ naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c) { - if (!NasalPositioned::isInit()) { - NasalPositioned::init("Positioned") .member("id", &FGPositioned::ident) .member("ident", &FGPositioned::ident) // TODO to we really need id and ident? @@ -504,7 +536,6 @@ naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c) .method("getStar", &FGAirport::findSTARWithIdent) .method("getIAP", &FGAirport::findApproachWithIdent) .method("tostring", &FGAirport::toString); - } nasal::Hash globals(globalsRef, c), positioned( globals.createHash("positioned") ); @@ -520,5 +551,7 @@ naRef initNasalPositioned_cppbind(naRef globalsRef, naContext c) positioned.set("courseAndDistance", &f_courseAndDistance); positioned.set("sortByRange", &f_sortByRange); + positioned.set("diff", &f_diff); + return naNil(); }