test.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
30 #include <boost/lexical_cast.hpp>
31 
32 using namespace cpp_introspection;
33 
34 void print_introspection(MessagePtr message, const std::string& prefix = "");
35 
36 int main(int argc, char **argv) {
37  if (argc < 2) {
38  std::cerr << "Syntax: cpp_introspection_test <filename or path> [<message type>]" << std::endl;
39  exit(1);
40  }
41 
42  load(argv[1]);
43  if (packages().empty()) return 1;
44 
45  if (argc < 3) {
46  PackagePtr package = packages().back();
47  std::cout << "Introspecting package " << package->getName() << "..." << std::endl;
48  for(Package::const_iterator it = package->begin(); it != package->end(); ++it) {
49  MessagePtr message = *it;
50  std::cout << std::endl << "Creating an instance of " << message->getName() << "..." << std::endl;
51  VoidPtr instance = message->createInstance();
52  MessagePtr introspected = message->introspect(instance.get());
53  print_introspection(introspected);
54  std::cout << std::endl << "...and expanded ..." << std::endl;
55  print_introspection(expand(introspected));
56  }
57 
58  } else {
59  std::cout << std::endl << "Introspecting " << argv[2] << ":" << std::endl;
60  MessagePtr introspected = messageByDataType(argv[2]);
61  if (!introspected) {
62  std::cout << "I am sorry, I don't know that message type." << std::endl;
63  } else {
64  V_string fields, types;
65  introspected->getFields(fields, true);
66  introspected->getTypes(types, true);
67  assert(fields.size() == types.size());
68  for(size_t i = 0; i < fields.size(); ++i) {
69  std::cout << " " << types[i] << "\t" << fields[i] << std::endl;
70  }
71  }
72  }
73 
74  exit(0);
75 }
76 
77 void print_introspection(MessagePtr message, const std::string& prefix) {
78  if (!message->hasInstance()) {
79  std::cout << "No instance!" << std::endl;
80  }
81 
82  for(Message::const_iterator it = message->begin(); it != message->end(); ++it) {
83  FieldPtr field = *it;
84 
85  std::cout << prefix << std::string(field->getDataType()) << " " << std::string(field->getName()) << " = ";
86  if (field->isContainer()) std::cout << "[";
87 
88  if (field->isMessage()) {
89  std::cout << std::endl;
90  for(std::size_t i = 0; i < field->size(); i++) {
91  MessagePtr expanded = field->expand(i);
92  if (!expanded) {
93  std::cout << prefix << " (unknown)" << std::endl;
94  continue;
95  }
96  print_introspection(expanded, prefix + " ");
97  }
98  std::cout << prefix;
99  } else {
100  for(std::size_t i = 0; i < field->size(); i++) {
101  if (i > 0) std::cout << ", ";
102  std::cout << field->as<std::string>(i);
103  }
104  }
105  if (field->isContainer()) std::cout << "]";
106  std::cout << std::endl;
107  }
108 }
PackagePtr load(const std::string &package_or_library_or_path)
MessagePtr expand(const MessagePtr &message, const std::string &separator=".", const std::string &prefix="")
const V_Package & packages()
std::vector< std::string > V_string
V_Field::const_iterator const_iterator
Definition: message.h:101
void print_introspection(MessagePtr message, const std::string &prefix="")
Definition: test.cpp:77
PackagePtr package(const std::string &pkg)
int main(int argc, char **argv)
Definition: test.cpp:36
V_Message::const_iterator const_iterator
Definition: package.h:52
MessagePtr messageByDataType(const std::string &data_type, const std::string &package=std::string())


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Mon Jun 10 2019 12:56:17