1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
25 #ifndef WW_NEEDED_CAST_HXX
26 #define WW_NEEDED_CAST_HXX
27 
28 #include <tools/debug.hxx>
29 #   include "staticassert.hxx"
30 
31 namespace ww
32 {
checking_cast(Param in,Ret)33     template<typename Ret, typename Param> Ret checking_cast(Param in, Ret)
34     {
35         return static_cast<Ret>(in);
36     }
37 
checking_cast(Ret in,Ret)38     template<typename Ret> Ret checking_cast(Ret in, Ret)
39     {
40         DBG_ASSERT( false, "UnnecessaryCast" );
41         return in;
42     }
43 
44     /*
45      needed_cast is the same as static_cast except that there will be a compile
46      time assert when NDEBUG is not defined and the in and out types are the
47      same.  i.e. needed_cast catches unneccessary casts
48     */
needed_cast(Param in)49     template<typename Ret, typename Param> Ret needed_cast(Param in)
50     {
51         /*
52          Massage a single argument and a ret value into two arguments to allow
53          a determination if the dest type is the same as the sourct type
54         */
55         return checking_cast(in, Ret());
56     }
57 }
58 #endif
59 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
60