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
36 std::shared_ptr<std::string> str_;
37
38
39protected:
40#ifndef DBC_IS_VOID
45 bool invariant() const noexcept override { /* LCOV_EXCL_START */
46 return str_ != nullptr;
47 } /* LCOV_EXCL_STOP */
48#endif
49
50
51public:
54 explicit SOS( const std::string & p_str )
55 : SOcomposite(),
56 str_(std::make_shared<std::string>(p_str)) {
58 }
59
60
62 SOS( const SOS & p_other ) : SOcomposite(p_other), str_(p_other.str_) {
64 }
65
66
68 ~SOS() override {
70 }
71
72
73 SOS() = delete;
74 SOS( SOS && ) = delete;
75 SOS & operator=( const SOS & ) = delete;
76 SOS & operator=( SOS && ) = delete;
77
78
79public: /* accessor */
81 std::string getString() const { return *str_; }
82
83
86 std::string & getStringRef() { return *str_; }
87
88
89public: /* virtual */
90 [[nodiscard]] SOS * dup() const override { return new SOS( *this ); }
91
96 [[nodiscard]] SOS * clone() const override {
97 auto rv = new SOS( *str_ );
98 rv->setExec( getExec() );
99
100 DBC_POST( rv != nullptr );
101 DBC_POST( rv->invariant() );
102 return rv;
103 }
104
105
106 std::string opequal() const override { return *str_; }
107
108
109 std::string opequalequal() const override { return "(" + *str_ + ")"; }
110
111
112 OTCode ot() const override { return OTCode::S; }
113
114
115 std::string type() const override { return "stringtype"; }
116
117
120 bool equal( const SO * p_other ) const override {
121 auto o = dynamic_cast<const SOS *>( p_other );
122 return o ? *str_ == *(o->str_) : false; // SOS are equal if their value is equal, even with non-shared pointers
123 }
124
125
128 bool gt( OpError & p_ec, const SO * p_other ) const override {
129 auto o = dynamic_cast<const SOS *>( p_other );
130 if( o == nullptr ) {
131 p_ec = typecheck;
132 return false;
133 }
134 p_ec = success;
135 return *str_ > *(o->str_);
136 }
137
138
141 bool ge( OpError & p_ec, const SO * p_other ) const override {
142 auto o = dynamic_cast<const SOS *>( p_other );
143 if( o == nullptr ) {
144 p_ec = typecheck;
145 return false;
146 }
147 p_ec = success;
148 return *str_ >= *(o->str_);
149 }
150
151
152 size_t getSize() const override { return str_->size(); }
153
154
155public: /* other */
161 void putCh( size_t p_index, int p_char );
162
163
165 void erase0() {
166 str_->erase(0,1);
167
168 DBC_INV;
169 }
170
171
177 void replace( size_t p_pos, const std::string & p_src ) {
178 DBC_PRE( p_pos + p_src.size() <= getSize() );
179
180 str_->replace( p_pos, p_src.size(), p_src );
181
182 DBC_INV;
183 }
184
185};
Semantic Object String.
Definition sos.h:33
std::string getString() const
Getter for the string value.
Definition sos.h:81
SOS * dup() const override
Creates a new instance as copy following the red book definition.
Definition sos.h:90
SOS * clone() const override
Creates a new instance as copy with deep cloning.
Definition sos.h:96
std::string & getStringRef()
Getter for reference to the string value.
Definition sos.h:86
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sos.h:106
bool invariant() const noexcept override
Checks class invariants.
Definition sos.h:45
void replace(size_t p_pos, const std::string &p_src)
Replaces characters at position p_pos.
Definition sos.h:177
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sos.h:120
void erase0()
Erases the fist character of the String.
Definition sos.h:165
~SOS() override
Dtor.
Definition sos.h:68
size_t getSize() const override
Returns the size of the SO.
Definition sos.h:152
std::string type() const override
Returns a type name.
Definition sos.h:115
std::shared_ptr< std::string > str_
The string value.
Definition sos.h:36
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition sos.h:141
OTCode ot() const override
Returns an OTCode.
Definition sos.h:112
void putCh(size_t p_index, int p_char)
Sets a single character within the string.
Definition sos.cpp:28
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sos.h:109
SOS(const SOS &p_other)
Copy ctor.
Definition sos.h:62
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition sos.h:128
SOS(const std::string &p_str)
Ctor from string reference.
Definition sos.h:54
Semantic Object.
Definition so.h:60
bool getExec() const
Getter for exec_.
Definition so.h:93
Composite Semantic Object.
Definition so.h:198
Helpers for design by contract idioms.
#define DBC_POST(XXX)
Assert for postconditions.
Definition dbc.h:109
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:121
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:113
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:103
OpError
Operator error code enum.
Definition error.h:30
@ success
No operator error.
Definition error.h:32
@ typecheck
operator error typecheck
Definition error.h:36
Miscellaneous definitions and functions.
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:35
@ S
SOS.
@ o
SOo.