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. [置顶] Isolation Forest算法实现详解

    本文算法完整实现源码已开源至本人的GitHub(如果对你有帮助,请给一个 star ),参看其中的 iforest 包下的 IForest 和 ITree 两个类: https://github.co ...

  2. java入门学习(2)—基本数据类型

    1.变量:定义变量:[数据类型] 变量名 = 赋值(这样定义的变量一般属于局部变量,放置在栈内存中): 2.标识符:可以有字母(可以使任意文字),数字,下划线,$等组成:但是不能以数字开头,不能是保留 ...

  3. python爬虫入门(4)-补充知识:XPath 教程(转自w3school)

    http://www.w3school.com.cn/xpath/index.asp 参考手册:http://www.w3school.com.cn/xpath/xpath_functions.asp ...

  4. swift 定义枚举和结构体 及使用

    //定义枚举 enum MapDirection { case North case South case East case West func simpleDescription() -> ...

  5. 【pandas】pandas.to_datatime()---时间格式转换

    标准时间格式:2012-12-21 时间转换函数:pandas.to_datatime() # -*- coding: utf- -*- # 生成数据 import pandas as pd data ...

  6. [QT]数据库SQLITE使用错误记录

    1.仿照创建数据库的例程编写代码,出现以下问题: ① 创建QSqlQuery query;   注意:这里没有将 query 与 QSqlSatabase db, 关联,正确的应该是 : QSqlQu ...

  7. 类(Classes)

    待写! 这里极力推荐博客园Vamei写的python系列文章,非常精彩,我只是遵照着The Python Tutorial目录来记录自己的学习体会,但也在看Vamei的文章,给大家推荐! 作者:Vam ...

  8. stm32寄存器版学习笔记03 外部中断

    stm32的每个I/O口都可以作为中断输入,要把I/O口设置为外部中断输入,必须将I/O口设置为上拉/下拉输入 或 浮空输入(但浮空的时候外部一定要带上拉或下拉电阻,否则可能导致 中断不停的触发),干 ...

  9. bean:write

    bean:write相当于<%=request.getAttribute("something")%> 例子一: 某处设置了request.setAttribute(& ...

  10. 《selenium2 python 自动化测试实战》(6)——打印信息和设置等待时间

    打印信息经常用的有两个: # coding: utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.get( ...