Leonardus
sod.h
Go to the documentation of this file.
1
17#pragma once
18
19#include <string>
20#include <map>
21#include <numeric>
22#include <vector>
23#include <memory>
24#include <algorithm>
25
26#include "dbc.h"
27#include "helper.h"
28#include "so.h"
29
30
31class Context;
32
33
37class SOD : public SOcomposite {
41 struct SODMap {
42 std::map<SOp,SOp> stlmap_;
46 std::for_each( stlmap_.begin(), stlmap_.end(),
47 []( const auto & p_pair ) {
48 delete p_pair.first;
49 delete p_pair.second;
50 } );
51 }
52 };
53
54 std::shared_ptr<SODMap> map_;
57protected:
58#ifndef DBC_IS_VOID
62 bool invariant() const noexcept override { /* LCOV_EXCL_START */
63 if( map_ == nullptr )
64 return false;
65 return std::none_of( map_->stlmap_.begin(), map_->stlmap_.end(),
66 []( const auto & p ) {
67 return p.first == nullptr or p.second == nullptr;
68 } );
69 } /* LCOV_EXCL_STOP */
70#endif
71
72public:
74 SOD() : map_(std::make_shared<SODMap>()) {
76 }
77
78public: /* virtual */
79 [[nodiscard]] SOD * dup() const override { return new SOD( *this ); }
80
84 [[nodiscard]] SOD * clone() const override;
85
86 std::string opequalequal() const override;
87
88 OTCode ot() const override { return OTCode::D; }
89
90 std::string type() const override { return "dicttype"; }
91
92 bool equal( const SO * p_other ) const override {
93 auto o = dynamic_cast<const SOD*>( p_other);
94 return o ? map_ == o->map_ : false;
95 }
96
97 size_t getSize() const override { return map_->stlmap_.size(); }
98
99 size_t treeheight( size_t p_myheight ) const override;
100
101public: /* other */
103 auto begin() { return map_->stlmap_.begin(); }
104
105
107 auto end() { return map_->stlmap_.end(); }
108
109
117 void insert( SOp p_key, SOp p_value, bool p_forcebegin = false );
118
119
124 SOp find( const SO * p_key ) const;
125
126
129 auto any_pop() {
130 DBC_PRE( getSize() > 0 );
131 DBC_INV_RAII( SOD );
132
133 auto it = begin();
134 auto retval( *it );
135 map_->stlmap_.erase( it );
136 return retval;
137 }
138
139
143 void undef( const SO * p_key );
144};
The context of execution.
Definition: context.h:37
Semantic Object Dictionary.
Definition: sod.h:37
SOD * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sod.h:79
void insert(SOp p_key, SOp p_value, bool p_forcebegin=false)
Insert with the red book definition of equal.
Definition: sod.cpp:89
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sod.h:97
SOD * clone() const override
Creates a new instance as copy with deep cloning.
Definition: sod.cpp:34
size_t treeheight(size_t p_myheight) const override
Tree Height.
Definition: sod.cpp:67
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition: sod.cpp:46
OTCode ot() const override
Returns an OTCode.
Definition: sod.h:88
void undef(const SO *p_key)
Removes both p_key and its value from the dictionary.
Definition: sod.cpp:124
auto begin()
Returns an iterator to the begin of the map of two SOps.
Definition: sod.h:103
std::string type() const override
Returns a type name.
Definition: sod.h:90
SOp find(const SO *p_key) const
Searches for p_key in the dictionary.
Definition: sod.cpp:114
SOD()
Ctor.
Definition: sod.h:74
bool equal(const SO *p_other) const override
Equality.
Definition: sod.h:92
bool invariant() const noexcept override
Checks class invariants.
Definition: sod.h:62
std::shared_ptr< SODMap > map_
The shared dictionary.
Definition: sod.h:54
auto any_pop()
Returns a pair from the dictionary.
Definition: sod.h:129
auto end()
Returns an iterator to the marker at end of the map of two SOps.
Definition: sod.h:107
Semantic Object.
Definition: so.h:54
Composite Semantic Object.
Definition: so.h:161
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition: dbc.h:88
#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
Miscellaneous definitions and functions.
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition: so.h:29
@ o
SOo.
@ D
SOD.
Semantic Object Dictionary - map member class.
Definition: sod.h:41
std::map< SOp, SOp > stlmap_
The map.
Definition: sod.h:42
~SODMap()
Dtor.
Definition: sod.h:45