cast.hh revision 5733
15733Snate@binkert.org/* 25733Snate@binkert.org * Copyright (c) 2008 The Hewlett-Packard Development Company 35733Snate@binkert.org * All rights reserved. 45733Snate@binkert.org * 55733Snate@binkert.org * Redistribution and use in source and binary forms, with or without 65733Snate@binkert.org * modification, are permitted provided that the following conditions are 75733Snate@binkert.org * met: redistributions of source code must retain the above copyright 85733Snate@binkert.org * notice, this list of conditions and the following disclaimer; 95733Snate@binkert.org * redistributions in binary form must reproduce the above copyright 105733Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 115733Snate@binkert.org * documentation and/or other materials provided with the distribution; 125733Snate@binkert.org * neither the name of the copyright holders nor the names of its 135733Snate@binkert.org * contributors may be used to endorse or promote products derived from 145733Snate@binkert.org * this software without specific prior written permission. 155733Snate@binkert.org * 165733Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175733Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185733Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195733Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205733Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215733Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225733Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235733Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245733Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255733Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265733Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275733Snate@binkert.org * 285733Snate@binkert.org * Authors: Nathan Binkert 295733Snate@binkert.org */ 305733Snate@binkert.org 315733Snate@binkert.org#ifndef __BASE_CAST_HH__ 325733Snate@binkert.org#define __BASE_CAST_HH__ 335733Snate@binkert.org 345733Snate@binkert.org#include <cassert> 355733Snate@binkert.org 365733Snate@binkert.org// This is designed for situations where we have a pointer to a base 375733Snate@binkert.org// type, but in all cases when we cast it to a derived type, we know 385733Snate@binkert.org// by construction that it should work correctly. 395733Snate@binkert.org 405733Snate@binkert.org#if defined(DEBUG) 415733Snate@binkert.org 425733Snate@binkert.org// In debug builds, do the dynamic cast and assert the result is good 435733Snate@binkert.org 445733Snate@binkert.orgtemplate <class T, class U> 455733Snate@binkert.orginline T 465733Snate@binkert.orgsafe_cast(U ptr) 475733Snate@binkert.org{ 485733Snate@binkert.org T ret = dynamic_cast<T>(ptr); 495733Snate@binkert.org assert(ret); 505733Snate@binkert.org return ret; 515733Snate@binkert.org} 525733Snate@binkert.org 535733Snate@binkert.org#else 545733Snate@binkert.org 555733Snate@binkert.org// In non debug builds statically cast the result to the pointer we 565733Snate@binkert.org// want to use. This is technically unsafe, but this is only for 575733Snate@binkert.org// cases where we know that this should work by construction. 585733Snate@binkert.org 595733Snate@binkert.orgtemplate <class T, class U> 605733Snate@binkert.orginline T 615733Snate@binkert.orgsafe_cast(U ptr) 625733Snate@binkert.org{ 635733Snate@binkert.org return static_cast<T>(ptr); 645733Snate@binkert.org} 655733Snate@binkert.org 665733Snate@binkert.org#endif 675733Snate@binkert.org 685733Snate@binkert.org#endif // __BASE_CAST_HH__ 69