/* * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #ifndef SHARE_VM_UTILITIES_TEMPLATE_IDIOMS_HPP #define SHARE_VM_UTILITIES_TEMPLATE_IDIOMS_HPP template struct IsBaseOfHost { operator Base*() const; operator Derived*(); }; // Statically check if two types are related in a class hierarchy template struct is_base_of { typedef char yes[1]; typedef char no[2]; template static yes &check(Derived*, T); static no &check(Base*, int); static const bool value = sizeof(check(IsBaseOfHost(), int())) == sizeof(yes); }; // Statically check if two types are equal template struct is_same { static const bool value = false; }; template struct is_same { static const bool value = true; }; template struct is_kind_of { static const bool value = is_same::value || is_base_of::value; }; // Enable a template substitution only if a certain condition holds template struct enable_if {}; template struct enable_if { typedef T type; }; // Like enable_if but infuse a dependency to a type not in the parameters to the template. // This would normally make SFINAE fail, but with the infused dependency, all is fine. template struct enable_if_depend: public enable_if<(sizeof(D) == -1 || value), T> {}; // Check for a certain typedef in a class #define GENERATE_SFINAE_TYPEDEF_CHECK(name) \ template \ struct has_typedef_##name { \ typedef char yes[1]; \ typedef char no[2]; \ \ template \ static yes& test(typename C::name*); \ \ template \ static no& test(...); \ \ static const bool value = sizeof(test(0)) == sizeof(yes); \ } // TODO: Currently generates compiler error when used on forward declarations // Check for a certain member declaration #define GENERATE_SFINAE_MEMBER_CHECK(name) \ template struct has_member_##name { \ struct Fallback { int name ; }; \ struct Derived: T, Fallback { }; \ \ template struct Check; \ \ template static char (&f(Check*))[1];\ template static char (&f(...))[2]; \ \ static bool const value = sizeof(f(0)) == 2; \ } // Checks for certain overload of a member function #define GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(function) \ template \ struct has_member_function_##function { \ typedef char yes[1]; \ typedef char no [2]; \ template struct Check; \ template static yes &check(Check *); \ template static no &check(...); \ static bool const value = sizeof(check(0)) == sizeof(yes); \ } GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_oop_nv); GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_metadata_nv); GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_klass_nv); GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_class_loader_data_nv); GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_oop); GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_metadata); GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_klass); GENERATE_SFINAE_MEMBER_FUNCTION_CHECK(do_class_loader_data); #endif // SHARE_VM_UTILITIES_TEMPLATE_IDIOMS_HPP