Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/template_util.h"
      6 
      7 #include <string>
      8 
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace base {
     12 namespace {
     13 
     14 enum SimpleEnum { SIMPLE_ENUM };
     15 enum EnumWithExplicitType : uint64_t { ENUM_WITH_EXPLICIT_TYPE };
     16 enum class ScopedEnum { SCOPED_ENUM };
     17 enum class ScopedEnumWithOperator { SCOPED_ENUM_WITH_OPERATOR };
     18 std::ostream& operator<<(std::ostream& os, ScopedEnumWithOperator v) {
     19   return os;
     20 }
     21 struct SimpleStruct {};
     22 struct StructWithOperator {};
     23 std::ostream& operator<<(std::ostream& os, const StructWithOperator& v) {
     24   return os;
     25 }
     26 
     27 // is_non_const_reference<Type>
     28 static_assert(!is_non_const_reference<int>::value, "IsNonConstReference");
     29 static_assert(!is_non_const_reference<const int&>::value,
     30               "IsNonConstReference");
     31 static_assert(is_non_const_reference<int&>::value, "IsNonConstReference");
     32 
     33 // A few standard types that definitely support printing.
     34 static_assert(internal::SupportsOstreamOperator<int>::value,
     35               "ints should be printable");
     36 static_assert(internal::SupportsOstreamOperator<const char*>::value,
     37               "C strings should be printable");
     38 static_assert(internal::SupportsOstreamOperator<std::string>::value,
     39               "std::string should be printable");
     40 
     41 // Various kinds of enums operator<< support.
     42 static_assert(internal::SupportsOstreamOperator<SimpleEnum>::value,
     43               "simple enum should be printable by value");
     44 static_assert(internal::SupportsOstreamOperator<const SimpleEnum&>::value,
     45               "simple enum should be printable by const ref");
     46 static_assert(internal::SupportsOstreamOperator<EnumWithExplicitType>::value,
     47               "enum with explicit type should be printable by value");
     48 static_assert(
     49     internal::SupportsOstreamOperator<const EnumWithExplicitType&>::value,
     50     "enum with explicit type should be printable by const ref");
     51 static_assert(!internal::SupportsOstreamOperator<ScopedEnum>::value,
     52               "scoped enum should not be printable by value");
     53 static_assert(!internal::SupportsOstreamOperator<const ScopedEnum&>::value,
     54               "simple enum should not be printable by const ref");
     55 static_assert(internal::SupportsOstreamOperator<ScopedEnumWithOperator>::value,
     56               "scoped enum with operator<< should be printable by value");
     57 static_assert(
     58     internal::SupportsOstreamOperator<const ScopedEnumWithOperator&>::value,
     59     "scoped enum with operator<< should be printable by const ref");
     60 
     61 // operator<< support on structs.
     62 static_assert(!internal::SupportsOstreamOperator<SimpleStruct>::value,
     63               "simple struct should not be printable by value");
     64 static_assert(!internal::SupportsOstreamOperator<const SimpleStruct&>::value,
     65               "simple struct should not be printable by const ref");
     66 static_assert(internal::SupportsOstreamOperator<StructWithOperator>::value,
     67               "struct with operator<< should be printable by value");
     68 static_assert(
     69     internal::SupportsOstreamOperator<const StructWithOperator&>::value,
     70     "struct with operator<< should be printable by const ref");
     71 
     72 // underlying type of enums
     73 static_assert(std::is_integral<underlying_type<SimpleEnum>::type>::value,
     74               "simple enum must have some integral type");
     75 static_assert(
     76     std::is_same<underlying_type<EnumWithExplicitType>::type, uint64_t>::value,
     77     "explicit type must be detected");
     78 static_assert(std::is_same<underlying_type<ScopedEnum>::type, int>::value,
     79               "scoped enum defaults to int");
     80 
     81 struct TriviallyDestructible {
     82   int field;
     83 };
     84 
     85 class NonTriviallyDestructible {
     86   ~NonTriviallyDestructible() {}
     87 };
     88 
     89 static_assert(is_trivially_destructible<int>::value, "IsTriviallyDestructible");
     90 static_assert(is_trivially_destructible<TriviallyDestructible>::value,
     91               "IsTriviallyDestructible");
     92 static_assert(!is_trivially_destructible<NonTriviallyDestructible>::value,
     93               "IsTriviallyDestructible");
     94 
     95 }  // namespace
     96 }  // namespace base
     97