pseudoPODs
Smart Class Members That Masquerade as POD Members
Copyright © 2010 David L. Mowery
all rights reserved
Summary:
pseudoPODs add properties to C++, which is a way of invisibly calling get() & set() functions. Ordinarily someone who wants to use a member of a class that is POD (Plain Old Data), such as an integer, can simply read or write the POD using statements like:
x = fred.int1; |
or |
fred.int1=37; |
If, however, the class has to execute some code to find out what the current value of the member is, or change it, then anyone who wants to read or write the member must instead use function calls, such as:
x = fred.get_int1(); |
or |
fred.set_int1(37); |
pseudoPODs hide that difference by adding code to the class that implicitly converts what appear to be simple POD reads and writes to the smarter get() & set() calls, thereby simplifying the class' user interface, and reducing the cost of using and maintaining the class.
pseudoPODs:
improve code readability, e.g.++fred.int1; is easier to figure out than fred.set_int1(fred.get_int1()+1);
provide, for existing or legacy code, a way of changing a class member from POD to get() & set() function calls without changing the code outside the class, so that old.member = 37; indirectly calls function old.set_member(37);
improve modularity & abstraction by hiding whether the member is POD or something smarter that executes get() & set() code, which ideally should be an invisible internal implementation detail buried inside the class.
are ready to use - the pseudoPOD code supports all 13 assignment operators, pointers, arrays & chaining.
are easy to code - they require 1 additional line to include the .h file, plus 1 additional line per member, and rely on the familiar get() & set() functions, so the coder doesn't have to learn new coding techniques, nor anything new about how C++ works.
use ordinary C++ code, so should work with all compilers (I've tested the code with gcc, MSVC, and IBM Rational Purity).
consume slightly more memory and execution time than get() & set() function calls (one additional pointer per pseudoPOD member, and the code must load the pointer once per call).
include 2000 lines of test code - not enough to explore all the multifarious possibilities C++ offers, but enough to give both reader and author some confidence that pseudoPODs work well in practice.
have both macro() and template() versions.