folly/DynamicConverter.h

When dynamic objects contain data of a known type, it is sometimes useful to have its well-typed representation. A broad set of type-conversions are contained in DynamicConverter.h, and facilitate the transformation of dynamic objects into their well-typed format.

Usage


Simply pass a dynamic into a templated convertTo:

    dynamic d = { { , ,  }, { ,  } }; // a vector of vector of int
auto vvi = convertTo<fbvector<fbvector<int>>>(d);

Supported Types


convertTo naturally supports conversions to

  1. arithmetic types (such as int64_t, unsigned short, bool, and double)
  2. fbstring, std::string
  3. containers and map-containers

NOTE:

convertTo will assume that Type is a container if

  • it has a Type::value_type, and
  • it has a Type::iterator, and
  • it has a constructor that accepts two InputIterators

Additionally, convertTo will assume that Type is a map if

  • it has a Type::key_type, and
  • it has a Type::mapped_type, and
  • value_type is a pair of const key_type and mapped_type

If Type meets the container criteria, then it will be constructed by calling its InputIterator constructor.

Customization


If you want to use convertTo to convert dynamics into your own custom class, then all you have to do is provide a template specialization of DynamicConverter with the static method convert. Make sure you put it in namespace folly.

Example:

    struct Token {
int kind_;
fbstring lexeme_; explicit Token(int kind, const fbstring& lexeme)
: kind_(kind), lexeme_(lexeme) {}
};
namespace folly {
template <> struct DynamicConverter<Token> {
static Token convert(const dynamic& d) {
int k = convertTo<int>(d["KIND"]);
fbstring lex = convertTo<fbstring>(d["LEXEME"]);
return Token(k, lex);
}
};
}

DynamicConverter的更多相关文章

  1. Slf4j MDC 使用和 基于 Logback 的实现分析

    前言 如今,在 Java 开发中,日志的打印输出是必不可少的, 关于  有了日志之后,我们就可以追踪各种线上问题.但是,在分布式系统中,各种无关日志穿行其中,导致我们可能无法直接定位整个操作流程.因此 ...

  2. Folly: Facebook Open-source Library Readme.md 和 Overview.md(感觉包含的东西并不多,还是Boost更有用)

    folly/ For a high level overview see the README Components Below is a list of (some) Folly component ...

随机推荐

  1. 关于Gradle2.0的翻译说明

    Gradle1.12的翻译情况 Gradle实际上在4月16日就已经在对应的OmegaT项目上完成了翻译,后因项目繁忙,直到7月20日才完成了Github上Gradledoc项目及七牛站点的更新. 总 ...

  2. HDU 3345

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 最近重写usaco压力好大,每天写的都想吐.. 水一道bfs 注意的是开始旁边有敌人可以随便走,但是一旦开 ...

  3. 第23课 #error和#line使用分析

    #error的用法: 示例程序: #include <stdio.h> #ifndef __cplusplus #error This file should be processed w ...

  4. js之无缝轮播图

      HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  5. [剑指offer]09用两个栈实现队列插入和删除操作,C++实现

    原创博文,转载请注明出处! # 本文为牛客网<剑指offer>刷题笔记 1.题目 # 用两个栈实现队列的插入和删除操作 2.思路 栈服从先入后出的原则处理数据,队列服从先入先出的原则处理数 ...

  6. 2017年 ACM Journal Latex templates 新模板生成 acmart.cls 文件

    假定你的文稿在:/user/acmart-master那么cd /user/acmart-masterlatex acmart.ins最后可得到acmart.cls.

  7. Jmter-Test Fragment、Include Controller和Module Controller

    Test Fragment--测试片段 The Test Fragment is used in conjunction with the Include Controller and Module ...

  8. 利用ajax完成项目图册上传删除【实际项目】

    [项目页面效果] [前台jsp页面] jsp的js代码 <script type="text/javascript"> //上传项目图片 function upload ...

  9. 日志组件logback的介绍及配置使用方法(二)

    四.Logback的默认配置 如果配置文件 logback-test.xml 和 logback.xml 都不存在,那么 logback 默认地会调用BasicConfigurator ,创建一个最小 ...

  10. 每天一个linux命令:【转载】mkdir命令

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...