After quite some hacking and hours of fiddling around I got this to run: //////////////////////////// var s = ("Some", "Strings","to","be","sorted");
var vs = varray[string] s; println "Hello"; println s; println vs; // Given a Felix comparator, we need to make an STL compliant one // This is very hard because the felix one takes a tuple // and the C++ one requires separate arguments header """ template<class CT, class FT2, class FFT> struct comparator { FFT cmp; comparator() : cmp(0) {} comparator(FFT cmp_a) : cmp(cmp_a) {} bool operator ()(CT x, CT y){ pair<CT,CT> z(x,y); return cmp->apply(*(FT2*)(void*)&z); } }; """ requires header "#include <utility>"; type _comparator[CT,FT2,FFT] = "comparator<?1,?2,?3>"; typedef comparator[T] = _comparator[T,T*T,T*T->bool]; fun _make_comparator[CT,FT2,FFT]: FFT -> comparator[CT] = "comparator<?1,?2,?3>($1)"; fun make_comparator[T](cmp:T * T -> bool) => _make_comparator[T, T*T, T*T->bool] (cmp); // invoke stl sort proc _sort[T]: comparator[T] * &T * &T = "std::sort($2, $3, $1);" requires header "#include <algorithm>"; // in place sort of varray using STL sort proc sort[t] (cmp: t * t -> bool) (a:varray[t]) { var first = a.stl_begin; var last = a.stl_end; println$ "Array from " + str (C_hack::_ctor_address first) + " to " + str (C_hack::_ctor_address last); var z: comparator[t] = make_comparator[t] (cmp); _sort[t] (z,first,last); } sort (eq of (string * string)) (vs); println vs; ///////////////////////////// This is a prototype for the library function Varray::sort. Imagine my dismay when it printed: Hello ('Some', 'Strings', 'to', 'be', 'sorted') varray('Some', 'Strings', 'to', 'be', 'sorted') Array from 1049472 to 1049512 varray('Some', 'Strings', 'to', 'be', 'sorted') Like, everything worked but it didn't sort the array! Anyone see the bug? [Hint: there's nothing wrong with the binding code .. :] BTW: most of the messing about you see is because Felix functions are C++ classes with an apply() method taking a single argument, which in this case is a tuple. Since I just happen to know this is layout compatible with any struct with the same member types, I used the STL pair<> template in C++ to make a pair and used a cast: pair<CT,CT> z(x,y); return cmp->apply(*(FT2*)(void*)&z); to call the Felix function. The variable z is there just so we can take its address so we can "reinterpret" it via a void*, since (stupidly) in C++ you can't take the address of an rvalue. A reinterpret<> cast can't do that either. [Yes, it is stupid because there is a one line workaround, which is used in Felix, look at reinterpret<> in http://184.106.135.43:1234/lib/rtl/flx_meta.hpp Oh, BTW, if you're wonder why I'm working on a sort, well look at: http://184.106.135.43:1234/lib/rtl Yeah. In OSX that directory listing is alphabetical, on Ubuntu 10.5LTS it isn't. So I'magunnasortitforya. -- john skaller skal...@users.sourceforge.net ------------------------------------------------------------------------------ The Next 800 Companies to Lead America's Growth: New Video Whitepaper David G. Thomson, author of the best-selling book "Blueprint to a Billion" shares his insights and actions to help propel your business during the next growth cycle. Listen Now! http://p.sf.net/sfu/SAP-dev2dev _______________________________________________ Felix-language mailing list Felix-language@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/felix-language