Update generator to use the new objects graph organization
authorRomain Forlot <romain.forlot@iot.bzh>
Wed, 17 May 2017 12:31:46 +0000 (14:31 +0200)
committerRomain Forlot <romain.forlot@iot.bzh>
Fri, 19 May 2017 09:36:43 +0000 (11:36 +0200)
Here is the objects graphs for configuration_t object:
vector<can_message_set_t>
{
  vector<can_message_definition_t>
  {
    vector<can_signal_t>
  }
  vector<diagnostic_message_t>
}

Change-Id: I865f6ec213505a578c0c6ec4b3a76bd061560d2a
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
CAN-config-generator/src/main.cpp

index e0240a8..5f2e28f 100644 (file)
@@ -92,16 +92,16 @@ std::ostream& operator<<(std::ostream& o, const generator<std::string>& v)
 template <typename T>\r
 std::ostream& operator<<(std::ostream& o, const generator<std::vector<T>>& v)\r
 {\r
-       o << v.line_prefix_ << "{\n";\r
+//     o << v.line_prefix_;\r
        auto sz = v.v_.size();\r
        for(const T& i : v.v_)\r
        {\r
                o << gen(i, v.line_prefix_ + '\t');\r
                if (sz > 1) o << ",";\r
                --sz;\r
-               o << '\n';\r
+//             o << '\n';\r
        }\r
-       o << v.line_prefix_ << '}';\r
+//     o << v.line_prefix_;\r
        return o;\r
 }\r
 \r
@@ -109,34 +109,16 @@ template <>
 std::ostream& operator<<(std::ostream& o, const generator<openxc::message_set>& v)\r
 {\r
        o       << v.line_prefix_\r
-               << '{'\r
-               << "0, "\r
-               << gen(v.v_.name()) << ", "\r
-               << v.v_.buses().size() << ", "\r
-               << v.v_.messages().size() << ", "\r
-               << std::accumulate(\r
-                       std::begin(v.v_.messages()),\r
-                       std::end(v.v_.messages()),\r
-                       0,\r
-                       [](int sum, const openxc::can_message& p) { return sum + p.signals().size(); }\r
-                       ) << ", "\r
-               << v.v_.commands().size() << ", "\r
-               << v.v_.diagnostic_messages().size() << "}";\r
-       return o;\r
-}\r
-\r
-template <>\r
-std::ostream& operator<<(std::ostream& o, const generator<openxc::can_message>& v)\r
-{\r
-       o       << v.line_prefix_\r
-               << "can_message_definition_t("\r
-               << "0, "\r
-               << gen(v.v_.bus()) << ", "\r
-               << v.v_.id() << ", "\r
-               << "can_message_format_t::STANDARD, "\r
-               << "frequency_clock_t(" << gen(v.v_.max_frequency()) << "), "\r
-               << gen(v.v_.force_send_changed())\r
-               << ')';\r
+               << "can_message_set_t{"\r
+               << "0,"\r
+               << gen(v.v_.name()) << ",\n"\r
+               << "\t\t\t{ // beginning can_message_definition_ vector\n"\r
+               << gen(v.v_.messages(), "\t\t\t")\r
+               << "\n\t\t}, // end can_message_definition vector\n"\r
+               << "\t\t\t{ // beginning diagnostic_messages_ vector\n"\r
+               << gen(v.v_.diagnostic_messages(),"\t\t\t") << "\n"\r
+               << "\t\t\t} // end diagnostic_messages_ vector\n"\r
+               << "\t\t} // end can_message_set entry\n";\r
        return o;\r
 }\r
 \r
@@ -144,13 +126,13 @@ template <>
 std::ostream& operator<<(std::ostream& o, const generator<std::map<std::string, std::vector<std::uint32_t>>>& v)\r
 {\r
        o << v.line_prefix_ << "{\n";\r
-       std::uint32_t c1 = v.v_.size();\r
+       std::uint32_t c1 = (uint32_t)v.v_.size();\r
        for(const auto& state : v.v_)\r
        {\r
-               std::uint32_t c2 = state.second.size();\r
+               std::uint32_t c2 = (uint32_t)state.second.size();\r
                for(const auto& i : state.second)\r
                {\r
-                       o << v.line_prefix_ << "\t" << "{" << i << ", " << gen(state.first) << "}";\r
+                       o << v.line_prefix_ << "\t" << "{" << i << "," << gen(state.first) << "}";\r
                        if (c1 > 1 || c2 > 1) o << ',';\r
                        o << '\n';\r
                        --c2;\r
@@ -164,14 +146,12 @@ std::ostream& operator<<(std::ostream& o, const generator<std::map<std::string,
 template <>\r
 std::ostream& operator<<(std::ostream& o, const generator<openxc::signal>& v)\r
 {\r
-       o       << v.line_prefix_ << "{\n"\r
-               << v.line_prefix_ << "\t0,\n"\r
-               << v.line_prefix_ << "\t" << v.index_ << ",\n"\r
+       o       << v.line_prefix_ << "{std::make_shared<can_signal_t> (can_signal_t{\n"\r
                << v.line_prefix_ << "\t" << gen(v.v_.generic_name()) << ",\n"\r
                << v.line_prefix_ << "\t" << v.v_.bit_position() << ",\n"\r
                << v.line_prefix_ << "\t" << v.v_.bit_size() << ",\n"\r
-               << v.line_prefix_ << "\t" << gen(v.v_.factor()) << ", \n"\r
-               << v.line_prefix_ << "\t" << v.v_.offset() << ", \n"\r
+               << v.line_prefix_ << "\t" << gen(v.v_.factor()) << ",\n"\r
+               << v.line_prefix_ << "\t" << v.v_.offset() << ",\n"\r
                << v.line_prefix_ << "\t" << "0,\n"\r
                << v.line_prefix_ << "\t" << "0,\n"\r
                << v.line_prefix_ << "\tfrequency_clock_t(" << gen(v.v_.max_frequency()) << "),\n"\r
@@ -182,14 +162,39 @@ std::ostream& operator<<(std::ostream& o, const generator<openxc::signal>& v)
                << v.line_prefix_ << '\t' << (v.v_.decoder().size() ? v.v_.decoder() : "nullptr") << ",\n"\r
                << v.line_prefix_ << '\t' << (v.v_.encoder().size() ? v.v_.encoder() : "nullptr") << ",\n"\r
                << v.line_prefix_ << '\t' << "false\n"\r
-               << v.line_prefix_ << "}";\r
+               << v.line_prefix_ << "})}";\r
+       return o;\r
+}\r
+\r
+template <>\r
+std::ostream& operator<<(std::ostream& o, const generator<openxc::can_message>& v)\r
+{\r
+       o       << v.line_prefix_\r
+               << "{std::make_shared<can_message_definition_t>(can_message_definition_t{"\r
+               << gen(v.v_.bus()) << ","\r
+               << v.v_.id() << ","\r
+               << "can_message_format_t::STANDARD,"\r
+               << "frequency_clock_t(" << gen(v.v_.max_frequency()) << "),"\r
+               << gen(v.v_.force_send_changed()) << ",\n";\r
+               std::uint32_t index = 0;\r
+       o       << "\t\t\t\t\t{ // beginning can_signals vector\n";\r
+                       std::uint32_t signal_count = (uint32_t)v.v_.signals().size();\r
+                       for(const openxc::signal& s : v.v_.signals())\r
+                       {\r
+       o                       << gen(s, index,"\t\t\t\t\t\t");\r
+                               if (signal_count > 1) o << ',';\r
+                               --signal_count;\r
+       o                       << '\n';\r
+                       }\r
+       o       << "\t\t\t\t\t} // end can_signals vector\n"\r
+               << "\t\t\t\t})} // end can_message_definition entry\n";\r
        return o;\r
 }\r
 \r
 template <>\r
 std::ostream& operator<<(std::ostream& o, const generator<openxc::diagnostic_message>& v)\r
 {\r
-       o       << v.line_prefix_ << "{\n"\r
+       o       << v.line_prefix_ << "{std::make_shared<diagnostic_message_t>(diagnostic_message_t{\n"\r
                << v.line_prefix_ << "\t" << v.v_.pid() << ",\n"\r
                << v.line_prefix_ << "\t" << gen(v.v_.name()) << ",\n"\r
                << v.line_prefix_ << "\t" << 0 << ",\n"\r
@@ -199,7 +204,7 @@ std::ostream& operator<<(std::ostream& o, const generator<openxc::diagnostic_mes
                << v.line_prefix_ << "\t" << (v.v_.decoder().size() ? v.v_.decoder() : "nullptr") << ",\n"\r
                << v.line_prefix_ << "\t" << (v.v_.callback().size() ? v.v_.callback() : "nullptr") << ",\n"\r
                << v.line_prefix_ << "\t" << "true" << "\n"\r
-               << v.line_prefix_ << "}";\r
+               << v.line_prefix_ << "})}\n";\r
        return o;\r
 }\r
 \r
@@ -211,44 +216,18 @@ std::ostream& operator<<(std::ostream& o, const generator<openxc::diagnostic_mes
 void generate(const std::string& header, const std::string& footer, const openxc::message_set& message_set, std::ostream& out)\r
 {\r
        out << "#include \"configuration.hpp\"\n"\r
-               << "#include \"can/can-decoder.hpp\"\n\n";\r
+               << "#include \"../can/can-decoder.hpp\"\n\n";\r
 \r
        if (header.size()) out << header << "\n";\r
 \r
        out     << "configuration_t::configuration_t()\n"\r
-               << "    : can_message_set_{" << gen(message_set) << "}\n"\r
-               << "    , can_message_definition_\n"\r
-               << "    {\n"\r
-                               << gen(message_set.messages(), "\t\t") << '\n'\r
-               << "    }\n"\r
-               << "    , can_signals_\n"\r
-               << "    {\n";\r
-               std::uint32_t message_count = message_set.messages().size();\r
-               std::uint32_t index = 0;\r
-               out << "                {\n";\r
-               for(const openxc::can_message& m : message_set.messages())\r
-               {\r
-                       std::uint32_t signal_count = m.signals().size();\r
-                       for(const openxc::signal& s : m.signals())\r
-                       {\r
-                               out << gen(s, index, "                  ");\r
-                               if (signal_count > 1) out << ',';\r
-                               --signal_count;\r
-                               out << '\n';\r
-                       }\r
-                       if (index + 1 < message_count) out << ',';\r
-                       ++index;\r
-               }\r
-               out << "                }\n";\r
-               out << "        }\n"\r
-                       << "    , diagnostic_messages_\n"\r
-                       << "    {\n"\r
-                       << gen(message_set.diagnostic_messages(), "             ") << "\n"\r
-                       << "    }\n"\r
-                       << "{\n"\r
-                       << "}\n\n"\r
-                       << "const std::string configuration_t::get_diagnostic_bus() const\n"\r
-                       << "{\n";\r
+               << "    : can_bus_manager_{utils::config_parser_t{\"/etc/dev-mapping.conf\"}}\n"\r
+               << "    , can_message_set_{\n"\r
+               << gen(message_set, "\t\t")\r
+               << "\t} // end can_message_set vector\n"\r
+               << "{}\n\n"\r
+               << "const std::string configuration_t::get_diagnostic_bus() const\n"\r
+               << "{\n";\r
 \r
                std::string active_bus = "";\r
                for (const auto& d : message_set.diagnostic_messages())\r
@@ -258,9 +237,9 @@ void generate(const std::string& header, const std::string& footer, const openxc
                        if (active_bus != d.bus()) std::cerr << "ERROR: The bus name should be the same for each diagnostic message." << std::endl;\r
                }\r
 \r
-               out     << "    return " << gen(active_bus) << ";\n"\r
-                       << "}\n\n";\r
-       out << footer << std::endl;\r
+       out     << "\treturn " << gen(active_bus) << ";\n"\r
+               << "}\n\n";\r
+       out     << footer << std::endl;\r
 }\r
 \r
 /// @brief Read whole file content to a string.\r
@@ -338,7 +317,7 @@ int main(int argc, char *argv[])
                /*use function getopt to get the arguments with option."hu:p:s:v" indicate \r
                that option h,v are the options without arguments while u,p,s are the\r
                options with arguments*/\r
-               while((tmp=getopt(argc,argv,"m:h:f:o:"))!=-1)\r
+               while((tmp=(char)getopt(argc,argv,"m:h:f:o:"))!=-1)\r
                {\r
                        switch(tmp)\r
                        {\r