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