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