C++ RPC ORM 高速解析

ops/2024/10/10 15:56:33/

支持所有常用编程语


https://capnproto.org/
GitHub - capnproto/capnproto: Cap'n Proto serialization/RPC system - core tools and C++ library
https://capnproto.org/capnproto-c++-win32-1.0.2.zip

常用命令:
    capnp help
    capnp compile -oc++ myschema.capnp
    capnp decode myschema.capnp MyType < message.bin > message.txt
    capnp encode myschema.capnp MyType < message.txt > message.bin
    capnp eval myschema.capnp myConstant

myschema.capnp:

@0xa8712255ec118b18;
struct Person {
  id @0 :UInt32;
  name @1 :Text;
  email @2 :Text;
  phones @3 :List(PhoneNumber);

  struct PhoneNumber {
    number @0 :Text;
    type @1 :Type;

    enum Type {
      mobile @0;
      home @1;
      work @2;
    }
  }

  employment :union {
    unemployed @4 :Void;
    employer @5 :Text;
    school @6 :Text;
    selfEmployed @7 :Void;
    # We assume that a person is only one of these.
  }
}

struct AddressBook {
  people @0 :List(Person);
}


示例代码:
    #include "myschema.capnp.h"
    #include <capnp/message.h>
    #include <capnp/serialize-packed.h>
    #include <iostream>

    void writeAddressBook(int fd) {
        ::capnp::MallocMessageBuilder message;

        AddressBook::Builder addressBook = message.initRoot<AddressBook>();
        ::capnp::List<Person>::Builder people = addressBook.initPeople(2);

        Person::Builder alice = people[0];
        alice.setId(123);
        alice.setName("Alice");
        alice.setEmail("alice@example.com");
        // Type shown for explanation purposes; normally you'd use auto.
        ::capnp::List<Person::PhoneNumber>::Builder alicePhones =
            alice.initPhones(1);
        alicePhones[0].setNumber("555-1212");
        alicePhones[0].setType(Person::PhoneNumber::Type::MOBILE);
        alice.getEmployment().setSchool("MIT");

        Person::Builder bob = people[1];
        bob.setId(456);
        bob.setName("Bob");
        bob.setEmail("bob@example.com");
        auto bobPhones = bob.initPhones(2);
        bobPhones[0].setNumber("555-4567");
        bobPhones[0].setType(Person::PhoneNumber::Type::HOME);
        bobPhones[1].setNumber("555-7654");
        bobPhones[1].setType(Person::PhoneNumber::Type::WORK);
        bob.getEmployment().setUnemployed();

        writePackedMessageToFd(fd, message);
    }

    void printAddressBook(int fd) {
        ::capnp::PackedFdMessageReader message(fd);

        AddressBook::Reader addressBook = message.getRoot<AddressBook>();

        for (Person::Reader person : addressBook.getPeople()) {
            std::cout << person.getName().cStr() << ": "
                << person.getEmail().cStr() << std::endl;
            for (Person::PhoneNumber::Reader phone : person.getPhones()) {
                const char* typeName = "UNKNOWN";
                switch (phone.getType()) {
                case Person::PhoneNumber::Type::MOBILE: typeName = "mobile"; break;
                case Person::PhoneNumber::Type::HOME: typeName = "home"; break;
                case Person::PhoneNumber::Type::WORK: typeName = "work"; break;
                }
                std::cout << "  " << typeName << " phone: "
                    << phone.getNumber().cStr() << std::endl;
            }
            Person::Employment::Reader employment = person.getEmployment();
            switch (employment.which()) {
            case Person::Employment::UNEMPLOYED:
                std::cout << "  unemployed" << std::endl;
                break;
            case Person::Employment::EMPLOYER:
                std::cout << "  employer: "
                    << employment.getEmployer().cStr() << std::endl;
                break;
            case Person::Employment::SCHOOL:
                std::cout << "  student at: "
                    << employment.getSchool().cStr() << std::endl;
                break;
            case Person::Employment::SELF_EMPLOYED:
                std::cout << "  self-employed" << std::endl;
                break;
            }
        }
    }

    void test() {
        auto file = fopen("example.bin", "wt+");
        if (!file)
            return;
        int fd = fileno(file); //如果是W+ 这里文件被清空的影响
        writeAddressBook(fd);
        fclose(file);
        fflush(file);
        file = fopen("example.bin", "rb+");
        fd = fileno(file);
        printAddressBook(fd);
        fclose(file);
    }

运行输出:
    Alice: alice@example.com
      mobile phone: 555-1212
      student at: MIT
    Bob: bob@example.com
      home phone: 555-4567
      work phone: 555-7654
      unemployed
  

其它类似参考:
GitHub - protocolbuffers/protobuf: Protocol Buffers - Google's data interchange format
    evanw (Evan Wallace) · GitHub


创作不易,小小的支持一下吧!


http://www.ppmy.cn/ops/43923.html

相关文章

LeeCode 3165 线段树

题意 传送门 LeeCode 3165 不包含相邻元素的子序列的最大和 题解 考虑不含相邻子序列的最大和&#xff0c;在不带修改的情况下容易想到&#xff0c;以最后一个元素是否被选取为状态进行DP。从线性递推的角度难以处理待修改的情况。 从分治的角度考虑&#xff0c;使用线段树…

Mac 安装 git

文章目录 前言一、介绍二、下载三、验证四、配置五、Git常用命令六、git提交和撤销工作流程代码提交和提交同步代码撤销和撤销同步 FAQ1.homebrew 下载解决方法一&#xff08;强烈推荐&#xff09;&#xff1a;解决方法二&#xff1a; 总结 前言 Git 是一个开源的分布式版本控…

NLP(18)--大模型发展(2)

前言 仅记录学习过程&#xff0c;有问题欢迎讨论 Transformer结构&#xff1a; LLM的结构变化&#xff1a; Muti-head 共享&#xff1a; Q继续切割为muti-head,但是K,V少切&#xff0c;比如切为2个&#xff0c;然后复制到n个muti-head减少参数量&#xff0c;加速训练 atte…

STM32无源蜂鸣器播放音乐

单片机&#xff1a;STM32F407ZGT6 开发软件&#xff1a;MDKSTM32CubeMX 文章目录 前言一、找一篇音乐的简谱二、确定音调三、确定节拍四、使用STM32CubeMX生成初始化代码五、代码分析 前言 本实验使用的是低电平触发的无源蜂鸣器 无源蜂鸣器是指没有振荡源的蜂鸣器&#xff0…

​你见过哪些不过度设计的优秀APP?​

优联前端https://ufrontend.com/ 提供一站式企业前端解决方案 “每日故宫”是一款以故宫博物院丰富的藏品为基础&#xff0c;结合日历形式展示每日精选藏品的移动应用。通过这款应用&#xff0c;用户可以随时随地欣赏到故宫的珍贵藏品&#xff0c;感受中华五千年文化的魅力。…

leecode 637 二叉树的层平均值

leetcode 二叉树相关-层序遍历专题 二叉树的层序遍历一般来说&#xff0c;我们是利用队列来实现的&#xff0c;先把根节点入队&#xff0c;然后在出队后将其对应的子节点入队&#xff0c;然后往复此种操作。相比于二叉树的遍历递归&#xff0c;层序遍历比较简单&#xff0c;有…

k8s使用Volcano调度gpu

k8s部署 https://www.yangxingzhen.com/9817.html cri-dockerd安装 https://zhuanlan.zhihu.com/p/632861515 安装nvidia-container-runtime https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html 安装k8s-device-plugin https://…