Flow123d  jenkins-Flow123d-windows-release-multijob-285
storage_transpose.cc
Go to the documentation of this file.
1 /*
2  * storage_modifier.cc
3  *
4  * Created on: Feb 13, 2014
5  * Author: jb
6  */
7 
9 
10 
11 namespace Input {
12 
13 StorageTranspose::StorageTranspose(const Type::TypeBase *target_type, const Type::TypeBase *source_type,
14  const StorageBase *source_storage, unsigned int vec_size)
15 : target_type_(target_type), source_type_(source_type), source_storage_(source_storage), vec_size_(vec_size)
16 {}
17 
18 
19 StorageBase * StorageTranspose::get_item(unsigned int index) {
21 }
22 
23 
25  const StorageBase *source_storage, unsigned int index) {
26 
27  if (typeid(*source_storage) == typeid(StorageNull)) {
28  return new StorageNull();
29  }
30 
31  // dispatch types
32  if (typeid(*source_type) == typeid(Type::Record)) {
33  return modify_storage(target_type, static_cast<const Type::Record *>(source_type),
34  source_storage, index);
35  } else
36  if (typeid(*source_type) == typeid(Type::Array)) {
37  return modify_storage(target_type, static_cast<const Type::Array *>(source_type),
38  source_storage, index);
39  } else
40  if (typeid(*source_type) == typeid(Type::Integer)) {
41  ASSERT( typeid(*target_type) == typeid(Type::Integer), "Incompatible type of target. Must be Type::Integer!\n");
42  ASSERT( typeid(*source_storage) == typeid(StorageInt), "Incompatible type of storage. Must be Integer!\n");
43  return source_storage->deep_copy();
44  } else
45  if (typeid(*source_type) == typeid(Type::Double)) {
46  ASSERT( typeid(*target_type) == typeid(Type::Double), "Incompatible type of target. Must be Type::Double!\n");
47  ASSERT( typeid(*source_storage) == typeid(StorageDouble), "Incompatible type of storage. Must be Double!\n");
48  return source_storage->deep_copy();
49  } else
50  if (typeid(*source_type) == typeid(Type::Bool)) {
51  ASSERT( typeid(*target_type) == typeid(Type::Bool), "Incompatible type of target. Must be Type::Boolean!\n");
52  ASSERT( typeid(*source_storage) == typeid(StorageBool), "Incompatible type of storage. Must be Boolean!\n");
53  return source_storage->deep_copy();
54  } else
55  if (typeid(*source_type) == typeid(Type::Selection)) {
56  ASSERT( typeid(*target_type) == typeid(Type::Selection), "Incompatible type of target. Must be Type::Selection!\n");
57  ASSERT( typeid(*source_storage) == typeid(StorageInt), "Incompatible type of storage. For selection must be Integer!\n");
58  return source_storage->deep_copy();
59  } else {
60  const Type::AbstractRecord * abstract_record_type = dynamic_cast<const Type::AbstractRecord *>(source_type);
61  if (abstract_record_type != NULL ) {
62  return modify_storage(target_type, abstract_record_type, source_storage, index);
63  }
64 
65  const Type::String * source_string = dynamic_cast<const Type::String *>(source_type);
66  if (source_string != NULL ) {
67  const Type::String * target_string = dynamic_cast<const Type::String *>(target_type);
68  ASSERT( target_string != NULL, "Incompatible type of target. Must be Type::String!\n");
69  ASSERT( typeid(*source_storage) == typeid(StorageString), "Incompatible type of storage. Must be String!\n");
70  return source_storage->deep_copy();
71  }
72 
73  // default -> error
74  xprintf(Err,"Unknown descendant of TypeBase class, name: %s\n", typeid(source_type).name());
75  }
76 
77  return new StorageNull();
78 }
79 
81  const StorageBase *source_storage, unsigned int index) {
82 
83  ASSERT( typeid(*target_type) == typeid(Type::Record), "Incompatible type of target type. Must be Record!\n");
84  ASSERT( typeid(*source_storage) == typeid(StorageArray), "Incompatible type of storage. Must be Array!\n");
85  ASSERT_EQUAL(source_type->size(), (static_cast<const Type::Record *>(target_type))->size());
86 
87  Type::Record::KeyIter target_it= (static_cast<const Type::Record *>(target_type))->begin();
88  StorageArray *storage_array = new StorageArray(source_type->size());
89 
90  for( Type::Record::KeyIter source_it= source_type->begin();
91  source_it != source_type->end();
92  ++source_it, ++target_it) {
93 
94  target_it->default_.has_same_type(source_it->default_);
95  ASSERT(target_it->default_.has_same_type(source_it->default_), "Incompatible default value of source type and target type!\n");
96 
97  StorageBase * sb = modify_storage( target_it->type_.get(), source_it->type_.get(),
98  source_storage->get_item(source_it->key_index), index );
99  storage_array->new_item(source_it->key_index, sb);
100  }
101 
102  return storage_array;
103 }
104 
105 
107  const StorageBase *source_storage, unsigned int index) {
108 
109  const Type::AbstractRecord *target_arec = dynamic_cast<const Type::AbstractRecord *>(target_type);
110  ASSERT( target_arec != NULL, "Incompatible type of target type. Must be AbstractRecord!\n");
111  ASSERT( typeid(*source_storage) == typeid(StorageArray), "Incompatible type of storage. Must be Array!\n");
112 
113  int descendant_index = source_storage->get_item(0)->get_int();
114 
115  return modify_storage( &( target_arec->get_descendant(descendant_index) ),
116  &( source_type->get_descendant(descendant_index) ),
117  source_storage, index );
118 }
119 
120 
122  const StorageBase *source_storage, unsigned int index) {
123  ASSERT( typeid(*source_storage) == typeid(StorageArray), "Incompatible type of storage. Must be Array!\n");
124 
125  if ( typeid(*target_type) == typeid(Type::Array) ) { // copy array
126  const Type::TypeBase *source_array_type = &( source_type->get_sub_type() );
127  const Type::TypeBase *target_array_type = &( static_cast<const Type::Array *>(target_type)->get_sub_type() );
128  unsigned int array_size = source_storage->get_array_size();
129  StorageArray *storage_array = new StorageArray(array_size);
130 
131  for (unsigned int i=0; i<array_size; i++) {
132  StorageBase * sb = modify_storage( target_array_type, source_array_type,
133  source_storage->get_item(i), index );
134  storage_array->new_item(i, sb);
135  }
136 
137  return storage_array;
138  }
139 
140  if ( *target_type == source_type->get_sub_type()) { // get member at index position
141  ASSERT(index < vec_size_, "Index of storage descendant is out of range.\n");
142  ASSERT_EQUAL(source_storage->get_array_size(), vec_size_);
143 
144  return source_storage->get_item(index)->deep_copy();
145  }
146 
147  ASSERT( false, "Incompatible type of target type. Must be Array or same type as subtype of source!\n");
148  return new StorageNull();
149 }
150 
151 
152 } /* namespace Input */
StorageBase * modify_storage(const Type::TypeBase *target_type, const Type::TypeBase *source_type, StorageBase const *source_storage, unsigned int index)
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
const StorageBase * source_storage_
std::vector< struct Key >::const_iterator KeyIter
Definition: type_record.hh:200
unsigned int size() const
Definition: type_record.hh:754
Class for declaration of the input of type Bool.
Definition: type_base.hh:332
StorageTranspose(const Type::TypeBase *target_type, const Type::TypeBase *source_type, StorageBase const *source_storage, unsigned int vec_size)
Class for declaration of the integral input data.
Definition: type_base.hh:355
KeyIter begin() const
Definition: type_record.hh:731
Class for declaration of inputs sequences.
Definition: type_base.hh:239
const Type::TypeBase * source_type_
virtual const StorageBase * get_item(const unsigned int index) const
Definition: storage.cc:50
virtual int get_int() const
Definition: storage.cc:19
const Type::TypeBase * target_type_
#define ASSERT(...)
Definition: global_defs.h:121
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:392
virtual StorageBase * deep_copy() const =0
#define ASSERT_EQUAL(a, b)
Definition: global_defs.h:136
#define xprintf(...)
Definition: system.hh:100
Class for declaration of polymorphic Record.
Definition: type_record.hh:487
void new_item(unsigned int index, StorageBase *item)
Definition: storage.cc:91
virtual unsigned int get_array_size() const
Definition: storage.cc:63
StorageBase * get_item(unsigned int index)
KeyIter end() const
Definition: type_record.hh:739
const TypeBase & get_sub_type() const
Getter for the type of array items.
Definition: type_base.hh:277
Definition: system.hh:72
Record type proxy class.
Definition: type_record.hh:169
const Record & get_descendant(const string &name) const
Definition: type_record.cc:597
Template for classes storing finite set of named values.