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 ...
随机推荐
- linux下如何启动和关闭weblogic .
在你定义的域中可以找到如下命令: /[youHome]/domains/[yourDomain]/startWebLogic.sh /[youHome]/domains/[yourDomain]/st ...
- Linux:uniq命令详解
uniq uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用. 语法 uniq(选项)(参数) 选项 -c或——count:在每列旁边显示该行重复出现的次数: -d或--repeat ...
- 桔子桑Blog(小程序)V 0.4
这两天对这个个人博客小程序的UI又作了一些补充,目前看来,小程序的主要功能如下: 1.博客/日常栏目的导航切换 为了避免两个模块的UI上的过于单一,我将两个模块的列表页作了区分: 边距是自适应的(针对 ...
- Android敏捷开发、CI(持续集成)探究
比较老的几篇文章,依旧有学习价值 http://blog.csdn.net/baodinglaolang/article/details/9530695 http://blog.csdn.net/ba ...
- 音乐随想——斯美塔那—G小调钢琴协奏曲
乐源 Music -> Piano Trio -> Smetana:Piano Trioin G minor Op.15 总结 每一乐章都会有一段美到极致的主旋律. 第一乐章 起头即有凄凉 ...
- BZOJ1002 FJOI2007 轮状病毒 【基尔霍夫矩阵+高精度】
BZOJ1002 FJOI2007 轮状病毒 Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原 ...
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 如何在 .NET 库的代码中判断当前程序运行在 Debug 下还是 Release 下
我们经常会使用条件编译符 #if DEBUG 在 Debug 下执行某些特殊代码.但是一旦我们把代码打包成 dll,然后发布给其他小伙伴使用的时候,这样的判断就失效了,因为发布的库是 Release ...
- 进阶的Redis之数据持久化RDB与AOF
大家都知道,Redis之所以性能好,读写快,是因为Redis是一个内存数据库,它的操作都几乎基于内存.但是内存型数据库有一个很大的弊端,就是当数据库进程崩溃或系统重启的时候,如果内存数据不保存的话,里 ...
- Unicode字符转换成字符串
/*** * Unicode字符转换成字符串 * @param str * Unicode字符 * @return * String * * @author WXW */ public static ...