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 <string_view>
22#include <memory>
23
24// Inc Pure
25#include "dbc.h"
26#include "helper.h"
27
28// Inc Rich
29#include "so.h"
30
31
35class SOS : public SOshptr {
36
38 std::shared_ptr<std::string> str_;
39
40protected:
41
42 void * shptr() const override { return static_cast<void *>( str_.get() ); }
43
44public:
45
48 explicit SOS( const std::string & p_str )
49 : SOshptr(),
50 str_(std::make_shared<std::string>(p_str)) {
52 }
53
54
56 SOS( const SOS & p_other ) : SOshptr(p_other), str_(p_other.str_) {
58 }
59
60
66 SOS( const SOshptr & p_other, const std::string & p_str )
67 : SOshptr( p_other ),
68 str_( std::make_shared<std::string>(p_str) ) {
70 }
71
72
74 ~SOS() override {
76 }
77
78
79 SOS() = delete;
80 SOS( SOS && ) = delete;
81 SOS & operator=( const SOS & ) = delete;
82 SOS & operator=( SOS && ) = delete;
83
84public: /* accessor */
85
87 std::string getString() const { return *str_; }
88
89
92 std::string & getStringRef() { return *str_; }
93
94public: /* virtual */
95
96 [[nodiscard]] SOS * dup() const override { return new SOS( *this ); }
97
98
103 [[nodiscard]] SOS * clone() const override {
104 auto rv = new SOS( static_cast<const SOshptr &>(*this), *str_ );
105
106 DBC_POST( rv != nullptr );
107 DBC_POST( rv->invariant() );
108 return rv;
109 }
110
111
112 std::string opequal() const override { return *str_; }
113
114
115 std::string opequalequal() const override { return "(" + *str_ + ")"; }
116
117
118 OTCode ot() const override { return OTCode::S; }
119
120
121 std::string_view type() const override { return "stringtype"; }
122
123
126 bool equal( const SO * p_other ) const override {
127 DBC_PRE( p_other != nullptr );
128
129 if( p_other->ot() != ot() )
130 return false;
131 const auto o = static_cast<const SOS *>( p_other );
132 return str_ == o->str_ or
133 *str_ == *(o->str_); // SOS are equal if their value is equal, even with non-shared pointers
134 }
135
136
140 bool gt( OpError & p_ec, const SO * p_other ) const override {
141 DBC_PRE( p_other != nullptr );
142
143 if( p_other->ot() != OTCode::S ) {
144 p_ec = typecheck;
145 return false;
146 }
147 const auto o = static_cast<const SOS *>( p_other );
148 p_ec = success;
149 return *str_ > *(o->str_);
150 }
151
152
156 bool ge( OpError & p_ec, const SO * p_other ) const override {
157 DBC_PRE( p_other != nullptr );
158
159 if( p_other->ot() != OTCode::S ) {
160 p_ec = typecheck;
161 return false;
162 }
163 const auto o = static_cast<const SOS *>( p_other );
164 p_ec = success;
165 return *str_ >= *(o->str_);
166 }
167
168
169 size_t getSize() const override { return str_->size(); }
170
171public: /* other */
172
178 void putCh( size_t p_index, int p_char );
179
180
182 void erase0() {
183 str_->erase(0,1);
184
185 DBC_INV;
186 }
187
188
194 void replace( size_t p_pos, const std::string & p_src ) {
195 DBC_PRE( p_pos + p_src.size() <= getSize() );
196
197 str_->replace( p_pos, p_src.size(), p_src );
198
199 DBC_INV;
200 }
201
202};
Semantic Object String.
Definition sos.h:35
std::string getString() const
Getter for the string value.
Definition sos.h:87
SOS * dup() const override
Creates a new instance as copy following the red book definition.
Definition sos.h:96
SOS * clone() const override
Creates a new instance as copy with deep cloning.
Definition sos.h:103
std::string & getStringRef()
Getter for reference to the string value.
Definition sos.h:92
void * shptr() const override
Pointer to the shared object.
Definition sos.h:42
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sos.h:112
SOS(const SOshptr &p_other, const std::string &p_str)
Ctor from parent class.
Definition sos.h:66
void replace(size_t p_pos, const std::string &p_src)
Replaces characters at position p_pos.
Definition sos.h:194
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sos.h:126
void erase0()
Erases the fist character of the String.
Definition sos.h:182
~SOS() override
Dtor.
Definition sos.h:74
size_t getSize() const override
Returns the size of the SO.
Definition sos.h:169
std::shared_ptr< std::string > str_
The string value.
Definition sos.h:38
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition sos.h:156
OTCode ot() const override
Returns an OTCode.
Definition sos.h:118
void putCh(size_t p_index, int p_char)
Sets a single character within the string.
Definition sos.cpp:27
std::string_view type() const override
Returns a type name.
Definition sos.h:121
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sos.h:115
SOS(const SOS &p_other)
Copy ctor.
Definition sos.h:56
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition sos.h:140
SOS(const std::string &p_str)
Ctor from string reference.
Definition sos.h:48
Semantic Object.
Definition so.h:67
virtual OTCode ot() const =0
Returns an OTCode.
Semantic Object with shared pointer to data.
Definition so.h:210
Helpers for design by contract idioms.
#define DBC_POST(XXX)
Assert for postconditions.
Definition dbc.h:110
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:122
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:114
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:104
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:38
@ S
SOS.
@ o
SOo.