Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: src/utils.h

Issue 6350012: Switch from template functions overloading to partial template specialization. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 // type of a variable of another type. Of course the end result is likely to 753 // type of a variable of another type. Of course the end result is likely to
754 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005) 754 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
755 // will completely optimize BitCast away. 755 // will completely optimize BitCast away.
756 // 756 //
757 // There is an additional use for BitCast. 757 // There is an additional use for BitCast.
758 // Recent gccs will warn when they see casts that may result in breakage due to 758 // Recent gccs will warn when they see casts that may result in breakage due to
759 // the type-based aliasing rule. If you have checked that there is no breakage 759 // the type-based aliasing rule. If you have checked that there is no breakage
760 // you can use BitCast to cast one pointer type to another. This confuses gcc 760 // you can use BitCast to cast one pointer type to another. This confuses gcc
761 // enough that it can no longer see that you have cast one pointer type to 761 // enough that it can no longer see that you have cast one pointer type to
762 // another thus avoiding the warning. 762 // another thus avoiding the warning.
763
764 // We need different implementations of BitCast for pointer and non-pointer
765 // values. We use partial specialization of auxiliary struct to work around
766 // issues with template functions overloading.
767 template <class Dest, class Source>
768 struct BitCastHelper {
769 STATIC_ASSERT(sizeof(Dest) == sizeof(Source));
770
771 INLINE(static Dest cast(const Source& source)) {
772 Dest dest;
773 memcpy(&dest, &source, sizeof(dest));
774 return dest;
775 }
776 };
777
778 template <class Dest, class Source>
779 struct BitCastHelper<Dest, Source*> {
780 INLINE(static Dest cast(Source* source)) {
781 return BitCastHelper<Dest, uintptr_t>::
782 cast(reinterpret_cast<uintptr_t>(source));
783 }
784 };
785
Erik Corry 2011/01/25 10:30:40 Two blank lines.
763 template <class Dest, class Source> 786 template <class Dest, class Source>
764 inline Dest BitCast(const Source& source) { 787 inline Dest BitCast(const Source& source) {
765 // Compile time assertion: sizeof(Dest) == sizeof(Source) 788 return BitCastHelper<Dest, Source>::cast(source);
766 // A compile error here means your Dest and Source have different sizes.
767 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
768
769 Dest dest;
770 memcpy(&dest, &source, sizeof(dest));
771 return dest;
772 }
773
774 template <class Dest, class Source>
775 inline Dest BitCast(Source* source) {
776 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
777 } 789 }
778 790
779 } } // namespace v8::internal 791 } } // namespace v8::internal
780 792
781 #endif // V8_UTILS_H_ 793 #endif // V8_UTILS_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698