Leonardus
sos.h
Go to the documentation of this file.
1
17#pragma once
18
19#include <string>
20#include <memory>
21
22#include "dbc.h"
23#include "helper.h"
24#include "so.h"
25
26
29class SOS : public SOcomposite {
30 std::shared_ptr<std::string> str_;
32protected:
33#ifndef DBC_IS_VOID
37 bool invariant() const noexcept override { /* LCOV_EXCL_START */
38 return str_ != nullptr;
39 } /* LCOV_EXCL_STOP */
40#endif
41
42public:
44 explicit SOS( const std::string & p_str ) : str_(std::make_shared<std::string>(p_str)) {
46 }
47
48public: /* accessor */
50 std::string getString() const { return *str_; }
51
54 void setch( size_t p_index, int p_char );
55
56public: /* virtual */
57 [[nodiscard]] SOS * dup() const override { return new SOS( *this ); }
58
62 [[nodiscard]] SOS * clone() const override {
63 auto rv = new SOS( *str_ );
64 rv->setExec( getExec() );
65
66 DBC_POST( rv != nullptr );
67 DBC_POST( rv->invariant() );
68 return rv;
69 }
70
71 std::string opequal() const override { return *str_; }
72
73 std::string opequalequal() const override { return "(" + *str_ + ")"; }
74
75 OTCode ot() const override { return OTCode::S; }
76
77 std::string type() const override { return "stringtype"; }
78
79 bool equal( const SO * p_other ) const override {
80 auto o = dynamic_cast<const SOS *>( p_other );
81 return o ? *str_ == *(o->str_) : false; // SOS are equal if their value is equal, even with non-shared pointers
82 }
83
84 bool gt( const SO * p_other ) const override {
85 auto o = dynamic_cast<const SOS *>( p_other );
86 if( o == nullptr )
88 return *str_ > *(o->str_);
89 }
90
91 bool ge( const SO * p_other ) const override {
92 auto o = dynamic_cast<const SOS *>( p_other );
93 if( o == nullptr )
95 return *str_ >= *(o->str_);
96 }
97
98 size_t getSize() const override { return str_->size(); }
99
100public: /* other */
102 void erase0() {
103 str_->erase(0,1);
104
105 DBC_INV;
106 }
107
110 void replace( size_t p_pos, const std::string & p_src ) {
111 DBC_PRE( p_pos + p_src.size() <= getSize() );
112
113 str_->replace( p_pos, p_src.size(), p_src );
114
115 DBC_INV;
116 }
117};
Semantic Object String.
Definition: sos.h:29
std::string getString() const
Getter for the string value.
Definition: sos.h:50
SOS * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sos.h:57
SOS * clone() const override
Creates a new instance as copy with deep cloning.
Definition: sos.h:62
bool ge(const SO *p_other) const override
Greater or equal.
Definition: sos.h:91
bool gt(const SO *p_other) const override
Greater than.
Definition: sos.h:84
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sos.h:71
bool invariant() const noexcept override
Checks class invariants.
Definition: sos.h:37
void replace(size_t p_pos, const std::string &p_src)
Replaces characters at position p_pos.
Definition: sos.h:110
void setch(size_t p_index, int p_char)
Sets a single character within the string.
Definition: sos.cpp:24
bool equal(const SO *p_other) const override
Equality.
Definition: sos.h:79
void erase0()
Erases the fist character of the String.
Definition: sos.h:102
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sos.h:98
std::string type() const override
Returns a type name.
Definition: sos.h:77
std::shared_ptr< std::string > str_
The string value.
Definition: sos.h:30
OTCode ot() const override
Returns an OTCode.
Definition: sos.h:75
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition: sos.h:73
SOS(const std::string &p_str)
Ctor.
Definition: sos.h:44
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_POST(XXX)
Assert for postconditions.
Definition: dbc.h:80
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition: dbc.h:88
#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
void opErrExit(OpError p_err, const std::string &p_details, const std::source_location p_location)
Operator error message to interpreter cout_ and exit( EC_OPERATOR ).
Definition: error.cpp:28
@ typecheck
PS operator error typecheck.
Definition: error.h:33
Miscellaneous definitions and functions.
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition: so.h:29
@ S
SOS.
@ o
SOo.