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 ...
随机推荐
- python的if判断补充
python的if判断补充 exit_flag = False # 标识符 if exit_flag == False: print('exit_flag == False') exit_flag = ...
- Objective C----手动管理内存和自动管理内存
对象的引用计数(Reference Counting) 正常情况下,当一段代码需要访问某个对象时,该对象的引用的计数加1:当这段代码不再访问该对象时,该对象的引用计数减1,表示这段代码不再访问该对象: ...
- 文件的copy
def mycopy(src_filename, dst_filename): try: fr = open(src_filename, "rb") try: try: fw = ...
- SEO中H1标签的用法- 1
在网上找了很多关于H1标签对SEO意义的资料,不可否认H1对SEO具有重大的意义,但是具体情况每个人有每个人的见解吧.这里主要根据网上搜索的资料,以及自己的一些经验整理出来的,但是本人毕竟不是专业SE ...
- BZOJ1407 NOI2002 Savage 【Exgcd】
BZOJ1407 NOI2002 Savage Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, L ...
- Linux下Eclipse配置安装 PyDev(Pydev插件一直不能成功,安装这个插件失败的问题)
pydev插件安装方式如果采取从网络上下载,然后解压到eclipse中文件夹到方式,运行到时候可能会导致重启eclipse后根本看不到这个插件! 原因以及解决方式,看下面! 转自:http://ww ...
- jekyll 安装使用
1. 安装 条件: ruby gem 注意版本,同时建议使用国内的镜像 gem install jekyll bundler 2. 创建网站 jekyll new my-awesome ...
- CMS搭建,织梦CMS使用教程
http://www.dedejs.com/ 织梦DedeCms 5.7全站去版权去广告方法(含后台) http://429006.com/article/technology/3367.htm 1. ...
- Python 函数 memoryview()
memoryview() 函数返回给定参数的内存查看对象(Momory view). 所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问.返回元组列表 ...
- Oracle DB备份恢复篇之丢失控制文件
实验目的 本篇主要模拟控制文件丢失后,如何根据实际情况恢复数据库,才能使数据库尽可能不丢失数据. 实验环境 1)Linux系统环境 [oracle@DG1 ~]$ lsb_release -a LSB ...