File tree Expand file tree Collapse file tree 11 files changed +322
-2
lines changed
back_to_basics_smart_pointers/SourceFiles
justintime_compilation_the_next_big_thing
macrofree_testing_with_cpp20 Expand file tree Collapse file tree 11 files changed +322
-2
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < memory>
3
+
4
+ struct Son ;
5
+ struct Daughter ;
6
+
7
+ struct Mother {
8
+ ~Mother () {
9
+ std::cout << " Mother gone" << std::endl;
10
+ }
11
+ void setSon (const std::shared_ptr<Son> s) {
12
+ mySon = s;
13
+ }
14
+ void setDaughter (const std::shared_ptr<Daughter> d) {
15
+ myDaughter = d;
16
+ }
17
+ std::shared_ptr<const Son> mySon;
18
+ std::weak_ptr<const Daughter> myDaughter;
19
+ };
20
+
21
+ struct Son {
22
+ Son (std::shared_ptr<Mother> m) : myMother(m) {}
23
+ ~Son () {
24
+ std::cout << " Son gone" << std::endl;
25
+ }
26
+ std::shared_ptr<const Mother> myMother;
27
+ };
28
+
29
+ struct Daughter {
30
+ Daughter (std::shared_ptr<Mother> m) : myMother(m) {}
31
+ ~Daughter () {
32
+ std::cout << " Daughter gone" << std::endl;
33
+ }
34
+ std::shared_ptr<const Mother> myMother;
35
+ };
36
+
37
+ int main () {
38
+ std::cout << std::endl;
39
+ {
40
+ std::shared_ptr<Mother> mother= std::shared_ptr<Mother>(new Mother);
41
+ std::shared_ptr<Son> son= std::shared_ptr<Son>(new Son (mother));
42
+ std::shared_ptr<Daughter> daughter= std::shared_ptr<Daughter>(new Daughter (mother));
43
+ mother->setSon (son);
44
+ mother->setDaughter (daughter);
45
+ }
46
+ std::cout << std::endl;
47
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < new>
3
+ #include < string>
4
+
5
+ class ResourceGuard {
6
+ private:
7
+ const std::string resource;
8
+ public:
9
+ ResourceGuard (const std::string& res):resource(res) {
10
+ std::cout << " Acquire the " << resource << " ." << std::endl;
11
+ }
12
+ ~ResourceGuard () {
13
+ std::cout << " Release the " << resource << " ." << std::endl;
14
+ }
15
+ };
16
+
17
+ int main () {
18
+
19
+ std::cout << std::endl;
20
+
21
+ ResourceGuard resGuard1{" memoryBlock1" };
22
+
23
+ std::cout << " \n Before local scope" << std::endl;
24
+ {
25
+ ResourceGuard resGuard2{" memoryBlock2" };
26
+ }
27
+ std::cout << " After local scope" << std::endl;
28
+
29
+ std::cout << std::endl;
30
+
31
+
32
+ std::cout << " \n Before try-catch block" << std::endl;
33
+ try {
34
+ ResourceGuard resGuard3{" memoryBlock3" };
35
+ throw std::bad_alloc ();
36
+ }
37
+ catch (std::bad_alloc& e) {
38
+ std::cout << e.what ();
39
+ }
40
+ std::cout << " \n After try-catch block" << std::endl;
41
+
42
+ std::cout << std::endl;
43
+
44
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < memory>
3
+
4
+ class MyInt {
5
+ public:
6
+ MyInt (int v) : val(v) {
7
+ std::cout << " Hello: " << val << std::endl;
8
+ }
9
+ ~MyInt () {
10
+ std::cout << " Good Bye: " << val << std::endl;
11
+ }
12
+ private:
13
+ int val;
14
+ };
15
+
16
+ int main () {
17
+
18
+ std::cout << std::endl;
19
+
20
+ std::shared_ptr<MyInt> sharPtr (new MyInt (1998 ));
21
+
22
+ std::cout << " sharedPtr.use_count(): " << sharPtr.use_count () << std::endl;
23
+ {
24
+ std::shared_ptr<MyInt> locSharPtr (sharPtr);
25
+ std::cout << " locSharPtr.use_count(): " << locSharPtr.use_count () << std::endl;
26
+ }
27
+ std::cout << " sharPtr.use_count(): " << sharPtr.use_count () << std::endl;
28
+
29
+ std::shared_ptr<MyInt> globSharPtr = sharPtr;
30
+ std::cout << " sharPtr.use_count(): " << sharPtr.use_count () << std::endl;
31
+
32
+ globSharPtr.reset ();
33
+ std::cout << " sharPtr.use_count(): " << sharPtr.use_count () << std::endl;
34
+
35
+ sharPtr = std::shared_ptr<MyInt>(new MyInt (2011 ));
36
+
37
+ std::cout << std::endl;
38
+
39
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < memory>
3
+ #include < random>
4
+ #include < string>
5
+ #include < typeinfo>
6
+
7
+ template <typename T>
8
+ class Deleter {
9
+ public:
10
+ void operator ()(T *ptr) {
11
+ ++Deleter::count;
12
+ delete ptr;
13
+ }
14
+ void getInfo () {
15
+ std::string typeId{typeid (T).name ()};
16
+ size_t sz= Deleter::count * sizeof (T);
17
+ std::cout << " Deleted " << Deleter::count << " objects of type: " << typeId << std::endl;
18
+ std::cout <<" Freed size in bytes: " << sz << " ." << std::endl;
19
+ std::cout << std::endl;
20
+
21
+ }
22
+ private:
23
+ static int count;
24
+ };
25
+
26
+ template <typename T>
27
+ int Deleter<T>::count = 0 ;
28
+
29
+ typedef Deleter<int > IntDeleter;
30
+
31
+ void createRandomNumbers () {
32
+
33
+ std::random_device seed;
34
+
35
+ // generator
36
+ std::mt19937 engine (seed ());
37
+
38
+ // distribution
39
+ std::uniform_int_distribution<int > thousand (1 , 1000 );
40
+ int ranNumber= thousand (engine);
41
+ for ( int i=0 ; i <= ranNumber; ++i) std::shared_ptr<int >(new int (i), IntDeleter ());
42
+
43
+ }
44
+
45
+ int main () {
46
+
47
+ std::cout << std::endl;
48
+
49
+ // define a local scope
50
+ {
51
+ std::shared_ptr<int > sharedPtr1 (new int , IntDeleter ());
52
+ std::shared_ptr<int > sharedPtr2 (new int , IntDeleter ());
53
+ auto intDeleter= std::get_deleter<IntDeleter>(sharedPtr1);
54
+ intDeleter->getInfo ();
55
+ sharedPtr2.reset ();
56
+ intDeleter->getInfo ();
57
+
58
+ }
59
+ // create up to 1000 std::shared_ptr of type int
60
+ createRandomNumbers ();
61
+ IntDeleter ().getInfo ();
62
+
63
+ }
64
+
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < memory>
3
+ #include < thread>
4
+
5
+ using namespace std ::literals::chrono_literals;
6
+
7
+ struct MyInt {
8
+ int val{2017 };
9
+ ~MyInt () {
10
+ std::cout << " Good Bye" << std::endl;
11
+ }
12
+ };
13
+
14
+ void showNumber (std::shared_ptr<MyInt> myInt) {
15
+ std::cout << myInt->val << std::endl;
16
+ }
17
+
18
+ void threadCreator () {
19
+ auto sharedPtr = std::make_shared<MyInt>();
20
+
21
+ std::thread t1 (showNumber, sharedPtr);
22
+ std::thread t2 (showNumber, sharedPtr);
23
+
24
+ t1.detach ();
25
+ t2.detach ();
26
+ }
27
+
28
+ int main () {
29
+
30
+ std::cout << std::endl;
31
+
32
+ threadCreator ();
33
+ std::this_thread::sleep_for (1s);
34
+
35
+ std::cout << std::endl;
36
+
37
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < memory>
3
+ #include < utility>
4
+
5
+ struct MyInt {
6
+ MyInt (int i) : i_(i) {}
7
+ ~MyInt () {
8
+ std::cout << " Good bye from " << i_ << std::endl;
9
+ }
10
+ int i_;
11
+ };
12
+
13
+
14
+ int main () {
15
+
16
+ std::cout << std::endl;
17
+
18
+ MyInt* myInt15 = new MyInt (15 );
19
+ std::unique_ptr<MyInt> uniquePtr1 (myInt15);
20
+ std::unique_ptr<MyInt> uniquePtr15 (myInt15);
21
+
22
+ std::cout << " uniquePtr1.get(): " << uniquePtr1.get () << std::endl;
23
+
24
+ std::unique_ptr<MyInt> uniquePtr2{std::move (uniquePtr1)};
25
+ std::cout << " uniquePtr1.get(): " << uniquePtr1.get () << std::endl;
26
+ std::cout << " uniquePtr2.get(): " << uniquePtr2.get () << std::endl;
27
+
28
+ std::cout << std::endl;
29
+
30
+
31
+ {
32
+ std::unique_ptr<MyInt> localPtr{new MyInt (2003 )};
33
+ }
34
+
35
+ std::cout << std::endl;
36
+
37
+ uniquePtr2.reset (new MyInt (2011 ));
38
+ MyInt* myInt= uniquePtr2.release ();
39
+ delete myInt;
40
+
41
+ std::cout << std::endl;
42
+
43
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < memory>
3
+
4
+ int main () {
5
+
6
+ std::cout << std::boolalpha << std::endl;
7
+
8
+ auto sharedPtr = std::make_shared<int >(2011 );
9
+ std::weak_ptr<int > weakPtr (sharedPtr);
10
+
11
+ std::cout << " weakPtr.use_count(): " << weakPtr.use_count () << std::endl;
12
+ std::cout << " sharedPtr.use_count(): " << sharedPtr.use_count () << std::endl;
13
+ std::cout << " weakPtr.expired(): " << weakPtr.expired () << std::endl;
14
+
15
+ if ( std::shared_ptr<int > sharedPtr1 = weakPtr.lock () ) {
16
+ std::cout << " *sharedPtr: " << *sharedPtr << std::endl;
17
+ }
18
+ else {
19
+ std::cout << " Don't get the resource!" << std::endl;
20
+ }
21
+
22
+ weakPtr.reset ();
23
+ if ( std::shared_ptr<int > sharedPtr1 = weakPtr.lock () ) {
24
+ std::cout << " *sharedPtr: " << *sharedPtr << std::endl;
25
+ }
26
+ else {
27
+ std::cout << " Don't get the resource!" << std::endl;
28
+ }
29
+
30
+ std::cout << std::endl;
31
+
32
+ }
Original file line number Diff line number Diff line change 1
- {"Title": "Just-in-Time Compilation The Next Big Thing", "Author": "Ben Deane Kris Jusiak"}
1
+ {"Title": "Just-in-Time Compilation The Next Big Thing", "Author": "Ben Deane & Kris Jusiak"}
Original file line number Diff line number Diff line change
1
+ ** Just-in-Time Compilation The Next Big Thing?** by ** Ben Deane & Kris Jusiak**
2
+
3
+ Additional links:
4
+
5
+ https://quantlabfinancial.github.io/talks/cppcon-2020/just-in-time_compilation_the-next-big-thing
6
+ https://wg21.link/p1609
7
+ https://github.com/hfinkel/llvm-project-cxxjit
Original file line number Diff line number Diff line change
1
+ ** Macro-free testing with C++20** by ** Kris Jusiak**
2
+
3
+ Additional links:
4
+
5
+ https://boost-ext.github.io/ut/cppcon-2020/
6
+ https://github.com/boost-ext/ut
7
+ https://godbolt.org/z/81b3K7
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ and code from [CppCon 2020](http://cppcon.org).
19
19
- [ Back to Basics Concurrency] ( Presentations/back_to_basics_concurrency/back_to_basics_concurrency__arthur_odwyer__cppcon_2020.pdf ) by Arthur O'Dwyer
20
20
- [ Back To Basics Lambda Expressions] ( Presentations/back_to_basics_lambda_expressions/back_to_basics_lambda_expressions__barbara_geller__ansel_sermersheim__cppcon_2020.pdf ) by Barbara Geller & Ansel Sermersheim
21
21
- [ Back to Basics Move Semantics] ( Presentations/back_to_basics_move_semantics/back_to_basics_move_semantics__david_olsen__cppcon_2020.pdf ) by David Olsen
22
- - [ Back to Basics - Smart Pointers] ( Presentations/back_to_basics_smart_pointers/back_to_basics_smart_pointers__rainer_grimm__cppcon_2020.pdf ) by Rainer Grimm
22
+ - [ Back to Basics - Smart Pointers] ( Presentations/back_to_basics_smart_pointers/back_to_basics_smart_pointers__rainer_grimm__cppcon_2020.pdf ) by Rainer Grimm \[ [ more materials ] ( Presentations/back_to_basics_smart_pointers ) \]
23
23
- [ Back to Basics - The Abstract Machine] ( Presentations/back_to_basics_the_abstract_machine/back_to_basics_the_abstract_machine__bob_steagall__cppcon_2020.pdf ) by Bob Steagall
24
24
- [ Back to Basics - The Structure of a Program] ( Presentations/back_to_basics_the_structure_of_a_program/back_to_basics_the_structure_of_a_program__bob_steagall__cppcon_2020.pdf ) by Bob Steagall
25
25
- [ Breaking Dependencies - The SOLID Principles] ( Presentations/breaking_dependencies_the_solid_principles/breaking_dependencies_the_solid_principles__klaus_iglberger__cppcon_2020.pdf ) by Klaus Iglberger
You can’t perform that action at this time.
0 commit comments