Updating the changelog in the VERSION file, and version_sync.
[shapes.git] / source / shapesvalue.h
blob6ee30dc8843a38d2839658f332653067521b7125
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 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 NoOperatorOverloadValue : public Lang::Value
89 public:
90 NoOperatorOverloadValue( ){ }
91 virtual ~NoOperatorOverloadValue( ){ }
92 DISPATCHDECL;
93 // TYPEINFODECL;
96 class Geometric2D : public Lang::Value
98 public:
99 Geometric2D( ){ }
100 virtual ~Geometric2D( ){ }
101 virtual RefCountPtr< const Lang::Geometric2D > transformed( const Lang::Transform2D & transform, const RefCountPtr< const Lang::Geometric2D > & self ) const = 0;
102 virtual RefCountPtr< const Lang::Geometric3D > to3D( const RefCountPtr< const Lang::Geometric2D > & self ) const = 0;
103 TYPEINFODECL;
106 class NoOperatorOverloadGeometric2D : public Lang::Geometric2D
108 public:
109 NoOperatorOverloadGeometric2D( ){ }
110 virtual ~NoOperatorOverloadGeometric2D( ){ }
111 DISPATCHDECL;
112 // TYPEINFODECL;
115 class Geometric3D : public Lang::Value
117 public:
118 Geometric3D( ){ }
119 virtual ~Geometric3D( ){ }
120 virtual RefCountPtr< const Lang::Geometric3D > transformed( const Lang::Transform3D & transform, const RefCountPtr< const Lang::Geometric3D > & self ) const = 0;
121 virtual RefCountPtr< const Lang::Geometric2D > to2D( const Kernel::PassedDyn & dyn, const RefCountPtr< const Lang::Geometric3D > & self ) const = 0;
122 TYPEINFODECL;
127 namespace Helpers
130 template< class T >
131 RefCountPtr< T >
132 down_cast( const RefCountPtr< const Lang::Value > & val, const Ast::SourceLocation & loc, bool voidIsNull = false )
134 RefCountPtr< T > res = val.down_cast< T >( );
135 if( res == NullPtr< T >( ) )
137 if( ! voidIsNull ||
138 dynamic_cast< const Lang::Void * >( val.getPtr( ) ) == 0 )
140 throw Exceptions::TypeMismatch( loc, val->getTypeName( ), T::staticTypeName( ) );
143 return res;
146 template< class T >
147 RefCountPtr< T >
148 down_cast( const RefCountPtr< const Lang::Value > & val, const char * internalLocStr, bool voidIsNull = false )
150 RefCountPtr< T > res = val.down_cast< T >( );
151 if( res == NullPtr< T >( ) )
153 if( ! voidIsNull ||
154 dynamic_cast< const Lang::Void * >( val.getPtr( ) ) == 0 )
156 throw Exceptions::TypeMismatch( Ast::SourceLocation( Ast::FileID::build_internal( internalLocStr ) ), val->getTypeName( ), T::staticTypeName( ) );
159 return res;
162 template< class T >
163 RefCountPtr< T >
164 down_cast_internal( const RefCountPtr< const Lang::Value > & val )
166 RefCountPtr< T > res = val.down_cast< T >( );
167 if( res == NullPtr< T >( ) )
169 throw Exceptions::InternalError( "down_cast_internal: type mismatch." );
171 return res;
174 Kernel::VariableHandle newValHandle( const Lang::Value * val );