Leonardus
Loading...
Searching...
No Matches
sod.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Library
20#include <string>
21#include <string_view>
22#include <numeric>
23#include <algorithm>
24#include <unordered_map>
25#include <functional> // for std::hash
26
27// Inc Pure
28#include "dbc.h"
29#include "helper.h"
30
31// Inc Rich
32#include "so.h"
33#include "sosimp.h" // we reference SON
34
35
36class Context;
37
38
43class SOD : public SOcomposite {
44
51 struct FunctorHash {
52
58 size_t operator()( const SOp & p_key ) const {
59 DBC_PRE( p_key != nullptr );
60
61 const OTCode isN = p_key->ot();
62 if( SON::otcodeIsName( isN ) ) {
63 const SON * son = static_cast<const SON *>( p_key );
64 return std::hash<std::string>{}( son->getName() );
65 }
66 return std::hash<char>{}( static_cast<char>( isN ) );
67 }
68
69 };
70
71
77 struct FunctorEqual {
78
86 bool operator()( const SOp & p_lhs, const SOp & p_rhs ) const {
87 DBC_PRE( p_lhs != nullptr );
88 DBC_PRE( p_rhs != nullptr );
89
90 return p_lhs->equal( p_rhs );
91 }
92
93 };
94
95
101 struct SODMap {
102
103 std::unordered_map<SOp,SOp,FunctorHash,FunctorEqual> stlmap_{};
107 std::for_each( stlmap_.begin(), stlmap_.end(),
108 []( const auto & p_pair ) {
109 delete p_pair.first;
110 delete p_pair.second;
111 } );
112 }
113
114 };
115
116
118 std::shared_ptr<SODMap> map_;
119
120protected:
121
122 void * shptr() const override { return static_cast<void *>( map_.get() ); }
123
124
125#ifndef DBC_IS_VOID
129 bool invariant() const noexcept override { /* LCOV_EXCL_START */
131 return false;
132 return std::none_of( map_->stlmap_.begin(), map_->stlmap_.end(),
133 []( const auto & p ) {
134 return p.first == nullptr or p.second == nullptr;
135 } );
136 } /* LCOV_EXCL_STOP */
137#endif
138
139public:
140
143 : SOcomposite(),
144 map_(std::make_shared<SODMap>()) {
146 }
147
148
151 explicit SOD( const SOcomposite & p_other )
152 : SOcomposite( p_other ),
153 map_(std::make_shared<SODMap>()) {
155 }
156
157
159 SOD( const SOD & p_other ) : SOcomposite(p_other), map_(p_other.map_) {
161 }
162
163
165 ~SOD() override {
167 }
168
169
170 SOD( SOD&& ) = delete;
171 SOD & operator=( const SOD & ) = delete;
172 SOD & operator=( SOD && ) = delete;
173
174public: /* virtual */
175
176 [[nodiscard]] SOD * dup() const override { return new SOD( *this ); }
177
182 [[nodiscard]] SOD * clone() const override;
183
184
185 std::string opequalequal() const override;
186
187
188 OTCode ot() const override { return OTCode::D; }
189
190
191 std::string_view type() const override { return "dicttype"; }
192
193
194 size_t getSize() const override { return map_->stlmap_.size(); }
195
196
197 size_t dagheight( size_t p_myheight ) const override;
198
199public: /* other */
200
202 auto begin() const { return map_->stlmap_.begin(); }
203
204
206 auto end() const { return map_->stlmap_.end(); }
207
208
216 void insert( SOp p_key, SOp p_value );
217
218
225 SOp find( SO * p_key ) const;
226
227
233 auto any_pop() {
234 DBC_PRE( getSize() > 0 );
235 DBC_INV_RAII( SOD );
236
237 auto it = begin();
238 auto retval( *it );
239 map_->stlmap_.erase( it );
240 return retval;
241 }
242
243
249 void undef( SO * p_key );
250
251};
The context of execution.
Definition context.h:46
virtual bool invariant() const noexcept
Checks the invariants of the class in which it is defined.
Definition dbc.h:40
Semantic Object Dictionary.
Definition sod.h:43
SOD * dup() const override
Creates a new instance as copy following the red book definition.
Definition sod.h:176
size_t getSize() const override
Returns the size of the SO.
Definition sod.h:194
SOD * clone() const override
Creates a new instance as copy with deep cloning.
Definition sod.cpp:40
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sod.cpp:51
OTCode ot() const override
Returns an OTCode.
Definition sod.h:188
void insert(SOp p_key, SOp p_value)
Insert with the red book definition of equal.
Definition sod.cpp:99
SOD(const SOcomposite &p_other)
Ctor from parent class object.
Definition sod.h:151
auto end() const
Returns an iterator to the marker at end of the map of two SOps.
Definition sod.h:206
SOp find(SO *p_key) const
Searches for p_key in the dictionary.
Definition sod.cpp:124
std::string_view type() const override
Returns a type name.
Definition sod.h:191
void undef(SO *p_key)
Removes both p_key and its value from the dictionary.
Definition sod.cpp:132
SOD()
Ctor.
Definition sod.h:142
size_t dagheight(size_t p_myheight) const override
DAG Height.
Definition sod.cpp:77
bool invariant() const noexcept override
Checks class invariants.
Definition sod.h:129
auto begin() const
Returns an iterator to the begin of the map of two SOps.
Definition sod.h:202
SOD(const SOD &p_other)
Copy ctor.
Definition sod.h:159
void * shptr() const override
Pointer to the shared object.
Definition sod.h:122
~SOD() override
Dtor.
Definition sod.h:165
std::shared_ptr< SODMap > map_
The shared dictionary.
Definition sod.h:118
auto any_pop()
Returns a pair from the dictionary.
Definition sod.h:233
Semantic Object Name.
Definition sosimp.h:525
std::string getName() const
Getter for name_.
Definition sosimp.h:574
static bool otcodeIsName(OTCode p_code)
Checks if an OTCode represents a name type (N or Nx).
Definition sosimp.h:621
Semantic Object.
Definition so.h:67
virtual bool equal(const SO *) const =0
Checks if this SO is equal to another SO.
virtual OTCode ot() const =0
Returns an OTCode.
Composite Semantic Object.
Definition so.h:261
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:122
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:104
#define DBC_INV_RAII(TT)
Defines an instance of the class DbCRAIIassert<>, which calls the invariant()-function at the return ...
Definition dbc.h:94
Miscellaneous definitions and functions.
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:38
@ D
SOD.
The simple SO* class hierarchy.
Equality functor for SO* keys in SOD's unordered_map.
Definition sod.h:77
bool operator()(const SOp &p_lhs, const SOp &p_rhs) const
Checks if two SO* objects are semantically equal.
Definition sod.h:86
Hash functor for SO* keys in SOD's unordered_map.
Definition sod.h:51
size_t operator()(const SOp &p_key) const
Computes the hash value for a given SO*.
Definition sod.h:58
Semantic Object Dictionary - map member class.
Definition sod.h:101
std::unordered_map< SOp, SOp, FunctorHash, FunctorEqual > stlmap_
The map.
Definition sod.h:103
~SODMap()
Dtor.
Definition sod.h:106