LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
variable.cpp
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 #include "variable.h"
18 #include "engineimp.h"
19 #include "item_heads.hpp"
20 #include "types/type_register.hpp"
24 void variable::print() const{
25  std::cout << "variable " << this->name() << "=" << value;
26 }
27 
34 void variable::_retrieve(retrieve_wrapper&& inval, item* caller){
35  inval.check_and_get_type(typeid(double), &value);
36  if(caller){
37  double* d = (double*)inval.get_ptr();
38  add_ref(*d, caller);
39  }
40 }
41 
42 void variable::add_ref(double& in, std::shared_ptr<item> inval){
43  in=value;
44  if(inval.use_count()){
45  safe_mods[inval].insert(&in);
46  }
47  else{
48 #ifdef DEBUG
49  err("Null pointer passed to variable add_ref", "variable::add_ref",
50  "engine/variable.cpp", FATAL_ERROR);
51 #else
52  err("Null pointer passed to variable add_ref", "variable::add_ref",
53  "engine/variable.cpp", WARNING);
54 #endif
55  }
56 }
57 void variable::add_ref(double& in, item* caller){
58  if(caller){
59  in = value;
60  double* d = &in;
61  //add pointer to the list of pointer for the given class
62  if(holder->item_exists(caller->name())){
63  safe_mods[std::weak_ptr<item>(holder->values[caller->name()].get_shared())].insert(d);
64  }
65  else{
66  modifiers[caller].insert(d);
67  err(std::string("Unsafe item ") + caller->name() +
68  std::string(" being added to a variable modifier list"),
69  "variable::retrieve", "engine/variable.cpp",
70  WARNING);
71  }
72  }
73  else{
74 #ifdef DEBUG
75  err("Null pointer passed to variable add_ref", "variable::add_ref",
76  "engine/variable.cpp", FATAL_ERROR);
77 #else
78  err("Null pointer passed to variable add_ref", "variable::add_ref",
79  "engine/variable.cpp", WARNING);
80 #endif
81  }
82 }
86 void variable::set(double p){
87  value = p;
88  //deal with periodicity here
89  while(value > high_bound){
91  }
92  while(value < low_bound){
94  }
95  //add items to engine's list of items that require updating
96  //Removes items from list if they no longer exist in the engine
97  {
98  std::map<item*, std::set<double*> >::iterator mit, mremove;
99 
100  for(mit = modifiers.begin(); mit!= modifiers.end();){
101  if(mit->first && !holder->item_exists(mit->first->name())){
102  mremove = mit;
103  ++mit;
104  modifiers.erase(mremove);
105  continue;
106  }
107  for(auto& sit:mit->second){
108  (*sit) = value;
109  }
110  if(mit->first){
111  holder->needs_updating(mit->first->name());
112  }
113  ++mit;
114  }
115  }
116  {
117  std::map<std::weak_ptr<item>, std::set<double*> >::iterator mit, mremove;
118 
119  for(mit = safe_mods.begin(); mit!= safe_mods.end();){
120  if(!mit->first.use_count()){
121  mremove = mit;
122  mit++;
123  safe_mods.erase(mremove);
124  continue;
125  }
126  for(auto& sit:mit->second){
127  (*sit) = value;
128  }
129  holder->needs_updating(mit->first.lock()->name());
130  mit++;
131  }
132  }
133 }
134 void variable::inc(double p){
135  set(value + p);
136 }
139  inc(inc_size);
140 }
141 
146 void variable::copy(double* inval){
147  *inval = value;
148 }
149 
150 void variable::parse(const std::string& inval){
151  _double::parse(inval);
152  inc_size=value;
153  low_bound=0;
154  high_bound=6.2382;
155 }
156 
158  return "var";
159 }
160 
161 template class type_register<variable>;
162