/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */

'folly/Traits.h'

‘folly/Traits.h’

Implements traits complementary to those provided in

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[1024];
  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


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.

Similarly,

Few common types, namely std::basic_string, std::vector, std::list, std::map, std::deque, std::set, std::unique_ptr, std::shared_ptr, std::function, which are compatible with fbvector are already instantiated and declared compatible with fbvector. fbvector 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 T matches the type of one of the other template parameter, T1, T2, …Tn. Recursion is used to implement this type trait.