Update procedures
[shapes.git] / source / elementarytypes.h
blobcd8f80d57dfbd31a9613615cf48642b4e665a8cd
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, 2010, 2013, 2014 Henrik Tidefelt
19 #pragma once
21 #include "Shapes_Ast_decls.h"
22 #include "Shapes_Kernel_decls.h"
23 #include "Shapes_Concrete_decls.h"
25 #include "ptrowner.h"
26 #include "refcount.h"
27 #include "pdfstructure.h"
28 #include "shapesvalue.h"
29 #include "charptrless.h"
30 #include "elementarylength.h"
31 #include "consts.h"
32 #include "charconverters.h"
34 #include <list>
35 #include <iostream>
36 #include <stack>
37 #include <set>
40 namespace Shapes
43 namespace Concrete
45 class SplineTime
47 Concrete::Time t_;
48 bool isPast_;
49 public:
50 SplineTime( const SplineTime & orig ) : t_( orig.t_ ), isPast_( orig.isPast_ ) { };
51 SplineTime( Concrete::Time t ) : t_( t ), isPast_( false ) { }
52 SplineTime( Concrete::Time t, bool isPast ) : t_( t ), isPast_( isPast ) { }
54 const Concrete::Time & t( ) const { return t_; }
55 const bool & isPast( ) const { return isPast_; }
57 SplineTime & operator ++ ( ) { t_ += UNIT_TIME; return *this; }
61 namespace Lang
64 class Symbol : public Lang::Value
66 public:
67 typedef int KeyType;
68 private:
69 typedef std::map< const char *, int, charPtrLess > NameTableType;
70 typedef std::map< int, RefCountPtr< const char > > ReverseTableType;
71 static NameTableType nameTable;
72 static ReverseTableType reverseTable;
73 static KeyType nextUnique;
74 KeyType key_;
75 public:
76 Symbol( );
77 Symbol( int key );
78 Symbol( const char * name );
80 bool operator == ( const Symbol & other ) const;
81 bool operator != ( const Symbol & other ) const;
82 bool operator < ( const Symbol & other ) const;
83 bool operator > ( const Symbol & other ) const;
84 bool operator <= ( const Symbol & other ) const;
85 bool operator >= ( const Symbol & other ) const;
86 bool isUnique( ) const;
87 KeyType getKey( ) const { return key_; }
88 RefCountPtr< const char > name( ) const;
89 static RefCountPtr< const char > nameFromKey( KeyType key );
90 TYPEINFODECL;
91 virtual void show( std::ostream & os ) const;
92 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
93 DISPATCHDECL;
96 class Float : public Lang::Value
98 public:
99 typedef double ValueType;
100 ValueType val_;
101 Float( ValueType val ) : val_( val ) { };
102 TYPEINFODECL;
103 virtual void show( std::ostream & os ) const;
104 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
105 DISPATCHDECL;
108 class Integer : public Lang::Value
110 public:
111 typedef int ValueType;
112 typedef unsigned int ValueType_unsigned;
113 ValueType val_;
114 Integer( ValueType val ) : val_( val ) { };
115 bool operator == ( const Integer & other ) const;
116 bool operator != ( const Integer & other ) const;
117 bool operator < ( const Integer & other ) const;
118 bool operator > ( const Integer & other ) const;
119 bool operator <= ( const Integer & other ) const;
120 bool operator >= ( const Integer & other ) const;
121 TYPEINFODECL;
122 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
123 virtual void show( std::ostream & os ) const;
124 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
125 DISPATCHDECL;
128 class Length : public Lang::Value
130 public:
131 typedef Concrete::Length ValueType;
132 private:
133 bool isOffset_;
134 ValueType val_;
135 public:
136 Length( const Lang::Length & orig ) : isOffset_( orig.isOffset_ ), val_( orig.val_ ) { };
137 /* If the following is not explicit, there is a risk that
138 * operator <<
139 * gets applied to output a Concrete::Length via Lang::Length, which is typically not what we want.
141 explicit Length( ValueType val ) : isOffset_( false ), val_( val ) { };
142 Length( bool isOffset, ValueType val ) : isOffset_( isOffset ), val_( val ) { };
143 Concrete::Length get( Concrete::Length baseLength ) const;
144 Concrete::Length get( ) const;
145 double getScalar( Concrete::Length baseLength ) const;
146 double getScalar( ) const;
147 Lang::Length operator + ( const Lang::Length & term ) const;
148 Lang::Length operator - ( const Lang::Length & term ) const;
149 TYPEINFODECL;
150 virtual void show( std::ostream & os ) const;
151 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
152 friend std::ostream & operator << ( std::ostream & os, const Lang::Length & self );
153 DISPATCHDECL;
156 class Boolean : public Lang::Value
158 public:
159 bool val_;
160 Boolean( bool val ) : val_( val ) { };
161 TYPEINFODECL;
162 virtual void show( std::ostream & os ) const;
163 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
164 DISPATCHDECL;
167 class Character : public Lang::Value
169 public:
170 typedef Kernel::UnicodeCodePoint ValueType;
171 ValueType val_;
172 Character( ValueType val ) : val_( val ) { };
173 Character( ValueType::value_type code ) : val_( code ) { };
174 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
175 TYPEINFODECL;
176 virtual void show( std::ostream & os ) const;
177 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
178 DISPATCHDECL;
181 class String : public Lang::Value
183 public:
184 RefCountPtr< const char > val_;
185 size_t bytecount_;
186 String( RefCountPtr< const char > val ) : val_( val ), bytecount_( strlen( val.getPtr( ) ) ) { };
187 String( RefCountPtr< const char > val, size_t bytecount ) : val_( val ), bytecount_( bytecount ) { };
188 String( const char * val, bool duplicate ) : val_( duplicate ? strdup( val ) : val ), bytecount_( strlen( val ) ) { };
189 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
190 virtual ~String( );
191 TYPEINFODECL;
192 virtual void show( std::ostream & os ) const;
193 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
194 size_t UTF8count( ) const;
195 DISPATCHDECL;
198 class Continuation : public Lang::NoOperatorOverloadValue
200 public:
201 Kernel::ContRef cont_;
202 Continuation( const Kernel::ContRef & cont );
203 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
204 virtual ~Continuation( );
205 TYPEINFODECL;
206 virtual void show( std::ostream & os ) const;
207 virtual void gcMark( Kernel::GCMarkedSet & marked );
210 class Exception : public Lang::NoOperatorOverloadValue
212 const Ast::SourceLocation & loc_;
213 RefCountPtr< const Lang::Symbol > kind_;
214 RefCountPtr< const Lang::String > source_;
215 RefCountPtr< const Lang::Value > details_;
216 RefCountPtr< const char > message_;
217 Kernel::ContRef cont_;
218 Interaction::ExitCode exitCode_;
219 public:
220 /* First constructor allows source location to be set directly, second takes the location from the continuation.
222 Exception( const Ast::SourceLocation & loc, const RefCountPtr< const Lang::Symbol > & kind, const RefCountPtr< const Lang::String > & source, const RefCountPtr< const Lang::Value > & details, const RefCountPtr< const char > & message, const Kernel::ContRef & cont, Interaction::ExitCode exitCode );
223 Exception( const RefCountPtr< const Lang::Symbol > & kind, const RefCountPtr< const Lang::String > & source, const RefCountPtr< const Lang::Value > & details, const RefCountPtr< const char > & message, const Kernel::ContRef & cont, Interaction::ExitCode exitCode );
225 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
226 virtual ~Exception( );
227 TYPEINFODECL;
228 const Ast::SourceLocation & loc( ) const { return loc_; }
229 const Kernel::ContRef & cont( ) const { return cont_; }
230 virtual void show( std::ostream & os ) const;
231 virtual void gcMark( Kernel::GCMarkedSet & marked );
232 Interaction::ExitCode exitCode( ) const { return exitCode_; }
235 class FloatPair : public Lang::Value
237 public:
238 double x_;
239 double y_;
240 FloatPair( double x, double y ) : x_( x ), y_( y ) { };
241 FloatPair( const Concrete::UnitFloatPair & orig );
242 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
243 TYPEINFODECL;
244 virtual RefCountPtr< const Lang::FloatPair > transformed( const Lang::Transform2D & tf ) const;
245 virtual void show( std::ostream & os ) const;
246 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
247 DISPATCHDECL;
250 class FloatTriple : public Lang::Value
252 public:
253 double x_;
254 double y_;
255 double z_;
256 FloatTriple( double x, double y, double z ) : x_( x ), y_( y ), z_( z ) { };
257 FloatTriple( const Concrete::UnitFloatTriple & orig );
258 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
259 TYPEINFODECL;
260 virtual RefCountPtr< const Lang::FloatTriple > transformed( const Lang::Transform3D & tf ) const;
261 virtual void show( std::ostream & os ) const;
262 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
263 DISPATCHDECL;
266 inline
267 Lang::FloatTriple
268 cross( const Lang::FloatTriple & a, const Lang::FloatTriple & b )
270 return Lang::FloatTriple( a.y_ * b.z_ - a.z_ * b.y_,
271 a.z_ * b.x_ - a.x_ * b.z_,
272 a.x_ * b.y_ - a.y_ * b.x_ );
275 class Coords2D : public Lang::Geometric2D
277 public:
278 Lang::Length x_;
279 Lang::Length y_;
280 Coords2D( const Lang::Coords2D & orig );
281 Coords2D( const Lang::Length & x, const Lang::Length & y );
282 Coords2D( const Concrete::Length & x, const Concrete::Length & y );
283 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
284 Coords2D * transformedPtr( const Lang::Transform2D & tf ) const;
285 virtual RefCountPtr< const Lang::Geometric2D > transformed( const Lang::Transform2D & tf, const RefCountPtr< const Lang::Geometric2D > & self ) const;
286 virtual RefCountPtr< const Lang::Geometric3D > to3D( const RefCountPtr< const Lang::Geometric2D > & self ) const;
287 TYPEINFODECL;
288 virtual void show( std::ostream & os ) const;
289 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
290 friend std::ostream & operator << ( std::ostream & os, const Lang::Coords2D & self );
291 DISPATCHDECL;
294 class CornerCoords2D : public Lang::Coords2D
296 public:
297 double a_;
298 CornerCoords2D( const Lang::Length & x, const Lang::Length & y, double a );
299 CornerCoords2D( const Concrete::Length & x, const Concrete::Length & y, double a );
300 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
301 CornerCoords2D * transformedPtr( const Lang::Transform2D & tf ) const;
302 virtual RefCountPtr< const Lang::Geometric2D > transformed( const Lang::Transform2D & tf, const RefCountPtr< const Lang::Geometric2D > & self ) const;
303 virtual RefCountPtr< const Lang::Geometric3D > to3D( const RefCountPtr< const Lang::Geometric2D > & self ) const;
304 TYPEINFODECL;
305 DISPATCHDECL;
308 class Coords3D : public Lang::Geometric3D
310 public:
311 Lang::Length x_;
312 Lang::Length y_;
313 Lang::Length z_;
314 Coords3D( const Coords3D & orig );
315 Coords3D( const Lang::Length & x, const Lang::Length & y, const Lang::Length & z );
316 Coords3D( const Concrete::Length & x, const Concrete::Length & y, const Concrete::Length & z );
317 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
318 Coords3D * transformedPtr( const Lang::Transform3D & tf ) const;
319 virtual RefCountPtr< const Lang::Geometric3D > transformed( const Lang::Transform3D & tf, const RefCountPtr< const Lang::Geometric3D > & self ) const;
320 RefCountPtr< const Lang::Coords2D > make2D( Concrete::Length eyez ) const;
321 virtual RefCountPtr< const Lang::Geometric2D > to2D( const Kernel::PassedDyn & dyn, const RefCountPtr< const Lang::Geometric3D > & self ) const;
322 TYPEINFODECL;
323 virtual void show( std::ostream & os ) const;
324 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
325 friend std::ostream & operator << ( std::ostream & os, const Lang::Coords3D & self );
326 DISPATCHDECL;