Skip to content

Commit 9a22ccf

Browse files
committed
going to bisect unique ptr world in tiles.cpp
1 parent 393643c commit 9a22ccf

13 files changed

Lines changed: 536 additions & 38 deletions

‎c/const.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int* int_ptr_func_int_ptr(int *ip) {
1818
}
1919
#endif
2020

21-
int main() {
21+
int main(void) {
2222
int i = 0;
2323
const int ic = 0;
2424
/* Same. */

‎cpp/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
1. [Pure virtual](pure_virtual.cpp)
4040
1. [Pure virtual from constructor](pure_virtual_from_constructor.cpp)
4141
1. [Covariant return](covariant_return.cpp)
42-
1. Virtual base class: TODO <http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class>
42+
1. [Virtual inheritance](http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class)
4343
1. [Inheriting constructor](inheriting_constructor.cpp)
4444
1. [Inherit overload_hide](inherit_overload_hide.cpp)
4545
1. [namespace](namespace.cpp)

‎cpp/class.cpp‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,19 +1928,6 @@ int main() {
19281928
//MultipleInheritanceConflictDerived::is;
19291929
//MultipleInheritanceConflictDerived().i;
19301930
//MultipleInheritanceConflictDerived().f();
1931-
1932-
/*
1933-
# Dreaded diamond
1934-
1935-
# Virtual inheritance
1936-
1937-
Solves the dreaded diamond problem.
1938-
1939-
Has nothing to do with the `virtual` keyword for methods:
1940-
everything is done at compile time in this usage.
1941-
1942-
http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class
1943-
*/
19441931
}
19451932
}
19461933

‎cpp/common.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include <list> // forward_list, list
2424
#include <limits> //
2525
#include <map> // map, multimap
26-
#include <memory> // shared_ptr
27-
#include <new> //
26+
#include <memory> // unique_ptr, shared_ptr
27+
#include <new>
2828
#include <numeric> // partial sums, differences on std::vectors of numbers
2929
#include <ostream> // ostream
3030
#include <set> // multiset, set

‎cpp/unique_ptr.cpp‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,104 @@
11
/*
22
# unique_ptr
33
4-
TODO
4+
Sample use case:
5+
6+
- you want a dynamic array a dynmically allocated derived class
7+
- thus you have to use pointers for polymorphism. Otherwise objects can have different sizes, and arrays can't be used.
8+
- how to prevent memory leaks?
9+
10+
http://en.cppreference.com/w/cpp/memory/unique_ptr
511
*/
612

713
#include "common.hpp"
814

15+
class Base {
16+
public:
17+
static int count;
18+
Base(int i) : i(i) {
19+
count++;
20+
}
21+
~Base() {
22+
count--;
23+
}
24+
int i;
25+
};
26+
27+
int Base::count = 0;
28+
29+
/* No memory leaked. Destructors called. */
30+
void unique_ptr_test() {
31+
std::vector<std::unique_ptr<Base>> bases;
32+
for (int i = 0; i < 10; ++i) {
33+
bases.push_back(std::unique_ptr<Base>(new Base(i)));
34+
}
35+
}
36+
37+
/* Memory leak. Destructors never called. */
38+
void raw_ptr_test() {
39+
std::vector<Base *> bases;
40+
for (int i = 0; i < 10; ++i) {
41+
bases.push_back(new Base(i));
42+
}
43+
}
44+
45+
void manual_ptr_test() {
46+
std::vector<Base *> bases;
47+
for (int i = 0; i < 10; ++i) {
48+
bases.push_back(new Base(i));
49+
}
50+
for (auto base : bases) {
51+
delete base;
52+
}
53+
}
54+
955
int main() {
56+
/* Basic example. */
57+
assert(Base::count == 0);
58+
unique_ptr_test();
59+
assert(Base::count == 0);
60+
raw_ptr_test();
61+
assert(Base::count == 10);
62+
manual_ptr_test();
63+
assert(Base::count == 10);
64+
65+
// Copy constructor is deleted.
66+
// This is what imposes uniqueness.
67+
{
68+
std::unique_ptr<int> p(new int);
69+
// ERROR.
70+
//std::unique_ptr<int> p2(p);
71+
72+
// Consequence: for loops over vectors must use references &.
73+
// http://stackoverflow.com/questions/20292682/iterating-through-vectorunique-ptrmytype-using-c11-for-loops
74+
{
75+
std::vector<std::unique_ptr<int>> is;
76+
for (auto& i : is) {}
77+
// ERROR.
78+
//for (auto i : is) {}
79+
}
80+
81+
// Must move glvalues.
82+
// http://stackoverflow.com/questions/3283778/why-can-i-not-push-back-a-unique-ptr-into-a-vector
83+
{
84+
std::vector<std::unique_ptr<int>> is;
85+
std::unique_ptr<int> i(new int(1));
86+
is.push_back(std::move(i));
87+
assert(*is.back() == 1);
88+
89+
// Without intermediate variable, we don't need to move, because it is an rvalue,
90+
// and unique_ptr does have an move constructor.
91+
is.push_back(std::unique_ptr<int>(new int(2)));
92+
assert(*is.back() == 2);
93+
94+
// Analogously, must move containers instead of copy.
95+
{
96+
std::vector<std::unique_ptr<int>> is;
97+
is.push_back(std::unique_ptr<int>(new int(1)));
98+
//std::vector<std::unique_ptr<int>> is2(is);
99+
std::vector<std::unique_ptr<int>> is2(std::move(is));
100+
assert(*(is2.front()) == 1);
101+
}
102+
}
103+
}
10104
}

‎cpp/vector.cpp‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ int main() {
402402
}
403403
}
404404

405-
// # remove_if # filter
405+
// # remove_if
406+
// # filter
406407
// Algorithm. Remove if a given function evaluates to true on an element.
407408
{
408409
std::vector<int> v{0, 1, 2, 3, 4};
@@ -478,11 +479,9 @@ int main() {
478479
}
479480

480481
/*
481-
#back Get reference to last element in vector.
482-
483-
#front Get reference to first element in vector.
484-
485-
#at Like `[]`, but does bound checking and throws `out_of_range` in case of overflow.
482+
# back Get reference to last element in vector.
483+
# front Get reference to first element in vector.
484+
# at Like `[]`, but does bound checking and throws `out_of_range` in case of overflow.
486485
*/
487486
{
488487
std::vector<int> v{0, 1, 2};

‎cpp/virtual_inheritance.cpp‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
# Virtual inheritance
3+
4+
# Virtual base class
5+
6+
# Dreaded diamond
7+
8+
Solves the dreaded diamond problem:
9+
10+
D
11+
/ \
12+
B C
13+
\ /
14+
A
15+
16+
The problem is that D would have two instances of A data.
17+
18+
With virtual inheritance, you say: don't make a copy of the data of this base class.
19+
20+
Has nothing to do with the `virtual` keyword for methods:
21+
everything is done at compile time in this usage.
22+
23+
- http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class
24+
- http://stackoverflow.com/questions/254673/multiple-inheritance-from-two-derived-classes
25+
*/
26+
27+
//#include "common.hpp"
28+
#include <cassert>
29+
30+
class A {
31+
public:
32+
A(){ A::aDefault++; }
33+
A(int i) : i(i) {}
34+
int i;
35+
virtual int f() = 0;
36+
virtual int g() = 0;
37+
virtual int h() = 0;
38+
static int aDefault;
39+
};
40+
int A::aDefault = 0;
41+
class B : public virtual A {
42+
public:
43+
B(int j) : j(j) {}
44+
int j;
45+
virtual int f() { return this->i + this->j; }
46+
};
47+
class C : public virtual A {
48+
public:
49+
C(int k) : k(k) {}
50+
int k;
51+
virtual int g() { return this->i + this->k; }
52+
};
53+
class D : public B, public C {
54+
public:
55+
D(int i, int j, int k) : A(i), B(j), C(k) {}
56+
virtual int h() { return this->i + this->j + this->k; }
57+
};
58+
59+
int main() {
60+
D d = D(1, 2, 4);
61+
// TODO: if the default constructor is never called, why is it required by B and C?
62+
// http://stackoverflow.com/questions/9907722/why-is-default-constructor-called-in-virtual-inheritance
63+
assert(A::aDefault == 0);
64+
assert(d.f() == 3);
65+
assert(d.g() == 5);
66+
assert(d.h() == 7);
67+
}

‎opengl/glfw_compute_shader_heat_equation.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ int main(int argc, char **argv) {
283283
square_width,
284284
square_height
285285
;
286-
unsigned int steps_per_frame;
286+
unsigned int steps_per_frame, window_x;
287287

288288
/* CLI arguments. */
289289
if (argc > 1) {
@@ -339,6 +339,10 @@ int main(int argc, char **argv) {
339339
if (argc > 7) {
340340
steps_per_frame = strtol(argv[7], NULL, 10);
341341
}
342+
window_x = 0;
343+
if (argc > 8) {
344+
window_x = strtol(argv[8], NULL, 10);
345+
}
342346
square_x = width / 2;
343347
square_y = height / 2;
344348
square_width = width / 20;
@@ -350,6 +354,7 @@ int main(int argc, char **argv) {
350354
glfwInit();
351355
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
352356
window = glfwCreateWindow(window_width, window_height, __FILE__, NULL, NULL);
357+
glfwSetWindowPos(window, window_x, 0);
353358
glfwMakeContextCurrent(window);
354359
glfwSwapInterval(1);
355360
glewInit();

‎sdl/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626
1. [Sound frequency](sound_frequency.c)
2727
1. Text
2828
1. [TTF](ttf.c)
29-
1. [Users](users.md)
30-
1. [Bibliography](bibliography.md)
29+
1. Theory
30+
1. [Users](users.md)
31+
1. [Build](build.md)
32+
1. [Bibliography](bibliography.md)

‎sdl/build.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Build
2+
3+
Build from source.
4+
5+
Ubuntu 16.10
6+
7+
hg clone http://hg.libsdl.org/SDL
8+
hg checkout release-2.0.4
9+
mkdir -p build
10+
cd build
11+
# TODO: -DVIDEO_WAYLAND=OFF or else link fails with undefined reference to
12+
# wl_proxy_marshal_constructor_versioned
13+
cmake -DVIDEO_WAYLAND=OFF ..
14+
cmake --build .
15+
16+
Run examples: you must first install it (or checkout to the same version as that installed by your package manager). Then:
17+
18+
cd test
19+
# TODO: patch with:
20+
# https://bugzilla.libsdl.org/show_bug.cgi?id=3508
21+
./configure
22+
make

0 commit comments

Comments
 (0)