summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-03-02 12:47:31 +0100
committerSven Gothel <[email protected]>2020-03-02 12:47:31 +0100
commit98a601a5c40bb122798aaf3d8152df9261f8ca16 (patch)
tree92f326015331ed072b75fc20a9029f629e89cc49 /src
Initial cpp-sandbox commit
Diffstat (limited to 'src')
-rw-r--r--src/rvalue_01.cpp161
-rw-r--r--src/string_01.cpp107
-rw-r--r--src/string_02.cpp121
3 files changed, 389 insertions, 0 deletions
diff --git a/src/rvalue_01.cpp b/src/rvalue_01.cpp
new file mode 100644
index 0000000..67a2599
--- /dev/null
+++ b/src/rvalue_01.cpp
@@ -0,0 +1,161 @@
+/*
+ * Author: Sven Gothel <[email protected]>
+ * Copyright (c) 2020 Gothel Software e.K.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <iostream>
+#include <cassert>
+#include <iterator>
+#include <string>
+#include <cctype>
+#include <atomic>
+
+class Bar {
+
+ private:
+ static std::atomic_int name_counter;
+ const int name;
+
+ public:
+ int n;
+ std::string s;
+
+ Bar()
+ : name(name_counter.fetch_add(1)), n(0), s() {
+ std::cout << "default-constructed " << name << std::endl;
+ }
+ Bar(const int i, const std::string &str)
+ : name(name_counter.fetch_add(1)), n(i), s(str) {
+ std::cout << "custom-constructed " << name << ": " << i << ", " << str << std::endl;
+ }
+
+ ~Bar() {
+ std::cout << "destructed " << name << std::endl;
+ }
+
+ Bar(const Bar &o) noexcept
+ : name(name_counter.fetch_add(1)), n(o.n), s(o.s)
+ {
+ std::cout << "copy-constructed " << name << std::endl;
+ }
+
+ Bar(Bar &&o) noexcept
+ : name(name_counter.fetch_add(1)), n(std::move(o.n)), s(std::move(o.s))
+ {
+ std::cout << "move-constructed " << name << std::endl;
+ }
+
+ Bar& operator=(Bar o) noexcept {
+ n = o.n;
+ std::swap(s, o.s);
+ std::cout << "copy-and-swap-assigned " << o.name << " -> " << name << std::endl;
+ return *this;
+ }
+
+ Bar& operator=(const Bar &o) noexcept {
+ if( &o == this ) {
+ std::cout << "copy-assigned(same) " << o.name << " -> " << name << std::endl;
+ return *this;
+ } else {
+ n = o.n;
+ s = o.s;
+ std::cout << "copy-assigned(copy) " << o.name << " -> " << name << std::endl;
+ return *this;
+ }
+ }
+
+ Bar& operator=(Bar &&o) noexcept {
+ n = o.n;
+ s = std::move(o.s);
+ std::cout << "move-assigned " << o.name << " -> " << name << std::endl;
+ return *this;
+ }
+};
+
+std::atomic_int Bar::name_counter(0);
+
+
+/** Best: Uses RVO or elision */
+Bar foo01() {
+ Bar localObj;
+ return localObj;
+}
+
+/** Best: Uses RVO or elision */
+Bar foo02(int c) {
+ if( !c ) {
+ return Bar();
+ } else {
+ return Bar();
+ }
+}
+
+
+/** Uses implicit move-ctor */
+Bar foo11(int c) {
+ Bar a, b;
+ if( !c ) {
+ return a;
+ } else {
+ return b;
+ }
+}
+
+/** Uses explicit move-ctor */
+Bar foo12(int c) {
+ Bar a, b;
+ if( !c ) {
+ return std::move(a);
+ } else {
+ return std::move(b);
+ }
+}
+
+int main() {
+ int b=1;
+ std::cout << "Block-" << b << " foo01 Start" << std::endl ;
+ {
+ Bar obj = foo01();
+ }
+ std::cout << "Block-" << (b++) << " foo01 End" << std::endl ;
+
+ std::cout << "Block-" << b << " foo02 Start" << std::endl ;
+ {
+ Bar obj = foo02(0);
+ }
+ std::cout << "Block-" << (b++) << " foo02 End" << std::endl ;
+
+ std::cout << "Block-" << b << " foo11 Start" << std::endl ;
+ {
+ Bar obj = foo11(0);
+ }
+ std::cout << "Block-" << (b++) << " foo11 End" << std::endl ;
+
+ std::cout << "Block-" << b << " foo12 Start" << std::endl ;
+ {
+ Bar obj = foo12(0);
+ }
+ std::cout << "Block-" << (b++) << " foo12 End" << std::endl ;
+
+ return 0;
+
+}
diff --git a/src/string_01.cpp b/src/string_01.cpp
new file mode 100644
index 0000000..a81aaa6
--- /dev/null
+++ b/src/string_01.cpp
@@ -0,0 +1,107 @@
+/*
+ * Author: Sven Gothel <[email protected]>
+ * Copyright (c) 2020 Gothel Software e.K.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <iostream>
+#include <cassert>
+#include <iterator>
+#include <string>
+#include <cstring>
+#include <cctype>
+#include <atomic>
+
+int main() {
+ int b=1;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ int i=0;
+ std::string s1;
+ s1.reserve(4);
+ s1[i++]='1';
+ s1[i++]='2';
+ s1[i++]='3';
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ std::string s1;
+ s1.reserve(4);
+ s1.push_back('1');
+ s1.push_back('2');
+ s1.push_back('3');
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ std::string s1(3, 0);
+ s1.push_back('1');
+ s1.push_back('2');
+ s1.push_back('3');
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ int i=0;
+ std::string s1(3, 0);
+ s1[i++]='1';
+ s1[i++]='2';
+ s1[i++]='3';
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ int i=0;
+ std::string s1(4, 0);
+ s1[i++]='1';
+ s1[i++]='2';
+ s1[i++]='3';
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ return 0;
+}
diff --git a/src/string_02.cpp b/src/string_02.cpp
new file mode 100644
index 0000000..66846fc
--- /dev/null
+++ b/src/string_02.cpp
@@ -0,0 +1,121 @@
+/*
+ * Author: Sven Gothel <[email protected]>
+ * Copyright (c) 2020 Gothel Software e.K.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <iostream>
+#include <cassert>
+#include <iterator>
+#include <string>
+#include <cstring>
+#include <cctype>
+#include <atomic>
+
+static const char* HEX_ARRAY = "0123456789ABCDEF";
+
+std::string bytesHexString(const uint8_t * bytes, const int offset, const int length, const bool lsbFirst, const bool leading0X) {
+ std::string str;
+
+ if( leading0X ) {
+ str.reserve(2 + length * 2 +1);
+ str.push_back('0');
+ str.push_back('x');
+ } else {
+ str.reserve(length * 2 +1);
+ }
+ if( lsbFirst ) {
+ for (int j = 0; j < length; j++) {
+ const int v = bytes[offset+j] & 0xFF;
+ str.push_back(HEX_ARRAY[v >> 4]);
+ str.push_back(HEX_ARRAY[v & 0x0F]);
+ }
+ } else {
+ for (int j = length-1; j >= 0; j--) {
+ const int v = bytes[offset+j] & 0xFF;
+ str.push_back(HEX_ARRAY[v >> 4]);
+ str.push_back(HEX_ARRAY[v & 0x0F]);
+ }
+ }
+ return str;
+}
+
+std::string uint32HexString(const uint32_t v, const bool leading0X) {
+ const int length = leading0X ? 10 : 8; // ( '0x00000000' | '00000000' )
+ std::string str;
+ str.reserve(length+1); // including EOS for snprintf
+ str.resize(length);
+
+ const int count = snprintf(&str[0], str.capacity(), ( leading0X ? "0x%.8X" : "%.8X" ), v);
+ if( length != count ) {
+ throw std::exception();
+ }
+ return str;
+}
+
+int main() {
+ int b=1;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ uint8_t bytes[] = { 0xAF, 0xFE, 0x00, 0x01, 0x02 };
+ std::string s1 = bytesHexString(bytes, 0, sizeof(bytes), true /* lsb */, true);
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << "/" << s1.size() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ uint8_t bytes[] = { 0xAF, 0xFE, 0x00, 0x01, 0x02 };
+ std::string s1 = bytesHexString(bytes, 0, sizeof(bytes), false /* lsb */, true);
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << "/" << s1.size() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ const uint32_t v = 0x01020304;
+ std::string s1 = uint32HexString(v, false);
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << "/" << s1.size() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+
+ std::cout << "Block-"<<b<<" Start" << std::endl ;
+ {
+ const uint32_t v = 0x00000304;
+ std::string s1 = uint32HexString(v, true);
+
+ std::cout << "s1.cppp: '" << s1 << "', length " << s1.length() << "/" << s1.size() << std::endl ;
+ const char * s1_cstr = s1.c_str();
+ std::cout << "s1.cstr: '" << s1_cstr << "', length " << strlen(s1_cstr) << std::endl ;
+ }
+ std::cout << "Block-"<<(b++)<<" End" << std::endl ;
+ return 0;
+}