//.............................................................................. // // This file is part of the Jancy toolkit. // // Jancy is distributed under the MIT license. // For details see accompanying license.txt file, // the public copy of which is also available at: // http://tibbo.com/downloads/archive/jancy/license.txt // //.............................................................................. // Properties are present in many modern languages. Property pointers, on the // other hand, require well-developed syntax and semantics of pointer // declarations and operators, and these are usually lacking. Jancy is proud // to be one of the first languages with full support for property pointers. // Property pointers resemble and are closely related to function pointers. // Unlike function pointers, however, dealing with property pointers requires // a careful application of address '&' and indirection '*' operators. This is // due to the implicit invocation of property accessors and the ambiguity // induced by this invocation (this ambiguity can't be automatically resolved). //.............................................................................. // simple autoget property int autoget property g_foo; // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // accessor implementation g_foo.set (int x) { printf ("g_foo.set (%d)\n", x); m_value = x; } //.............................................................................. // simple indexed property int property g_baz ( size_t i, size_t j ); // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // accessors implementation int g_baz.get ( size_t i, size_t j ) { printf ("g_baz.get (%d, %d)\n", i, j); return g_bazTable [i] [j]; } g_baz.set ( size_t i, size_t j, int x ) { printf ("g_baz.set (%d, %d, %d)\n", i, j, x); g_bazTable [i] [j] = x; } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // global variable to store the property value int g_bazTable [2] [2]; //.............................................................................. // a class containing a property (which might look simple but is, in fact, // indexed because of the implicit 'this' argument) class C1 { int autoget property m_bar; m_bar.set (int x) { printf ("C1.m_bar.set (%d)\n", x); m_value = x; } } //.............................................................................. // entry point int main () { printf ("main ()\n"); // thin property pointer is a pointer to the property accessor table int property thin* p1 = &g_foo; *p1 = 10; // like with function pointers, the argument conversion is automated -- // the compiler will generate thunks when needed typedef double property FpProp; FpProp thin* p2 = (FpProp thin*) &g_foo; *p2 = 15.8; // like with function pointers, the property pointers can capture values of // index arguments in the closure C1 c; // in this case the pointer to 'c' will be captured int property* p3 = &c.m_bar; *p3 = 200; // use an index operator to capture index arguments in the closure, thus // reducing dimensions of an indexed property or completely de-indexing it int property* p4 = &(g_baz [0] [1]); *p4 = 300; // like with function pointers, it's OK to skip indexes when capturing int indexed property* p5 (size_t) = &(g_baz [] [0]); (*p5) [1] = 400; return 0; } //..............................................................................