Add enum category example
This commit is contained in:
parent
0f9b90a32b
commit
0b71d7d3b4
5 changed files with 122 additions and 5 deletions
116
example/enum_category.cpp
Normal file
116
example/enum_category.cpp
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
// 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 category functions.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#include <cppast/cpp_enum.hpp> // cpp_enum
|
||||
#include <cppast/cpp_function.hpp> // cpp_function
|
||||
#include <cppast/visitor.hpp> // visit()
|
||||
|
||||
#include "example_parser.hpp"
|
||||
|
||||
bool is_category(const cppast::cpp_enum_value& e, const std::string& name)
|
||||
{
|
||||
if (auto attr = cppast::has_attribute(e, "generate::enum_category"))
|
||||
{
|
||||
auto iter =
|
||||
std::find_if(attr.value().arguments().value().begin(),
|
||||
attr.value().arguments().value().end(),
|
||||
[&](const cppast::cpp_token& tok) { return tok.spelling == name; });
|
||||
return iter != attr.value().arguments().value().end();
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
const cppast::cpp_enum& get_enum(const cppast::cpp_entity_index& index,
|
||||
const cppast::cpp_function_parameter& param)
|
||||
{
|
||||
auto& param_type = param.type();
|
||||
// is an enum
|
||||
assert(param_type.kind() == cppast::cpp_type_kind::user_defined_t);
|
||||
auto& definition =
|
||||
static_cast<const cppast::cpp_user_defined_type&>(param_type).entity().get(index)[0u].get();
|
||||
|
||||
assert(definition.kind() == cppast::cpp_entity_kind::enum_t);
|
||||
return static_cast<const cppast::cpp_enum&>(definition);
|
||||
}
|
||||
|
||||
void generate_enum_category(const cppast::cpp_entity_index& index, const cppast::cpp_file& file)
|
||||
{
|
||||
cppast::visit(file,
|
||||
[](const cppast::cpp_entity& e) {
|
||||
// only visit function declarations that have the attribute set
|
||||
return (e.kind() == cppast::cpp_entity_kind::function_t
|
||||
&& !cppast::is_definition(e)
|
||||
&& cppast::has_attribute(e, "generate::enum_category"))
|
||||
// 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::function_t)
|
||||
{
|
||||
// a new function, generate implementation
|
||||
assert(info.is_new_entity());
|
||||
|
||||
auto category = cppast::has_attribute(e, "generate::enum_category")
|
||||
.value()
|
||||
.arguments()
|
||||
.value()
|
||||
.as_string();
|
||||
|
||||
auto& func = static_cast<const cppast::cpp_function&>(e);
|
||||
// return type must be bool
|
||||
assert(func.return_type().kind() == cppast::cpp_type_kind::builtin_t
|
||||
&& static_cast<const cppast::cpp_builtin_type&>(func.return_type())
|
||||
.builtin_type_kind()
|
||||
== cppast::cpp_bool);
|
||||
|
||||
// single parameter...
|
||||
assert(std::next(func.parameters().begin()) == func.parameters().end());
|
||||
auto& param = *func.parameters().begin();
|
||||
auto& enum_ = get_enum(index, param);
|
||||
|
||||
// generate function definition
|
||||
std::cout << "inline bool " << func.name() << "("
|
||||
<< cppast::to_string(param.type()) << " e) {\n";
|
||||
|
||||
// generate switch
|
||||
std::cout << " switch (e) {\n";
|
||||
for (const auto& enumerator : enum_)
|
||||
{
|
||||
std::cout << " case " << enum_.name() << "::" << enumerator.name()
|
||||
<< ":\n";
|
||||
if (is_category(enumerator, category))
|
||||
std::cout << " return true;\n";
|
||||
else
|
||||
std::cout << " return false;\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[])
|
||||
{
|
||||
cppast::cpp_entity_index index;
|
||||
return example_main(argc, argv, index,
|
||||
[&](const cppast::cpp_file& file) { generate_enum_category(index, file); });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue