Files
logos-cpp-sdk/cpp-generator/legacy/generator_lib.h
T
Dario LipicarandClaude Opus 4.7 eb71a1aa90 feat: dependency interfaces — SDK code generator (bound wrappers) (#74)
* feat: dependency interfaces — runtime-bound typed wrappers

Turn a declared "interface" (a .lidl file or a pure-C++ header with a
logos_events: block) into a BOUND client wrapper: the target module name
is a constructor argument instead of a baked-in literal, so one interface
can be bound to any satisfying module at runtime.

- generator_lib: new BindMode { Static, Bound }. In Bound mode the ctor
  takes (LogosAPI*, const QString& moduleName) and every invokeRemoteMethod*
  / ensureReplica routes through m_moduleName. Default Static leaves existing
  name-baked output byte-for-byte unchanged.
- legacy/main.cpp: repeatable --interface <name>=<path>[=<impl_class>] flag,
  consumed in --general-only. Parses .lidl via lidlParse and .h via
  parseImplHeader, emits the bound <name>_api.{h,cpp}, and adds
  bind_<name>(moduleName) factories (QString + std::string) to the
  LogosModules umbrella. Also self-resolves local interface_dependencies
  from metadata.json for non-nix builds.
- experimental/lidl_gen_client: same BindMode parity for the --lidl path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* address review: dedup and validate --interface specs

A repeated --interface <name>=... would emit duplicate #include and
bind_<name>(...) into logos_sdk.h and fail to compile; empty name/path were
silently accepted. Dedup the flag-derived specs by name and drop malformed
ones with an explanatory message on stderr. (Copilot review, PR #74.)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-05 20:44:43 -03:00

71 lines
3.6 KiB
C++

#ifndef GENERATOR_LIB_H
#define GENERATOR_LIB_H
#include <QString>
#include <QJsonArray>
#include <QTextStream>
#include <QVector>
#include <QPair>
struct ParsedMethod {
QString returnType;
QString name;
QVector<QPair<QString, QString>> params; // (type, name)
QString description; // doc comment adjacent to the LOGOS_METHOD declaration
};
// Which type surface to expose on the generated per-module wrapper.
// Each module's build picks ONE — there's no composite output. Default
// is Qt for backward compatibility; `interface: "universal"` modules
// flip to Std via the -DLOGOS_API_STYLE=std CMake flag the module
// builder threads through.
enum class ApiStyle { Qt, Std };
// Whether the generated wrapper targets ONE fixed module (the historical
// behaviour) or binds to a module name chosen at runtime.
// Static — the module name is baked into the ctor + every remote call,
// so `<Class>(LogosAPI*)` always talks to that one module.
// This is what name-baked dependency wrappers use.
// Bound — the ctor takes `(LogosAPI*, const QString& moduleName)` and
// stores it in `m_moduleName`; every remote call routes through
// that member. This is what *interface* wrappers use: one
// interface, bound to a concrete module name at runtime.
// Default is Static so existing callers and their generated output are
// byte-for-byte unchanged.
enum class BindMode { Static, Bound };
QString toPascalCase(const QString& name);
QString normalizeType(QString t);
QString mapParamType(const QString& qtType);
QString mapReturnType(const QString& qtType);
QString toQVariantConversion(const QString& type, const QString& argExpr);
// makeHeader / makeSource emit the single `<Class>` wrapper for a
// module. When `apiStyle == Std`, parameter / return types come from
// the std-typed mapping table (std::string / std::vector<std::string>
// / LogosMap / LogosList / int64_t / StdLogosResult) and the .cpp
// body wraps the QVariant wire with inline Qt↔std conversions —
// callers never include Qt headers. When `apiStyle == Qt`, the output
// matches the legacy Qt-typed surface (QString / QStringList /
// QVariantList / QVariantMap / int / LogosResult). The class name is
// always `<Module>` either way; the two styles are mutually exclusive.
//
// `events` carries typed event prototypes loaded from a `.lidl`
// sidecar via --events-from. Each entry is
// { "name": "<event>", "params": [ { "name": "...", "type": "<QtTypeName>" } ] }
// (Qt-typed names — same surface methods come through). When non-empty,
// the wrapper also gets one `on<EventName>(callback)` accessor per
// event next to the existing generic `onEvent(name, callback)` channel.
// The accessor signature uses the apiStyle's type surface for the
// callback's argument types.
//
// `bindMode` selects a fixed-module wrapper (Static, default) or a
// runtime-bound interface wrapper (Bound) — see BindMode above. In Bound
// mode `moduleName` is used only for the class/file naming the caller
// already decided; the emitted code never bakes it into a call.
QString makeHeader(const QString& moduleName, const QString& className, const QJsonArray& methods, ApiStyle apiStyle = ApiStyle::Qt, const QJsonArray& events = {}, BindMode bindMode = BindMode::Static);
QString makeSource(const QString& moduleName, const QString& className, const QString& headerBaseName, const QJsonArray& methods, ApiStyle apiStyle = ApiStyle::Qt, const QJsonArray& events = {}, BindMode bindMode = BindMode::Static);
QVector<ParsedMethod> parseProviderHeader(const QString& headerPath, QTextStream& err);
#endif // GENERATOR_LIB_H