summaryrefslogtreecommitdiffstats
path: root/raslib/structure.cc
blob: 4c8101556efb95091c5b27fa1075e04136ec9d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE:   structure.cc
 *
 * MODULE:   raslib
 * CLASS:    r_Structure
 *
 * COMMENTS:
 *
*/

#include "raslib/structure.hh"
#include "raslib/primitive.hh"
#include "raslib/structuretype.hh"
#include "raslib/error.hh"
#include "raslib/rminit.hh"
#include "mymalloc/mymalloc.h"

#ifdef __VISUALC__
#include <strstrea.h>
#else
#include <strstream>
#endif
#include <string.h>
#include <fstream>
#include <stdlib.h>




r_Structure::r_Structure( const char* newBuffer, const r_Structure_Type* newType )
  : r_Scalar( newType ),
    numElements(0),
    elements(NULL),
    valueBuffer(NULL)
{
  if( newType )
  {
  
    numElements = newType->count_elements(); 
    elements = new r_Scalar*[numElements];

    valueBuffer = (char*)mymalloc(newType->size());
  
    if(newBuffer)
      memcpy(valueBuffer, newBuffer, newType->size());
    else
      memset(valueBuffer, 0, newType->size());
      
    r_Structure_Type::attribute_iterator iter( newType->defines_attribute_begin() );

    for( int i=0; iter != newType->defines_attribute_end(); iter++, i++ )
    {
      if( (*iter).type_of().type_id() == r_Type::STRUCTURETYPE )     
        elements[i] = new r_Structure( valueBuffer + (*iter).offset(), (r_Structure_Type*)&((*iter).type_of()) );
      else
        elements[i] = new r_Primitive( valueBuffer + (*iter).offset(), (r_Primitive_Type*)&((*iter).type_of()) );
    }
  }
}



r_Structure::r_Structure( const r_Structure& obj )
  : r_Scalar( obj ),
    numElements(obj.numElements),
    elements(NULL),
    valueBuffer(NULL)
{
  if( numElements )
  {
    elements = new r_Scalar*[numElements];

    for( unsigned int i=0; i < numElements; i++ )
      elements[i] = obj.elements[i]->clone();
  }
  
  valueBuffer = (char*) mymalloc(valueType->size());
  
  if(obj.valueBuffer)
    memcpy(valueBuffer, obj.valueBuffer, valueType->size());
  else
    memset(valueBuffer, 0, valueType->size());
}



r_Structure::~r_Structure() 
{
  if( numElements )
  {
    for( unsigned int i =0; i < numElements; i++ )
      delete elements[i];

    delete[] elements;
  }  
  
  if(valueBuffer)
    free(valueBuffer);
}



r_Scalar* 
r_Structure::clone() const
{
  return new r_Structure( *this );
}



const r_Structure&
r_Structure::operator=( const r_Structure& obj )
{
  if( this != &obj )
  {
    // assign scalar
    r_Scalar::operator=( obj );

    if( numElements )
    {
      for( unsigned int i =0; i < numElements; i++ )
        delete elements[i];

      delete[] elements;
    }    

    if( obj.numElements )
    {
      numElements = obj.numElements;
      elements = new r_Scalar*[numElements];

      for( unsigned int i =0; i < numElements; i++ )
        elements[i] = obj.elements[i]->clone();
    }
    
    if(valueBuffer)
      free(valueBuffer);
    
    valueBuffer = (char*) mymalloc(valueType->size());

    if(obj.valueBuffer)
       memcpy(valueBuffer, obj.valueBuffer, valueType->size());
    else
       memset(valueBuffer, 0, valueType->size());
      
  }

  return *this;
}



unsigned int
r_Structure::count_elements() const
{
  return numElements;
}

const char* 
r_Structure::get_buffer() const
{
 memset(valueBuffer, 0, valueType->size());
 
 r_Structure_Type::attribute_iterator iter( ((r_Structure_Type*)valueType)->defines_attribute_begin() );
  
 for( int i=0; iter != ((r_Structure_Type*)valueType)->defines_attribute_end(); iter++, i++ )
 if( (*iter).type_of().type_id() == r_Type::STRUCTURETYPE )
  memcpy( valueBuffer + (*iter).offset(), ((r_Structure*)elements[i])->get_buffer(), elements[i]->get_type()->size());
 else
  memcpy( valueBuffer + (*iter).offset(), ((r_Primitive*)elements[i])->get_buffer(), elements[i]->get_type()->size());
 
 return valueBuffer;
}


bool
r_Structure::isStructure() const
{
  return true;
}



const r_Scalar&
r_Structure::operator[]( unsigned int index ) const throw( r_Error )
{
  if( !valueType )
	{
	RMInit::logOut << "r_Structure::operator[](" << index << ") const value type is NULL" << endl;
    throw r_Error( r_Error::r_Error_TypeInvalid );
	}

  if( index > numElements )
	{
	RMInit::logOut << "r_Structure::operator[](" << index << ") const index is out of bounds (" << numElements - 1 << ")" << endl;
    throw r_Eindex_violation( 0, numElements, index );
	}

  return *(elements[index]);
}



const r_Scalar&
r_Structure::operator[]( const char* name ) const throw( r_Error )
{
  if( !valueType )
  {
	RMInit::logOut << "r_Structure::operator[](" << name << ") value type is NULL" << endl;
    r_Error err( r_Error::r_Error_TypeInvalid );     
    throw( err );     
  }

  r_Structure_Type* structType = (r_Structure_Type*)valueType;

  r_Structure_Type::attribute_iterator iter( structType->defines_attribute_begin() );

  int index = 0;
  for( ; iter != structType->defines_attribute_end() && strcmp((*iter).name(), name); iter++, index++ );
  
  if( iter == structType->defines_attribute_end() )
  {
	RMInit::logOut << "r_Structure::operator[](" << name << ") name is not valid" << endl;
    r_Error err( r_Error::r_Error_NameInvalid );     
    throw( err );     
  }

  return *(elements[index]);
}


void
r_Structure::print_status( std::ostream& s ) const
{
  if( valueType )
  {
    s << "{ " << std::flush;

    for( unsigned int i =0; i < numElements; i++ )
    {
      s << *(elements[i]) << std::flush;

      if( i < numElements-1 )
        s << ", " << std::flush;
    }

    s << " }" << std::flush;
  }
  else
    s << "<nn>" << std::flush;
}



std::ostream& operator<<( std::ostream& s, const r_Structure& obj )
{
  obj.print_status( s );
  return s;
}