Flow123d  jenkins-Flow123d-windows-release-multijob-285
accessors_impl.hh
Go to the documentation of this file.
1 /*
2  * accessors_impl.hh
3  *
4  * Created on: Aug 1, 2012
5  * Author: jb
6  */
7 
8 #ifndef ACCESSORS_IMPL_HH_
9 #define ACCESSORS_IMPL_HH_
10 
11 
12 namespace Input {
13 
14 using std::string;
15 
16 /******************************************************************************************
17  * Implementation of Input::Record
18  */
19 template <class Ret>
20 inline const Ret Record::val(const string &key) const {
21  try {
23 
24  ASSERT( key_it->default_.is_obligatory() || key_it->default_.has_value_at_declaration(),
25  "The key '%s' is declared as optional or with default value at read time,"
26  " you have to use Record::find instead.\n", key.c_str());
27 
28  Iterator<Ret> it = Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
29  return *it;
30  }
31  // we catch all possible exceptions
32  catch (Type::Record::ExcRecordKeyNotFound & e) {
33  throw;
34  }
35  catch (ExcTypeMismatch & e) {
36  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
37  throw;
38  }
39  catch (ExcStorageTypeMismatch &e) {
40  throw;
41  }
42  catch (ExcAccessorForNullStorage &e) {
43  throw;
44  }
45 }
46 
47 
48 
49 template <class Ret>
50 inline const Ret Record::val(const string &key, const Ret default_val ) const {
51  try {
53 
54  ASSERT( key_it->default_.has_value_at_read_time(),
55  "The key %s is not declared with default value at read time,"
56  " you have to use Record::val or Record::find instead.\n", key.c_str());
57 
58  Iterator<Ret> it = Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
59  if (it)
60  return *it;
61  else
62  return default_val;
63  }
64  // we catch all possible exceptions
65  catch (Type::Record::ExcRecordKeyNotFound & e) {
66  throw;
67  }
68  catch (ExcTypeMismatch & e) {
69  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
70  throw;
71  }
72  catch (ExcStorageTypeMismatch &e) {
73  throw;
74  }
75  catch (ExcAccessorForNullStorage &e) {
76  throw;
77  }
78 }
79 
80 
81 
82 template <class Ret>
83 inline Iterator<Ret> Record::find(const string &key) const {
84  try {
86  return Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
87  }
88  // we catch all possible exceptions
89  catch (Type::Record::ExcRecordKeyNotFound & e) {
90  throw;
91  }
92  catch (ExcTypeMismatch & e) {
93  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
94  throw;
95  }
96 }
97 
98 template <class Ret>
99 inline bool Record::opt_val(const string &key, Ret &value) const {
100  try {
102  Iterator<Ret> it=Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
103  if (it) {
104  value = *it;
105  } else {
106  return false;
107  }
108  }
109  // we catch all possible exceptions
110  catch (Type::Record::ExcRecordKeyNotFound & e) {
111  throw;
112  }
113  catch (ExcTypeMismatch & e) {
114  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
115  throw;
116  }
117 
118  return true;
119 }
120 
121 
122 /******************************************************************************************
123  * Implementation of Input::Array
124  */
125 
126 template <class ValueType>
128  try {
130  }
131  catch (ExcTypeMismatch & e) {
132  e << EI_CPPRequiredType(typeid(ValueType).name()) << EI_KeyName("begin()");
133  throw e;
134  }
135 }
136 
137 
138 
139 inline IteratorBase Array::end() const {
141 }
142 
143 
144 
145 inline unsigned int Array::size() const {
147 }
148 
149 
150 
151 template <class Container>
152 void Array::copy_to(Container &out) const {
153  out.clear();
154  Iterator<typename Container::value_type> it = begin<typename Container::value_type>();
155 
156  for(;it != end(); ++ it) {
157  out.push_back(*it);
158  }
159 }
160 
161 
162 /******************************************************************************************
163  * Implementation of Input::IteratorBase
164  */
165 
166 inline bool IteratorBase::operator == (const IteratorBase &that) const
167  { return ( address_.storage_head() == that.address_.storage_head() && index_ == that.index_); }
168 
169 
170 
171 inline bool IteratorBase::operator != (const IteratorBase &that) const
172  { return ! ( *this == that ); }
173 
174 
175 
176 inline IteratorBase::operator bool() const {
177  const StorageBase *s = address_.storage_head()->get_item(index_);
178  return ( s && ! s->is_null() );
179 }
180 
181 
182 
183 inline unsigned int IteratorBase::idx() const {
184  return index_;
185 }
186 
187 
188 /******************************************************************************************
189  * Implementation of Input::Iterator<Type>
190  */
191 
192 
193 template<class T>
195  index_++;
196  return *this;
197 }
198 
199 
200 
201 template<class T>
203  index_--;
204  return *this;
205 }
206 
207 
208 
209 template<class T>
211 
212  auto new_address =address_.down(index_);
213 
214  ASSERT(new_address->storage_head(), "NULL pointer to storage in address object!!! \n");
215 
216  return internal::TypeDispatch < DispatchType > ::value(*new_address, type_);
217 }
218 
219 template<class T>
221  BOOST_STATIC_ASSERT(
222  (boost::is_same < Record, OutputType > ::value || boost::is_same < AbstractRecord, OutputType > ::value
223  || boost::is_same < Array, OutputType > ::value));
224 
225  // we have to make save temporary
226  temporary_value_ = this->operator*();
227  return &(temporary_value_);
228 
229 }
230 
231 
232 template<class T>
234  if (typeid(type) == typeid(InputType)) {
235  return static_cast<const InputType &>(type);
236  } else {
237  THROW(ExcTypeMismatch() << EI_InputType(type.type_name()) << EI_RequiredType(typeid(InputType).name()));
238  }
239 }
240 
241 
242 } // namespace Input
243 
244 #endif /* ACCESSORS_IMPL_HH_ */
Iterator< ValueType > begin() const
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
std::vector< struct Key >::const_iterator KeyIter
Definition: type_record.hh:200
virtual bool is_null() const =0
Definition: storage.cc:57
bool operator!=(const IteratorBase &that) const
bool operator==(const IteratorBase &that) const
Comparison of two Iterators. Do no compare types only position in the storage.
Address address_
Contains address and relationships with record ancestor.
Definition: accessors.hh:433
Iterator< Ret > find(const string &key) const
virtual string type_name() const
Returns an identification of the type. Useful for error messages.
Definition: type_base.hh:75
Input::Type::Array array_type_
Corresponding Type::Array.
Definition: accessors.hh:628
KeyIter key_iterator(const string &key) const
Definition: type_record.hh:707
const StorageBase * storage_head() const
Definition: accessors.hh:260
UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:172
Input::Type::Record record_type_
Corresponding Type::Record object.
Definition: accessors.hh:429
IteratorBase end() const
virtual const StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:50
bool opt_val(const string &key, Ret &value) const
#define ASSERT(...)
Definition: global_defs.h:121
const Ret val(const string &key) const
static InputType type_check_and_convert(const Input::Type::TypeBase &type)
virtual unsigned int get_array_size() const
Definition: storage.cc:63
Iterator< T > & operator++()
Prefix. Advance operator.
OutputType operator*() const
void copy_to(Container &out) const
unsigned int index_
Definition: accessors.hh:731
const TypeBase & get_sub_type() const
Getter for the type of array items.
Definition: type_base.hh:277
unsigned int idx() const
unsigned int size() const
internal::TypeDispatch< DispatchType >::ReadType OutputType
Definition: accessors.hh:756
OutputType * operator->() const
Iterator< T > & operator--()
Prefix. Back operator.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
Template for classes storing finite set of named values.
Address address_
Contains address and relationships with array ancestor.
Definition: accessors.hh:631