Skip to content

Commit

Permalink
- move add_const_from from traits.h to its own header (closes #27)
Browse files Browse the repository at this point in the history
- move `add_reference_from` from `traits.h` to its own header (closes #28)
- move `constify` from `traits.h` to its own header (closes #29)
- move `constify_from` from `traits.h` to its own header (closes #30)
- move `data_member_getter` from `traits.h` to its own header (closes #31)
- move `remove_rvalue_reference` from `traits.h` to its own header (closes #32)
- move `same_reference_as` from `traits.h` to its own header (closes #33)
  • Loading branch information
Marcelo Juchem committed Sep 13, 2016
1 parent e963e02 commit 12276ef
Show file tree
Hide file tree
Showing 16 changed files with 3,136 additions and 2,913 deletions.
47 changes: 47 additions & 0 deletions fatal/type/add_const_from.h
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#ifndef FATAL_INCLUDE_fatal_type_add_const_from_h
#define FATAL_INCLUDE_fatal_type_add_const_from_h

#include <type_traits>

namespace fatal {

/**
* Applies `std::add_const` to a type iff some other type is const.
*
* Example:
*
* struct foo {};
*
* // yields `foo`
* using result1 = add_const_from<foo, int>::type;
*
* // yields `foo const`
* using result2 = add_const_from<foo, int const>::type;
*
* // yields `foo const`
* using result3 = add_const_from<foo const, int const>::type;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T, typename>
struct add_const_from {
using type = T;
};

template <typename T, typename TFrom>
struct add_const_from<T, TFrom const> {
using type = typename std::add_const<T>::type;
};

} // namespace fatal

#endif // FATAL_INCLUDE_fatal_type_add_const_from_h
64 changes: 64 additions & 0 deletions fatal/type/add_reference_from.h
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#ifndef FATAL_INCLUDE_fatal_type_add_reference_from_h
#define FATAL_INCLUDE_fatal_type_add_reference_from_h

#include <type_traits>

namespace fatal {

/**
* Given types `T` and `U`:
* - if `U` is not a reference, yield `T`
* - if `U` is an l-value reference, yield `std::add_lvalue_reference<T>::type`
* - if `U` is an r-value reference, yield `std::add_rvalue_reference<T>::type`
*
* Example:
*
* struct foo {};
*
* // yields `foo`
* using result1 = add_reference_from<foo, int>::type;
*
* // yields `foo &&`
* using result2 = add_reference_from<foo &&, int>::type;
*
* // yields `foo &`
* using result3 = add_reference_from<foo, int &>::type;
*
* // yields `foo &`
* using result4 = add_reference_from<foo &&, int &>::type;
*
* // yields `foo &&`
* using result5 = add_reference_from<foo, int &&>::type;
*
* // yields `foo &`
* using result6 = add_reference_from<foo &, int &&>::type;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T, typename>
struct add_reference_from {
using type = T;
};

template <typename T, typename TFrom>
struct add_reference_from<T, TFrom &> {
using type = typename std::add_lvalue_reference<T>::type;
};

template <typename T, typename TFrom>
struct add_reference_from<T, TFrom &&> {
using type = typename std::add_rvalue_reference<T>::type;
};

} // namespace fatal

#endif // FATAL_INCLUDE_fatal_type_add_reference_from_h
54 changes: 54 additions & 0 deletions fatal/type/constify.h
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#ifndef FATAL_INCLUDE_fatal_type_constify_h
#define FATAL_INCLUDE_fatal_type_constify_h

#include <fatal/type/add_reference_from.h>

#include <type_traits>

namespace fatal {

/**
* Adds the const qualifier to a type.
*
* If the type is a reference, to some other type `U`,
* make it the same kind of reference to `U const`.
*
* Example:
*
* // yields `int const`
* using result1 = constify<int>::type;
*
* // yields `int const &`
* using result2 = constify<int &>::type;
*
* // yields `int const &&`
* using result3 = constify<int &&>::type;
*
* // yields `int const &`
* using result4 = constify<int const &>::type;
*
* // yields `int *const`
* using result4 = constify<int *>::type;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T>
struct constify {
using type = typename add_reference_from<
typename std::decay<T>::type const,
T
>::type;
};

} // namespace fatal

#endif // FATAL_INCLUDE_fatal_type_constify_h
57 changes: 57 additions & 0 deletions fatal/type/constify_from.h
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#ifndef FATAL_INCLUDE_fatal_type_constify_from_h
#define FATAL_INCLUDE_fatal_type_constify_from_h

#include <fatal/type/constify.h>

namespace fatal {

///////////////////
// constify_from //
///////////////////

/**
* Applies `constify` to a type iff some other type is const.
*
* Example:
*
* struct foo {};
*
* // yields `foo const`
* using result1 = constify_from<foo, int const>::type;
*
* // yields `foo const &`
* using result2 = constify_from<foo &, int const>::type;
*
* // yields `foo &&`
* using result3 = constify_from<foo &&, int>::type;
*
* // yields `foo const &`
* using result4 = constify_from<foo const &, int const>::type;
*
* // yields `foo const &`
* using result5 = constify_from<foo const &, int>::type;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T, typename>
struct constify_from {
using type = T;
};

template <typename T, typename TFrom>
struct constify_from<T, TFrom const> {
using type = typename constify<T>::type;
};

} // namespace fatal

#endif // FATAL_INCLUDE_fatal_type_constify_from_h

0 comments on commit 12276ef

Please sign in to comment.