1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
* Copyright © 2012 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef CORE_DBUS_COMPILER_H_
#define CORE_DBUS_COMPILER_H_
#include <core/dbus/introspection_parser.h>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
namespace core
{
namespace dbus
{
class Generator;
struct GeneratorConfiguration;
/**
* \brief Parses and processes a DBus introspection XML.
*/
class Compiler
{
public:
static int main(int argc, const char** argv);
class Element
{
public:
enum class Type
{
node,
interface,
method,
signal,
property,
argument,
annotation
};
inline Element(const std::shared_ptr<Element>& parent, const IntrospectionParser::Node& node)
: d{parent, Type::node, node}
{
}
inline Element(const std::shared_ptr<Element>& parent, const IntrospectionParser::Interface& interface)
: d{parent, Type::interface, interface}
{
}
inline Element(const std::shared_ptr<Element>& parent, const IntrospectionParser::Method& method)
: d{parent, Type::method, method}
{
}
inline Element(const std::shared_ptr<Element>& parent, const IntrospectionParser::Signal& signal)
: d{parent, Type::signal, signal}
{
}
inline Element(const std::shared_ptr<Element>& parent, const IntrospectionParser::Property& property)
: d{parent, Type::property, property}
{
}
inline Element(const std::shared_ptr<Element>& parent, const IntrospectionParser::Argument& argument)
: d{parent, Type::argument, argument}
{
}
inline Element(const std::shared_ptr<Element>& parent, const IntrospectionParser::Annotation& annotation)
: d{parent, Type::annotation, annotation}
{
}
Element(const Element&) = delete;
Element& operator=(const Element&) = delete;
bool operator<(const Element& rhs) const
{
return static_cast<int>(d.type) < static_cast<int>(rhs.d.type);
}
inline void apply(std::function<void(const Element&)> before,
std::function<void(const Element&)> visitor,
std::function<void(const Element&)> after)
{
visitor(*this);
for(auto child : d.children)
{
if (before)
before(*child);
child->apply(before, visitor, after);
if (after)
after(*child);
}
}
inline const std::shared_ptr<Element>& parent() const { return d.parent; }
inline Type type() const { return d.type; }
inline void add_child(const std::shared_ptr<Element>& child) { d.children.insert(child); }
inline const IntrospectionParser::Node& node() const { if (d.type != Type::node) throw std::runtime_error("Type mismatch"); return d.payload.node; }
inline const IntrospectionParser::Interface& interface() const { if (d.type != Type::interface) throw std::runtime_error("Type mismatch"); return d.payload.interface; }
inline const IntrospectionParser::Method& method() const { if (d.type != Type::method) throw std::runtime_error("Type mismatch"); return d.payload.method; }
inline const IntrospectionParser::Signal& signal() const { if (d.type != Type::signal) throw std::runtime_error("Type mismatch"); return d.payload.signal; }
inline const IntrospectionParser::Property& property() const { if (d.type != Type::property) throw std::runtime_error("Type mismatch"); return d.payload.property; }
inline const IntrospectionParser::Argument& argument() const { if (d.type != Type::argument) throw std::runtime_error("Type mismatch"); return d.payload.argument; }
inline const IntrospectionParser::Annotation& annotation() const { if (d.type != Type::annotation) throw std::runtime_error("Type mismatch"); return d.payload.annotation; }
private:
struct Private
{
struct KeyCompare
{
bool operator()(const std::shared_ptr<Element>& lhs,
const std::shared_ptr<Element>& rhs) const
{
return *lhs < *rhs;
}
};
template<typename T>
Private(const std::shared_ptr<Element>& parent, Type type, const T& t)
: parent(parent),
children(KeyCompare()),
type(type),
payload(t)
{
}
std::shared_ptr<Element> parent;
std::multiset<std::shared_ptr<Element>, KeyCompare> children;
Type type;
union Payload
{
Payload(const IntrospectionParser::Node& node) : node(node) {}
Payload(const IntrospectionParser::Interface& interface) : interface(interface) {}
Payload(const IntrospectionParser::Method& method) : method(method) {}
Payload(const IntrospectionParser::Signal& signal) : signal(signal) {}
Payload(const IntrospectionParser::Property& property) : property(property) {}
Payload(const IntrospectionParser::Argument& argument) : argument(argument) {}
Payload(const IntrospectionParser::Annotation& annotation) : annotation(annotation) {}
~Payload() {}
IntrospectionParser::Node node;
IntrospectionParser::Interface interface;
IntrospectionParser::Method method;
IntrospectionParser::Signal signal;
IntrospectionParser::Property property;
IntrospectionParser::Argument argument;
IntrospectionParser::Annotation annotation;
} payload;
} d;
};
/**
* \brief Result of processing a DBus introspection XML.
*/
struct Result
{
/**
* \brief Stub elements for usage on clients go here.
*/
struct
{
std::string header_file_path;
std::string impl_file_path;
} stub;
/**
* \brief Skeleton elements for usage/implementation by services go here.
*/
struct
{
std::string header_file_path;
std::string impl_file_path;
} skeleton;
};
/**
* \brief Creates a compiler instance with the given parser and generator.
* \param[in] parser The parser implementation.
* \param[in] generator The generator implementation.
*/
Compiler(const std::shared_ptr<IntrospectionParser>& parser,
const std::shared_ptr<Generator>& generator);
Compiler(const Compiler&) = delete;
~Compiler();
bool operator==(const Compiler&) const = delete;
Compiler& operator=(const Compiler&) = delete;
/**
* \brief Parses and processes the DBus introspection XML in the file pointed to by fn.
* \brief[in] fn Name of the file containing the DBus introspection XML.
* \returns An instance of result on success.
* \throws std::runtime_error in case of errors.
*/
bool process_introspection_file_with_generator_config(
const std::string& fn,
const GeneratorConfiguration& config);
private:
struct Private;
std::unique_ptr<Private> d;
};
}
}
#endif // CORE_DBUS_COMPILER_H_
|