LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
input.h
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 INPUT_H
19 #define INPUT_H
20 #include "item.h"
21 #include "item_wrapper.h"
24 /*
25  * The input class serves to abstract the actual values that were created by the input
26  * away from the act of retrieving them. This is used for two purposes as of now:
27  * - When a variable is defined locally in another varaible, this class provides the remapping
28  * - When a variable name is mapped to another, this class performs the mapping
29  *
30  * In addition, this class prevents other classes from ever seeing the actual input representation
31  *
32  * Inside the engine, each variable is given an input class, which serves to make redirections and mappings local
33  */
34 class input{
36  std::map<std::string, item_wrapper>* invals;
37 
39  std::map<std::string, item_wrapper> re_directions;
40 
42  std::map<std::string, std::string> re_mappings;
43 
45  item_wrapper& get_val(const std::string& value);
46  public:
47  input(){}
48  input(std::map<std::string, item_wrapper>& values);
49  inline const item* operator[](const std::string& inval){
50  return get_val(inval).get();
51  }
52 
54  void add_redir(const std::string& name, const item_wrapper& inv);
55 
57  void add_remap(const std::string& oldname, const std::string& newname);
58 
59  void insert_item(std::shared_ptr<item> inval);
60  //templated version to simplfy passing base classes
61  template<class T>
62  void insert_item(std::shared_ptr<T> inval){
63  static_assert(std::is_base_of<item, T>::value,
64  "Must insert types which are a base of item into the input");
65  insert_item(std::dynamic_pointer_cast<item>(inval));
66  }
67  friend void eval_lisp(std::string in_cmd, input& inv);
68 
69 
70 
72 
85  template<class T>
86  void retrieve(T& inval, const std::string& name, item* caller){
87 
88  item* sender = get_val(name).get();
89 
90  if(sender){
91  sender->_retrieve(__retrieve_checker<T>(inval), caller);
92  }
93  else{
94  if(caller){
95  err(caller->name() + "Has requested an item that does not exist", "retrieve",
96  "utils/retrieve_checker.hpp", caller, FATAL_ERROR);
97  }
98  else{
99  err("A nonexistent item has been requested", "retreive", "utils/retrieve_checker.hpp",
100  FATAL_ERROR);
101  }
102  }
103  }
104 
110  template<class T>
111  void retrieve(T& inval, const std::string& name, item* caller, T&& stdval){
112  item* sender = get_val(name).get();
113  if(sender){
114  sender->_retrieve(__retrieve_checker<T>(inval), caller);
115  }
116  else{
117  inval=std::forward<T>(stdval);
118  }
119  }
120  template<class T>
121  void retrieve(T& inval, const std::string& name, item* caller, const T& stdval){
122  item* sender = get_val(name).get();
123  if(sender){
124  sender->_retrieve(__retrieve_checker<T>(inval), caller);
125  }
126  else{
127  inval=stdval;
128  }
129  }
130 
131 
132  //non-tracking versions of retrieve
138  template<class T>
139  void retrieve_notrack(T& inval, const std::string& name){
140  retrieve(inval, name, 0);
141  }
146  template<class T>
147  void retrieve_notrack(T& inval, const std::string& name, T&& stdval){
148  retrieve(inval, name, 0, stdval);
149  }
150 
155  template<class T>
156  void retrieve_notrack(T& inval, const std::string& name, const T& stdval){
157  retrieve(inval, name, 0, stdval);
158  }
159 
160 };
161 #endif