/*************************************************************************** * Copyright (C) 2004 by Rick L. Vinyard, Jr. * * rvinyard@cs.nmsu.edu * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation version 2.1. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include <idioskopos.h> #include <iostream> using namespace Idioskopos; using namespace std; #include "print_obj.h" // This is an introspectable class and inherits from Introspectable class Simple: public Introspectable { public: // Declare this as an introspectable object. // Macro creates a minimal set of methods that are necessary. IDIOSKOPOS_OBJECT(Simple); Simple(const string& name) : Introspectable(name), pi( "pi", 3.14159, *this ) // Not mandatory, but easier to configure the property in the initialization list. {} // Declare the member property variable Property<double> pi; }; int main() { double d; int i; // Create an instance of the introspectable class Simple // We'll also give it a name Simple simple("MySimple"); // This calls a utility function in print_obj.h that iterates through all the // properties of an introspectable object. print( simple ); // The property can be used in assignment as any double would be d = simple.pi; cout << "d = simple.pi = " << d << endl << endl; // The property template supports implicit casts to the contained // type, so we can take advantage of implicit casts from the // contained type to other types. There is no cast from // Property<double> to int, but we do have a cast from // Property<double> to double, and C/C++ provides us with a default // cast from double to int. Thus, in assignment we end up with // the same truncation as seen when implicitly casting a double to // an int. i = simple.pi; cout << "i = simple.pi = " << i << endl << endl; // Let's change the value of d for the next example. d *= 2.0; cout << "d *= 2.0 = " << d << endl << endl; // We can also assign to the property from a double... simple.pi = d; // Change the introspection name at run-time... simple.pi.set_name( "two_pi" ); print( simple ); // Use the property in expressions... d = simple.pi / 2.0; cout << "d = simple.pi / 2.0 = " << d << endl << endl; // Even in arithmetic assignment... simple.pi *= 2.0; simple.pi.set_name( "four_pi" ); print(simple); return 0; }