Flow123d  3.9.1-8f37ba8
accessors_impl.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file accessors_impl.hh
15  * @brief
16  */
17 
18 #ifndef ACCESSORS_IMPL_HH_
19 #define ACCESSORS_IMPL_HH_
20 
21 #include "input/factory.hh"
22 
23 namespace Input {
24 
25 using std::string;
26 
27 /******************************************************************************************
28  * Implementation of Input::Record
29  */
30 template <class Ret>
31 inline const Ret Record::val(const string &key) const {
32  try {
34 
35  ASSERT(key_it->default_.is_obligatory() || key_it->default_.has_value_at_declaration())(key)
36  .error("You have to use Record::find instead.");
37 
38  Iterator<Ret> it = Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
39  return *it;
40  }
41  // we catch all possible exceptions
42  catch (Type::Record::ExcRecordKeyNotFound & e) {
43  throw;
44  }
45  catch (ExcTypeMismatch & e) {
46  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
47  throw;
48  }
49  catch (ExcStorageTypeMismatch &e) {
50  throw;
51  }
52  catch (ExcAccessorForNullStorage &e) {
53  throw;
54  }
55 }
56 
57 
58 
59 template <class Ret>
60 inline const Ret Record::val(const string &key, const Ret default_val ) const {
61  try {
63 
64  ASSERT(key_it->default_.has_value_at_read_time())(key).error("You have to use Record::val or Record::find instead.");
65 
66  Iterator<Ret> it = Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
67  if (it)
68  return *it;
69  else
70  return default_val;
71  }
72  // we catch all possible exceptions
73  catch (Type::Record::ExcRecordKeyNotFound & e) {
74  throw;
75  }
76  catch (ExcTypeMismatch & e) {
77  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
78  throw;
79  }
80  catch (ExcStorageTypeMismatch &e) {
81  throw;
82  }
83  catch (ExcAccessorForNullStorage &e) {
84  throw;
85  }
86 }
87 
88 
89 
90 template <class Ret>
91 inline Iterator<Ret> Record::find(const string &key) const {
92  try {
94  return Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
95  }
96  // we catch all possible exceptions
97  catch (Type::Record::ExcRecordKeyNotFound & e) {
98  throw;
99  }
100  catch (ExcTypeMismatch & e) {
101  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
102  throw;
103  }
104 }
105 
106 template <class Ret>
107 inline bool Record::opt_val(const string &key, Ret &value) const {
108  try {
110  Iterator<Ret> it=Iterator<Ret>( *(key_it->type_), address_, key_it->key_index);
111  if (it) {
112  value = *it;
113  } else {
114  return false;
115  }
116  }
117  // we catch all possible exceptions
118  catch (Type::Record::ExcRecordKeyNotFound & e) {
119  throw;
120  }
121  catch (ExcTypeMismatch & e) {
122  e << EI_CPPRequiredType(typeid(Ret).name()) << EI_KeyName(key);
123  throw;
124  }
125 
126  return true;
127 }
128 
129 
130 /******************************************************************************************
131  * Implementation of Input::AbstractRecord
132  */
133 
134 template<class Type, class... Arguments>
135 const std::shared_ptr<Type> AbstractRecord::factory(Arguments... arguments) const {
136  return Input::Factory<Type, Arguments...>::instance()->create(this->type().type_name(), arguments...);
137 }
138 
139 
140 /******************************************************************************************
141  * Implementation of Input::Array
142  */
143 
144 template <class ValueType>
146  try {
148  }
149  catch (ExcTypeMismatch & e) {
150  e << EI_CPPRequiredType(typeid(ValueType).name()) << EI_KeyName("begin()");
151  throw e;
152  }
153 }
154 
155 
156 
157 inline IteratorBase Array::end() const {
159 }
160 
161 
162 
163 inline unsigned int Array::size() const {
165 }
166 
167 
168 
169 template <class Container>
170 void Array::copy_to(Container &out) const {
171  out.clear();
172  Iterator<typename Container::value_type> it = begin<typename Container::value_type>();
173 
174  for(;it != end(); ++ it) {
175  out.push_back(*it);
176  }
177 }
178 
179 
180 /******************************************************************************************
181  * Implementation of Input::IteratorBase
182  */
183 
184 inline bool IteratorBase::operator == (const IteratorBase &that) const
185  { return ( address_.storage_head() == that.address_.storage_head() && index_ == that.index_); }
186 
187 
188 
189 inline bool IteratorBase::operator != (const IteratorBase &that) const
190  { return ! ( *this == that ); }
191 
192 
193 
194 inline IteratorBase::operator bool() const {
195  StorageBase *s = address_.storage_head()->get_item(index_);
196  return ( s && ! s->is_null() );
197 }
198 
199 
200 
201 inline unsigned int IteratorBase::idx() const {
202  return index_;
203 }
204 
205 
206 /******************************************************************************************
207  * Implementation of Input::Iterator<Type>
208  */
209 
210 
211 template<class T>
213  index_++;
214  return *this;
215 }
216 
217 
218 
219 template<class T>
221  index_--;
222  return *this;
223 }
224 
225 
226 
227 template<class T>
229 
230  auto new_address =address_.down(index_);
231 
232  ASSERT_PTR(new_address->storage_head()).error();
233 
234  return internal::TypeDispatch < DispatchType > ::value(*new_address, type_);
235 }
236 
237 template<class T>
239  // OutputType has to be an accessor to call its method, e.g. iter->val("xyz"). Variable iter has to be e.g. Iterator.
240  BOOST_STATIC_ASSERT(
243 
244  // we have to make save temporary
245  temporary_value_ = this->operator*();
246  return &(temporary_value_);
247 
248 }
249 
250 
251 template<class T>
253  if (typeid(type) == typeid(InputType)) {
254  return static_cast<const InputType &>(type);
255  } else {
256  THROW(ExcTypeMismatch() << EI_InputType(type.type_name()) << EI_RequiredType(typeid(InputType).name()));
257  }
258 }
259 
260 
261 } // namespace Input
262 
263 #endif /* ACCESSORS_IMPL_HH_ */
Input::StorageBase
Base class for nodes of a data storage tree.
Definition: storage.hh:68
Input::Iterator::InputType
internal::TypeDispatch< DispatchType >::InputType InputType
Definition: accessors.hh:740
Input::AbstractRecord::factory
const std::shared_ptr< Type > factory(Arguments... arguments) const
Definition: accessors_impl.hh:135
Input::Address::storage_head
const StorageBase * storage_head() const
Definition: accessors.hh:224
Input::IteratorBase
Definition: accessors.hh:678
factory.hh
Input::Type::Record::KeyIter
std::vector< struct Key >::const_iterator KeyIter
Public typedef of constant iterator into array of keys.
Definition: type_record.hh:216
Input
Abstract linear system class.
Definition: balance.hh:40
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
Input::Iterator::operator->
OutputType * operator->() const
Definition: accessors_impl.hh:238
Input::Record::val
const Ret val(const string &key) const
Definition: accessors_impl.hh:31
value
static constexpr bool value
Definition: json.hpp:87
Input::StorageBase::get_array_size
virtual unsigned int get_array_size() const
Definition: storage.cc:79
Input::IteratorBase::operator==
bool operator==(const IteratorBase &that) const
Comparison of two Iterators. Do no compare types only position in the storage.
Definition: accessors_impl.hh:184
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
operator*
UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:235
Input::AbstractRecord::type
Input::Type::Record type() const
Definition: accessors.cc:273
Input::internal::TypeDispatch< DispatchType >
Input::Array::begin
Iterator< ValueType > begin() const
Definition: accessors_impl.hh:145
Input::Iterator
Definition: accessors.hh:143
Input::IteratorBase::address_
Address address_
Definition: accessors.hh:710
Input::Record::opt_val
bool opt_val(const string &key, Ret &value) const
Definition: accessors_impl.hh:107
Input::Array::size
unsigned int size() const
Definition: accessors_impl.hh:163
Input::Record::find
Iterator< Ret > find(const string &key) const
Definition: accessors_impl.hh:91
Input::Type
Definition: balance.hh:41
Input::Array::address_
Address address_
Contains address and relationships with array ancestor.
Definition: accessors.hh:639
Input::Record::get_type_key_iterator
virtual Type::Record::KeyIter get_type_key_iterator(const string &key) const
Return iterator to Type::Record key of given name.
Definition: accessors.cc:195
Input::Iterator::type_check_and_convert
static InputType type_check_and_convert(const Input::Type::TypeBase &type)
Definition: accessors_impl.hh:252
Input::IteratorBase::index_
unsigned int index_
Definition: accessors.hh:711
Input::Iterator::operator++
Iterator< T > & operator++()
Prefix. Advance operator.
Definition: accessors_impl.hh:212
Input::Type::TypeBase::type_name
virtual string type_name() const
Returns an identification of the type. Useful for error messages.
Definition: type_base.cc:206
Input::Type::Array::get_sub_type
const TypeBase & get_sub_type() const
Getter for the type of array items.
Definition: type_base.hh:397
Input::Iterator::operator*
OutputType operator*() const
Definition: accessors_impl.hh:228
Input::StorageBase::is_null
virtual bool is_null() const =0
Definition: storage.cc:73
Input::Array::array_type_
Input::Type::Array array_type_
Corresponding Type::Array.
Definition: accessors.hh:636
Input::StorageBase::get_item
virtual StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:66
Input::Factory::instance
static Factory * instance()
Get the single instance of the factory.
Definition: factory_impl.hh:27
Input::Factory::create
const shared_ptr< Type > create(string name, Arguments... arguments) const
create an instance of a registered class
Definition: factory_impl.hh:49
Input::Iterator::OutputType
internal::TypeDispatch< DispatchType >::ReadType OutputType
Definition: accessors.hh:736
Input::Type::TypeBase
Base of classes for declaring structure of the input data.
Definition: type_base.hh:92
ASSERT_PTR
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
Input::Array::end
IteratorBase end() const
Definition: accessors_impl.hh:157
Input::IteratorBase::operator!=
bool operator!=(const IteratorBase &that) const
Definition: accessors_impl.hh:189
Input::Record::address_
Address address_
Contains address and relationships with record ancestor.
Definition: accessors.hh:396
Input::Iterator::operator--
Iterator< T > & operator--()
Prefix. Back operator.
Definition: accessors_impl.hh:220
Input::Array::copy_to
void copy_to(Container &out) const
Definition: accessors_impl.hh:170
Input::IteratorBase::idx
unsigned int idx() const
Definition: accessors_impl.hh:201