Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <introspection/introspection.h>
00030 #include <boost/lexical_cast.hpp>
00031
00032 using namespace cpp_introspection;
00033
00034 void print_introspection(MessagePtr message, const std::string& prefix = "");
00035
00036 int main(int argc, char **argv) {
00037 if (argc < 2) {
00038 std::cerr << "Syntax: cpp_introspection_test <filename or path> [<message type>]" << std::endl;
00039 exit(1);
00040 }
00041
00042 load(argv[1]);
00043 if (packages().empty()) return 1;
00044
00045 if (argc < 3) {
00046 PackagePtr package = packages().back();
00047 std::cout << "Introspecting package " << package->getName() << "..." << std::endl;
00048 for(Package::const_iterator it = package->begin(); it != package->end(); ++it) {
00049 MessagePtr message = *it;
00050 std::cout << std::endl << "Creating an instance of " << message->getName() << "..." << std::endl;
00051 VoidPtr instance = message->createInstance();
00052 MessagePtr introspected = message->introspect(instance.get());
00053 print_introspection(introspected);
00054 std::cout << std::endl << "...and expanded ..." << std::endl;
00055 print_introspection(expand(introspected));
00056 }
00057
00058 } else {
00059 std::cout << std::endl << "Introspecting " << argv[2] << ":" << std::endl;
00060 MessagePtr introspected = messageByDataType(argv[2]);
00061 if (!introspected) {
00062 std::cout << "I am sorry, I don't know that message type." << std::endl;
00063 } else {
00064 V_string fields, types;
00065 introspected->getFields(fields, true);
00066 introspected->getTypes(types, true);
00067 assert(fields.size() == types.size());
00068 for(size_t i = 0; i < fields.size(); ++i) {
00069 std::cout << " " << types[i] << "\t" << fields[i] << std::endl;
00070 }
00071 }
00072 }
00073
00074 exit(0);
00075 }
00076
00077 void print_introspection(MessagePtr message, const std::string& prefix) {
00078 if (!message->hasInstance()) {
00079 std::cout << "No instance!" << std::endl;
00080 }
00081
00082 for(Message::const_iterator it = message->begin(); it != message->end(); ++it) {
00083 FieldPtr field = *it;
00084
00085 std::cout << prefix << std::string(field->getDataType()) << " " << std::string(field->getName()) << " = ";
00086 if (field->isContainer()) std::cout << "[";
00087
00088 if (field->isMessage()) {
00089 std::cout << std::endl;
00090 for(std::size_t i = 0; i < field->size(); i++) {
00091 MessagePtr expanded = field->expand(i);
00092 if (!expanded) {
00093 std::cout << prefix << " (unknown)" << std::endl;
00094 continue;
00095 }
00096 print_introspection(expanded, prefix + " ");
00097 }
00098 std::cout << prefix;
00099 } else {
00100 for(std::size_t i = 0; i < field->size(); i++) {
00101 if (i > 0) std::cout << ", ";
00102 std::cout << field->as<std::string>(i);
00103 }
00104 }
00105 if (field->isContainer()) std::cout << "]";
00106 std::cout << std::endl;
00107 }
00108 }