Coder Social home page Coder Social logo

nekoprototools's Issues

宏换取更少的侵入 or 继承更便利的使用

现在,该仓库分别在main_v_1_0_0分支和main_v_2_0_0分支提供了两种不同的静态反射支持。两者的思路都是将类注册并赋予唯一的id来实现,同时也都提供了设置默认序列化器并通过基类的序列化反序列化接口。版本1示例如下:

struct ProtoMessage ProtoBase<ProtoMessage, JsonSerializer> {
    int a;
    std::string b;

    CS_SERIALIZE(a, b)
}

int main() {
    ProtoFactory factory(1, 0, 0);
    IProto* msg = factory.create("ProtoMessage");
    auto proto = dynamic_cast<ProtoMessage*>(msg);
    proto->a = 1;
    proto->b = "hello";
    std::vector<char> data;
    data = msg->toData();
    // do something
    delete msg;
    return 0;
}

版本2示例如下:

class SerializerAble {
    int a;
    std::string b;
    CS_SERIALIZER(a, b);
}
CS_DECLARE_PROTOCOL(SerializerAble, JsonSerializer)

int main() {
    // make protocol by type
    // auto sa = makeProtocol(SerializerAble{});
    // (*sa)->a = 1;
    // (*sa)->b = "hello";
    // make protocol by ProtoFactory
    ProtoFactory factory(1, 0, 0);
    IProto* msg = factory.create("SerializerAble");
    auto proto = msg->cast<SerializerAble>();
    sa->a = 1;
    sa->b = "hello";
    auto data = sa->toData(); // for proto message, you can serialize it by toData() and deserialize it by fromData(data)

    ProtoFactory factory(1, 0, 0); // you can generate the factory. and proto message while auto regist to this factory.
    auto proto = factory.create("SerializerAble"); // you can create the proto message by the name or type value.
    proto->fromData(data);
    return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.