Skip to content

Commit 3adaf86

Browse files
committed
SDL texture and GL
1 parent f65fd49 commit 3adaf86

15 files changed

Lines changed: 398 additions & 165 deletions

‎cpp/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
1. [exception](exception.cpp)
4949
1. [typedef](typedef.cpp)
5050
1. [typecast](typecast.cpp)
51+
1. [const_cast](const_cast.cpp)
52+
1. [static_cast](static_cast.cpp)
5153
1. [new](new.cpp)
5254
1. [Preprocessor](preprocessor.cpp)
5355
1. using

‎cpp/common.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <new>
2828
#include <numeric> // partial sums, differences on std::vectors of numbers
2929
#include <ostream> // ostream
30+
#include <ratio> // ratio, nano
3031
#include <set> // multiset, set
3132
#include <string> // getline, string
3233
#include <sstream> // stringstream

‎cpp/const_cast.cpp‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
# const_cast
3+
4+
Change (add/remove) constantness and volatility of objects (property called cv-qualification).
5+
6+
Does not compile to any extra CPU instructions:
7+
only changes type information for the compiler.
8+
9+
TODO when should it be used?
10+
11+
- http://stackoverflow.com/questions/357600/is-const-cast-safe/357640#357640
12+
- avoiding repetition between const and non const versions of arguments is an use case:
13+
- http://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-func
14+
- http://stackoverflow.com/questions/5324256/is-const-castconst-type-ever-useful/5326238#comment69750469_5326238
15+
*/
16+
17+
#include "common.hpp"
18+
19+
int main() {
20+
// Remove const.
21+
{
22+
// UB if data is const.
23+
{
24+
const int i = 0;
25+
const int *ip = &i;
26+
27+
// ERROR.
28+
//*ip = 1;
29+
30+
// UB
31+
// http://stackoverflow.com/questions/19208560/can-we-use-const-cast-to-modify-a-constant-variable
32+
//*(const_cast<int*>(ip)) = 1;
33+
}
34+
35+
// Fine otherwise.
36+
{
37+
int i = 0;
38+
const int *ip = &i;
39+
40+
// ERROR.
41+
//*ip = 1;
42+
43+
// OK.
44+
*(const_cast<int*>(ip)) = 1;
45+
assert(i == 1);
46+
}
47+
}
48+
49+
// Only works for references or pointers.
50+
{
51+
int i;
52+
// ERROR
53+
//const_cast<const int>(i);
54+
const_cast<const int&>(i);
55+
const_cast<const int*>(&i);
56+
}
57+
58+
// ERROR: Returns rvalues. Therefore cannot initialize non-const references.
59+
{
60+
int i = 0;
61+
int *ip;
62+
//const int*& ipa = const_cast<const int*>(ip);
63+
//ipa is a non const reference.
64+
//int *const& would be a const reference.
65+
}
66+
67+
// Only has effect for a single statement.
68+
{
69+
int i = 0;
70+
const int *ip = &i;
71+
const_cast<int*>(ip);
72+
// ERROR.
73+
//*ip = 1;
74+
}
75+
}

‎cpp/interactive/chrono.cpp‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,60 @@
11
/*
22
# chrono
3+
4+
What the clocks map to in GCC Linux:
5+
http://stackoverflow.com/questions/12392278/measure-time-in-linux-time-vs-clock-vs-getrusage-vs-clock-gettime-vs-gettimeof
36
*/
47

58
#include "common.hpp"
69

710
int main() {
811
#if __cplusplus >= 201103L
9-
// TODO
12+
// # period
1013
std::cout << "high_resolution_clock::period::den = " << std::chrono::high_resolution_clock::period::den << std::endl;
14+
std::cout << "steady_clock::period::den = " << std::chrono::steady_clock::period::den << std::endl;
15+
std::cout << "system_clock::period::den = " << std::chrono::system_clock::period::den << std::endl;
1116

17+
// high_resolution_clock
1218
// Wall clock: sleeps are counted.
13-
std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
14-
std::this_thread::sleep_for(std::chrono::seconds(1));
15-
std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now();
16-
auto dt = end_time - start_time;
17-
18-
std::cout << typeid(dt).name() << std::endl;
19-
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl;
20-
std::cout << std::chrono::duration_cast<std::chrono::seconds>(dt).count() << std::endl;
19+
{
20+
std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
21+
std::this_thread::sleep_for(std::chrono::seconds(1));
22+
std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now();
23+
std::chrono::duration<double, std::nano> dt = end_time - start_time;
24+
std::cout << "high_resolution_clock after sleep = " << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl;
25+
}
26+
27+
// steady_clock
28+
{
29+
auto start_time = std::chrono::steady_clock::now();
30+
std::this_thread::sleep_for(std::chrono::seconds(1));
31+
auto end_time = std::chrono::steady_clock::now();
32+
auto dt = end_time - start_time;
33+
std::cout << "steady_clock after sleep = " << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl;
34+
}
35+
36+
// system_clock
37+
{
38+
auto start_time = std::chrono::system_clock::now();
39+
std::this_thread::sleep_for(std::chrono::seconds(1));
40+
auto end_time = std::chrono::system_clock::now();
41+
auto dt = end_time - start_time;
42+
std::cout << "system_clock after sleep = " << std::chrono::duration_cast<std::chrono::nanoseconds>(dt).count() << std::endl;
43+
}
44+
45+
/*
46+
# time_point
47+
48+
Convertions to base types:
49+
50+
- http://stackoverflow.com/questions/12835577/how-to-convert-stdchronotime-point-to-calendar-datetime-string-with-fraction
51+
- http://stackoverflow.com/questions/33785564/how-do-i-get-seconds-since-epoch-as-a-double-given-a-time-point
52+
- http://stackoverflow.com/questions/31255486/c-how-do-i-convert-a-stdchronotime-point-to-long-and-back
53+
*/
54+
{
55+
std::cout << "nanoseconds since system_clock epoch = " << std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() << std::endl;
56+
std::cout << "nanoseconds since steady_clock epoch = " << std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() << std::endl;
57+
}
2158
return 0;
2259
#endif
2360
}

‎cpp/iterator.cpp‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ int main() {
6363
}
6464
}
6565

66-
// begin is overloaded by method const-ness, to return either
67-
// ::iterator or ::const_iterator
68-
// http://stackoverflow.com/questions/12646998/how-does-begin-know-which-return-type-to-return-const-or-non-const
69-
{
70-
const std::vector<std::unique_ptr<int>> v;
71-
// ERROR.
72-
//std::vector<std::unique_ptr<int>>::iterator it = v.begin();
73-
std::vector<std::unique_ptr<int>>::const_iterator it = v.begin();
74-
}
75-
7666
/*
7767
# backwards iteration
7868
@@ -286,7 +276,7 @@ int main() {
286276
int i;
287277
int is[] = {1, 2, 0};
288278

289-
//forward
279+
// forward
290280
{
291281
i = 2;
292282
for (auto it = v.rbegin(); it != v.rend(); ++it) {
@@ -435,4 +425,39 @@ int main() {
435425
== typeid(std::random_access_iterator_tag));
436426
}
437427
}
428+
429+
/*
430+
# const_iterator
431+
432+
begin is overloaded by method const-ness, to return either
433+
::iterator or ::const_iterator
434+
435+
http://stackoverflow.com/questions/12646998/how-does-begin-know-which-return-type-to-return-const-or-non-const
436+
437+
const_iterator returns const values, so it cannot be used to modify the container.
438+
*/
439+
{
440+
// Basic example.
441+
{
442+
const std::vector<int> v{0};
443+
444+
// ERROR.
445+
//std::vector<int>::iterator it = v.begin();
446+
447+
// OK.
448+
std::vector<int>::const_iterator it = v.begin();
449+
// ERROR: cannot modify because const_iterator.
450+
//*it = 1;
451+
}
452+
453+
// iterator can be converted to const_iterator.
454+
{
455+
std::vector<int> v{0};
456+
std::vector<int>::iterator it = v.begin();
457+
*it = 1;
458+
std::vector<int>::const_iterator itc = it;
459+
// ERROR.
460+
//*itc = 1;
461+
}
462+
}
438463
}

0 commit comments

Comments
 (0)