Leonardus
Loading...
Searching...
No Matches
soa.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Library
20#include <string>
21#include <vector>
22#include <memory>
23#include <algorithm>
24
25// Inc HAA
26#include "dbc.h"
27#include "helper.h"
28
29// Inc Rich
30#include "so.h"
31
32
33class Context;
34
35
40class SOA : public SOcomposite {
44 struct SOAArray {
45 std::vector<SOp> stlvec_;
49 for( auto * ptr : stlvec_ )
50 delete ptr;
51 }
52 };
53
54 std::shared_ptr<SOAArray> vec_;
57protected:
58#ifndef DBC_IS_VOID
62 bool invariant() const noexcept override { /* LCOV_EXCL_START */
63 if( vec_ == nullptr )
64 return false;
65 return std::none_of( vec_->stlvec_.begin(), vec_->stlvec_.end(),
66 []( const SO * p ) {
67 return p == nullptr;
68 } );
69 } /* LCOV_EXCL_STOP */
70#endif
71
72
73public:
75 explicit SOA( size_t p_len = 0, bool p_exec = false );
76
77public: /* accessor */
85 void setSO( size_t p_index, SOp p_sop ) {
86 DBC_PRE( p_index < getSize() );
87 DBC_PRE( p_sop != nullptr );
88
89 delete vec_->stlvec_[p_index];
90 vec_->stlvec_[p_index] = p_sop;
91
92 if( treeheight( 0 ) >= maxtreeheight )
93 inErrExit( semantics, "composite object tree height exceeds maximum" );
94
95 DBC_INV;
96 }
97
103 SOp at( size_t p_pos ) const {
104 DBC_PRE( p_pos < getSize() );
105
106 return vec_->stlvec_[ p_pos ];
107 }
108
109public: /* virtual */
110 [[nodiscard]] SOA * dup() const override { return new SOA( *this ); }
111
115 [[nodiscard]] SOA * clone() const override;
116
117 std::string opequalequal() const override;
118
119 OTCode ot() const override {
120 return getExec() ? OTCode::Ax : OTCode::A;
121 }
122
123 std::string type() const override { return "arraytype"; }
124
125 bool equal( const SO * p_other ) const override {
126 auto o = dynamic_cast<const SOA*>( p_other );
127 if( !o ) return false;
128 if( getSize() == 0 && o->getSize() == 0 )
129 return true; // Exception: arrays of size zero are always equal.
130 return vec_ == o->vec_;
131 }
132
133 size_t getSize() const override { return vec_->stlvec_.size(); }
134
135 size_t treeheight( size_t p_myheight ) const override;
136
137public: /* other */
139 auto begin() { return vec_->stlvec_.begin(); }
140
142 auto end() { return vec_->stlvec_.end(); }
143
147 void reduce();
148
150 void unfold2exec( Context & k ) const;
151
156 DBC_PRE( getSize() > 0 );
158
159 SOp retval = at( 0 );
160 vec_->stlvec_.erase( begin() );
161 return retval;
162 }
163
165 void bind( Context & k );
166};
The context of execution.
Definition context.h:42
Semantic Object Array.
Definition soa.h:40
size_t treeheight(size_t p_myheight) const override
Tree Height.
Definition soa.cpp:113
SOp front_pop()
Returns a copy of the SOp at position 0 and removes this first position from the array.
Definition soa.h:155
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition soa.h:133
void unfold2exec(Context &k) const
Unfolds duplicates of the array-content to the execution stack.
Definition soa.cpp:86
std::string type() const override
Returns a type name.
Definition soa.h:123
SOA * clone() const override
Creates a new instance as copy with deep cloning.
Definition soa.cpp:74
void bind(Context &k)
Replaces executable names with operator objects recursively into elements that are SOA.
Definition soa.cpp:49
SOp at(size_t p_pos) const
Returns a copy of the SOp at the given position.
Definition soa.h:103
std::shared_ptr< SOAArray > vec_
The shared array.
Definition soa.h:54
void setSO(size_t p_index, SOp p_sop)
Setter for an array object.
Definition soa.h:85
bool equal(const SO *p_other) const override
Equality.
Definition soa.h:125
OTCode ot() const override
Returns an OTCode.
Definition soa.h:119
bool invariant() const noexcept override
Checks class invariants.
Definition soa.h:62
void reduce()
Reduces the array by one SO at the end.
Definition soa.cpp:129
SOA * dup() const override
Creates a new instance as copy following the red book definition.
Definition soa.h:110
auto begin()
Returns an iterator to the first SOp.
Definition soa.h:139
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition soa.cpp:92
auto end()
Returns an iterator to the marker at end of the vector of SOps.
Definition soa.h:142
Semantic Object.
Definition so.h:58
bool getExec() const
Getter for exec_.
Definition so.h:74
Composite Semantic Object.
Definition so.h:165
Helpers for design by contract idioms.
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:84
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:78
#define DBC_INV_RAII(TT)
Defines an instance of the class DbCRAIIassert<>, which calls the invariant()-function at the return ...
Definition dbc.h:71
void inErrExit(InError p_err, const std::string &p_details, const std::source_location p_location)
Interpreter error message to interpreter cout_ and exit( EC_INTERPRETER / EC_CMDLINE ).
Definition error.cpp:74
@ semantics
infinite nesting of arrays; infinite loop loading values from dictionary;
Definition error.h:45
Miscellaneous definitions and functions.
constexpr size_t maxtreeheight
The maximum number of nesting levels of composite semantic objects.
Definition helper.h:78
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:33
@ A
SOA literal.
@ Ax
SOA executable.
@ o
SOo.
Semantic Object Array - array member class.
Definition soa.h:44
~SOAArray()
Dtor.
Definition soa.h:48
std::vector< SOp > stlvec_
The inner array.
Definition soa.h:45