LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
retrieve_checker.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #pragma once
18 #ifndef RETRIEVE_CHECKER_HPP
19 #define RETRIEVE_CHECKER_HPP
20 #include "engine/item.h"
21 /*
22  *
23  * I know that this is a fairly complex template setup, and would be simplified by having
24  * two distinct children for the native type converison case and the item* case.
25  *
26  * However, that would spread the complexity into the rest of the program, where the
27  * user would have to specify whether a type was being checked with the native checking
28  * API or the item* API.
29  *
30  * I am choosing in this case (and doing my best to with the other specialized template types)
31  * to consolidate complexity into one location and allow the user to take advantage of generic programming
32  * without dealing with the in's and out's of it.
33  *
34  * Maybe if a newer C++ version allows for template specialization inside of a template class this would be
35  * simpler. Also, there's probably a better way and I just don't know template metaprogramming that well
36  *
37  * If I have to use dynamic cast like this again I'm just going to write a new version of dynamic cast so it
38  * just returns 0 and doesn't fail on compilation.
39  *
40  */
41 namespace __HIDER__{
42  //helper classes to help with template specialization
43  template<class T, bool>
45  public:
46  //This cannot be a compile time since these functions are always instantiated
47  //regardless of the true/false, even for types that don't call it
48  static void check(const item* inval, T& val){
49  err(std::string() + "an value of type " + typeid(T).name() +
50  " is trying to retrieve a value from " +
51  inval->name() + ", an item of incompatible type " + inval->type(),
52  "__retrieve_checker::check_and_get_type(item*)",
53  "engine/item.h", FATAL_ERROR);
54  }
55  };
56  template<class T>
57  struct __retrieve_helper<T, true>{
58  //inside this struct we can treat T as a pointer type
59  //since this can only get here it T is a pointer inside the item heirarchy
60  public:
61  static void check(item* inval, T& val){
62  //if only dynamic cast just returned 0 when it statically couldn't cast
63  //If I find the need to repeat this idiom again I'll write out another dynamic_cast
64  //struct that would just return 0 upon a failed static cast, like int* to item*
65  T i_cast = dynamic_cast<T>(inval);
66  if(!i_cast){
67  err(std::string() + "An item of type " + val->type() +
68  " is trying to retrieve a value from " +
69  inval->name() + ", an item of incompatible type " + inval->type(),
70  "__retrieve_checker::check_and_get_type(item*)",
71  "engine/item.h", FATAL_ERROR);
72  }
73  val = i_cast;
74  }
75  };
76 }
77 
78 
81  public:
82  //Although void* is the opposite of type safety, it is simply an unparameterized wrapper
83  //to a type-safe function
84  virtual void check_and_get_type(const std::type_info& type, void* inval) = 0;
85  virtual void check_and_get_type(item* inval) = 0;
86  virtual void* get_ptr() = 0;
87  virtual ~retrieve_wrapper(){}
88 };
89 
90 //main retrieve_checker class
91 template<class T> class __retrieve_checker:public retrieve_wrapper{
92  T& val;
93  __retrieve_checker(T& inval):val(inval){
94  }
95  public:
96  virtual ~__retrieve_checker(){}
97  //this is to assist with data structures dealing with pointers
98  virtual void* get_ptr(){
99  return &val;
100  }
101  virtual void check_and_get_type(const std::type_info& type, void* inval){
102  if(typeid(T) == type){
103  val = *(T*)inval;
104  return;
105  }
106  err(std::string() + "An value of type " + typeid(T).name() +
107  " is trying to retrieve a value from a type of " +
108  type.name(),
109  "__retrieve_checker::check_and_get_type(item*)",
110  "engine/item.h", FATAL_ERROR);
111  }
112 
113  public:
114  inline void check_and_get_type(item* inval){
115  //The type traits ensure that first, T is a pointer (hopefully an item *),
116  //and second, that whatever type T is a pointer to is part of the item hierarchy
117  constexpr bool is_item_ptr = std::is_convertible<T, item*>::value;
119  }
120  friend class input;
121 };
122 #endif