Update procedures
[shapes.git] / source / shapesvalue.h
blob9da940f8e43ab9ecfc0a6c10301cf8604ac4cfd8
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008, 2013, 2014 Henrik Tidefelt
19 #pragma once
21 #include "Shapes_Lang_decls.h"
22 #include "Shapes_Kernel_decls.h"
24 #include "refcount.h"
25 #include "classtreemacros.h"
26 #include "shapesexceptions.h"
28 #include <iostream>
29 #include <set>
30 #include <limits.h>
32 namespace Shapes
35 #define MAKE_TYPEID( T ) TYPEID_ ## T ,
36 namespace Kernel
38 enum QuickTypeID
39 { SINGLELOOP1( CLASSTREE1_ROOT, MAKE_TYPEID )
40 NUMBER_OF_TYPEID };
43 #define TYPEINFODECL \
44 static RefCountPtr< const ::Shapes::Lang::Class > TypeID; \
45 virtual const RefCountPtr< const ::Shapes::Lang::Class > & getClass( ) const; \
46 static RefCountPtr< const char > staticTypeName( );
49 #define TYPEINFOIMPL( T ) \
50 const RefCountPtr< const ::Shapes::Lang::Class > & \
51 Lang::T::getClass( ) const \
52 { \
53 return TypeID; \
54 } \
55 RefCountPtr< const char > \
56 Lang::T::staticTypeName( ) \
57 { \
58 return TypeID->getPrettyName( ); \
61 namespace Lang
64 class Value
66 mutable const Ast::Node * node_;
67 public:
68 Value( );
69 virtual ~Value( );
70 virtual const RefCountPtr< const ::Shapes::Lang::Class > & getClass( ) const = 0;
71 virtual Kernel::QuickTypeID getTypeID( ) const = 0;
73 virtual void gcMark( Kernel::GCMarkedSet & marked ) = 0;
75 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
77 virtual void show( std::ostream & os ) const;
79 RefCountPtr< const char > getTypeName( ) const;
81 inline void set_node( const Ast::Node * node ) const { node_ = node; }
82 inline const Ast::Node * node( ) const { return node_; }
84 DISPATCHBASEDECL;
87 class Void : public Lang::Value
89 public:
90 Void( );
91 virtual void show( std::ostream & os ) const;
92 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
93 TYPEINFODECL;
94 virtual Kernel::QuickTypeID getTypeID( ) const { return Kernel::TYPEID_NoOperatorOverloadValue; };
97 class NoOperatorOverloadValue : public Lang::Value
99 public:
100 NoOperatorOverloadValue( ){ }
101 virtual ~NoOperatorOverloadValue( ){ }
102 DISPATCHDECL;
103 // TYPEINFODECL;
106 class Geometric2D : public Lang::Value
108 public:
109 Geometric2D( ){ }
110 virtual ~Geometric2D( ){ }
111 virtual RefCountPtr< const Lang::Geometric2D > transformed( const Lang::Transform2D & transform, const RefCountPtr< const Lang::Geometric2D > & self ) const = 0;
112 virtual RefCountPtr< const Lang::Geometric3D > to3D( const RefCountPtr< const Lang::Geometric2D > & self ) const = 0;
113 TYPEINFODECL;
116 class NoOperatorOverloadGeometric2D : public Lang::Geometric2D
118 public:
119 NoOperatorOverloadGeometric2D( ){ }
120 virtual ~NoOperatorOverloadGeometric2D( ){ }
121 DISPATCHDECL;
122 // TYPEINFODECL;
125 class Geometric3D : public Lang::Value
127 public:
128 Geometric3D( ){ }
129 virtual ~Geometric3D( ){ }
130 virtual RefCountPtr< const Lang::Geometric3D > transformed( const Lang::Transform3D & transform, const RefCountPtr< const Lang::Geometric3D > & self ) const = 0;
131 virtual RefCountPtr< const Lang::Geometric2D > to2D( const Kernel::PassedDyn & dyn, const RefCountPtr< const Lang::Geometric3D > & self ) const = 0;
132 TYPEINFODECL;
135 class Hot : public Lang::NoOperatorOverloadValue
137 public:
138 Hot( );
139 virtual ~Hot( );
140 virtual Kernel::State * newState( ) const = 0;
141 TYPEINFODECL;
146 namespace Helpers
149 template< class T >
150 RefCountPtr< T >
151 down_cast( const RefCountPtr< const Lang::Value > & val, const Ast::SourceLocation & loc, bool voidIsNull = false )
153 RefCountPtr< T > res = val.down_cast< T >( );
154 if( res == NullPtr< T >( ) )
156 if( ! voidIsNull ||
157 dynamic_cast< const Lang::Void * >( val.getPtr( ) ) == 0 )
159 throw Exceptions::TypeMismatch( loc, val->getTypeName( ), T::staticTypeName( ) );
162 return res;
165 template< class T >
166 RefCountPtr< T >
167 down_cast( const RefCountPtr< const Lang::Value > & val, const char * internalLocStr, bool voidIsNull = false )
169 RefCountPtr< T > res = val.down_cast< T >( );
170 if( res == NullPtr< T >( ) )
172 if( ! voidIsNull ||
173 dynamic_cast< const Lang::Void * >( val.getPtr( ) ) == 0 )
175 throw Exceptions::TypeMismatch( Ast::theSourceLocationFactory.construct_internal( internalLocStr ), val->getTypeName( ), T::staticTypeName( ) );
178 return res;
181 template< class T >
182 RefCountPtr< T >
183 down_cast_internal( const RefCountPtr< const Lang::Value > & val )
185 RefCountPtr< T > res = val.down_cast< T >( );
186 if( res == NullPtr< T >( ) )
188 throw Exceptions::InternalError( "down_cast_internal: type mismatch." );
190 return res;
193 Kernel::VariableHandle newValHandle( const Lang::Value * val );