GithubHelp home page GithubHelp logo

julyerr / troy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wq19901103wq/troy

0.0 1.0 0.0 50 KB

An auto serializer for C++ non-pod struct, base on C++17 feature structured-binding , constexpr if and boost preprocessor

License: Apache License 2.0

Python 13.03% C++ 86.97%

troy's Introduction

Code Example

struct SomeStruct {
  int a;
  float b;
};
struct AnotherStruct {
  int a;
  double b;
  std::vector<SomeStruct> c;
  std::unordered_map<std::string, uint32_t> d;
  // ....
} another_struct;
// ... set another_struct
std::string str = SerializeToString(another_struct);
if (auto opb = ParseFromString<AnotherStruct>(str)) {
  // *opb is actualy equal to another_struct
}

Difference between other serialization

Method cross-language modify code needed container supprot memory
Troy by your alothgrim no yes low
Protobuf/Thrift yes need to write proto only vector and map (for proto3) high
boost serialization no yes no low
C#.NET framework only windows no yes low

How To Install

gcc-7.1

  • for ubuntu 14.04
sudo add-apt-repository ppa:jonathonf/gcc-7.1
sudo apt-get install gcc-7
  • for centos
 yum install gcc-7
  • for installation package

install gcc

bazel

  • Install JDK 8

  • Install JDK 8 by using:

sudo apt-get install openjdk-8-jdk
  • On Ubuntu 14.04 LTS you'll have to use a PPA:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java8-installer
  • Add Bazel distribution URI as a package source (one time setup)
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -

If you want to install the testing version of Bazel, replace stable with testing.

  • Install and update Bazel
sudo apt-get update && sudo apt-get install bazel
  • Once installed, you can upgrade to a newer version of Bazel with:
sudo apt-get upgrade bazel
  • More Documents about bazel

  • bazel

boost

  • for ubuntu 14.04

Run command

sudo apt-get install libboost-all-dev

Or you can call

aptitude search boost

find packages you need and install them using the apt-get command.

  • for installation package

install boost

Build And Test

Build

bazel build ...

Test

bazel test ...

Structure of Troy

structure

C++17 Language Features Used in Troy

Template argument deduction for class templates

Automatic template argument deduction much like how it's done for functions, but now including class constructors.

template <typename T = float>
struct MyContainer {
  T val;
  MyContainer() : val() {}
  MyContainer(T val) : val(val) {}
  // ...
};
MyContainer c1{ 1 }; // OK MyContainer<int>
MyContainer c2; // OK MyContainer<float>

Structured bindings

A proposal for de-structuring initialization, that would allow writing auto [ x, y, z ] = expr; where the type of expr was a tuple-like object, whose elements would be bound to the variables x, y, and z (which this construct declares). Tuple-like objects include std::tuple, std::pair, std::array, and aggregate structures.

using Coordinate = std::pair<int, int>;
Coordinate origin() {
  return Coordinate{0, 0};
}
const auto [ x, y ] = origin();
x; // == 0
y; // == 0

Selection statements with initializer

New versions of the if and switch statements which simplify common code patterns and help users keep scopes tight.

{
  std::lock_guard<std::mutex> lk(mx);
  if (v.empty()) v.push_back(val);
}
// vs.
if (std::lock_guard<std::mutex> lk(mx); v.empty()) {
  v.push_back(val);
}
Foo gadget(args);
switch (auto s = gadget.status()) {
  case OK: gadget.zip(); break;
  case Bad: throw BadFoo(s.message());
}
// vs.
switch (Foo gadget(args); auto s = gadget.status()) {
  case OK: gadget.zip(); break;
  case Bad: throw BadFoo(s.message());
}

constexpr if

Write code that is instantiated depending on a compile-time condition.

template <typename T>
constexpr bool isIntegral() {
  if constexpr (std::is_integral<T>::value) {
    return true;
  } else {
    return false;
  }
}
static_assert(isIntegral<int>() == true);
static_assert(isIntegral<char>() == true);
static_assert(isIntegral<double>() == false);
struct S {};
static_assert(isIntegral<S>() == false);

std::optional

The class template std::optional manages an optional contained value, i.e. a value that may or may not be present. A common use case for optional is the return value of a function that may fail.

std::optional<std::string> create(bool b) {
  if (b) {
    return "Godzilla";
  } else {
    return {};
  }
}

Acknowledgements

Author

Qian Wang([email protected])

License

Apache-2.0 license.

TODO

version control

  • 版本控制,前向兼容

protobuf / boost::archive alogthiom release

  • protobuf 和 boost::archive 算法在troy上的实现

verification interface

  • 提供校验接口, 防止文件被污染或错读

troy's People

Watchers

James Cloos avatar

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.