File: coreclass.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 (156 lines) | stat: -rw-r--r-- 3,297 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
/*
   FALCON - The Falcon Programming Language.
   FILE: coreclass.cpp

   Core Class implementation
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Thu Jan 20 2005

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

   See LICENSE file for licensing details.
*/

/** \file
   CoreClass implementation
*/

#include <falcon/cclass.h>
#include <falcon/coreobject.h>
#include <falcon/vm.h>
#include <falcon/itemdict.h>

namespace Falcon {

CoreClass::CoreClass( const Symbol *sym, LiveModule *lmod, PropertyTable *pt ):
   Garbageable(),
   m_lmod( lmod ),
   m_sym( sym ),
   m_properties( pt ),
   m_factory(sym->getClassDef()->factory()),
   m_states( 0 ),
   m_initState( 0 ),
   m_bHasInitEnter( false )
{
}

bool CoreClass::derivedFrom( const String &className ) const
{
   // is this class?
   if ( m_sym->name() == className )
      return true;

   // else, try with the properties/inheritance
   for( uint32 i = 0; i < properties().added(); ++i )
   {
      const Item& p = *properties().getValue(i)->dereference();
      if( p.isClass() && p.asClass()->derivedFrom( className ) )
      {
         return true;
      }
   }

   return false;
}

bool CoreClass::derivedFrom( const Symbol *sym ) const
{
   // is this class?
   /*if ( m_sym == sym || m_sym->name() == sym->name() )
      return true;
   */

   if ( m_sym == sym )
         return true;

   // else, try with the properties/inheritance
   for( uint32 i = 0; i < properties().added(); ++i )
   {
      const Item& p = *properties().getValue(i)->dereference();
      if( p.isClass() && p.asClass()->derivedFrom( sym ) )
      {
         return true;
      }
   }

   return false;
}



CoreClass::~CoreClass()
{
   delete m_properties;
   delete m_states;
}


void CoreClass::gcMark( uint32 gen )
{
   // first, mark ourselves.
   if ( gen != mark() )
   {
      mark( gen );

      // then mark our items,
      memPool->markItem( m_constructor );
      for( uint32 i = 0; i < properties().added(); i++ )
      {
         // ancestors are in the property table as classItems
         memPool->markItem( *properties().getValue(i) );
      }

      // and our states
      if( m_states != 0 )
      {
         m_states->gcMark( gen );
      }

      // and our module
      m_lmod->gcMark( gen );
   }
}


void CoreClass::states( ItemDict* sd, ItemDict* is )
{
   delete m_states;
   m_states = sd;
   // have we got an init state?
   m_initState = is;

   String name("__enter");
   if( is != 0 && is->find( &name ) )
      m_bHasInitEnter = true;

}


CoreObject *CoreClass::createInstance( void *userdata, bool bDeserial ) const
{
   if ( m_sym->isEnum() )
   {
      throw new CodeError( ErrorParam( e_noninst_cls, __LINE__ )
            .extra( m_sym->name() ) );
      // anyhow, flow through to allow user to see the object
   }


   // The core object will self configure,
   // eventually calling the user data constructor and creating the property vector.
   CoreObject *instance = m_factory( this, userdata, bDeserial );

   if( m_initState != 0 )
   {
      instance->setState( "init", m_initState );
   }

   return instance;
}

}


/* end of coreclass.cpp */