Update procedures
[shapes.git] / source / containertypes.h
blobe5bdfdfc8d7b1002c27c1b581b4bd15a4d8e96e7
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 Henrik Tidefelt
19 #pragma once
21 #include <list>
22 #include <iostream>
23 #include <stack>
24 #include <set>
26 #include "ptrowner.h"
27 #include "refcount.h"
28 #include "pdfstructure.h"
29 #include "shapesvalue.h"
30 #include "environment.h"
31 #include "charptrless.h"
33 namespace Shapes
35 namespace Lang
38 class SingleList : public Lang::NoOperatorOverloadValue
40 public:
41 SingleList( );
42 virtual ~SingleList( );
43 virtual bool isNull( ) const = 0;
44 virtual bool isForced( ) const = 0;
46 /* In order to be able to use the same method template as ConsPair, we support that the <op> argument is additionally passed as a VariableHandle. */
47 inline void foldl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const
49 this->foldl( evalState, op, nullResult, callLoc );
51 inline void foldr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const
53 this->foldr( evalState, op, nullResult, callLoc );
55 inline void foldsl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const
57 this->foldsl( evalState, op, nullResult, state, callLoc );
59 inline void foldsr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const
61 this->foldsr( evalState, op, nullResult, state, callLoc );
64 virtual void foldl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const = 0;
65 virtual void foldr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const = 0;
66 virtual void foldsl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const = 0;
67 virtual void foldsr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const = 0;
68 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
69 TYPEINFODECL;
72 class SingleListPair : public Lang::SingleList
74 public:
75 /* The data is provided public because it is used in function application
77 Kernel::VariableHandle car_;
78 RefCountPtr< const Lang::SingleList > cdr_;
80 private:
81 bool forced_;
83 public:
84 SingleListPair( const Kernel::VariableHandle & car, const RefCountPtr< const Lang::SingleList > & cdr );
85 virtual ~SingleListPair( );
86 virtual void show( std::ostream & os ) const;
87 virtual bool isNull( ) const;
88 virtual bool isForced( ) const;
89 virtual void foldl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
90 virtual void foldr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
91 virtual void foldsl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
92 virtual void foldsr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
93 virtual void gcMark( Kernel::GCMarkedSet & marked );
94 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
97 class SingleListNull : public Lang::SingleList
99 public:
100 SingleListNull( );
101 virtual ~SingleListNull( );
102 virtual void show( std::ostream & os ) const;
103 virtual bool isNull( ) const;
104 virtual bool isForced( ) const;
105 virtual void foldl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
106 virtual void foldr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
107 virtual void foldsl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
108 virtual void foldsr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
109 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
110 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
111 TYPEINFODECL;
114 /* Don't forget that there is also the template SingleListRange that implements Lang::SingleList, see singlelistrange.h!
117 class Structure : public Lang::NoOperatorOverloadValue
119 bool argListOwner_;
120 public:
121 /* The data is provided public because it is used in function application
123 const Ast::ArgListExprs * argList_;
124 RefCountPtr< const Lang::SingleList > values_;
125 Structure( const Ast::ArgListExprs * argList, const RefCountPtr< const Lang::SingleList > & values, bool argListOwner = false );
126 virtual ~Structure( );
127 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
128 Kernel::VariableHandle getPosition( size_t pos, const RefCountPtr< const Lang::Value > & selfRef ) const;
129 size_t valueCount( ) const; /* Number of elements in values_ (deduced in constant time from argList_). */
130 RefCountPtr< const Lang::Structure > getSink( size_t consumedArguments ) const;
131 virtual void gcMark( Kernel::GCMarkedSet & marked );
132 TYPEINFODECL;
135 class ConsPair : public Lang::NoOperatorOverloadValue
137 Kernel::VariableHandle car_;
138 Kernel::VariableHandle cdr_;
139 public:
140 ConsPair( const Kernel::VariableHandle & car, const Kernel::VariableHandle & cdr );
141 virtual ~ConsPair( );
142 const Kernel::VariableHandle & car( ) const { return car_; }
143 const Kernel::VariableHandle & cdr( ) const { return cdr_; }
144 virtual void show( std::ostream & os ) const;
145 void foldl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
146 void foldr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, const Ast::SourceLocation & callLoc ) const;
147 void foldsl( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
148 void foldsr( Kernel::EvalState * evalState, const RefCountPtr< const Lang::Function > & op, const Kernel::VariableHandle & opHandle, const Kernel::VariableHandle & nullResult, Kernel::StateHandle state, const Ast::SourceLocation & callLoc ) const;
149 virtual Kernel::VariableHandle getField( const char * fieldID, const RefCountPtr< const Lang::Value > & selfRef ) const;
150 virtual void gcMark( Kernel::GCMarkedSet & marked );
151 TYPEINFODECL;
156 namespace Kernel
158 class StructureFactory
160 const Ast::ArgListExprs * argList_;
161 std::map< const char *, Kernel::VariableHandle, charPtrLess > values_;
162 private:
163 void init( const std::list< const char * > & fields );
164 public:
165 StructureFactory( const std::list< const char * > & fields );
166 StructureFactory( const char * field1 );
167 StructureFactory( const char * field1, const char * field2 );
168 StructureFactory( const char * field1, const char * field2, const char * field3 );
169 StructureFactory( const char * field1, const char * field2, const char * field3, const char * field4 );
170 void clear( );
171 void set( const char * field, const Kernel::VariableHandle & value );
172 RefCountPtr< const Lang::Structure > build( );
175 class UnnamedStructureFactory
177 mutable std::map< size_t, const Ast::ArgListExprs * > argLists_;
178 public:
179 UnnamedStructureFactory( );
180 RefCountPtr< const Lang::Structure > build( const RefCountPtr< const Lang::SingleList > & values ) const;
184 namespace Helpers
186 RefCountPtr< const Lang::SingleList > SingleList_cons( Lang::Value * car, const RefCountPtr< const Lang::SingleList > & cdr );
187 RefCountPtr< const Lang::SingleList > SingleList_cons( const RefCountPtr< const Lang::Value > & car, const RefCountPtr< const Lang::SingleList > & cdr );