Previous: Your first Flusspferd program
Let's have a bird's-eye-view look at the basic value types supported by Flusspferd. We shall create a few values of different types to get the hang of it and we will also assert some basic facts about the values.
#include <flusspferd.hpp> #include <cassert> int main() { flusspferd::current_context_scope context_scope( flusspferd::context::create()); // See below. You can ignore this line for now. flusspferd::local_root_scope root_scope; // undefined flusspferd::value v1; assert(v1.is_undefined()); // boolean (true) flusspferd::value v2(true); assert(v2.is_boolean()); assert(v2.get_boolean()); // integer (1) flusspferd::value v3(1); assert(v3.is_int()); assert(v3.is_number()); assert(v3.get_int() == 1); // double (1.1) flusspferd::value v4(1.1); assert(v4.is_double()); assert(v4.is_number()); assert(v4.get_double() == 1.1); // object (null) flusspferd::object o1; assert(o1.is_null()); flusspferd::value v5(o1); assert(v5.is_object()); assert(v5.is_null()); // another object (the global object) flusspferd::object o2 = flusspferd::global(); assert(!o2.is_null()); // string (empty) flusspferd::string s; // <- valid (!), empty string assert(s.empty()); flusspferd::value v6(s); assert(v6.is_string()); }
help/examples/values1.cpp.Let us translate this simple Javascript code to a C++ equivalent:
// Create a simple object let x = {} // Set a property x.property = 1234 // And another - same value x.property2 = x.property // Iterate for (let i in x) { if (x.hasOwnProperty(i)) { print(i + ': ' + x[i]); } } // Uneval print(x.toSource()); // Check if 'toSource' is enumerable print(x.propertyIsEnumerable('toSource'));
Translated to C++:
#include <flusspferd.hpp> #include <iostream> #include <ostream> int main() { flusspferd::current_context_scope context_scope( flusspferd::context::create()); // Create a simple object flusspferd::root_object x(flusspferd::create<flusspferd::object>()); // Set a property x.set_property("property", 1234); // Add another - same value flusspferd::value tmp = x.get_property("property"); x.set_property("property2", tmp); // Iterate for (flusspferd::property_iterator it = x.begin(); it != x.end(); ++it) { if (x.has_own_property(*it)) { std::cout << *it << ": " << x.get_property(*it) << std::endl; } } // Uneval std::cout << x.call("toSource") << std::endl; // Check if 'toSource' is enumerable // (Note that there is another way to do it (see flusspferd::property::get_attributes), // but we call the method here to show how methods can be called.) std::cout << x.call("propertyIsEnumerable", "toSource") << std::endl; }
help/examples/simple_objects.js and simple_objects.cpp.The class flusspferd::object (together with the other classes in Property types) provides many methods for creating, accessing, querying and manipulating properties. We will briefly look at some of them here.
For example, you can easily create a non-enumerable, read-only property:
obj.define_property("prop", initial_value, flusspferd::object::dont_enumerate | flusspferd::object::read_only_property);
Or, a property with a custom getter and setter:
flusspferd::property_attributes attr; attr.getter = getter_function; attr.setter = setter_function; obj.define_property("prop", initial_value, attr);
getter_function and setter_function have to be of type flusspferd::function, which you can create with one of these functions.And, among many other things, you can delete properties altogether:
obj.delete_property("prop");
Next: Garbage collection
Contact us at team -AT- flusspferd -DOT- org or as described on our homepage.
Generated on Fri Jul 30 02:05:02 2010 for Flusspferd by
1.6.1