#pragma once
#include <string>
#include <string_view>
#include <unordered_map>
namespace rapidproto::codegen {
struct ScalarWire {
std::string_view
wire; std::string_view pre; std::string_view post; std::string_view packed_conv;
};
inline const ScalarWire* find_scalar_wire(std::string_view type) {
static const std::unordered_map<std::string_view, ScalarWire> kTable = {
{"int32",
{"Varint", "::rapidproto::varint_to_int32(", ")", "::rapidproto::wire::conv_int32"}},
{"int64",
{"Varint", "::rapidproto::varint_to_int64(", ")", "::rapidproto::wire::conv_int64"}},
{"uint32",
{"Varint", "static_cast<std::uint32_t>(", ")", "::rapidproto::wire::conv_uint32"}},
{"uint64", {"Varint", "", "", "::rapidproto::wire::conv_uint64"}},
{"sint32",
{"Varint", "::rapidproto::zigzag_decode_32(static_cast<std::uint32_t>(", "))",
"::rapidproto::wire::conv_sint32"}},
{"sint64",
{"Varint", "::rapidproto::zigzag_decode_64(", ")", "::rapidproto::wire::conv_sint64"}},
{"bool", {"Varint", "::rapidproto::varint_to_bool(", ")", "::rapidproto::wire::conv_bool"}},
{"fixed32", {"I32", "", "", ""}},
{"sfixed32", {"I32", "static_cast<std::int32_t>(", ")", ""}},
{"float", {"I32", "::rapidproto::bit_cast_float(", ")", ""}},
{"fixed64", {"I64", "", "", ""}},
{"sfixed64", {"I64", "static_cast<std::int64_t>(", ")", ""}},
{"double", {"I64", "::rapidproto::bit_cast_double(", ")", ""}},
{"string", {"Len", "", "", ""}},
{"bytes", {"Len", "", "", ""}},
};
const auto it = kTable.find(type);
return it != kTable.end() ? &it->second : nullptr;
}
inline std::string_view cpp_numeric_type(std::string_view type) {
static const std::unordered_map<std::string_view, std::string_view> kTable = {
{"int32", "std::int32_t"},
{"sint32", "std::int32_t"},
{"sfixed32", "std::int32_t"},
{"uint32", "std::uint32_t"},
{"fixed32", "std::uint32_t"},
{"int64", "std::int64_t"},
{"sint64", "std::int64_t"},
{"sfixed64", "std::int64_t"},
{"uint64", "std::uint64_t"},
{"fixed64", "std::uint64_t"},
{"float", "float"},
{"double", "double"},
{"bool", "bool"},
};
const auto it = kTable.find(type);
return it != kTable.end() ? it->second : std::string_view{};
}
inline bool is_packable_wire(std::string_view wire) {
return wire != "Len" && wire != "SGroup";
}
struct WireRead {
std::string_view reader; std::string_view raw_type; };
inline WireRead wire_read(std::string_view wire) {
if (wire == "I32") {
return {"read_fixed32", "std::uint32_t"};
}
if (wire == "I64") {
return {"read_fixed64", "std::uint64_t"};
}
return {"read_varint", "std::uint64_t"}; }
inline std::string import_header(std::string_view path, std::string_view extension) {
constexpr std::string_view kProto = ".proto";
if (path.size() >= kProto.size() && path.substr(path.size() - kProto.size()) == kProto) {
path.remove_suffix(kProto.size());
}
return std::string(path) + std::string(extension);
}
inline std::string sibling_include(std::string_view filename, std::string_view extension) {
const auto slash = filename.find_last_of('/');
if (slash != std::string_view::npos) {
filename.remove_prefix(slash + 1);
}
return import_header(filename, extension);
}
inline std::string common_sibling_include(std::string_view filename) {
return sibling_include(filename, ".rp.common.hpp");
}
}