File: cacheobject.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (203 lines) | stat: -rw-r--r-- 4,675 bytes parent folder | download | duplicates (2)
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
/*
   FALCON - The Falcon Programming Language.
   FILE: cacheobject.cpp

   Cache Object - Standard instance of classes in script - base
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: dom dic 5 2004

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Falcon Object - Standard instance of classes in script - base
*/

#include <falcon/globals.h>
#include <falcon/cacheobject.h>
#include <falcon/string.h>
#include <falcon/stream.h>

#include <falcon/vm.h>

namespace Falcon {

CacheObject::CacheObject( const CoreClass *generator, bool bSeralizing ):
   CoreObject( generator ),
   m_cache( 0 )
{
   const PropertyTable &pt = m_generatedBy->properties();
   m_cache = (Item *) memAlloc( sizeof( Item ) * pt.added() );

   // We don't need to copy the default values if we're going to overwrite them.
   if ( ! bSeralizing )
   {
      // TODO: Check if a plain copy would do.
      for ( uint32 i = 0; i < pt.added(); i ++ )
      {
         const Item &itm = *pt.getValue(i);
         m_cache[i] = itm.isString() ? new CoreString( *itm.asString() ) :
                     itm;
      }
   }
}


CacheObject::CacheObject( const CacheObject &other ):
   CoreObject( other.generator() ),
   m_cache( 0 )
{
   const PropertyTable &pt = m_generatedBy->properties();

   m_cache = (Item *) memAlloc( sizeof( Item ) * pt.added() );
   for( uint32 i = 0; i < pt.added(); i++ )
   {
      const Item &itm = other.m_cache[i];
      //TODO: GARBAGE
      m_cache[i] = itm.isString() ? new CoreString( *itm.asString() ) :
                        itm;
   }
}

CacheObject::~CacheObject()
{
   memFree( m_cache );
}

void CacheObject::gcMark( uint32 mk )
{
   if( mk != mark() )
   {
      CoreObject::gcMark( mk );

      const PropertyTable &props = m_generatedBy->properties();
      for ( uint32 i = 0; i < props.added(); i ++ )
      {
        memPool->markItem( m_cache[i] );
      }
   }
}

bool CacheObject::setProperty( const String &propName, const Item &value )
{
   fassert( m_generatedBy != 0 );

   register uint32 pos;
   const PropertyTable &pt = m_generatedBy->properties();
   if ( pt.findKey( propName, pos ) )
   {
      if ( value.isReference() )
         m_cache[ pos ] = value;
      else
         *m_cache[ pos ].dereference() = value;
      return true;
   }

   return false;
}


bool CacheObject::getProperty( const String &propName, Item &ret ) const
{
   fassert( m_generatedBy != 0 );

   register uint32 pos;
   const PropertyTable &pt = m_generatedBy->properties();

   if ( pt.findKey( propName, pos ) )
   {
      ret = *m_cache[pos].dereference();
      return true;
   }

   return false;
}

bool CacheObject::serialize( Stream *stream, bool bLive ) const
{
   uint32 len = m_generatedBy->properties().added();
   uint32 elen = endianInt32( m_generatedBy->properties().added() );
   stream->write((byte *) &elen, sizeof(elen) );

   for( uint32 i = 0; i < len; i ++ )
   {
      if ( m_cache[i].serialize( stream, bLive ) != Item::sc_ok )
         return false;
   }

   CoreObject::serialize( stream, bLive );

   return stream->good();
}


bool CacheObject::deserialize( Stream *stream, bool bLive )
{
   VMachine *vm = VMachine::getCurrent();

   // Read the class property table.
   uint32 len;
   stream->read( (byte *) &len, sizeof( len ) );
   len = endianInt32( len );
   if ( len != m_generatedBy->properties().added() )
   {
      return false;
   }

   for( uint32 i = 0; i < len; i ++ )
   {
      if( ! stream->good() ) {
         return false;
      }

      Item temp;
      Item::e_sercode sc = m_cache[i].deserialize( stream, vm );
      if ( sc != Item::sc_ok )
      {
         return false;
      }
   }

   CoreObject::deserialize( stream, bLive );

   return true;
}


void CacheObject::reflectFrom( void *user_data )
{
   const PropertyTable &pt = m_generatedBy->properties();

   for ( uint32 i = 0; i < pt.added(); i ++ )
   {
      const PropEntry &entry = pt.getEntry(i);
      if( entry.m_eReflectMode != e_reflectNone )
      {
         entry.reflectFrom( this, user_data, m_cache[i] );
      }
   }
}


void CacheObject::reflectTo( void *user_data ) const
{
   const PropertyTable &pt = m_generatedBy->properties();

   for ( uint32 i = 0; i < pt.added(); i ++ )
   {
      const PropEntry &entry = pt.getEntry(i);
      if( entry.m_eReflectMode != e_reflectNone )
      {
         entry.reflectTo( const_cast<CacheObject *>(this), user_data, m_cache[i] );
      }
   }
}


}

/* end of cacheobject.cpp */