'folly/Traits.h'

Implements traits complementary to those provided in <type_traits>

  • Implements IsRelocatable trait.
  • Implements IsOneOf trait
  • Macros to state the assumptions easily

Motivation


<type_traits> is the Standard type-traits library defining a variety of traits such as is_integral or is_floating_point. This helps to gain more information about a given type.

folly/Traits.h implements traits complementing those present in the Standard.

IsRelocatable


In C++, the default way to move an object is by calling the copy constructor and destroying the old copy instead of directly copying the memory contents by using memcpy(). The conservative approach of moving an object assumes that the copied object is not relocatable. The two following code sequences should be semantically equivalent for a relocatable type:

{
void conservativeMove(T * from, T * to) {
new(to) T(from);
(*from).~T();
}
} {
void optimizedMove(T * from, T * to) {
memcpy(to, from, sizeof(T));
}
}

Very few C++ types are non-relocatable. The type defined below maintains a pointer inside an embedded buffer and hence would be non-relocatable. Moving the object by simply copying its memory contents would leave the internal pointer pointing to the old buffer.

class NonRelocatableType {
private:
char buffer[];
char * pointerToBuffer;
...
public:
NonRelocatableType() : pointerToBuffer(buffer) {}
...
};

We can optimize the task of moving a relocatable type T using memcpy. IsRelocatable::value describes the ability of moving around memory a value of type T by using memcpy.

Usage


  • Declaring types

template <class T1, class T2>
class MyParameterizedType; class MySimpleType;
  • Declaring a type as relocatable

Appending the lines below after definition of My*Type (MyParameterizedType or MySimpleType) will declare it as relocatable

/* Definition of My*Type goes here */
// global namespace (not inside any namespace)
namespace folly {
// defining specialization of IsRelocatable for MySimpleType
template <>
struct IsRelocatable<MySimpleType> : std::true_type {};
// defining specialization of IsRelocatable for MyParameterizedType
template <class T1, class T2>
struct IsRelocatable<MyParameterizedType<T1, T2>>
: ::std::true_type {};
}
  • To make it easy to state assumptions for a regular type or a family of parameterized type, various macros can be used as shown below.

  • Stating that a type is Relocatable using a macro

// global namespace
namespace folly {
// For a Regular Type
FOLLY_ASSUME_RELOCATABLE(MySimpleType);
// For a Parameterized Type
FOLLY_ASSUME_RELOCATABLE(MyParameterizedType<T1, T2>);
}

fbvector only works with relocatable objects. If assumptions are not stated explicitly, fbvector<MySimpleType> or fbvector<MyParameterizedType> will fail to compile due to assertion below:

static_assert(IsRelocatable<My*Type>::value, "");

FOLLY_ASSUME_FBVECTOR_COMPATIBLE*(type) macros can be used to state that type is relocatable and has nothrow constructor.

  • Stating that a type is fbvector-compatible using macros i.e. relocatable and has nothrow default constructor
// at global level, i.e no namespace
// macro for regular type
FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MySimpleType)
// macro for types having 2 template parameters (MyParameterizedType)
FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyParameterizedType)

Similarly,

  • FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(MyTypeHavingOneParameter) macro is for family of parameterized types having 1 parameter

  • FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(MyTypeHavingThreeParameters) macro is for family of parameterized types having 3 parameters

  • FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(MyTypeHavingFourParameters) macro is for family of parameterized types having 4 parameters

Few common types, namely std::basic_stringstd::vectorstd::liststd::mapstd::dequestd::setstd::unique_ptrstd::shared_ptrstd::function, which are compatible with fbvector are already instantiated and declared compatible with fbvectorfbvector can be directly used with any of these C++ types.

std::pair can be safely assumed to be compatible with fbvector if both of its components are.

IsOneOf


std::is_same<T1, T2>::value can be used to test if types of T1 and T2 are same. folly::IsOneOf<T, T1, Ts...>::value can be used to test if type of T1 matches the type of one of the other template parameter, T1, T2, ...Tn. Recursion is used to implement this type trait.

Traits的更多相关文章

  1. 仿SGI STL的traits技法

    首先是iterator traits,这个是用来萃取迭代器的特性的 #ifndef _STL_ITERATOR_H_ #define _STL_ITERATOR_H_ #include <cst ...

  2. PHP的学习--Traits新特性

    在阅读yii2源码的时候接触到了trait,就学习了一下,写下博客记录一下. 自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits. Traits 是一种为类似 PHP 的 ...

  3. Effective C++ -----条款47:请使用traits classes表现类型信息

    Traits classes使得“类型相关信息”在编译期可用.它们以template和“templates特化”完成实现. 整合重载技术(overloading)后,traits classes有可能 ...

  4. traits的使用

    trait的作用是可以在任何地方使用trait中的方法. trait的定义与定义类相同,定义实例如下: trait tSoneTrait{ //定义一些属性 function someFunction ...

  5. STL源码--iterator和traits编程技法

    第一部分 iterator学习 STL iterators定义: 提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素,而又无需暴露该聚合物的内部表达方式. 任何iteartor都应该提供5 ...

  6. PHP系列之一traits的应用

    Traits 在PHP中实现在方法的重复使用:Traits与Class相似,但是它能够在Class中使用自己的方法而不用继承: Traits在Class中优先于原Class中的方法,引用PHP Doc ...

  7. C++ traits

    [本文链接] http://www.cnblogs.com/hellogiser/p/cplusplus-traits.html [分析] 什么是traits?其实它并不是一个新的概念,上个世纪90年 ...

  8. STL源码分析《4》----Traits技术

    在 STL 源码中,到处可见 Traits 的身影,其实 Traits 不是一种语法,更确切地说是一种技术. STL库中,有一个函数叫做 advance, 用来将某个迭代器(具有指针行为的一种 cla ...

  9. C++设计新思维的traits和policy

    http://blog.csdn.net/zhoudaxia/article/details/4486487 这篇博客讲得挺清楚的,本来想自己写写看总结下的,不过看了下这个文章已经写得很清楚了,倒没有 ...

  10. STL源码分析读书笔记--第三章--迭代器(iterator)概念与traits编程技法

    1.准备知识 typename用法 用法1:等效于模板编程中的class 用法2:用于显式地告诉编译器接下来的名称是类型名,对于这个区分,下面的参考链接中说得好,如果编译器不知道 T::bar 是类型 ...

随机推荐

  1. Numpy 练习题

    1. 使用循环和向量化两种不同的方法来计算 100 以内的质数之和. 先定义个判断质数的函数.ps:纯手工打造,原生态,哈哈. def checkprime(x): if x<=1: retur ...

  2. redis优缺点

    redis主要是一个内存数据库.很多时候是被作为缓存来使用.redis的优势主要体现在和其他缓存方案进行比较,redis的不足主要是和其他数据库进行比较时体现的. 优点: 读写性能优异 支持数据持久化 ...

  3. Intel Omin-Path Architecture 搭建调优与测试

    OPA在Centos上的搭建 1. 首先确认Omni-Path Host Fabric Interfaces (HFIs) # yum install –y pciutils # lspci -vv ...

  4. 2018-2019-2 20165332《网络攻防技术》Exp5 MSF基础应用

    2018-2019-2 20165332<网络攻防技术>Exp5 MSF基础应用 1.基础问题回答 用自己的话解释什么是exploit,payload,encode. exploit:就是 ...

  5. 《Effective C#》读书笔记——条目13:正确地初始化静态成员变量<.NET资源管理>

      我们知道在C#语言中创建一个类型的实例前,就应该初始化该类型的所有静态成员变量.C#语言为我们提供了静态初始化器和静态构造函数.其中,静态构造函数是一个特殊的构造函数,将在其他所有方法执行前以及变 ...

  6. 《图解Http》 2-6章: 基础,报文,状态码,首部。

    HTTP协议和Cookie 是stateless协议,自身不对请求和响应之间的通信状态进行保存.但随着技术发展,为了实现保存状态的功能,引入了Cookie技术. Cookie在请求和响应报文中写入信息 ...

  7. VirtualBox使用物理硬盘建立磁盘

    VirtualBox,只能用命令行来 建立磁盘才可以使用物理硬盘. 1.运行cmd,cd进入你的VirtualBox目录,如:  cd C:\Program Files\Sun\VirtualBox ...

  8. EPANET头文件解读系列4——EPANET2.H

    该头文件的功能与系列3中的TOOLKIT.H类似,而且内容也几乎一致,所以也就不再详细介绍.

  9. 作业要求20181023-4 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 01

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284] 版本控制:https://git.coding.net/liuyy08 ...

  10. resin WED服务器初用遇到的问题和解决方法 java.lang.RuntimeException: java.net.SocketException: Unrecognized Windows Socke ts error: 0: JVM_Bind

    开启resin 服务器以后提示如下:(控制台不断的循环循环打印如下错误提示) java.lang.RuntimeException: java.net.SocketException: Unrecog ...