Skip to content

Commit

Permalink
Added additional code from extended chapter02
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin committed Apr 13, 2017
1 parent ff7c3ec commit 26c1e51
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Chapter02/01_scoped_ptr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,37 @@ bool foo3_1() {
return true;
}

#include <iostream>

struct base {
virtual ~base(){}
};


class derived: public base {
std::string str_;

public:
explicit derived(const char* str)
: str_(str)
{}

~derived() /*override*/ {
std::cout << "str == " << str_ << '\n';
}
};

void base_and_derived() {
const boost::movelib::unique_ptr<base> p1(
new derived("unique_ptr")
);

const boost::scoped_ptr<base> p2(
new derived("scoped_ptr")
);
}


bool g_exit_on_first_function = true;

#include <assert.h>
Expand All @@ -83,6 +114,8 @@ int main() {
try { foo2(); assert(false); } catch(...){}
try { foo3(); assert(false); } catch(...){}
try { foo3_1(); assert(false); } catch(...){}

base_and_derived();
}


Expand Down
30 changes: 30 additions & 0 deletions Chapter02/02_shared_ptr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,31 @@ void foo3() {
.detach();
}

void process_cstr1(boost::shared_ptr<const std::string> p);
void process_cstr2(const boost::shared_ptr<const std::string>& p);

void foo3_const() {
boost::shared_ptr<const std::string> ps
= boost::make_shared<const std::string>(
"Guess why make_shared<std::string> "
"is faster than shared_ptr<std::string> "
"ps(new std::string('this string'))"
);

boost::thread(boost::bind(&process_cstr1, ps))
.detach();
boost::thread(boost::bind(&process_cstr2, ps))
.detach();

// *ps = "qwe"; // Compile time error, string is const!
}

#include <boost/chrono/duration.hpp>
int main() {
// foo1(); // Will cause a memory leak
foo2();
foo3();
foo3_const();

// Give all the threads a chance to finish
// Note: It is an awfull design, but it is OK
Expand Down Expand Up @@ -119,6 +139,16 @@ void process_str1(boost::shared_ptr<std::string> p) {
void process_str2(const boost::shared_ptr<std::string>& p) {
assert(p);
}

void process_cstr1(boost::shared_ptr<const std::string> p) {
assert(p);
}

void process_cstr2(const boost::shared_ptr<const std::string>& p) {
assert(p);
}


void process_sp1(const boost::shared_ptr<foo_class>& p) {
assert(!!p);
}
Expand Down
1 change: 1 addition & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class tester:
'Chapter01/12_A_noncopyable_movable': ('no C++11\n', '', 0),
'Chapter01/12_B_noncopyable_movable_c++11': ('C++11\n', '', 0),
'Chapter01/13_algorithm': ('48656C6C6F20776F7264\n48656C6C6F20776F7264\n', '', 0),
'Chapter02/01_scoped_ptr': ('str == scoped_ptr\nstr == unique_ptr\n', '', 0),
'Chapter03/03_numeric_cast': ('#47 bad numeric conversion: negative overflow\n#58 bad numeric conversion: positive overflow\n\n\n\nNEG OVERFLOW in #47 bad numeric conversion: negative overflow\nPOS OVERFLOW in #59 bad numeric conversion: positive overflow\n\n\n\nIt works! val = 0 Error msg: Not in range!\n', '', 0),
'Chapter04/mpl_int_': (' 0 1 2 \x03 4 5\n', '', 0),
'Chapter04/static_assert': ('01', '', 0),
Expand Down

0 comments on commit 26c1e51

Please sign in to comment.