Skip to content

Commit a91befb

Browse files
committed
Split cpp a lot
1 parent bc46513 commit a91befb

44 files changed

Lines changed: 2875 additions & 2696 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Makefile_many‎

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
MYCC ?= gcc
1010
MYCXX ?= g++
1111
D ?= #-D_XOPEN_SOURCE 700
12+
G ?= gdb3
1213
I ?= #-I/usr/include
1314
O ?= 0
1415
STD ?= c11
1516
PEDANTIC ?= -pedantic-errors
16-
CFLAGS ?= $(D) -O$(O) -std=$(STD) -Wextra $(PEDANTIC) $(CFLAGS_EXTRA)
17-
MYCXXFLAGS ?= $(D) -O$(O) -std=c++11 -Wextra $(PEDANTIC) $(CXXFLAGS_EXTRA)
17+
CFLAGS ?= $(D) -g$(G) -O$(O) -std=$(STD) -Wextra $(PEDANTIC) $(CFLAGS_EXTRA)
18+
MYCXXFLAGS ?= $(D) -g$(G) -O$(O) -std=c++11 -Wextra $(PEDANTIC) $(CXXFLAGS_EXTRA)
1819

19-
LIBS ?= -lm #-lGL -lGLU -lglut
20+
LIBS ?= -lm -pthread #-lGL -lGLU -lglut
2021

2122
# Fortran
2223
FF ?= gfortran
@@ -40,10 +41,6 @@ RUN_INPUT ?= input
4041
TEST ?= test
4142

4243
ASSEMBLER_NOEXT ?= $(IN_DIR)$(RUN)
43-
DEBUG_DEFINE ?=
44-
DEBUG_FLAGS ?=
45-
PROFILE_DEFINE ?= #-DDEBUG
46-
PROFILE_FLAGS ?= #
4744

4845
RUN_BNAME := $(RUN)$(OUT_EXT)
4946

@@ -53,7 +50,7 @@ OUTS_NODIR := $(basename $(INS_NODIR))
5350
OUTS_NODIR := $(addsuffix $(OUT_EXT), $(OUTS_NODIR))
5451
OUTS := $(addprefix $(OUT_DIR), $(OUTS_NODIR))
5552

56-
.PHONY: all asm set_asm_flags clean debug set_debug_flags help mkdir objdump set_objdump_flags profile set_profile_flags test $(PHONY)
53+
.PHONY: all asm clean debug help mkdir objdump set_objdump_flags profile set_profile_flags test $(PHONY)
5754

5855
all: mkdir $(OUTS)
5956
@#TODO ignore errors if not present
@@ -65,26 +62,26 @@ ifneq ($(strip $(run)),)
6562
endif
6663

6764
$(OUT_DIR)%$(OUT_EXT): $(IN_DIR)%.c
68-
$(MYCC) $(CFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(I) -o "$@" "$<" $(LIBS)
65+
$(MYCC) $(CFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(I) -o "$@" "$<" $(LIBS)
6966

7067
$(OUT_DIR)%$(OUT_EXT): $(IN_DIR)%.cpp
71-
$(MYCXX) $(MYCXXFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(I) -o "$@" "$<" $(LIBS)
68+
$(MYCXX) $(MYCXXFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(I) -o "$@" "$<" $(LIBS)
7269

7370
$(OUT_DIR)%$(OUT_EXT): $(IN_DIR)%.f
74-
$(FF) $(FFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) -o "$@" "$<" $(FFLIBS)
71+
$(FF) $(FFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) -o "$@" "$<" $(FFLIBS)
7572

7673
# Make assembly intermingled with original C code to stdout>
7774
# TODO0: how not to rewrite the make rules?
78-
# For bare asm: $(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(CFLAGS) -fverbose-asm -S "$(ASSEMBLER_NOEXT)$$EXT"
75+
# For bare asm: $(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(CFLAGS) -fverbose-asm -S "$(ASSEMBLER_NOEXT)$$EXT"
7976
asm: mkdir set_asm_flags
8077
for EXT in $(IN_EXTS); do \
8178
if [ -f "$(ASSEMBLER_NOEXT)$$EXT" ]; then \
8279
case "$$EXT" in \
8380
.c)\
84-
$(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(CFLAGS) -c -fverbose-asm -Wa,-adhln "$(ASSEMBLER_NOEXT)$$EXT" $(LIBS) -o $(OUT_DIR)asm.o\
81+
$(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(CFLAGS) -c -fverbose-asm -Wa,-adhln "$(ASSEMBLER_NOEXT)$$EXT" $(LIBS) -o $(OUT_DIR)asm.o\
8582
;;\
8683
.cpp)\
87-
$(MYCXX) $(MYCXXFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) -c -fverbose-asm -Wa,-adhln "$(ASSEMBLER_NOEXT)$$EXT" $(LIBS)\
84+
$(MYCXX) $(MYCXXFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) -c -fverbose-asm -Wa,-adhln "$(ASSEMBLER_NOEXT)$$EXT" $(LIBS)\
8885
;;\
8986
.f)\
9087
;;\
@@ -93,9 +90,6 @@ asm: mkdir set_asm_flags
9390
fi;\
9491
done
9592

96-
set_asm_flags:
97-
$(eval DEBUG_FLAGS := -ggdb3)
98-
9993
clean:
10094
if [ ! '$(OUT_DIR)' = './' ]; then \
10195
rm -rf '$(OUT_DIR)' ;\
@@ -106,21 +100,14 @@ clean:
106100
debug: clean set_debug_flags all
107101
cd $(OUT_DIR) && gdb "$(RUN_BNAME)"
108102

109-
set_debug_flags:
110-
$(eval DEBUG_FLAGS := -ggdb3)
111-
$(eval DEBUG_DEFINE := -DDEBUG)
112-
113103
mkdir:
114104
@mkdir -p "$(OUT_DIR)"
115105

116106
objdump: mkdir set_objdump_flags all
117-
cd $(OUT_DIR) && objdump -S $(RUN_BNAME)
118-
119-
set_objdump_flags:
120-
$(eval DEBUG_FLAGS := -ggdb3)
107+
cd '$(OUT_DIR)' && objdump -CSr '$(RUN_BNAME)'
121108

122109
profile: clean set_profile_flags all run
123-
cd $(OUT_DIR) && gprof -b $(RUN_BNAME) gmon.out | tee "$(RUN_BNAME).profile_out" | less
110+
cd '$(OUT_DIR)' && gprof -b '$(RUN_BNAME)' gmon.out | tee "$(RUN_BNAME).profile_out" | less
124111

125112
run: all
126113
@echo

‎c/enum.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,27 @@ int main() {
4141
/*, E5*/
4242
};
4343

44+
/* Enum types can then be cast to int. */
4445
/* If unspecified, the first is 0. */
4546
assert(E0 == 0);
4647
assert(E1 == 1);
4748
assert(E2 == 3);
4849
/* Continue from the last one. */
4950
assert(E3 == 4);
5051
assert(E4 == INT_MAX);
52+
53+
// int can also be cast to enum?
54+
{
55+
{
56+
enum E e = 0;
57+
assert(e == E0);
58+
}
59+
60+
{
61+
enum E e = -1;
62+
assert(e == -1);
63+
}
64+
}
5165
}
5266

5367
/*

‎c/posix/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile

‎c/posix/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# POSIX
2+
3+
C features which require POSIX to demonstrate, possibly because some parts haven't been implemented yet, mainly multi-threading for now.

‎c/posix/atomic.c‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Optional language feature, checked with `__STDC_NO_ATOMICS__`.
3+
4+
Modeled on C++ `std::atomic` and analogous to it, just without using C++ language features.
5+
6+
# _Atomic
7+
8+
- http://stackoverflow.com/questions/24557728/does-any-c-library-implement-c11-threads-for-gnu-linux
9+
10+
# ATOMIC_VAR_INIT
11+
12+
Required to initialize auto `_Atomic`, but not globals
13+
14+
- http://stackoverflow.com/questions/15317666/how-to-initialize-and-use-a-c11-atomic-variable
15+
*/
16+
17+
#ifndef __STDC_NO_ATOMICS__
18+
#include <assert.h>
19+
#include <pthread.h>
20+
/* Not in GCC 5.1 yet. */
21+
/*#include <threads.h>*/
22+
#include <stdlib.h>
23+
24+
enum CONSTANTS {
25+
NUM_THREADS = 1000,
26+
NUM_ITERS = 1000
27+
};
28+
29+
_Atomic int global = 0;
30+
31+
void* main_thread(void *arg) {
32+
int i;
33+
for (i = 0; i < NUM_ITERS; ++i) {
34+
/*
35+
Postrix ++ and compound assignment are atomic. TODO Other operators?
36+
37+
Other operations need `stdatomic` functions.
38+
39+
6.5.2.4/2 "Postfix increment and decrement operators"
40+
41+
> Postfix ++ on an object with atomic type is a read-modify-write operation
42+
with memory_order_seq_cst memory order semantics.
43+
44+
6.5.16.2/3 "Compound assignment":
45+
46+
> If E1 has an atomic type, compound assignment is a read-modify-write operation
47+
with memory_order_seq_cst memory order semantics.
48+
*/
49+
global++;
50+
}
51+
return NULL;
52+
}
53+
#endif
54+
55+
int main() {
56+
#ifndef __STDC_NO_ATOMICS__
57+
int i;
58+
59+
pthread_t threads[NUM_THREADS];
60+
for (i = 0; i < NUM_THREADS; ++i)
61+
pthread_create(&threads[i], NULL, main_thread, NULL);
62+
for (i = 0; i < NUM_THREADS; ++i)
63+
pthread_join(threads[i], NULL);
64+
65+
/* Not in GCC 5.1 yet. */
66+
/*
67+
thrd_t thr[NUM_THREADS];
68+
for(int i = 0; n < NUM_THREADS; ++n)
69+
thrd_create(&thr[i], main_thread, NULL);
70+
for(int n = 0; n < NUM_THREADS; ++n)
71+
thrd_join(thr[i], NULL);
72+
*/
73+
74+
/* If we still had many threads here, I think we would need to use `atomic_load`. */
75+
assert(global == NUM_THREADS * NUM_ITERS);
76+
#endif
77+
return EXIT_SUCCESS;
78+
}

‎c/preprocessor.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,14 @@ assert(false);
568568
assert(sizeof(double) == 8);
569569
#endif
570570
}
571+
572+
#ifndef __STDC_NO_ATOMICS__
573+
/* Indicates no C11 support for `_Atomic` and `<stdatomic.h>`. */
574+
puts("__STDC_NO_ATOMICS__");
575+
#else
576+
#include <stdatomic.h>
577+
_Atomic int i;
578+
#endif
571579
}
572580

573581
return EXIT_SUCCESS;

‎cpp/Makefile_params‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CXXFLAGS_EXTRA := -Wno-sequence-point -Wno-unused-variable -Wno-unused-but-set-variable -pthread
1+
CXXFLAGS_EXTRA := -Wno-sequence-point -Wno-unused-variable -Wno-unused-but-set-variable -pthread

‎cpp/README.md‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
1. [main.cpp](main.cpp)
99
1. [Hello world](hello_world.cpp)
1010
1. [const](const.cpp)
11-
1. [constexpr](constexpr.cpp)
12-
1. [auto](auto.cpp)
13-
1. [decltype](decltype.cpp)
11+
1. [RTTI](rtti.md)
12+
1. Compile time magic
13+
1. [constexpr](constexpr.cpp)
14+
1. [static_assert](static_assert.cpp)
15+
1. [typeid](typeid.cpp)
16+
1. [auto](auto.cpp)
17+
1. [decltype](decltype.cpp)
1418
1. [nullptr](nullptr.cpp)
15-
1. [reference](reference.cpp)
16-
1. [rvalue reference](rvalue_reference.cpp)
19+
1. References
20+
1. [reference](reference.cpp)
21+
1. [rvalue reference](rvalue_reference.cpp)
1722
1. [Function](function.cpp)
1823
1. [Operator overload](operator_overload.cpp)
1924
1. [Conversion function](conversion_function.cpp)
@@ -29,10 +34,25 @@
2934
1. [Preprocessor](preprocessor.cpp)
3035
1. [Standard library](standard_library.md)
3136
1. [Headers](common.hpp)
32-
1. [limits](limits.cpp)
37+
1. [Containers](containers.md)
38+
1. [vector](vector.cpp)
39+
1. [list](list.cpp)
40+
1. [map](map.cpp)
41+
1. [set](set.cpp)
42+
1. [valarray](valarray.cpp)
43+
1. [deque](deque.cpp)
44+
1. [string](string.cpp)
3345
1. [thread](thread.cpp)
34-
1. [atomic](atomic.cpp)
35-
1. [mutex](mutex.cpp)
46+
1. [sleep_for](sleep_for.cpp)
47+
1. [atomic](atomic.cpp)
48+
1. [atomic<bool>](atomic_bool.cpp.off)
49+
1. [mutex](mutex.cpp)
50+
1. [algorithm](algorithm.cpp)
51+
1. [functional](functional.cpp)
52+
1. [iterator](iterator.cpp)
53+
1. [limits](limits.cpp)
3654
1. [memory](memory.cpp)
55+
1. [regex](regex.cpp)
56+
1. [utility](utility.cpp)
3757
1. Applications
3858
1. [Design patterns](design_patterns.cpp)

0 commit comments

Comments
 (0)