DynamicConverter
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
- arithmetic types (such as int64_t, unsigned short, bool, and double)
- fbstring, std::string
- 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的更多相关文章
- Slf4j MDC 使用和 基于 Logback 的实现分析
前言 如今,在 Java 开发中,日志的打印输出是必不可少的, 关于 有了日志之后,我们就可以追踪各种线上问题.但是,在分布式系统中,各种无关日志穿行其中,导致我们可能无法直接定位整个操作流程.因此 ...
- 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 ...
随机推荐
- 实现一个简单的lazyman
function lazyman(name) { return new lazyman.fn.init(name); } lazyman.fn = lazyman.prototype = { cons ...
- opencv 图像增强
Mat src= imread( "D:\\OCR\\Debug\\Temp\\Rects\\wholeMat_00191_0_0.jpg"); Mat dest; Mat ker ...
- Mac安装三方软件
安装提示“xxx软件已损坏,打不开,您应该将它移到废纸篓”的提示,其实并不是软件本身有问题,而是Mac系统的一个安全机制问题步骤1:Spotlight搜索(快捷键:command+空格或右上角搜索的符 ...
- Sublime 笔记
1. 安装PackageControl 官网:https://packagecontrol.io/ v2: import urllib2,os,hashlib; h = '2915d1851351e5 ...
- MySQL 5.7笔记
1. 初始化 重命名my-default.ini为my.ini 添加character_set_server=utf8 运行mysqld --initialize 2. 重置密码 服务器运行: mys ...
- HihoCoder1049 后序遍历 分治水题
水题,是为了给难题(树形DP)做铺垫 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思的玩具——一棵由小球和木棍连接起来的二叉树! 小Ho对 ...
- 颜色叠加模式:mix-blend-mode
文章转自叠加模式 http://www.cgspread.com/3551.html 注释:1.混合模式的数学计算公式,另外还介绍了不透明度.2.这些公式仅适用于RGB图像,对于Lab颜色图像而言,这 ...
- Java并发--ConcurrentModificationException(并发修改异常)异常原因和解决方法
在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常.下面我们就来讨论 ...
- (function($){...})(jQuery)与$(function(){...})区别
$(function(){...}) 这种写法和jQuery(function(){...}),$(document).ready(function(){...})作用一样,表示文档载入后需要运行的代 ...
- 一个查看Cookie的便捷工具——EditThisCookie
Appium正在努力准备中,很快就要和大家见面了- 今天给大家分享一个查看cookies的工具,用fiddler总感觉有点麻烦,还乱七八糟的找不到到底哪个链接是当前网站的cookies: 首先,你用的 ...