Add enum_to_string example

This commit is contained in:
Jonathan Müller 2017-10-31 18:28:18 +01:00
commit b69e7bfacd
2 changed files with 62 additions and 0 deletions

View file

@ -9,3 +9,4 @@ endfunction()
_cppast_example(ast_printer)
_cppast_example(documentation_generator)
_cppast_example(enum_to_string)

View file

@ -0,0 +1,61 @@
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
/// \file
/// Generates enum `to_string()` code.
#include <iostream>
#include <cppast/cpp_enum.hpp> // cpp_enum
#include <cppast/visitor.hpp> // visit()
#include "example_parser.hpp"
void generate_to_string(const cppast::cpp_file& file)
{
cppast::visit(file,
[](const cppast::cpp_entity& e) {
// only visit enums that have the attribute set
return (e.kind() == cppast::cpp_entity_kind::enum_t
&& cppast::has_attribute(e, "generate::to_string"))
// or all namespaces
|| e.kind() == cppast::cpp_entity_kind::namespace_t;
},
[](const cppast::cpp_entity& e, const cppast::visitor_info& info) {
if (e.kind() == cppast::cpp_entity_kind::enum_t && !info.is_old_entity())
{
// a new enum, generate to string function
auto& enum_ = static_cast<const cppast::cpp_enum&>(e);
// write function header
std::cout << "const char* to_string(const " << enum_.name() << "& e) {\n";
// generate switch
std::cout << " switch (e) {\n";
for (const auto& enumerator : enum_)
{
std::cout << " case " << enum_.name() << "::" << enumerator.name()
<< ":\n";
std::cout << " return \"" << enumerator.name() << "\";\n";
}
std::cout << " }\n";
std::cout << "}\n\n";
}
else if (e.kind() == cppast::cpp_entity_kind::namespace_t)
{
if (info.event == cppast::visitor_info::container_entity_enter)
// open namespace
std::cout << "namespace " << e.name() << " {\n\n";
else // if (info.event == cppast::visitor_info::container_entity_exit)
// close namespace
std::cout << "}\n";
}
});
}
int main(int argc, char* argv[])
{
return example_main(argc, argv, {}, &generate_to_string);
}