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 ...
随机推荐
- GridView 控制默认分页页码间距 及字体大小
GridView 控制默认分页页码间距 及字体大小 PagerCss TD A:hover { WIDTH: 20px; COLOR: black; padding-left: 4px; paddin ...
- 手机通过笔记本开的WIFI访问TOMCAT服务器站点示例
我一直想用手机连上笔记本上的服务器TOMCAT,尝试了好久没连上,实验室一个妹子会这个技术,我也想学,自己摸索着学了几次,没成功,今晚想个办法试了一下,可以连接了,以后可以做手机网站开发了,这也是移动 ...
- 评价指标的计算:accuracy、precision、recall、F1-score等
记正样本为P,负样本为N,下表比较完整地总结了准确率accuracy.精度precision.召回率recall.F1-score等评价指标的计算方式: (右键点击在新页面打开,可查看清晰图像) 简单 ...
- python常用模块之shelve模块
python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...
- HDU 1073
http://acm.hdu.edu.cn/showproblem.php?pid=1073 模拟oj判题 随便搞,开始字符串读入的细节地方没处理好,wa了好久 #include <iostre ...
- SQL批量插入出现 类型转换错误
1.原因:在使用SqlBulkCopy批量操作时,Map映射会出现表结点对应错误 2.解决方案:自己先建立字段映射 using (SqlConnection con = new SqlConnecti ...
- selenium-java,启动谷歌浏览器和火狐浏览器
selenium3.4.0-java,启动谷歌浏览器和火狐浏览器-------------------------------------------------------------------- ...
- 日志组件logback的介绍及配置使用方法(一)
一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-acc ...
- 从BZOJ2242看数论基础算法:快速幂,gcd,exgcd,BSGS
LINK 其实就是三个板子 1.快速幂 快速幂,通过把指数转化成二进制位来优化幂运算,基础知识 2.gcd和exgcd gcd就是所谓的辗转相除法,在这里用取模的形式体现出来 \(gcd(a,b)\) ...
- BZOJ4903 UOJ300 CTSC2017 吉夫特 【Lucas定理】
BZOJ4903 UOJ300 CTSC2017 吉夫特 弱弱地放上题目链接 Lucas定理可以推一推,发现C(n,m)是奇数的条件是n" role="presentation&q ...