LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
engine.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 "engine.h"
18 #include "engineimp.h"
19 #include "item.h"
20 #include "input.h"
21 #include "native_item.hpp"
22 
23 
24 void write_data(std::mutex& wait_m, std::mutex& data,
25  std::condition_variable& write_notify, data_io_info invals);
26 
27 char f_is_empty(std::ifstream& fstr){
28  return fstr.peek()==std::ifstream::traits_type::eof();
29 }
30 engineimp::engineimp(const std::string& fname, const std::string& outname,
31  const std::string& start_index):datas_queued(0), is_over(0){
32  values["!out_file"] = std::make_shared<string>();
33  ((native_item*)(values["!out_file"].get()))->parse(outname);
34  values["!out_file"]->setname("!out_file");
35  ofile=fopen(outname.c_str(), "w");
36  auto p = std::make_shared<integer>();
37  p->parse(start_index);
38  p->setname("!start_ind");
39  values["!start_ind"]=p;
40  std::ifstream fstr(fname.c_str());
41  if(f_is_empty(fstr)){
42  err("Empty/non-existant file passed as configuration parameter",
43  "engineimp::engineimp(std::string)", "engine/engine.cpp", FATAL_ERROR);
44  }
45  read(fstr);
46  datas_queued=0;
47  data_io_info dat_inf;
48  dat_inf.datas_queued = &datas_queued;
49  dat_inf.file = ofile;
50  dat_inf.writers = &async_write_queue;
51  dat_inf.is_over = &is_over;
52  write_thread = std::thread(
53  [dat_inf, this](){
54  ::write_data(this->condition_lock, this->data_lock, this->write_cond, dat_inf);
55  });
56 };
57 
59  this->write_dat();
60  is_over++;
61  while(datas_queued > 0){
62  }
63  std::unique_lock<std::mutex> lock(condition_lock);
64  write_cond.notify_all();
65  write_thread.join();
66  fclose(ofile);
67 }
68 bool engineimp::item_exists(std::shared_ptr<item> p) const{
69  if(!p){
70  return false;
71  }
72  for(auto& val : values){
73  if(val.second.get_shared() == p){
74  return true;
75  }
76  }
77  return false;
78 }
79 bool engineimp::item_exists(const std::string& val) const{
80  return values.count(val);
81 }
87  for(auto& val : update_vars){
88  val->update();
89  }
90  update_vars.clear();
91 }
97  auto pos = values.find(name);
98  if(pos == values.end()){
99  std::string errmess = "Lookup for item \"";
100  errmess.append(name);
101  errmess.append("\" has failed");
102  err(errmess, "engineimp::needs_updating", "engine/engine.cpp", WARNING);
103  return;
104  }
105  needs_updating(pos->second);
106 }
113 void engineimp::needs_updating(std::shared_ptr<item> inval){
114  if(!inval){
115  err("Null pointer passed to needs_updating", "engineimp::needs_updating",
116  "engine/engine.cpp", WARNING);
117  return;
118  }
119  update_vars.insert(inval);
120 }
121 /*
122  *
123  *Engine wrapper functions
124  *
125  *
126  *
127  *
128  */
129 
134 engine::engine(const std::string fname, const std::string outname, std::string index){
135  eng = new engineimp(fname, outname, index);
136 }
138  if(eng){
139  delete eng;
140  }
141 }
142 void engine::run(){
143  eng->run();
144 }