test.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Wed Aug 26 2015 11:11:00