1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_TRAITS_IS_BASE_OF_HPP
  26 #define SHARE_VM_UTILITIES_TRAITS_IS_BASE_OF_HPP
  27 
  28 #include "utilities/traits/isClassOrUnion.hpp"
  29 #include "utilities/traits/isSame.hpp"
  30 #include "utilities/traits/removeCv.hpp"
  31 
  32 /**
  33  * IsBaseOfAndDerived<Base, Derived>::value is true if Derived is a derived class of Base, disgarding their CV qualifiers.
  34  * Returns false for non-class types.
  35  */
  36 template<typename Base, typename Derived>
  37 class IsBaseOfAndDerived {
  38   typedef char yes[1];
  39   typedef char no[2];
  40 
  41   template<typename T>
  42   static yes &check(Derived*, T);
  43   static no &check(Base*, int);
  44 
  45   template<typename B, typename D>
  46   struct IsBaseOfHost {
  47     operator B*() const;
  48     operator D*();
  49   };
  50 
  51   template<typename B, typename D>
  52   struct IsBaseOfAndDerivedInternal {
  53     static const bool value = sizeof(check(IsBaseOfHost<B, D>(), int())) == sizeof(yes);
  54   };
  55 
  56 public:
  57   static const bool value = IsClassOrUnion<Base>::value &&
  58       IsBaseOfAndDerivedInternal<typename RemoveCv<Base>::type, typename RemoveCv<Derived>::type>::value &&
  59       !IsSame<typename RemoveCv<Base>::type, typename RemoveCv<Derived>::type>::value;
  60 };
  61 
  62 /**
  63  * IsBaseOf<Base, Derived>::value is true if either Derived is a derived class of Base 
  64  * disregarding CV qualifiers or Derived and Base are the same type disregarding CV qualifiers.
  65  * Returns false for all non-class types. 
  66  * Note: It can currently return true for unions, but that behaviour
  67  * is subject to change when compiler compatibility for detecting unions is better, and hence
  68  * that behaviour should not be relied upon.
  69  */
  70 template<typename Base, typename Derived>
  71 class IsBaseOf {
  72 public:
  73   static const bool value = (IsBaseOfAndDerived<Base, Derived>::value ||
  74       IsSame<typename RemoveCv<Base>::type, typename RemoveCv<Derived>::type>::value) && 
  75       IsClassOrUnion<Base>::value;
  76 };
  77 
  78 #endif // SHARE_VM_UTILITIES_TRAITS_IS_BASE_OF_HPP
  79