C++ RPC ORM 高速解析

13 篇文章 0 订阅

支持所有常用编程语


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


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

  • 57
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值