25.3TheGem 401
There are four related programs:
■ A test program, which exercises the API mechanisms one at a time to let you
easily verify the functionality of the different pieces.
■ A server program, which lets you host a simple chat server (from the com-
mand line).
■ A client program, which lets you connect to a running chat server and ex-
change chat messages with other users (again, from the command line).
■ An editor program, which lets you edit the list of users used by the server
program, using the introspection mechanisms outlined in this chapter.
These are baked into two executables: the
introspection test program, to verify
that the API works as intended, and the
simplechat program, to act as a client,
server, or user list editor for the simple chat system.
To build the programs, either use the included solution and project files for
Microsoft Visual Studio 2010 (tested on Windows 7) or use the included GNU
make file for GCC (tested on Ubuntu Linux 10.04). Run the sample programs
from the command line.
25.3TheGem
The implementation of this gem lives in the files introspection.h, intro-
spection.cpp
, and protocol.cpp. While the implementation makes use of type
inference and other template metaprogramming tricks, the API from a user’s per-
spective is designed to be simple.
Decorate each data structure you want to introspect using an
INTROSPEC-
TION()
macro, as illustrated by the example shown in Listing 25.3. This macro
takes two arguments: the name of the data structure itself and a list of members
that you want to introspect. Each introspection member is described using a sepa-
rate macro that lists its name and a description of the role of the member. This
can be as simple as a plain C string (to display in an editor interface, for exam-
ple) or as complex as a rules-enforcing and network-encoding object that makes
sure floating-point values are within a given range and transmitted using a speci-
fied number of bits and other variant behavior.
While you still have to list each member twice, once for declaring it as an
actual member and once for declaring it introspectable, this mechanism cuts
down on the amount of boilerplate code you need to write. It also keeps the dec-
laration of introspection in the header, right near the declaration of the members,
to reduce the likelihood that someone adds a member without adding the appro-