// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef TALK_BASE_TEMPLATE_UTIL_H_ #define TALK_BASE_TEMPLATE_UTIL_H_ #include // For size_t. namespace talk_base { // template definitions from tr1 template struct integral_constant { static const T value = v; typedef T value_type; typedef integral_constant type; }; template const T integral_constant::value; typedef integral_constant true_type; typedef integral_constant false_type; template struct is_pointer : false_type {}; template struct is_pointer : true_type {}; template struct is_same : public false_type {}; template struct is_same : true_type {}; template struct is_array : public false_type {}; template struct is_array : public true_type {}; template struct is_array : public true_type {}; template struct is_non_const_reference : false_type {}; template struct is_non_const_reference : true_type {}; template struct is_non_const_reference : false_type {}; template struct is_void : false_type {}; template <> struct is_void : true_type {}; namespace internal { // Types YesType and NoType are guaranteed such that sizeof(YesType) < // sizeof(NoType). typedef char YesType; struct NoType { YesType dummy[2]; }; // This class is an implementation detail for is_convertible, and you // don't need to know how it works to use is_convertible. For those // who care: we declare two different functions, one whose argument is // of type To and one with a variadic argument list. We give them // return types of different size, so we can use sizeof to trick the // compiler into telling us which function it would have chosen if we // had called it with an argument of type From. See Alexandrescu's // _Modern C++ Design_ for more details on this sort of trick. struct ConvertHelper { template static YesType Test(To); template static NoType Test(...); template static From& Create(); }; // Used to determine if a type is a struct/union/class. Inspired by Boost's // is_class type_trait implementation. struct IsClassHelper { template static YesType Test(void(C::*)(void)); template static NoType Test(...); }; } // namespace internal // Inherits from true_type if From is convertible to To, false_type otherwise. // // Note that if the type is convertible, this will be a true_type REGARDLESS // of whether or not the conversion would emit a warning. template struct is_convertible : integral_constant( internal::ConvertHelper::Create())) == sizeof(internal::YesType)> { }; template struct is_class : integral_constant(0)) == sizeof(internal::YesType)> { }; } // namespace talk_base #endif // TALK_BASE_TEMPLATE_UTIL_H_