Flow123d  jenkins-Flow123d-windows32-release-multijob-51
accessors.cc
Go to the documentation of this file.
1 /*
2  * accessors.cc
3  *
4  * Created on: Apr 26, 2012
5  * Author: jb
6  */
7 
8 
9 #include <boost/make_shared.hpp>
10 #include <boost/lexical_cast.hpp>
11 #include "input/accessors.hh"
12 
13 
14 namespace Input {
15 
16 
17 /*************************************************************************************************************************************
18  * Implementation of InputException
19  */
20 
21 
22 const char * Exception::what() const throw () {
23  // have preallocated some space for error message we want to return
24  // Is there any difference, if we move this into ExceptionBase ??
25  static std::string message(1024,' ');
26 
27 
28  // Be sure that this function do not throw.
29  try {
30  std::ostringstream converter;
31 
32  converter << std::endl << std::endl;
33  converter << "--------------------------------------------------------" << std::endl;
34  converter << "User Error: ";
35  print_info(converter);
36 #ifdef DEBUG_MESSAGES
37  converter << "\n** Diagnosting info **\n" ;
38  converter << boost::diagnostic_information_what( *this );
39  print_stacktrace(converter);
40 #endif
41  converter << "--------------------------------------------------------" << std::endl;
42 
43  message = converter.str();
44  return message.c_str();
45 
46  } catch (std::exception &exc) {
47  std::cerr << "*** Exception encountered in exception handling routines ***" << std::endl << "*** Message is " << std::endl
48  << exc.what() << std::endl << "*** Aborting! ***" << std::endl;
49 
50  std::abort();
51  } catch (...) {
52  std::cerr << "*** Exception encountered in exception handling routines ***" << std::endl << "*** Aborting! ***"
53  << std::endl;
54 
55  std::abort();
56  }
57  return 0;
58 }
59 
60 
61 
62 
63 
64 /*****************************************************************************
65  * Implementation of the class Input::Address
66  */
67 
69 : data_(boost::make_shared<AddressData>())
70 {
71  data_->root_type_ = nullptr;
72  data_->root_storage_ = &Array::empty_storage_;
73  data_->parent_ = nullptr;
74  data_->descendant_order_ = 0;
75  data_->actual_storage_ = &Array::empty_storage_;
76 }
77 
78 
79 Address::Address(const StorageBase * storage_root, const Type::TypeBase *type_root)
80 : data_( boost::make_shared<AddressData>() )
81 {
82  if (! storage_root)
83  THROW( ExcAddressNullPointer() << EI_AccessorName("storage_root") );
84  if (! type_root )
85  THROW( ExcAddressNullPointer() << EI_AccessorName("type_root") );
86 
87  data_->root_type_ = type_root;
88  data_->root_storage_ = storage_root;
89  data_->parent_ = nullptr;
90  data_->descendant_order_ = 0;
91  data_->actual_storage_ = storage_root;
92 }
93 
94 
95 
97 : data_(other.data_)
98 {}
99 
100 
101 const Address * Address::down(unsigned int idx) const {
102 
103  Address *addr = new Address(this->data_->root_storage_, this->data_->root_type_);
104  addr->data_->parent_ = this->data_.get();
105  addr->data_->descendant_order_ = idx;
106  addr->data_->actual_storage_ = data_->actual_storage_->get_item(idx);
107 
108  return addr;
109 }
110 
111 
112 std::string Address::make_full_address() const {
114  AddressData * address_data = data_.get();
115  while (address_data->parent_ != NULL) {
116  path.push_back(address_data->descendant_order_);
117  address_data = address_data->parent_;
118  }
119 
120  // for empty path is returned address of root node
121  if (path.size() == 0) {
122  return "/";
123  }
124 
125  const StorageBase * storage = address_data->root_storage_;
126  const Type::TypeBase * input_type = address_data->root_type_;
127  std::string address = "";
128  int i = path.size()-1;
129 
130  while (i >= 0) {
131 
132  // dispatch types
133  if (typeid(*input_type) == typeid(Type::Record)) {
134  storage = storage->get_item(path[i]);
135  const Type::Record * rec = static_cast<const Type::Record *>(input_type);
136  Type::Record::KeyIter it = rec->begin() + path[i];
137  address = address + "/" + it->key_;
138  input_type = it->type_.get();
139  i--;
140  } else
141  if (typeid(*input_type) == typeid(Type::AbstractRecord)) {
142  const Type::AbstractRecord * a_rec = static_cast<const Type::AbstractRecord *>(input_type);
143  const StorageInt * storage_type = static_cast<const StorageInt *>(storage->get_item(0));
144  input_type = & a_rec->get_descendant(storage_type->get_int());
145  } else
146  if (typeid(*input_type) == typeid(Type::Array)) {
147  storage = storage->get_item(path[i]);
148  const Type::Array * arr = static_cast<const Type::Array *>(input_type);
149  address = address + "/" + boost::lexical_cast<std::string>(path[i]);
150  input_type = & arr->get_sub_type();
151  i--;
152  }
153  }
154 
155  return address;
156 }
157 
158 
159 
160 /*****************************************************************************
161  * Implementation of the class Input::Record
162  */
163 
164 
166 : record_type_(), address_( Address() )
167 {}
168 
169 
170 
172 : record_type_(rec.record_type_), address_(rec.address_)
173 {}
174 
175 
176 
177 Record::Record(const Address &address, const Type::Record type)
178 : record_type_(type), address_(address)
179 {
180  if (address.storage_head()->is_null())
181  THROW( ExcAccessorForNullStorage() << EI_AccessorName("Record") );
182 }
183 
184 
185 Input::EI_Address Record::ei_address() const
186 {
187  return EI_Address(address_string());
188 }
189 
190 
192 {
193  return address_.make_full_address();
194 }
195 
196 
197 
198 /*****************************************************************************
199  * Implementation of the class Input::AbstractRecord
200  */
201 
203 : record_type_(), address_( Address() )
204 {}
205 
206 
207 
209 : record_type_(rec.record_type_), address_(rec.address_)
210 {}
211 
212 
213 
215 : record_type_(type), address_(address)
216 {
217  if (address.storage_head()->is_null())
218  THROW( ExcAccessorForNullStorage() << EI_AccessorName("AbstractRecord") );
219 }
220 
221 
222 
223 AbstractRecord::operator Record() const
224 { return Record(address_,type()); }
225 
226 
227 
229 {
230  unsigned int type_id = address_.storage_head()->get_item(0)->get_int();
231  return record_type_.get_descendant(type_id);
232 }
233 
234 
235 Input::EI_Address AbstractRecord::ei_address() const
236 {
237  return EI_Address(address_string());
238 }
239 
241 {
242  return address_.make_full_address();
243 }
244 
245 
246 /*****************************************************************************
247  * Implementation of the class Input::Array
248  */
249 
250 
252 : array_type_(Type::Bool()), address_( Address() )
253 {}
254 
255 
256 Array::Array(const Array &ar)
257 : array_type_(ar.array_type_), address_(ar.address_)
258 {}
259 
260 
261 Array::Array(const Address &address, const Type::Array type)
262 : array_type_(type), address_(address)
263 {
264  if (address.storage_head()->is_null())
265  THROW( ExcAccessorForNullStorage() << EI_AccessorName("Array") );
266 }
267 
268 
269 Input::EI_Address Array::ei_address() const
270 {
271  return EI_Address(address_string());
272 }
273 
274 
275 
276 string Array::address_string() const
277 {
278  return address_.make_full_address();
279 }
280 
281 
283 
284 
285 /*****************************************************************************
286  * Explicit instantiation of accessor's templates
287  *
288  * .. TODO
289  */
290 
291 
292 
293 } // closing namespace Input
Address address_
Contains address and relationships with abstract record ancestor.
Definition: accessors.hh:481
Base of classes for declaring structure of the input data.
Definition: type_base.hh:63
Base class for nodes of a data storage tree.
Definition: storage.hh:57
Accessor to input data conforming to declared Array.
Definition: accessors.hh:521
std::vector< struct Key >::const_iterator KeyIter
Definition: type_record.hh:192
EI_Address ei_address() const
Definition: accessors.cc:269
const StorageBase * root_storage_
Definition: accessors.hh:194
virtual bool is_null() const =0
Definition: storage.cc:57
std::string make_full_address() const
Definition: accessors.cc:112
const char * what() const
Definition: accessors.cc:22
virtual int get_int() const
Definition: storage.cc:176
Input::Type::AbstractRecord record_type_
Corresponding Type::AbstractRecord object.
Definition: accessors.hh:478
Address address_
Contains address and relationships with record ancestor.
Definition: accessors.hh:408
boost::shared_ptr< AddressData > data_
Definition: accessors.hh:259
KeyIter begin() const
Definition: type_record.hh:702
const StorageBase * storage_head() const
Definition: accessors.hh:241
Class for declaration of inputs sequences.
Definition: type_base.hh:230
unsigned int descendant_order_
Definition: accessors.hh:186
virtual const StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:50
const Address * down(unsigned int idx) const
Definition: accessors.cc:101
EI_Address ei_address() const
Definition: accessors.cc:185
virtual int get_int() const
Definition: storage.cc:19
Input::Type::Record type() const
Definition: accessors.cc:228
Accessor to the data with type Type::Record.
Definition: accessors.hh:308
Class for declaration of polymorphic Record.
Definition: type_record.hh:463
string address_string() const
Definition: accessors.cc:240
static StorageArray empty_storage_
Need persisting empty instance of StorageArray that can be used to create an empty Address...
Definition: accessors.hh:587
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:423
EI_Address ei_address() const
Definition: accessors.cc:235
string address_string() const
Definition: accessors.cc:276
const Input::Type::TypeBase * root_type_
Definition: accessors.hh:190
const TypeBase & get_sub_type() const
Getter for the type of array items.
Definition: type_base.hh:266
Record type proxy class.
Definition: type_record.hh:161
const Record & get_descendant(const string &name) const
Definition: type_record.cc:540
void print_stacktrace(std::ostream &out) const
Prints formated stacktrace into given stream out.
Definition: exceptions.cc:75
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
virtual void print_info(std::ostringstream &out) const =0
Address address_
Contains address and relationships with array ancestor.
Definition: accessors.hh:594
string address_string() const
Definition: accessors.cc:191