LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
type_register.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 TYPE_REGISTER_HPP
19 #define TYPE_REGISTER_HPP
20 #include <type_traits>
21 #include <string>
22 #include <iostream>
23 #include "item_factory.h"
24 #include "defs.hpp"
25 class item;
26 
28 /*
29  * This function allows customizable construction code for use in type_register.
30  * The pointer it returns is owned by the type_register functionality
31  *
32  * type_register will fail if a specialization if this function doesn't return an item*,
33  * and if it returns a pointer that can't dynamic_cast to T*
34  */
35 template<class T>
37  return new T();
38 }
40 /*
41  * This class serves to force registration in item creator at program start
42  * An instantiation of type_register<T> will register T into the item_factory class,
43  * such that a call to item_factory::create_item(T::type())
44  * will return a pointer to a default constructed T
45  * \sa item_factory
46  */
47 template<class T>
49  static bool regis;
50  static bool init();
51  static item* create();
53 };
54 
55 template<class T>
57 
58 template<class T>
60  //initializes error code so error code doesn't fail
61  std::ios_base::Init init_output;
62  static bool has_been_called=false;
63  static_assert(std::is_base_of<item, T>::value, "type_register can only be used on classes that inherit from item");
64  static_assert(!std::is_abstract<T>::value, "type_register cannot be used with an abstract class");
65  //these conditions also assert that T defines a type function
66  //
67  //
68  //strictly, static_cast works since we have already established that T is a base of item,
69  //but a re-implementation of maker<T> may remove this behavior
70  T* temp = dynamic_cast<T*>(__maker__<T>());
71  if(temp){
72  if(has_been_called){
73  //redundant check but can't hurt, especially if code changes in the future
74  err(std::string() + "Type register has already registered type " + temp->type(),
75  "type_register::init", "utils/type_register.hpp", FATAL_ERROR);
76  }
77  has_been_called=true;
78  item_factory::register_type(temp->type(), __maker__<T>);
79  delete temp;
80  }
81  else{
82  err(std::string() + "type_register could not allocate a type of " + typeid(T).name(),
83  "type_register::init", "utils/type_register.hpp", FATAL_ERROR);
84  }
85 
86  return true;
87 }
88 
89 template<class T>
91  return new T();
92 }
93 #endif