first commit

This commit is contained in:
Johannes Theiner 2018-03-09 09:47:53 +01:00
commit 937f18667d
88 changed files with 4774 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
cmake-build-debug
xcode_projects

2
.idea/C_CPP.iml generated Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

29
.idea/codeStyles/Project.xml generated Normal file
View File

@ -0,0 +1,29 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>

4
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/C_CPP.iml" filepath="$PROJECT_DIR$/.idea/C_CPP.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

14
01_ENV/build.sh Normal file
View File

@ -0,0 +1,14 @@
#!/bin/sh
# generate main.s, main.o, b.out
pcc -O0 -S main.c
as -o main.o main.s
#ld -o b.out main.o /lib/crt0.o /lib/crti.o -lc
pcc -o b.out main.o
# generate a.out
pcc -O0 -g main.c
# generate assembly intermixed with source code
objdump -S a.out > objdump-S_a.out.txt

4
01_ENV/clean.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/sh
rm *.o *.s *.out

21
01_ENV/main.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
int global = 8150;
int sum(int,int);
int sum(int a, int b){
int result = 451;
result = a + b;
return result;
}
int main(int argc, char **argv)
{
int local=4711;
printf("Hello, world!\nglobal=%d local=%d\n", global, local);
local = sum(global, local);
return local;
}

58
01_ENV/main.s Normal file
View File

@ -0,0 +1,58 @@
.data
.align 4
.globl global
.type global,@object
.size global,4
global:
.long 8150
.text
.align 4
.globl sum
.type sum,@function
sum:
enter $8,$0
.L461:
.L465:
movl $451,-4(%ebp)
movl 8(%ebp),%eax
addl 12(%ebp),%eax
movl %eax,-4(%ebp)
movl -4(%ebp),%eax
movl %eax,-8(%ebp)
jmp .L463
.L463:
movl -8(%ebp),%eax
leave
ret
.size sum,.-sum
.align 4
.globl main
.type main,@function
main:
enter $8,$0
.L469:
.L473:
movl $4711,-4(%ebp)
pushl -4(%ebp)
pushl global
pushl $.L475
call printf
addl $12, %esp
pushl -4(%ebp)
pushl global
call sum
addl $8, %esp
movl %eax,-4(%ebp)
movl -4(%ebp),%eax
movl %eax,-8(%ebp)
jmp .L471
.L471:
movl -8(%ebp),%eax
leave
ret
.size main,.-main
.section .rodata
.size .L475,34
.L475:
.ascii "Hello, world!\012global=%d local=%d\012\0"
.ident "PCC: Portable C Compiler 1.2.0.DEVEL 20160115 for i686-pc-linux-gnu"

19
01_ENV/main_plain.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
int global = 8150;
int sum(int a, int b){
int result = 451;
result = a + b;
return result;
}
int main(int argc, char **argv)
{
int local=4711;
printf("Hello, world!\nglobal=%d local=%d\n", global, local);
local = sum(global, local);
return local;
}

324
01_ENV/objdump-S_a.out.txt Normal file
View File

@ -0,0 +1,324 @@
a.out: file format elf32-i386
Disassembly of section .init:
08048278 <_init>:
8048278: 53 push %ebx
8048279: 83 ec 08 sub $0x8,%esp
804827c: e8 8f 00 00 00 call 8048310 <__x86.get_pc_thunk.bx>
8048281: 81 c3 7f 1d 00 00 add $0x1d7f,%ebx
8048287: 8b 83 fc ff ff ff mov -0x4(%ebx),%eax
804828d: 85 c0 test %eax,%eax
804828f: 74 05 je 8048296 <_init+0x1e>
8048291: e8 2a 00 00 00 call 80482c0 <__gmon_start__@plt>
8048296: e8 45 01 00 00 call 80483e0 <__do_global_ctors_aux>
804829b: 83 c4 08 add $0x8,%esp
804829e: 5b pop %ebx
804829f: c3 ret
Disassembly of section .plt:
080482a0 <printf@plt-0x10>:
80482a0: ff 35 04 a0 04 08 pushl 0x804a004
80482a6: ff 25 08 a0 04 08 jmp *0x804a008
80482ac: 00 00 add %al,(%eax)
...
080482b0 <printf@plt>:
80482b0: ff 25 0c a0 04 08 jmp *0x804a00c
80482b6: 68 00 00 00 00 push $0x0
80482bb: e9 e0 ff ff ff jmp 80482a0 <_init+0x28>
080482c0 <__gmon_start__@plt>:
80482c0: ff 25 10 a0 04 08 jmp *0x804a010
80482c6: 68 08 00 00 00 push $0x8
80482cb: e9 d0 ff ff ff jmp 80482a0 <_init+0x28>
080482d0 <__libc_start_main@plt>:
80482d0: ff 25 14 a0 04 08 jmp *0x804a014
80482d6: 68 10 00 00 00 push $0x10
80482db: e9 c0 ff ff ff jmp 80482a0 <_init+0x28>
Disassembly of section .text:
080482e0 <_start>:
80482e0: 31 ed xor %ebp,%ebp
80482e2: 5e pop %esi
80482e3: 89 e1 mov %esp,%ecx
80482e5: 83 e4 f0 and $0xfffffff0,%esp
80482e8: 50 push %eax
80482e9: 54 push %esp
80482ea: 52 push %edx
80482eb: 68 30 85 04 08 push $0x8048530
80482f0: 68 d0 84 04 08 push $0x80484d0
80482f5: 51 push %ecx
80482f6: 56 push %esi
80482f7: 68 8c 84 04 08 push $0x804848c
80482fc: e8 cf ff ff ff call 80482d0 <__libc_start_main@plt>
8048301: f4 hlt
8048302: 66 90 xchg %ax,%ax
8048304: 66 90 xchg %ax,%ax
8048306: 66 90 xchg %ax,%ax
8048308: 66 90 xchg %ax,%ax
804830a: 66 90 xchg %ax,%ax
804830c: 66 90 xchg %ax,%ax
804830e: 66 90 xchg %ax,%ax
08048310 <__x86.get_pc_thunk.bx>:
8048310: 8b 1c 24 mov (%esp),%ebx
8048313: c3 ret
8048314: 66 90 xchg %ax,%ax
8048316: 66 90 xchg %ax,%ax
8048318: 66 90 xchg %ax,%ax
804831a: 66 90 xchg %ax,%ax
804831c: 66 90 xchg %ax,%ax
804831e: 66 90 xchg %ax,%ax
08048320 <__ctors>:
8048320: e8 34 01 00 00 call 8048459 <__x86.get_pc_thunk.ax>
8048325: 05 db 1c 00 00 add $0x1cdb,%eax
804832a: 56 push %esi
804832b: 53 push %ebx
804832c: 8d 64 24 fc lea -0x4(%esp),%esp
8048330: 8d 88 24 ff ff ff lea -0xdc(%eax),%ecx
8048336: 8b 11 mov (%ecx),%edx
8048338: 83 fa ff cmp $0xffffffff,%edx
804833b: 74 2b je 8048368 <__ctors+0x48>
804833d: 8d 5a ff lea -0x1(%edx),%ebx
8048340: 8d 34 91 lea (%ecx,%edx,4),%esi
8048343: 8d 04 9d 00 00 00 00 lea 0x0(,%ebx,4),%eax
804834a: 29 c6 sub %eax,%esi
804834c: 85 d2 test %edx,%edx
804834e: 74 0a je 804835a <__ctors+0x3a>
8048350: ff 14 9e call *(%esi,%ebx,4)
8048353: 85 db test %ebx,%ebx
8048355: 8d 5b ff lea -0x1(%ebx),%ebx
8048358: 75 f6 jne 8048350 <__ctors+0x30>
804835a: 8d 64 24 04 lea 0x4(%esp),%esp
804835e: 5b pop %ebx
804835f: 5e pop %esi
8048360: c3 ret
8048361: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
8048368: 8b 71 04 mov 0x4(%ecx),%esi
804836b: 85 f6 test %esi,%esi
804836d: 74 eb je 804835a <__ctors+0x3a>
804836f: ba 01 00 00 00 mov $0x1,%edx
8048374: eb 04 jmp 804837a <__ctors+0x5a>
8048376: 66 90 xchg %ax,%ax
8048378: 89 c2 mov %eax,%edx
804837a: 8d 42 01 lea 0x1(%edx),%eax
804837d: 8b 1c 81 mov (%ecx,%eax,4),%ebx
8048380: 85 db test %ebx,%ebx
8048382: 75 f4 jne 8048378 <__ctors+0x58>
8048384: eb b7 jmp 804833d <__ctors+0x1d>
8048386: 8d 76 00 lea 0x0(%esi),%esi
8048389: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
08048390 <__dtors>:
8048390: e8 c4 00 00 00 call 8048459 <__x86.get_pc_thunk.ax>
8048395: 05 6b 1c 00 00 add $0x1c6b,%eax
804839a: 53 push %ebx
804839b: 8d 64 24 f8 lea -0x8(%esp),%esp
804839f: 8d 90 2c ff ff ff lea -0xd4(%eax),%edx
80483a5: 89 d3 mov %edx,%ebx
80483a7: 83 c3 04 add $0x4,%ebx
80483aa: 8b 42 04 mov 0x4(%edx),%eax
80483ad: 85 c0 test %eax,%eax
80483af: 74 1a je 80483cb <__dtors+0x3b>
80483b1: eb 0d jmp 80483c0 <__dtors+0x30>
80483b3: 90 nop
80483b4: 90 nop
80483b5: 90 nop
80483b6: 90 nop
80483b7: 90 nop
80483b8: 90 nop
80483b9: 90 nop
80483ba: 90 nop
80483bb: 90 nop
80483bc: 90 nop
80483bd: 90 nop
80483be: 90 nop
80483bf: 90 nop
80483c0: 8d 5b 04 lea 0x4(%ebx),%ebx
80483c3: ff d0 call *%eax
80483c5: 8b 03 mov (%ebx),%eax
80483c7: 85 c0 test %eax,%eax
80483c9: 75 f5 jne 80483c0 <__dtors+0x30>
80483cb: 8d 64 24 08 lea 0x8(%esp),%esp
80483cf: 5b pop %ebx
80483d0: c3 ret
80483d1: eb 0d jmp 80483e0 <__do_global_ctors_aux>
80483d3: 90 nop
80483d4: 90 nop
80483d5: 90 nop
80483d6: 90 nop
80483d7: 90 nop
80483d8: 90 nop
80483d9: 90 nop
80483da: 90 nop
80483db: 90 nop
80483dc: 90 nop
80483dd: 90 nop
80483de: 90 nop
80483df: 90 nop
080483e0 <__do_global_ctors_aux>:
80483e0: e8 74 00 00 00 call 8048459 <__x86.get_pc_thunk.ax>
80483e5: 05 1b 1c 00 00 add $0x1c1b,%eax
80483ea: 8b 90 24 00 00 00 mov 0x24(%eax),%edx
80483f0: 85 d2 test %edx,%edx
80483f2: 74 04 je 80483f8 <__do_global_ctors_aux+0x18>
80483f4: c3 ret
80483f5: 8d 76 00 lea 0x0(%esi),%esi
80483f8: c7 80 24 00 00 00 01 movl $0x1,0x24(%eax)
80483ff: 00 00 00
8048402: e9 19 ff ff ff jmp 8048320 <__ctors>
8048407: 89 f6 mov %esi,%esi
8048409: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
08048410 <__do_global_dtors_aux>:
8048410: 53 push %ebx
8048411: e8 fa fe ff ff call 8048310 <__x86.get_pc_thunk.bx>
8048416: 81 c3 ea 1b 00 00 add $0x1bea,%ebx
804841c: 8d 64 24 f8 lea -0x8(%esp),%esp
8048420: 8b 83 20 00 00 00 mov 0x20(%ebx),%eax
8048426: 85 c0 test %eax,%eax
8048428: 75 0f jne 8048439 <__do_global_dtors_aux+0x29>
804842a: e8 61 ff ff ff call 8048390 <__dtors>
804842f: c7 83 20 00 00 00 01 movl $0x1,0x20(%ebx)
8048436: 00 00 00
8048439: 8d 64 24 08 lea 0x8(%esp),%esp
804843d: 5b pop %ebx
804843e: c3 ret
804843f: 90 nop
08048440 <__call___do_global_ctors_aux>:
8048440: 8d 64 24 f4 lea -0xc(%esp),%esp
8048444: 8d 64 24 0c lea 0xc(%esp),%esp
8048448: c3 ret
8048449: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
08048450 <__call___do_global_dtors_aux>:
8048450: 8d 64 24 f4 lea -0xc(%esp),%esp
8048454: 8d 64 24 0c lea 0xc(%esp),%esp
8048458: c3 ret
08048459 <__x86.get_pc_thunk.ax>:
8048459: 90 nop
804845a: 90 nop
804845b: 90 nop
804845c: 90 nop
804845d: 90 nop
804845e: 90 nop
804845f: 90 nop
8048460: 90 nop
8048461: 8b 04 24 mov (%esp),%eax
8048464: c3 ret
8048465: 66 90 xchg %ax,%ax
8048467: 90 nop
08048468 <sum>:
int global = 8150;
int sum(int,int);
int sum(int a, int b){
8048468: c8 08 00 00 enter $0x8,$0x0
int result = 451;
804846c: c7 45 fc c3 01 00 00 movl $0x1c3,-0x4(%ebp)
result = a + b;
8048473: 8b 45 08 mov 0x8(%ebp),%eax
8048476: 03 45 0c add 0xc(%ebp),%eax
8048479: 89 45 fc mov %eax,-0x4(%ebp)
return result;
804847c: 8b 45 fc mov -0x4(%ebp),%eax
804847f: 89 45 f8 mov %eax,-0x8(%ebp)
8048482: eb 00 jmp 8048484 <sum+0x1c>
}
8048484: 8b 45 f8 mov -0x8(%ebp),%eax
8048487: c9 leave
8048488: c3 ret
8048489: 8d 76 00 lea 0x0(%esi),%esi
0804848c <main>:
int main(int argc, char **argv)
{
804848c: c8 08 00 00 enter $0x8,$0x0
int local=4711;
8048490: c7 45 fc 67 12 00 00 movl $0x1267,-0x4(%ebp)
printf("Hello, world!\nglobal=%d local=%d\n", global, local);
8048497: ff 75 fc pushl -0x4(%ebp)
804849a: ff 35 1c a0 04 08 pushl 0x804a01c
80484a0: 68 58 85 04 08 push $0x8048558
80484a5: e8 06 fe ff ff call 80482b0 <printf@plt>
80484aa: 83 c4 0c add $0xc,%esp
local = sum(global, local);
80484ad: ff 75 fc pushl -0x4(%ebp)
80484b0: ff 35 1c a0 04 08 pushl 0x804a01c
80484b6: e8 ad ff ff ff call 8048468 <sum>
80484bb: 83 c4 08 add $0x8,%esp
80484be: 89 45 fc mov %eax,-0x4(%ebp)
return local;
80484c1: 8b 45 fc mov -0x4(%ebp),%eax
80484c4: 89 45 f8 mov %eax,-0x8(%ebp)
80484c7: eb 00 jmp 80484c9 <main+0x3d>
}
80484c9: 8b 45 f8 mov -0x8(%ebp),%eax
80484cc: c9 leave
80484cd: c3 ret
80484ce: 66 90 xchg %ax,%ax
080484d0 <__libc_csu_init>:
80484d0: 55 push %ebp
80484d1: 57 push %edi
80484d2: 56 push %esi
80484d3: 53 push %ebx
80484d4: e8 37 fe ff ff call 8048310 <__x86.get_pc_thunk.bx>
80484d9: 81 c3 27 1b 00 00 add $0x1b27,%ebx
80484df: 83 ec 0c sub $0xc,%esp
80484e2: 8b 6c 24 20 mov 0x20(%esp),%ebp
80484e6: 8d b3 24 ff ff ff lea -0xdc(%ebx),%esi
80484ec: e8 87 fd ff ff call 8048278 <_init>
80484f1: 8d 83 24 ff ff ff lea -0xdc(%ebx),%eax
80484f7: 29 c6 sub %eax,%esi
80484f9: c1 fe 02 sar $0x2,%esi
80484fc: 85 f6 test %esi,%esi
80484fe: 74 25 je 8048525 <__libc_csu_init+0x55>
8048500: 31 ff xor %edi,%edi
8048502: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
8048508: 83 ec 04 sub $0x4,%esp
804850b: ff 74 24 2c pushl 0x2c(%esp)
804850f: ff 74 24 2c pushl 0x2c(%esp)
8048513: 55 push %ebp
8048514: ff 94 bb 24 ff ff ff call *-0xdc(%ebx,%edi,4)
804851b: 83 c7 01 add $0x1,%edi
804851e: 83 c4 10 add $0x10,%esp
8048521: 39 f7 cmp %esi,%edi
8048523: 75 e3 jne 8048508 <__libc_csu_init+0x38>
8048525: 83 c4 0c add $0xc,%esp
8048528: 5b pop %ebx
8048529: 5e pop %esi
804852a: 5f pop %edi
804852b: 5d pop %ebp
804852c: c3 ret
804852d: 8d 76 00 lea 0x0(%esi),%esi
08048530 <__libc_csu_fini>:
8048530: f3 c3 repz ret
Disassembly of section .fini:
08048534 <_fini>:
8048534: 53 push %ebx
8048535: 83 ec 08 sub $0x8,%esp
8048538: e8 d3 fd ff ff call 8048310 <__x86.get_pc_thunk.bx>
804853d: 81 c3 c3 1a 00 00 add $0x1ac3,%ebx
8048543: e8 c8 fe ff ff call 8048410 <__do_global_dtors_aux>
8048548: 83 c4 08 add $0x8,%esp
804854b: 5b pop %ebx
804854c: c3 ret

3
02_MENT/build.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
clang++ -std=c++14 -I../helpers/ -o limits.out limits_println.cpp
clang++ -std=c++14 -I../helpers/ -o println.out printlnDemo.cpp

20
02_MENT/limits.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tsizeof\tlowest\t\thighest\n";
std::cout << "float\t" << sizeof(float) << '\t'
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t" << sizeof(double) << '\t'
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
std::cout << "int\t" << sizeof(int) << '\t'
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "long\t" << sizeof(long) << '\t'
<< std::numeric_limits<long>::lowest() << '\t'
<< std::numeric_limits<long>::max() << '\n';
}

View File

@ -0,0 +1,21 @@
#include "println.hpp"
#include <limits>
int main()
{
println("type\t sizeof\t\t lowest\t\t\t\t highest\n");
println("float \t ",sizeof(float), "\t\t\t",
std::numeric_limits<float>::lowest(), "\t\t",
std::numeric_limits<float>::max());
println("double\t ",sizeof(double), "\t\t\t",
std::numeric_limits<double>::lowest(), "\t\t",
std::numeric_limits<double>::max());
println("int \t ",sizeof(int), "\t\t\t",
std::numeric_limits<int>::lowest(), "\t\t\t",
std::numeric_limits<int>::max());
println("long \t ",sizeof(long), " \t\t",
std::numeric_limits<long>::lowest(), '\t',
std::numeric_limits<long>::max());
}

31
02_MENT/main_02_MENT.cpp Normal file
View File

@ -0,0 +1,31 @@
// file: main_02_MENT.cpp
// THIS IS C++, use clang++
#include "../helpers/println.hpp"
#include <iostream>
struct PascalString{
int length; // number of chars used
char characters[256]; // chars of some character string
};
int hexDigitToInt(char hexDigit){
int value = -1;
// TBD
return value;
}
int hexStringToInt(PascalString binaryDigits){
int returnValue = -1;
return returnValue;
}
int main(int argc, char** argv, char** envp){
PascalString s = {3, '1', '0', '0'};
PascalString s2 = {4, 'f', 'f', 'f', 'f'};
println(hexStringToInt(s));
println(hexStringToInt(s2));
return 0;
}

17
02_MENT/printlnDemo.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "println.hpp"
int main()
{
println("this program shows how to use println()");
int i=1;
println(i); // type int
double d=2.78;
println(d); // type double
println(""); // just an empty line:
println("i=", i, " d=", d);// more than one
print("i=", i); // print all with separate statements
print(" d=", d); // no "ln" -> no newline
println("");
}

View File

@ -0,0 +1,259 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
654370B41E9788E30085F153 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 654370B31E9788E30085F153 /* main.cpp */; };
654371471E9ED16D0085F153 /* typedMemory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 654371451E9ED16D0085F153 /* typedMemory.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
654370AE1E9788E30085F153 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
654370B01E9788E30085F153 /* typedMemory */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = typedMemory; sourceTree = BUILT_PRODUCTS_DIR; };
654370B31E9788E30085F153 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
654371451E9ED16D0085F153 /* typedMemory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = typedMemory.cpp; sourceTree = "<group>"; };
654371461E9ED16D0085F153 /* typedMemory.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = typedMemory.hpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
654370AD1E9788E30085F153 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
654370A71E9788E30085F153 = {
isa = PBXGroup;
children = (
654370B21E9788E30085F153 /* typedMemory */,
654370B11E9788E30085F153 /* Products */,
);
sourceTree = "<group>";
};
654370B11E9788E30085F153 /* Products */ = {
isa = PBXGroup;
children = (
654370B01E9788E30085F153 /* typedMemory */,
);
name = Products;
sourceTree = "<group>";
};
654370B21E9788E30085F153 /* typedMemory */ = {
isa = PBXGroup;
children = (
654370B31E9788E30085F153 /* main.cpp */,
654371451E9ED16D0085F153 /* typedMemory.cpp */,
654371461E9ED16D0085F153 /* typedMemory.hpp */,
);
path = typedMemory;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
654370AF1E9788E30085F153 /* typedMemory */ = {
isa = PBXNativeTarget;
buildConfigurationList = 654370B71E9788E30085F153 /* Build configuration list for PBXNativeTarget "typedMemory" */;
buildPhases = (
654370AC1E9788E30085F153 /* Sources */,
654370AD1E9788E30085F153 /* Frameworks */,
654370AE1E9788E30085F153 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = typedMemory;
productName = typedMemory;
productReference = 654370B01E9788E30085F153 /* typedMemory */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
654370A81E9788E30085F153 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = HSEL;
TargetAttributes = {
654370AF1E9788E30085F153 = {
CreatedOnToolsVersion = 8.2.1;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 654370AB1E9788E30085F153 /* Build configuration list for PBXProject "typedMemory" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 654370A71E9788E30085F153;
productRefGroup = 654370B11E9788E30085F153 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
654370AF1E9788E30085F153 /* typedMemory */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
654370AC1E9788E30085F153 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
654370B41E9788E30085F153 /* main.cpp in Sources */,
654371471E9ED16D0085F153 /* typedMemory.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
654370B51E9788E30085F153 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
};
name = Debug;
};
654370B61E9788E30085F153 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
};
name = Release;
};
654370B81E9788E30085F153 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
654370B91E9788E30085F153 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
654370AB1E9788E30085F153 /* Build configuration list for PBXProject "typedMemory" */ = {
isa = XCConfigurationList;
buildConfigurations = (
654370B51E9788E30085F153 /* Debug */,
654370B61E9788E30085F153 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
654370B71E9788E30085F153 /* Build configuration list for PBXNativeTarget "typedMemory" */ = {
isa = XCConfigurationList;
buildConfigurations = (
654370B81E9788E30085F153 /* Debug */,
654370B91E9788E30085F153 /* Release */,
);
defaultConfigurationIsVisible = 0;
};
/* End XCConfigurationList section */
};
rootObject = 654370A81E9788E30085F153 /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:typedMemory.xcodeproj">
</FileRef>
</Workspace>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>

View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "654370AF1E9788E30085F153"
BuildableName = "typedMemory"
BlueprintName = "typedMemory"
ReferencedContainer = "container:typedMemory.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "654370AF1E9788E30085F153"
BuildableName = "typedMemory"
BlueprintName = "typedMemory"
ReferencedContainer = "container:typedMemory.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "654370AF1E9788E30085F153"
BuildableName = "typedMemory"
BlueprintName = "typedMemory"
ReferencedContainer = "container:typedMemory.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "654370AF1E9788E30085F153"
BuildableName = "typedMemory"
BlueprintName = "typedMemory"
ReferencedContainer = "container:typedMemory.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>typedMemory.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>654370AF1E9788E30085F153</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,73 @@
// main.cpp
// typedMemory
#include <iostream>
#include <iomanip>
#include "typedMemory.hpp"
struct compund {
int theInt;
double theDouble;
};
/* this illustrates that what the compiler needs to do
to store local variables in the stack frame. Compare
this to the code in main().*/
/*void foobar(){
unsigned int x = 0x04030201;
char c1 = 'C';
char c2 = '+';
char c3 = '+';
char c4 = '!';
double d1 = 16.0;
compund k1 = {0x04030201, 16.0};
compund k2 = {0, 0.0};
}*/
int main(int argc, const char * argv[]) {
TypedMemory mem;
mem.putUInt(0, 0x04030201);
mem.putChar(4,'C');
mem.putChar(5,'+');
mem.putChar(6,'+');
mem.putChar(7,'!');
mem.putDouble(0x30, 16.0 /*355.0/113.0*/);
std::cout << std::setbase(16);
std::cout << "0x" << mem.getUInt(0) << std::endl;
std::cout << mem.getChar(4)
<< mem.getChar(5)
<< mem.getChar(6)
<< mem.getChar(7) << std::endl;
std::cout << "0x" << mem.getUInt(4) << std::endl;
std::cout << mem.getDouble(0x30) << " (" << sizeof(double) << " bytes)" << std::endl;
compund a = {0x04030201, 16.0};
compund b = {0, 0.0};
mem.putAnything(16, &a, sizeof(compund));
mem.getAnything(16, &b, sizeof(compund));
std::cout << "a = (0x" << a.theInt << ", " << a.theDouble << ")" << std::endl;
std::cout << "b = (0x" << b.theInt << ", " << b.theDouble << ")" << std::endl;
std::cout << std::setbase(10);
std::cout << mem.hexDump() << std::endl;
return 0;
}

View File

@ -0,0 +1,104 @@
// typedMemory.cpp
#include "typedMemory.hpp"
#include <iomanip>
#include <sstream>
void TypedMemory::putChar(int position, unsigned char c)
{
rawMemory[position] = reinterpret_cast<uint8_t>(c);
}
unsigned char TypedMemory::getChar(int position){
return reinterpret_cast<unsigned char>(rawMemory[position]);
}
void TypedMemory::putUInt(int position, unsigned int i){
int numBytes = sizeof(i);
unsigned int byteMask = 0xff << ((numBytes-1)*8);
for(int k=0; k<numBytes;k++){
rawMemory[position+k] = (i & byteMask) >> ((numBytes-1-k)*8);
byteMask >>= 8;
}
/* equvalent code for sizeof(unsigned int) == 4:
uint8_t byte_a = (i & 0xff);
uint8_t byte_b = (i & 0xff00) >> 8;
uint8_t byte_c = (i & 0xff0000) >> 16;
uint8_t byte_d = (i & 0xff000000) >> 24;
rawMemory[position+0] = byte_d;
rawMemory[position+1] = byte_c;
rawMemory[position+2] = byte_b;
rawMemory[position+3] = byte_a;
*/
}
unsigned int TypedMemory::getUInt(int position){
unsigned int result = 0;
int numBytes = sizeof(result);
for(int i=0; i<numBytes;i++){
result |= rawMemory[position+i] << ((numBytes-1-i)*8);
}
return result;
}
void TypedMemory::putDouble(int position, double d){
void * pv = reinterpret_cast<void*>(&rawMemory[position]);
double * pd = reinterpret_cast<double*>(pv);
*pd=d;
}
double TypedMemory::getDouble(int position){
void * pv = reinterpret_cast<void*>(&rawMemory[position]);
double * pd = reinterpret_cast<double*>(pv);
return *pd;
}
void TypedMemory::putAnything(int position, void* src, int size){
for (int i=0; i<size; i++){
rawMemory[position+i] = reinterpret_cast<uint8_t*>(src)[i];
}
}
void TypedMemory::getAnything(int position, void* dest, int size){
for (int i=0; i<size; i++){
reinterpret_cast<uint8_t*>(dest)[i] = rawMemory[position+i];
}
}
std::string TypedMemory::hexDump(){
std::string result;
std::string printables;
std::ostringstream os;
os << std::setbase(16);
int i=0;
while(i<RAWMEMORYSIZE){
os << std::setfill('0');
os << std::setw(4);
os << i << ": ";
for (int j=0;j<16;j++){
os << std::setfill('0');
os << std::setw(2);
os << static_cast<int>(rawMemory[i]);
os << " ";
if(std::isprint(rawMemory[i])){
printables += static_cast<char>(rawMemory[i]);
}else{
printables += '.';
}
i++;
}
os << printables << std::endl;
printables = "";
}
os << std::setbase(10);
result = os.str();
return result;
}

View File

@ -0,0 +1,25 @@
// typedMemory.hpp
#ifndef typedMemory_hpp
#define typedMemory_hpp
#include <iostream>
#include <ctype.h>
struct TypedMemory {
static const int RAWMEMORYSIZE=128;//1024;
uint8_t rawMemory[RAWMEMORYSIZE];
void putChar(int position, unsigned char c); // position in bytes starting at 0
unsigned char getChar(int position);
// void putShort(int position);
// char getShort(int position);
void putUInt(int position, unsigned int i);
unsigned int getUInt(int position);
void putDouble(int position, double d);
double getDouble(int position);
void putAnything(int position, void* src, int size);
void getAnything(int position, void* dest, int size);
std::string hexDump();
};
#endif

2
03_FLOW_a/build.sh Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
clang++ -std=c++14 -I ../helpers -o flow.out main_mp2_FLOW_a.cpp ../helpers/AnsiConsole.cpp

View File

@ -0,0 +1,137 @@
#include <stdio.h> /* file main_mp2_FLOW_a.cpp */
#include "AnsiConsole.h"
AnsiConsole console; int firstLine; int currentTick; Colors currentColor;
#define INITPRINT(label) {firstLine=__LINE__;console.printText(2*currentTick,0,label,Colors::BLACK);}
#define PRINT printLineNumber(__LINE__)
void printLineNumber(int lineNumber);
void recurse(int); // forward declaration because of firstLine initialization
void start_Recursion(){
INITPRINT("Recursion");
PRINT;recurse(10);
PRINT;
}
void recurse(int turns){
PRINT; if(turns>0){
PRINT; recurse(turns - 1);
}
PRINT;
}
void start_Sequence(void){
INITPRINT("Sequence");
PRINT;
PRINT;
PRINT;
PRINT;
PRINT;
PRINT;
PRINT;
PRINT;
PRINT;
PRINT;
}
void start_SeqSelection(void){
INITPRINT("Selection");
PRINT; int b = 1;
PRINT; if (b == 1){
PRINT;
PRINT;
PRINT;
PRINT;
PRINT; }else{
PRINT;
PRINT;
PRINT;
PRINT;
PRINT;
}PRINT;
PRINT;
}
void start_Iteration(void){
INITPRINT("Iteration");
PRINT; int i=9;
PRINT; while(i>=0){
PRINT; i--;
}PRINT;
PRINT;
}
int func_1(int arg);
// Sequence, Selection, Iteration, Subroutine
void start_subroutine(void){
INITPRINT("Subroutine");
PRINT; func_1(1);
PRINT; func_1(1);
PRINT;
}
int func_1(int arg){
PRINT; int local_1;
PRINT;
PRINT;
PRINT;
PRINT; return arg * local_1;
}
int main(int argc, const char * argv[]) {
console.clearScreen();
currentColor = Colors::BLUE;
start_Sequence();
currentColor = Colors::RED;
start_SeqSelection();
currentColor = Colors::YELLOW;
start_subroutine();
currentColor = Colors::GREEN;
start_Iteration();
currentColor = Colors::CYAN;
start_Recursion();
//start_goto();
//start_switch();
//start_interrupt();
//start_coroutine();
//start_exception();
std::string s;
std::cin >> s;
return 0;
}
void printLineNumber(int lineNumber){
std::string line = std::to_string(lineNumber);
currentTick++;
console.printText(currentTick*2-1, 1+lineNumber-firstLine, line, currentColor);
}
/*void printLineNumber(int lineNumber){
std::string line = std::to_string(lineNumber);
console.printText(currentTick*2, 1+lineNumber-firstLine, line, currentColor);
currentTick++;
}*/

2
03_FLOW_b/build.sh Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
clang++ -std=c++14 -o controlFlowIntr main_ISR.cpp

BIN
03_FLOW_b/controlFlowIntr Normal file

Binary file not shown.

27
03_FLOW_b/main_ISR.cpp Normal file
View File

@ -0,0 +1,27 @@
// file main_ISR.cpp
#include <iostream>
#include <signal.h>
#include <unistd.h>
volatile int j;
//extern "C" {
void ISR(int iarg){
std::cout << "ISR j=" << j << "\n";
}
//}
void install_ISR(void){
signal(SIGUSR1, ISR);
}
int main(int argc, const char * argv[]) {
std::cout << "process ID = " << getpid() << "\n";
install_ISR();
for(int i=0; i<300*1000*1000; i++){
i= i +10; i-=10; j=i;
}
std::cout << "done.\n";
return 0;
}

9
03_FLOW_b/run.sh Normal file
View File

@ -0,0 +1,9 @@
#!/bin/bash
./controlFlowIntr &
echo $!
sleep 1
kill -SIGUSR1 $!
sleep 1
kill -SIGUSR1 $!
kill -SIGUSR1 $!

239
03_FLOW_c/AP2-dc.cpp Normal file
View File

@ -0,0 +1,239 @@
// based on http://www.stroustrup.com/dc.c
// by Bjarne Stroustrup
// The desk calculator
// pp 107-117, sec 6.1, A Desk calculator
// No guarantees offered. Constructive comments to bs@research.att.com
/*
expr_list:
expression PRINT // PRINT is \n
expression PRINT expr_list
expression:
expression + term
expression - term
term
term:
term / power
term * power
power
power:
power # primary // char '^' produces problems with terminal input
primary
primary:
NUMBER
( expression )
while statt for
erweitern um o,i oder HexaDezimal mit &, |, x,
*/
#include <string>
#include <cctype>
#include <iostream>
using namespace std;
int no_of_errors; // note: default initialized to 0
bool doPrintCalculations = true;
unsigned int error(const char* s)
{
no_of_errors++;
cerr << "error: " << s << '\n';
return 1;
}
enum Token_value {
NUMBER, // '0' ... '9'
PLUS='+', MINUS='-', MUL='*', DIV='/',
PRINT='\n', POWER='#', LP='(', RP=')'
};
Token_value global_token = PRINT;
unsigned int number_value;
unsigned int asciToInt(string string_value){
unsigned int result = 0;
for(size_t i=0; i<string_value.length();i++){
result *= 10;
result += string_value[i] - 48;
}
return result;
}
Token_value get_token()
{
char ch;
do { // skip whitespace except '\n'
if(!cin.get(ch)) {error("EOF"); exit(1);} //return curr_tok = END;
} while (ch!='\n' && isspace(ch));
switch (ch) {
//case ';':
case '\n':
return global_token=PRINT;
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '#':
return global_token=Token_value(ch);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':{
// the easy way: cin.putback(ch);
// the easy way: cin >> number_value;
string string_value;
string_value = ch;
while (cin.get(ch) && isdigit(ch))
string_value += ch; // string_value.push_back(ch); // to work around library bug
cin.putback(ch);
number_value = asciToInt(string_value);
return global_token=NUMBER;
}
default:
error("bad token");
return global_token=PRINT;
}
}
unsigned int expression(); // cannot do without (indirect recursion)
unsigned int primary() // handle primaries
{
Token_value current_token = get_token();
switch (current_token) {
case NUMBER:{
unsigned int v = number_value;
get_token(); // proceed global_token to next token (i.e. operator or '\n')
return v;
}
case LP:{
unsigned int e = expression();
if (global_token != RP) return error(") expected");
get_token(); // eat ')' in order to proceed global_token to next token
return e;
}
default:
return error("primary expected");
}
}
unsigned int power() // 2 ^ 3
{
unsigned int left = primary();
unsigned int right = 0;
for (;;)
switch (global_token) {
case POWER:{
right = primary();
if (doPrintCalculations) printf("%u # %u\n", left, right);
unsigned int base = left;
left = 1;
for (int i=0; i<right; i++)
left *= base;
break;
}
default:
return left;
}
}
unsigned int term() // multiply or divide
{
unsigned int left = power();
unsigned int right = 0;
for (;;)
switch (global_token) {
case MUL:{
right = power();
if (doPrintCalculations) printf("%u * %u\n", left, right);
left *= right;
break;
}
case DIV:{
if ((right = power())) {
if (doPrintCalculations) printf("%u / %u\n", left, right);
left /= right;
break;
}
return error("divide by 0");
}
default:
return left;
}
}
/*
unsigned int ops_level_a() // e.g. add or subtract
{
unsigned int left = ops_level_b(); // e.g. multiply or divide (which in turn calls ops_level_c() immediately)
unsigned int right = 0;
for (;;)
switch (global_token) {
case TOKEN_LEVELA_1:{
right = ops_level_b(); // e.g. multiply or divide
left = left TOKEN_LEVELA_1 right;
break;
}
case TOKEN_LEVELA_2:{
right = ops_level_b(); // e.g. multiply or divide
left = left TOKEN_LEVELA_1 right;
break;
}
default: // an operator of another (lower) level, or \n
return left; // there is no further operator (and operands) of this level
}
}
*/
unsigned int expression() // add or subtract
{
unsigned int left = term();
unsigned int right = 0;
for (;;) // ``forever''
switch (global_token) {
case PLUS:{
right = term();
if (doPrintCalculations) printf("%u + %u\n", left, right);
left += right;
break;
}
case MINUS:{
right = term();
if (doPrintCalculations) printf("%u - %u\n", left, right);
left -= right;
break;
}
default:
return left;
}
}
int main()
{
while (cin){
//cout << " > ";
cout << expression() << '\n';
}
return no_of_errors;
}

2
03_FLOW_c/build.sh Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
clang++ -std=c++14 -o dc.out AP2-dc.cpp

View File

@ -0,0 +1,28 @@
// file: RationalNumber.cpp
#include "RationalNumber.hpp"
RationalNumber addRationalNumbers(RationalNumber left, RationalNumber right){
RationalNumber result;
// add left and right
return result;
}
RationalNumber operator+ (RationalNumber left, RationalNumber right){
return addRationalNumbers(left, right);
}
// for println()
std::string as_string(RationalNumber r){
std::string result = "(";
result += std::to_string(r.zaehler);
result += "/";
result += std::to_string(r.nenner);
result += ")";
return result;
}
/*std::ostream& operator<< (std::ostream& os, RationalNumber &toBePrinted){
os << "(" << toBePrinted.zaehler << "/"
<< toBePrinted.nenner << ")";
return os;
}*/

View File

@ -0,0 +1,12 @@
// file: RationalNumber.hpp
#include <iostream>
struct RationalNumber{
int zaehler;
int nenner;
};
RationalNumber addRationalNumbers(RationalNumber left, RationalNumber right);
RationalNumber operator+ (RationalNumber left, RationalNumber right);
std::string as_string(RationalNumber); // for println()
//std::ostream& operator<< (std::ostream& os, RationalNumber &toBePrinted); // for cout

View File

@ -0,0 +1,40 @@
// file: main_04_UDEF_a.cpp
#include "../helpers/println.hpp"
#include "RationalNumber.hpp"
#include <iostream>
// location 1
// this declares an alias for type <see below>, which is called calctype
//typedef unsigned int calctype;
//typedef int calctype;
//typedef double calctype;
typedef RationalNumber calctype;
// location 2
void doCalculation(calctype a, calctype b){
calctype c = a + b;
// here is some advanced computation on arguments a and b
// for(){ ...
// if(){ ...
// for(){ ...
println("a = ", a);
println("b = ", b);
println("c = ", c);
}
int main(int argc, char** argv, char** envp){
calctype a;
calctype b;
doCalculation(a,b);
return 0;
}

View File

@ -0,0 +1,52 @@
// file main_04_UDEF_e.cpp
#include "../helpers/println.hpp"
#include <iostream>
const int bitsPerOctet=8;
struct BinaryOctet {
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise
char bitsAsDigits[bitsPerOctet]; // bit values as chars
BinaryOctet(int x=0);
BinaryOctet(const BinaryOctet&) = default;
};
BinaryOctet::BinaryOctet(int x){
// TODO: implement
}
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b){
BinaryOctet result;
for(; a != b; b--){
a = a + 1;
a = a / b;
}
result = a + b;
return result;
}
// for println();
std::string as_string(BinaryOctet a){
std::string result = "(";
// TODO: implement
result += ")";
return result;
}
// for std::cout
/*std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){
os << "(" << ")";
return os;
}*/
int main(int argc, char **argv)
{
BinaryOctet a = 0b00001111;
BinaryOctet b = 0b00000110;
println("result = ", doCalculation(a,b));
//std::cout << "result = " << doCalculation(a,b) << std::endl;
return 0;
}

29
05_OO/AnsiConsoleDemo.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <cmath>
#include "AnsiConsole.h"
/*
see
https://en.wikipedia.org/wiki/ANSI_escape_code
https://gist.github.com/vratiu/9780109
https://gist.github.com/RobinMalfait/7881398
*/
int main(int argc, char **argv)
{
ansiConsole.clearScreen();
// x=1 and y=1 is the upper left corner
// x and y are more like column and row
ansiConsole.printText(4,4,"Hello, World!");
// draw a pixel, sort of
ansiConsole.printText(3,3,"*");
// draw a line
for(int i=0;i<10;i++){
ansiConsole.printText(2+i,2,"-", Colors::GREEN);
}
ansiConsole.printText(5,15,"end.");
return 0;
}

3
05_OO/build.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
clang++ -std=c++14 -o shape_main.out -I ../helpers/ shapes_main.cpp ../helpers/AnsiConsole.cpp
clang++ -std=c++14 -o ansiConsole.out -I ../helpers/ AnsiConsoleDemo.cpp ../helpers/AnsiConsole.cpp

View File

@ -0,0 +1,105 @@
#include <iostream>
#include <typeinfo>
//======================================
class Vehicle {
protected:
int _numSeats;
public:
Vehicle(int numSeats=0);// may serve as default ctor (i.e. no arguments)
virtual ~Vehicle();
virtual int payload() = 0;
int numSeats(); // a 'getter' method to get a value; no 'setter' here
};
//======================================
class Car : public Vehicle {
protected:
int _maxWeight; // german: zulässiges Gesamtgewicht
public:
Car(int numSeats, int maxWeight);
virtual int payload();
};
//======================================
class Truck : public Vehicle {
protected:
int _payload;
public:
Truck(int numSeats, int payload);
virtual int payload();
};
//======================================
Vehicle::Vehicle(int numSeats){
_numSeats = numSeats;
}
Vehicle::~Vehicle(){
std::cout << "destroying a Vehicle" << std::endl;
}
int Vehicle::numSeats(){
return _numSeats;
}
//======================================
Car::Car(int numSeats, int maxWeight){
_numSeats = numSeats;
_maxWeight = maxWeight;
}
int Car::payload(){
return _maxWeight - (_numSeats*75);
}
//======================================
Truck::Truck(int numSeats, int payload){
_numSeats = numSeats;
_payload = payload;
}
int Truck::payload(){
return _payload;
}
//======================================
void printVehicleInfo(Vehicle* v){
std::cout << "typeid=`" << typeid(*v).name() << "`"
<< " numSeats=" << v->numSeats()
<< " payload=" << v->payload() << std::endl;
}
int main(int argc, const char * argv[]) {
Car* c = new Car(5, 1000); // create a new object of class Car in free store
Truck* t = new Truck(3, 7500);
std::cout << "1" << std::endl;
std::cout << "c: numSeats=" << c->numSeats() << " payload=" << c->payload() << std::endl;
std::cout << "t: numSeats=" << t->numSeats() << " payload=" << t->payload() << std::endl;
std::cout << std::endl << "2" << std::endl;
Vehicle* v = c; // a Car `is a` Vehicle => implicitly convertible
printVehicleInfo(v);
v = t; // a Truck `is a` Vehicle => implicitly convertible
printVehicleInfo(v);
// release memory occupied by t,c for use by future objects created by `new`
// do NOT release v. it is only an alias
delete t;
delete c;
return 0;
}

198
05_OO/main_mp4_OO_b.cpp Normal file
View File

@ -0,0 +1,198 @@
// file main_mp4_OO_b.cpp
#include <iostream>
#include <cassert>
//=======================================
struct FooVal {
FooVal(int initialValue=0);
~FooVal();
private:
int _someValue;
};
//=======================================
class FooBase {
FooVal _value;
public:
FooBase();
FooBase(int initialValue);
virtual ~FooBase();
};
//=======================================
class FooDerived : public FooBase {
int _ival;
public:
FooDerived(int initialValue);
~FooDerived();
};
//=======================================
class Bar {
FooBase *_helperObject;
public:
Bar();
~Bar();
};
//=======================================
FooVal::FooVal(int initialValue)
: _someValue(initialValue)
{
std::cout << "FooVal::FooVal()" << std::endl;
}
FooVal::~FooVal(){
std::cout << "FooVal::~FooVal()" << std::endl;
}
FooBase::FooBase(){
std::cout << "FooBase::FooBase()" << std::endl;
}
FooBase::FooBase(int initialValue)
: _value(initialValue)
{
std::cout << "FooBase::FooBase(int)" << std::endl;
}
FooBase::~FooBase(){
std::cout << "FooBase::~FooBase(" << std::endl;
}
FooDerived::FooDerived(int initialValue)
: FooBase(initialValue), _ival(0)
{
std::cout << "FooDerived::FooDerived()" << std::endl;
}
FooDerived::~FooDerived(){
std::cout << "FooDerived::~FooDerived()" << std::endl;
}
Bar::Bar(){
_helperObject = new FooDerived(0);
}
Bar::~Bar(){
delete _helperObject;
}
struct StackObject {
private:
void* operator new(size_t size) noexcept {
bool noStackObjectOnHeap = false;
assert(noStackObjectOnHeap);
return nullptr;
}
};
struct A : public StackObject {
A(){std::cout << "+A ";}
//A(const A&){std::cout << "+A";}
~A(){std::cout << "-A ";}
};
struct B : public StackObject {
B(){std::cout << "+B ";}
//B(const B&){std::cout << "+B";}
~B(){std::cout << "-B ";}
};
struct C : public StackObject {
C(){std::cout << "+C ";}
//C(const C&){std::cout << "+C";}
~C(){std::cout << "-C ";}
};
class HeapObject{
public:
void* operator new (size_t size);
HeapObject();
virtual ~HeapObject();
static bool assertionsHold();
protected:
private:
static int ctorCount;
static int dtorCount;
static int newCount;
// static void remove(HeapObject *);
HeapObject(const HeapObject&) = delete;
HeapObject& operator=(const HeapObject&) = delete;
};
int HeapObject::ctorCount = 0;
int HeapObject::dtorCount = 0;
int HeapObject::newCount = 0;
void* HeapObject::operator new (size_t size){
newCount++;
return new char[size];
}
HeapObject::HeapObject(){
ctorCount++;
}
HeapObject::~HeapObject(){
dtorCount++;
}
bool HeapObject::assertionsHold(){
assert(ctorCount == newCount); // all objects have been allocated on heap
assert(ctorCount == dtorCount); // all objects have been deleted
return true;
}
class K : public HeapObject
{
public:
K(){std::cout << "+K ";}
~K(){std::cout << "-K ";}
};
class L {
public:
L(){std::cout << "+L ";}
~L(){std::cout << "-L ";}
};
class M {
public:
M(){std::cout << "+M ";}
~M(){std::cout << "-M ";}
};
int main(int argc, const char * argv[]) {
Bar * bar = new Bar();
// ...
delete bar; // implies "delete _helperObject";
FooBase *obj = new FooDerived(7);
delete obj;
{
C c;
K* k = new K();
delete k;
}
HeapObject::assertionsHold();
std::cout << " ENDE" << std::endl;
return 0;
}

87
05_OO/shapes_main.cpp Normal file
View File

@ -0,0 +1,87 @@
#include <iostream>
#include <cmath>
#include "AnsiConsole.h"
struct Position{
int x;
int y;
Position(int x_=0, int y_=0){ x=x_;y=y_;}
};
class Point{
protected:
Position _position;
public:
Point(int x=0, int y=0);
void draw();
};
Point::Point(int x, int y){
_position = Position(x,y);
}
void Point::draw(){
ansiConsole.printText(_position.x,_position.y,"*", Colors::RED);
}
class Circle{
protected:
Position _position;
int _radius;
public:
Circle(int x=0, int y=0, int radius=0);
void draw();
};
Circle::Circle(int x, int y, int radius){
_position = Position(x,y);
_radius=radius;
}
void Circle::draw(){
/* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie
* Höhensatz des Euklid
* */
int x_start = _position.x - _radius/2;
int x_stop = _position.x + _radius/2;
for(int i=x_start; i<=x_stop; i++){
double x_relative = double(i) - double(x_start);
double h = sqrt(x_relative*(x_stop-x_start-x_relative));
ansiConsole.printText(_position.x + int(x_relative)- _radius/2,
_position.y +h,"#",
Colors::GREEN);
ansiConsole.printText(_position.x + int(x_relative)- _radius/2,
_position.y -h,"#",
Colors::GREEN);
}
}
int main(int argc, char **argv)
{
// x=1 and y=1 is the upper left corner
// x and y are more like column and row
ansiConsole.printText(5,5,"Hello, World!");
Point* p = new Point(10,10);
p->draw();
Point* p2 = new Point(2,10);
p2->draw();
Circle* c = new Circle(30, 15, 10);
c->draw();
Point* p3= new Point(30,15);
p3->draw();
delete p;
delete p2;
delete p3;
delete c;
return 0;
}

281
06_POLY/main_mp5_POLY.cpp Normal file
View File

@ -0,0 +1,281 @@
// file main_mp5_POLY.cpp
#include <iostream>
#include <vector>
class Base {
public:
void overriddenMethod();
void overriddenMethod(int);
void overriddenMethod(std::string);
void nonVirtualMethod(void);
virtual void virtualMethod(void);
/*
virtual void virtualAndOverridden(void);
virtual void virtualAndOverridden(int);
virtual void virtualAndOverridden(std::string);*/
};
class Derived_1 : public Base {
public:
Derived_1();
~Derived_1();
void nonVirtualMethod(void);
virtual void virtualMethod(void);
/* virtual void virtualAndOverridden(void);
virtual void virtualAndOverridden(int);
virtual void virtualAndOverridden(std::string);*/
};
class Derived_2 : public Derived_1 {
public:
void nonVirtualMethod(void);
virtual void virtualMethod(void);
};
// see https://isocpp.org/wiki/faq/strange-inheritance#calling-virtuals-from-ctors
Derived_1::Derived_1(){
virtualMethod(); // 6a
}
Derived_1::~Derived_1(){
virtualMethod(); // 6b
}
// -------------------
void Base::overriddenMethod(void){
std::cout << "Base::overriddenMethod(void)" << std::endl;
}
void Base::overriddenMethod(int){
std::cout << "Base::overriddenMethod(int)" << std::endl;
}
void Base::overriddenMethod(std::string){
std::cout << "Base::overriddenMethod(std::string)" << std::endl;
}
void Base::nonVirtualMethod(void){
std::cout << "Base::nonVirtualMethod(void)" << std::endl;
}
void Base::virtualMethod(void){
std::cout << "Base::virtualMethod" << std::endl;
}
/*
void Base::virtualAndOverridden(void){
std::cout << "Base::virtualAndOverridden(void)" << std::endl;
}
void Base::virtualAndOverridden(int){
std::cout << "Base::virtualAndOverridden(int)" << std::endl;
}
void Base::virtualAndOverridden(std::string){
std::cout << "Base::virtualAndOverridden(std::string)" << std::endl;
}
*/
// -------------------
void Derived_1::nonVirtualMethod(void){
std::cout << "Derived_1::nonVirtualMethod" << std::endl;
}
void Derived_1::virtualMethod(void){
std::cout << "Derived_1::virtualMethod" << std::endl;
}
/*
void Derived_1::virtualAndOverridden(void){
std::cout << "Derived_1::virtualAndOverridden(void)" << std::endl;
}
void Derived_1::virtualAndOverridden(int){
std::cout << "Derived_1::virtualAndOverridden(int)" << std::endl;
}
void Derived_1::virtualAndOverridden(std::string){
std::cout << "Derived_1::virtualAndOverridden(std::string)" << std::endl;
}
*/
// -------------------
void Derived_2::nonVirtualMethod(void){
std::cout << "Derived_2::nonVirtualMethod" << std::endl;
}
void Derived_2::virtualMethod(void){
std::cout << "Derived_2::virtualMethod" << std::endl;
}
/*
illustrates binding of method invocations
compile time vs. run time binding
*/
void foobar(void){
std::cout << "6a) ";
Derived_2* pR = new Derived_2();
Base* pBase = pR;
Derived_1* pDerived_1 = pR;
Derived_2* pDerived_2 = pR;
std::cout << "1) ";
pBase->virtualMethod(); // 1)
std::cout << "2) ";
pBase->nonVirtualMethod(); // 2)
std::cout << "3) ";
pDerived_2->virtualMethod(); // 3)
std::cout << "4) ";
pDerived_2->nonVirtualMethod(); // 4)
std::cout << "5) ";
static_cast<Base*>(pDerived_2)->nonVirtualMethod(); // 5)
std::cout << "7) ";
static_cast<Derived_1*>(pDerived_2)->nonVirtualMethod(); // 7)
std::cout << "8) ";
pDerived_1->nonVirtualMethod(); // 8)
std::cout << "9) ";
pDerived_1->virtualMethod(); // 9)
std::cout << "10a) ";
pBase->Base::virtualMethod(); // 10a)
std::cout << "10b) ";
pDerived_2->Derived_1::virtualMethod(); // 10b)
std::cout << "11 ";
pDerived_2->overriddenMethod(17); // 11)
std::cout << "12 ";
pDerived_2->overriddenMethod(); // 12)
std::cout << "13 ";
pDerived_2->overriddenMethod(std::string("x")); // 13)
std::cout << "14 ";
dynamic_cast<Derived_2*>(pBase)->virtualMethod(); // 14)
std::cout << "15 ";
Base* baseObject = new Base();
Derived_2* d2Object = dynamic_cast<Derived_2*>(baseObject);
if(d2Object){ // 15)
std::cout << "+" << std::endl;
}else{
std::cout << "-" << std::endl;
}
/*
std::cout << "16 ";
pDerived_2->virtualAndOverridden(17); // 16)
std::cout << "17 ";
pDerived_2->virtualAndOverridden(); // 17)
std::cout << "18 ";
pDerived_2->virtualAndOverridden(std::string("x")); // 18)
*/
std::cout << "6b) ";
delete pR;
}
/*
void foo(){
for(int i=0; i<LOOPS;i++){
Base* ps = new Base();
ps->nonVirtualMethod2();
delete ps;
}
}
void bar(int i){
Base s;
s.nonVirtualMethod2();
if(i>0){
bar(i-1);
}
}*/
typedef void (*funcPointer_void_void)(void);
void sing_gingle(void){
std::cout << "gingle ";
}
void sing_bells(void){
std::cout << "bells ";
}
void sing_surf(void){
std::cout << "surf ";
}
void sing_safari(void){
std::cout << "surfin safari ";
}
std::vector<funcPointer_void_void> songOfSeason(int currentTemperatureCelsius) {
std::vector<funcPointer_void_void> result;
if(currentTemperatureCelsius <= 0){
result.push_back(sing_gingle);
result.push_back(sing_bells);
result.push_back(sing_gingle);
result.push_back(sing_bells);
}else{
result.push_back(sing_surf);
result.push_back(sing_surf);
result.push_back(sing_safari);
result.push_back(sing_surf);
result.push_back(sing_surf);
result.push_back(sing_safari);
}
return result;
}
void sing(int currentTemperatureCelsius){
std::vector<funcPointer_void_void> song = songOfSeason(currentTemperatureCelsius);
// traditional C-style for-loop
for(int i=0; i<song.size(); ++i){
song[i](); // invoke funtion (pointed to by function pointer at song[i])
}
std::cout << std::endl;
// C++11 range-based for-loop looks better:
for(auto func: song){
func();
}
std::cout << std::endl;
}
int main(int argc, const char * argv[]) {
foobar();
sing(-17);
/*
std::cout << "foo " << std::endl;
foo();
std::cout << "bar " << std::endl;
bar(LOOPS);
*/
return 0;
}

View File

@ -0,0 +1,72 @@
// genericStack.hpp
// 6_STD_stacks
#ifndef genericStack_hpp
#define genericStack_hpp
#include <iostream>
// good: encapsulation
// good: separation of interface and implementation
// good: arbitrary element type
// good: arbitrary capacity
// bad: implementation in .hpp
// bad: compiler error messages are difficult to read
template <typename ElementType, int capacity>
struct genericStack {
int tos; // top of stack, i.e. index of next write/push
ElementType elements[capacity];
public:
genericStack();
void push(ElementType element);
ElementType pop();
int size(); // number of elements pushed
bool isEmpty();
void clear();
void print();
};
// ============= implementation =============
template <typename ElementType, int capacity>
genericStack<ElementType, capacity>::genericStack()
: tos(0)
{
}
template <typename ElementType, int capacity>
void genericStack<ElementType, capacity>::push(ElementType element){
elements[tos++] = element;
}
template <typename ElementType, int capacity>
ElementType genericStack<ElementType, capacity>::pop(){
return elements[--tos];
}
template <typename ElementType, int capacity>
int genericStack<ElementType, capacity>::size(){ // number of elements pushed
return tos;
}
template <typename ElementType, int capacity>
bool genericStack<ElementType, capacity>::isEmpty(){
return tos == 0;
}
template <typename ElementType, int capacity>
void genericStack<ElementType, capacity>::clear(){
tos = 0;
}
template <typename ElementType, int capacity>
void genericStack<ElementType, capacity>::print(){
std::cout << size() << " of " << capacity << " allocated." << std::endl;
for (int i=0; i<tos; i++){
std::cout << i << ": " << elements[i] << std::endl;
}
}
#endif

View File

@ -0,0 +1,40 @@
// intStack.cpp
// 6_STD_stacks
#include "intStack.hpp"
#include <iostream>
const int intStack::capacity;
intStack::intStack()
: tos(0)
{
}
void intStack::push(int element){
elements[tos++] = element;
}
int intStack::pop(){
return elements[--tos];
}
int intStack::size(){ // number of elements pushed
return tos;
}
bool intStack::isEmpty(){
return tos == 0;
}
void intStack::clear(){
tos = 0;
}
void intStack::print(){
std::cout << size() << " of " << capacity << " allocated." << std::endl;
for (int i=0; i<tos; i++){
std::cout << i << ": " << elements[i] << std::endl;
}
}

View File

@ -0,0 +1,26 @@
// intStack.hpp
// 6_STD_stacks
#ifndef intStack_hpp
#define intStack_hpp
// good: encapsulation
// good: separation of interface and implementation
// bad: fixed element type
// bad: fixed capacity
struct intStack {
static const int capacity=1024;
int tos; // top of stack, i.e. index of next write/push
int elements[capacity];
public:
intStack();
void push(int element);
int pop();
int size(); // number of elements pushed
bool isEmpty();
void clear();
void print();
};
#endif

View File

@ -0,0 +1,97 @@
// main.cpp
// 6_STD_stacks
#include <iostream>
#include "intStack.hpp"
#include "genericStack.hpp"
#include <vector>
#include <list>
void use_intStack(){
intStack s_1;
for(int i=0; i<10;i++){
s_1.push(i*i);
}
std::cout << "s_1 : " << std::endl;
s_1.print();
// replace top element by 4711:
s_1.pop();
s_1.push(4711);
// swap topmost two elements:
int tmp1 = s_1.pop();
int tmp2 = s_1.pop();
s_1.push(tmp1);
s_1.push(tmp2);
std::cout << "draining s_1 : " << std::endl;
while(!s_1.isEmpty()){
std::cout << s_1.pop() << std::endl;
}
}
void use_genericStack(){
genericStack<int, 20> s_2;
genericStack<double, 30> s_3;
for(int i=0; i<10;i++){
s_2.push(i*i);
s_3.push(i*i * 1.1);
}
s_2.print();
s_3.print();
}
void use_std_containers(){
std::vector<int> v;
v.push_back(17); // append an element at the end
int i = v.back(); // get last element
int j = v[0]; // access by subscript operator
if ((i==j) && (i==17)){ //
v.pop_back(); // remove last element
v.clear(); // remove all elements
}
long s = v.size(); // get size; size() <= max_size()
std::list<long> l;
l.push_back(17); // append an element at the end
l.push_front(s); // put an element in front of the list
}
void printVector(const std::vector<int>& v){
for(int j=0; j<v.size(); j++){
std::cout << "[" << j << "]=" << v[j] << ", ";
}
std::cout << std::endl;
}
void use_std_algorithms(){
std::vector<int> v(100);
for(int i=0; i<10; i++){
v[i*i] = i;
}
v.push_back(17);
printVector(v);
std::sort(v.begin(), v.end());
printVector(v);
std::transform(v.begin(), v.end(), v.begin(),
[](int i) { return i*10; }); // use lambda expression here
printVector(v);
}
int main(int argc, const char * argv[]) {
std::cout << "use_intStack()" << std::endl;
std::cout << "--------------" << std::endl;
use_intStack();
std::cout << "use_genericStack()" << std::endl;
std::cout << "------------------" << std::endl;
use_genericStack();
std::cout << "use_std_containers()" << std::endl;
std::cout << "------------------" << std::endl;
use_std_containers();
std::cout << "use_std_algorithms()" << std::endl;
std::cout << "------------------" << std::endl;
use_std_algorithms();
return 0;
}

17
07_STD/grundgeruest.cpp Normal file
View File

@ -0,0 +1,17 @@
// based on example from http://en.cppreference.com/w/cpp/numeric/random/rand
#include <cstdlib>
#include <iostream>
#include <ctime>
struct BinaryOctet {
bool evenParity;
char bitsAsDigits[bitsPerOctet];
};
int main()
{
std::srand(std::time(0)); // use current time as seed for random generator
int random_variable = std::rand();
std::cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}

19
08_PTRN/Car.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Car.hpp"
Car::Car(std::string model, int maxWeight)
: Vehicle(model)
{
_maxWeight = maxWeight;
}
int Car::maxWeight(){
return _maxWeight;
}
int Car::payload_kg(){
return _maxWeight - 75 * 5; // subtract weight of passangers
}
Vehicle* Car::clone(){
return new Car(model(), _maxWeight);
}

18
08_PTRN/Car.hpp Normal file
View File

@ -0,0 +1,18 @@
#ifndef Car_hpp
#define Car_hpp
#include "Vehicle.hpp"
class Car : public Vehicle {
int _maxWeight;
public:
Car(std::string model, int maxWeight);
int maxWeight();
virtual int payload_kg();
virtual Vehicle* clone();
//virtual Vehicle* newInstance();
};
#endif

15
08_PTRN/Logger.cpp Normal file
View File

@ -0,0 +1,15 @@
// file Logger.cpp
#include "Logger.hpp"
Logger* Logger::_theInstance = nullptr;
Logger* Logger::getInstance(){
if (!_theInstance){
_theInstance = new StdoutLogger();
}
return _theInstance;
}
void StdoutLogger::log(std::string message, std::string file, int line){
std::cerr << message << " in " << file << " line " << line << std::endl;
}

19
08_PTRN/Logger.hpp Normal file
View File

@ -0,0 +1,19 @@
// file Logger.hpp
#ifndef Logger_hpp
#define Logger_hpp
#include <iostream>
class Logger {
static Logger* _theInstance;
public:
virtual void log(std::string message, std::string file="", int line=0) = 0;
static Logger* getInstance();
};
class StdoutLogger: public Logger {
public:
void log(std::string message, std::string file="", int line=0);
};
#endif

16
08_PTRN/Truck.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "Truck.hpp"
Truck::Truck(std::string model, int payload_kg)
: Vehicle(model)
{
_payload_kg = payload_kg;
}
int Truck::payload_kg(){
return _payload_kg;
}
Vehicle* Truck::clone(){
return new Truck(model(),_payload_kg);
}

17
08_PTRN/Truck.hpp Normal file
View File

@ -0,0 +1,17 @@
#ifndef Truck_hpp
#define Truck_hpp
#include "Vehicle.hpp"
class Truck : public Vehicle {
int _payload_kg; // max number kilograms to load
public:
Truck(std::string model, int payload_kg);
virtual int payload_kg();
virtual Vehicle* clone();
//virtual Vehicle* newInstance();
};
#endif

15
08_PTRN/Vehicle.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "Vehicle.hpp"
Vehicle::Vehicle(std::string model){
this->_model = model;
}
Vehicle::~Vehicle(){
}
std::string Vehicle::model(){
return _model;
}

18
08_PTRN/Vehicle.hpp Normal file
View File

@ -0,0 +1,18 @@
#ifndef Vehicle_hpp
#define Vehicle_hpp
#include <iostream>
class Vehicle {
std::string _model;
public:
Vehicle(std::string model);
virtual ~Vehicle();
std::string model();
virtual int payload_kg() = 0;
virtual Vehicle* clone() {return nullptr;} // idiom "virtual constructor"
//virtual Vehicle* newInstance() = 0; // idiom "virtual constructor"
};
#endif

View File

@ -0,0 +1,19 @@
// file VehicleFactory.cpp
#include "VehicleFactory.hpp"
#include "Car.hpp"
#include "Truck.hpp"
/*
A factory creates objects based un runtime information
a) within the process (data values, Types i.e.e virtual methods)
b) input from files or user input
Th is simple Factory differentiates only based on a value.
*/
Vehicle* vehicleForPayload(std::string model, int weight){
if (weight < 3500){
return new Car(model, weight);
}else{
return new Truck(model, weight);
}
}

View File

@ -0,0 +1,10 @@
// file VehicleFactory.hpp
#ifndef VehicleFactory_hpp
#define VehicleFactory_hpp
#include "Vehicle.hpp"
Vehicle* vehicleForPayload(std::string model, int weight);
#endif

71
08_PTRN/main.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "Car.hpp"
#include "Truck.hpp"
#include "Logger.hpp"
#include "VehicleFactory.hpp"
#include <iostream>
#include <vector>
void workOnCopy_naive(std::vector<Vehicle*> vehicles){
std::vector<Vehicle*> tempVec;
for (int i=0; i<vehicles.size(); i++){
Vehicle * currentVehicle = vehicles[i];
Car* carToCopy = dynamic_cast<Car*>(currentVehicle); // RTTI type check
Truck* truckToCopy = dynamic_cast<Truck*>(currentVehicle);// may be nullptr
if(carToCopy){
tempVec.push_back(new Car(carToCopy->model(), carToCopy->maxWeight())); // type dependency to Car
}else if(truckToCopy){
tempVec.push_back(new Truck(truckToCopy->model(), truckToCopy->payload_kg())); // type dependecy
}else{
// TODO: what do we do here?
// Did someone add a new class to the code base?
return; // BUG: copies aren't free'd
}
}
// work on copies ...
// ...
for(auto vehi : tempVec){
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
}
for (int i=0; i<tempVec.size(); i++){
delete tempVec[i];
}
tempVec.clear();
}
void workOnCopy_smart(std::vector<Vehicle*> vehicles){ // uses RAII, virtual ctor
class RAIIvector { // local RAII helper class
public:
std::vector<Vehicle*> tempVec;
~RAIIvector(){
for(auto vehi : tempVec){
delete vehi;
}
tempVec.clear();
}
};
RAIIvector rv;
for(auto vehi : vehicles){
rv.tempVec.push_back(vehi->clone());
}
// work on copies ...
// ...
for(auto vehi : rv.tempVec){
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
}
// compiler WILL invoke RAIIvector::~RAIIvector() here
}
int main(int argc, const char * argv[]) {
std::vector<Vehicle*> vehicles;
vehicles.push_back(vehicleForPayload("Volkswagen Golf", 900)); // use factory for construction
vehicles.push_back(vehicleForPayload("Magirus Deutz", 18*1000));
Logger::getInstance()->log("work naively", __FILE__, __LINE__);
workOnCopy_naive(vehicles);
Logger::getInstance()->log("work smartly", __FILE__, __LINE__);
workOnCopy_smart(vehicles);
return 0;
}

View File

@ -0,0 +1,121 @@
#include <iostream>
#include <cassert>
#include <vector>
#include "println.hpp"
struct StackObject {
private:
void* operator new(size_t size) noexcept {
bool noStackObjectOnHeap = false;
assert(noStackObjectOnHeap);
return nullptr;
}
};
class HeapObject{
public:
void* operator new (size_t size);
HeapObject();
virtual ~HeapObject();
static bool assertionsHold();
protected:
private:
static int ctorCount;
static int dtorCount;
static int newCount;
// static void remove(HeapObject *);
HeapObject(const HeapObject&) = delete;
HeapObject& operator=(const HeapObject&) = delete;
};
int HeapObject::ctorCount = 0;
int HeapObject::dtorCount = 0;
int HeapObject::newCount = 0;
void* HeapObject::operator new (size_t size){
newCount++;
return new char[size];
}
HeapObject::HeapObject(){
ctorCount++;
}
HeapObject::~HeapObject(){
dtorCount++;
}
bool HeapObject::assertionsHold(){
assert(ctorCount == newCount); // all objects have been allocated on heap
assert(ctorCount == dtorCount); // all objects have been deleted
return true;
}
typedef double MoneyUnitType; // should be fixed point class
class Cash : public HeapObject {
public:
MoneyUnitType _value;
Cash(MoneyUnitType value) : _value(value){}
};
typedef std::vector<Cash*> Wallet;
Wallet consumerWallet_A;
Wallet consumerWallet_B;
Cash * createCashFor(int i){
return new Cash(static_cast<MoneyUnitType>(i));
}
void flushWallet(Wallet& toBeEmptied){ // should be in dtor of sorrounding class
for(int i=0; i<toBeEmptied.size();i++){
delete toBeEmptied[i];
}
toBeEmptied.clear();
}
void setup(){
println("setup()...");
// fill A's wallet
for(int i=1;i<=10;i++){
consumerWallet_A.push_back(createCashFor(i));
}
println("setup() - done");
}
void simulate(){
println("simulate()...");
// move A's last Cash object to B's wallet
Cash * toBeMoved = consumerWallet_A.back();
consumerWallet_B.push_back(toBeMoved);
consumerWallet_A.pop_back();
// delete A's last Cash object
delete consumerWallet_A.back();
consumerWallet_A.pop_back();
println("simulate() - done");
}
void teardown(){
println("cleaning up...");
flushWallet(consumerWallet_A);
flushWallet(consumerWallet_B);
println("cleaning up - done");
}
int main(int argc, const char * argv[]) {
setup();
simulate();
teardown();
HeapObject::assertionsHold();
std::cout << " ---- errors above? ----" << std::endl;
return 0;
}

3
10_PITF/build.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
clang++ -std=c++14 -I../helpers -o banking_rawptr.out banking_base_rawptr.cpp
#clang++ -std=c++14 -I../helpers -o banking_smartptr.out banking_base_smartptr.cpp

View File

@ -0,0 +1,54 @@
// LifeCycleProbe.cpp
// DoubleDelete
#include "LifeCycleProbe.hpp"
#include <iostream>
LifeCycleProbe::LifeCycleProbe(){
this->id=0;
std::cout << "default ctor id=" << id << std::endl;
#ifdef HAS_RESOURCE_POINTER
resource = new int;
#endif
}
LifeCycleProbe::LifeCycleProbe(int id){
this->id=id;
std::cout << "ctor(int) id=" << id << std::endl;
#ifdef HAS_RESOURCE_POINTER
resource = new int;
#endif
}
LifeCycleProbe::LifeCycleProbe(const LifeCycleProbe& other){
std::cout << "copy ctor (source id=" << other.id
<< " to id=" << other.id + 9 << ")" << std::endl;
this->id=other.id + 9;
#ifdef HAS_RESOURCE_POINTER
this->resource = other.resource;
#endif
}
LifeCycleProbe& LifeCycleProbe::operator=(const LifeCycleProbe& other){
std::cout << "assignment (old id=" << this->id
<< " source id=" << other.id
<< " new id=" << other.id + 11 << ")" << std::endl;
this->id = other.id + 11;
return *this;
}
LifeCycleProbe::~LifeCycleProbe(){
std::cout << "dtor id=" << id << std::endl;
#ifdef HAS_RESOURCE_POINTER
std::cout << "deleting resource" << *resource << std::endl;
delete resource;
#endif
}
void LifeCycleProbe::printID(){
std::cout << "printID() id=" << id << std::endl;
}

View File

@ -0,0 +1,23 @@
// LifeCycleProbe.hpp
// DoubleDelete
#ifndef LifeCycleProbe_hpp
#define LifeCycleProbe_hpp
//#define HAS_RESOURCE_POINTER
class LifeCycleProbe {
int id;
#ifdef HAS_RESOURCE_POINTER
int *resource;
#endif
public:
LifeCycleProbe();
LifeCycleProbe(int id);
LifeCycleProbe(const LifeCycleProbe&);
LifeCycleProbe& operator=(const LifeCycleProbe&);
~LifeCycleProbe();
void printID();
};
#endif

View File

@ -0,0 +1,38 @@
// main.cpp
// DoubleDelete
#include "LifeCycleProbe.hpp"
#include <iostream>
void callByPointer(LifeCycleProbe* p){
p->printID();
}
void callByReference(LifeCycleProbe& r){
r.printID();
}
void callByValue(LifeCycleProbe v){
v.printID();
}
int main(int argc, const char * argv[]) {
LifeCycleProbe probe(1000);
std::cout << "three invocations ===========" << std::endl;
callByPointer(&probe);
callByReference(probe);
callByValue(probe);
{
std::cout << "assigment ===========" << std::endl;
LifeCycleProbe blockProbe(2000);
blockProbe = probe;
blockProbe.printID();
}
std::cout << "The End. ===========" << std::endl;
return 0;
}

View File

@ -0,0 +1,67 @@
// main.cpp
// objectSlicing
#include <iostream>
#include <vector>
class Base{
int _a;
public:
Base(){}
virtual ~Base() {}
Base(int aa) :_a(aa) {}
int value_a();
virtual int value_virtual();
};
int Base::value_a(){
return _a;
}
int Base::value_virtual(){
return _a;
}
class Derived : public Base {
int _b;
public:
Derived(int x, int y) : Base(x), _b(y) {}
virtual int value_virtual();
};
int Derived::value_virtual(){
return _b;
}
void passByPointer(Base* bp){
std::cout << "->value_a() = " << bp->value_a() << std::endl;
std::cout << "->value_virtual() = " << bp->value_virtual() << std::endl;
}
void passByValue(Base b){
std::cout << ".value_a() = " << b.value_a() << std::endl;
std::cout << ".value_virtual() = " << b.value_virtual() << std::endl;
}
int main(int argc, const char * argv[]) {
Derived d(1,2);
std::cout << "d.value_virtual() = " << d.value_virtual() << std::endl;
std::cout << std::endl << "pass pointer to base class " << std::endl;
passByPointer(&d); // pass pointer to base class
std::cout << std::endl << "pass by value (i.e. object of class Base) " << std::endl;
passByValue(d); // pass Base-object by value -> object slicing: d is "transformed" int an object of class Base
std::cout << std::endl << "store Derived objects in std::vector<Base>" << std::endl;
std::vector<Base> v;
v.push_back(d); // object slicing
std::cout << ".value_a() = " << v[0].value_a() << std::endl;
std::cout << ".value_virtual() = " << v[0].value_virtual() << std::endl;
return 0;
}

View File

@ -0,0 +1,245 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
65413E911CEB65E5008BE849 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 65413E901CEB65E5008BE849 /* main.cpp */; };
65413E991CEB6612008BE849 /* OneByOneMatrix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 65413E971CEB6612008BE849 /* OneByOneMatrix.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
65413E8B1CEB65E5008BE849 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
65413E8D1CEB65E5008BE849 /* CopyOnWrite */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CopyOnWrite; sourceTree = BUILT_PRODUCTS_DIR; };
65413E901CEB65E5008BE849 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
65413E971CEB6612008BE849 /* OneByOneMatrix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OneByOneMatrix.cpp; sourceTree = "<group>"; };
65413E981CEB6612008BE849 /* OneByOneMatrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OneByOneMatrix.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
65413E8A1CEB65E5008BE849 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
65413E841CEB65E5008BE849 = {
isa = PBXGroup;
children = (
65413E8F1CEB65E5008BE849 /* CopyOnWrite */,
65413E8E1CEB65E5008BE849 /* Products */,
);
sourceTree = "<group>";
};
65413E8E1CEB65E5008BE849 /* Products */ = {
isa = PBXGroup;
children = (
65413E8D1CEB65E5008BE849 /* CopyOnWrite */,
);
name = Products;
sourceTree = "<group>";
};
65413E8F1CEB65E5008BE849 /* CopyOnWrite */ = {
isa = PBXGroup;
children = (
65413E901CEB65E5008BE849 /* main.cpp */,
65413E971CEB6612008BE849 /* OneByOneMatrix.cpp */,
65413E981CEB6612008BE849 /* OneByOneMatrix.h */,
);
path = CopyOnWrite;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
65413E8C1CEB65E5008BE849 /* CopyOnWrite */ = {
isa = PBXNativeTarget;
buildConfigurationList = 65413E941CEB65E5008BE849 /* Build configuration list for PBXNativeTarget "CopyOnWrite" */;
buildPhases = (
65413E891CEB65E5008BE849 /* Sources */,
65413E8A1CEB65E5008BE849 /* Frameworks */,
65413E8B1CEB65E5008BE849 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = CopyOnWrite;
productName = CopyOnWrite;
productReference = 65413E8D1CEB65E5008BE849 /* CopyOnWrite */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
65413E851CEB65E5008BE849 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0620;
ORGANIZATIONNAME = HSEL;
TargetAttributes = {
65413E8C1CEB65E5008BE849 = {
CreatedOnToolsVersion = 6.2;
};
};
};
buildConfigurationList = 65413E881CEB65E5008BE849 /* Build configuration list for PBXProject "CopyOnWrite" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 65413E841CEB65E5008BE849;
productRefGroup = 65413E8E1CEB65E5008BE849 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
65413E8C1CEB65E5008BE849 /* CopyOnWrite */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
65413E891CEB65E5008BE849 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
65413E911CEB65E5008BE849 /* main.cpp in Sources */,
65413E991CEB6612008BE849 /* OneByOneMatrix.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
65413E921CEB65E5008BE849 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
};
name = Debug;
};
65413E931CEB65E5008BE849 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
};
name = Release;
};
65413E951CEB65E5008BE849 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
65413E961CEB65E5008BE849 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
65413E881CEB65E5008BE849 /* Build configuration list for PBXProject "CopyOnWrite" */ = {
isa = XCConfigurationList;
buildConfigurations = (
65413E921CEB65E5008BE849 /* Debug */,
65413E931CEB65E5008BE849 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
65413E941CEB65E5008BE849 /* Build configuration list for PBXNativeTarget "CopyOnWrite" */ = {
isa = XCConfigurationList;
buildConfigurations = (
65413E951CEB65E5008BE849 /* Debug */,
65413E961CEB65E5008BE849 /* Release */,
);
defaultConfigurationIsVisible = 0;
};
/* End XCConfigurationList section */
};
rootObject = 65413E851CEB65E5008BE849 /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:CopyOnWrite.xcodeproj">
</FileRef>
</Workspace>

View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0620"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65413E8C1CEB65E5008BE849"
BuildableName = "CopyOnWrite"
BlueprintName = "CopyOnWrite"
ReferencedContainer = "container:CopyOnWrite.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65413E8C1CEB65E5008BE849"
BuildableName = "CopyOnWrite"
BlueprintName = "CopyOnWrite"
ReferencedContainer = "container:CopyOnWrite.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65413E8C1CEB65E5008BE849"
BuildableName = "CopyOnWrite"
BlueprintName = "CopyOnWrite"
ReferencedContainer = "container:CopyOnWrite.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "65413E8C1CEB65E5008BE849"
BuildableName = "CopyOnWrite"
BlueprintName = "CopyOnWrite"
ReferencedContainer = "container:CopyOnWrite.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>CopyOnWrite.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>65413E8C1CEB65E5008BE849</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,77 @@
//
// OneByOneMatrix.cpp
// CopyOnWrite
//
// Created by clink on 17/05/16.
// Copyright (c) 2016 HSEL. All rights reserved.
//
#include "OneByOneMatrix.h"
#include <iostream>
int OneByOneMatrix::s_instanceCounter=0;
OneByOneMatrix::OneByOneMatrix()
: m_value(-1), m_referenceCounter(0)
{
s_instanceCounter++;
}
OneByOneMatrix::OneByOneMatrix(int initialValue)
: m_value(initialValue), m_referenceCounter(0)
{
s_instanceCounter++;
}
OneByOneMatrix::OneByOneMatrix(const OneByOneMatrix& source)
: m_value(source.m_value), m_referenceCounter(source.m_referenceCounter)
{
s_instanceCounter++;
}
OneByOneMatrix::~OneByOneMatrix(){
s_instanceCounter--;
}
/*
int OneByOneMatrix::value(void) const {
return m_value;
}*/
OneByOneMatrix OneByOneMatrix::operator+(const OneByOneMatrix & other){
return OneByOneMatrix(m_value + other.m_value);
}
OneByOneMatrix::operator int(){
return m_value;
}
OneByOneMatrix& OneByOneMatrix::operator ++(int i){
m_value++;
return *this;
}
bool OneByOneMatrix::operator==(const OneByOneMatrix& other){
return this->m_value == other.m_value;
}
bool OneByOneMatrix::operator!=(const OneByOneMatrix& other){
return !(*this==other);
}
bool OneByOneMatrix::instanceCountExceeds(int max)
{
bool retval = s_instanceCounter>max;
if (retval){
std::cout << OneByOneMatrix::s_instanceCounter << " > max=" << max << std::endl;
}
return retval;
}

View File

@ -0,0 +1,42 @@
//
// OneByOneMatrix.h
// CopyOnWrite
//
// Created by clink on 17/05/16.
// Copyright (c) 2016 HSEL. All rights reserved.
//
#ifndef __CopyOnWrite__OneByOneMatrix__
#define __CopyOnWrite__OneByOneMatrix__
class OneByOneMatrix {
int m_value;
int m_referenceCounter; // refcounting
static int s_instanceCounter; // assertions
public:
OneByOneMatrix();
explicit OneByOneMatrix(int initialValue);
OneByOneMatrix(const OneByOneMatrix&);
~OneByOneMatrix();
explicit operator int();
OneByOneMatrix operator+(const OneByOneMatrix & other);
OneByOneMatrix& operator ++(int);
bool operator==(const OneByOneMatrix& other);
bool operator!=(const OneByOneMatrix& other);
friend class OneByOneMatrix_CopyOnWrite;
static bool instanceCountExceeds(int max); // assertions
};
// imagine this class as an abstraction for very large matrices
class LargeCowMatrix {
};
#endif /* defined(__CopyOnWrite__OneByOneMatrix__) */

View File

@ -0,0 +1,86 @@
//
// main.cpp
// leistungstestatSurrogat
//
// Created by clink on 11/12/14.
// Copyright (c) 2014 HSEL. All rights reserved.
//
#include <iostream>
#include "OneByOneMatrix.h"
int failCounter=0;
typedef int NumberType;
//typedef OneByOneMatrix NumberType;
//typedef LargeCowMatrix NumberType;
void simpleTests(){
NumberType i1(1);
NumberType i2(2);
NumberType i3=i1+i2;
if (i3!=NumberType(3)){
failCounter++;
std::cout << "fail!" << std::endl;
}
i3++;
if (i3!=NumberType(4)){
failCounter++;
std::cout << "fail!" << std::endl;
}
i3=i1;
if (i3!=NumberType(1)){
failCounter++;
std::cout << "fail!" << std::endl;
}
if (OneByOneMatrix::instanceCountExceeds(3)){
failCounter++;
}
}
void arrayTests(){
{
const int BUFSIZE=1000;
NumberType singletonValue(4711);
NumberType numbers[BUFSIZE];
bool didFail=false;
for(int i=0; i<BUFSIZE;i++){
numbers[i] = singletonValue;
}
for(int i=0; i<BUFSIZE;i++){
if((int)numbers[i] != (int)singletonValue){
didFail |= true;
}
}
if(didFail) {
failCounter++;
}
}
if (OneByOneMatrix::instanceCountExceeds(1)){
failCounter++;
}
}
int main(int argc, const char * argv[]) {
simpleTests();
arrayTests();
if (OneByOneMatrix::instanceCountExceeds(0)){
failCounter++;
}
std::cout << failCounter << " failures" << std::endl;
return 0;
}

58
helpers/AnsiConsole.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "AnsiConsole.h"
/*
see
https://en.wikipedia.org/wiki/ANSI_escape_code
https://gist.github.com/vratiu/9780109
https://gist.github.com/RobinMalfait/7881398
*/
AnsiConsole ansiConsole;
AnsiConsole::AnsiConsole(){
pushCursorPos();
hideCursor();
//clearScreen();
}
AnsiConsole::~AnsiConsole(){
popCursorPos();
showCursor();
}
void AnsiConsole::hideCursor(){
std::cout << "\033[?25l";
}
void AnsiConsole::showCursor(){
std::cout << "\033[?25h";
}
void AnsiConsole::pushCursorPos(){
std::cout << "\033[s";
}
void AnsiConsole::popCursorPos(){
std::cout << "\033[u";
}
void AnsiConsole::clearScreen(){
// std::cout << "\033[0J";
std::cout << "\033[2J";
}
void AnsiConsole::printText(int x, int y, std::string text, Colors color){
std::cout << "\033[" << y << ";" << x << "f"; // move cursor to x,y
std::cout << "\033[0;" << static_cast<int>(color) << "m"; // blue
std::cout << text;
std::cout << "\033[0m\n"; // reset attributes
}

34
helpers/AnsiConsole.h Normal file
View File

@ -0,0 +1,34 @@
#include <iostream>
enum class Colors {
BLACK = 30,
RED = 31,
GREEN = 32,
YELLOW = 33,
BLUE = 34,
MAGENTA = 35,
CYAN = 36,
WHITE = 37
};
class AnsiConsole {
// protected:
// can only be used by members of AnsiConsole and its
// derived classes
protected:
void hideCursor();
void showCursor();
void pushCursorPos();
void popCursorPos();
public:
AnsiConsole();
~AnsiConsole();
void clearScreen();
void printText(int x, int y, std::string text, Colors color=Colors::BLUE);
};
extern AnsiConsole ansiConsole;

124
helpers/println.hpp Normal file
View File

@ -0,0 +1,124 @@
//
// println.hpp
// printline
#ifndef println_hpp
#define println_hpp
/*
based on: http://stackoverflow.com/questions/16084379/system-out-println-function-syntax-in-c/16085135#16085135
initial Author: http://stackoverflow.com/users/264338/mkaes
*/
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
/*
template <typename T>
void printer(T t)
{
std::cout << t << std::endl;
}
template <typename T, typename ...U>
void printer(T t, U ...u)
{
std::cout << t;
printer(u...);
}*/
//namespace std {
//string to_string(const string s);
//string to_string(const char* s);
/*
inline string to_string(const string s){
return s;
}
inline string to_string(const char* s){
return string(s);
}*/
//}
/*
void println(T t)
{
std::cout << std::to_string(t) << std::endl;
}
template <typename T, typename ...U>
void println(T t, U ...u)
{
std::cout << std::to_string(t);
println(u...);
}
*/
//namespace {
// from: https://stackoverflow.com/questions/16605967/set-precision-of-stdto-string-when-converting-floating-point-values
template <typename T>
std::string to_string_with_precision(const T a_value, const int n = 6)
{
std::ostringstream out;
out << std::setprecision(n) << std::scientific << a_value;
return out.str();
}
template <typename T>
std::string as_string(T t){
using namespace std;
return to_string_with_precision(t);
//return to_string(t);
}
template <>
std::string as_string<const char *>(const char * s){
return std::string(s);
}
template <>
std::string as_string<std::string>(std::string s){
return s;
}
//}
template <typename T>
void println(T t)
{
std::cout << as_string(t) << std::endl;
}
template <typename T, typename ...U>
void println(T t, U ...u)
{
std::cout << as_string(t);
println(u...);
}
template <typename T>
void print(T t)
{
std::cout << as_string(t);
}
template <typename T, typename ...U>
void print(T t, U ...u)
{
std::cout << as_string(t);
print(u...);
}
#endif /* println_hpp */

View File

@ -0,0 +1,253 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
6590867D1EE9FA9400A93466 /* main_02_MENT.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6590867C1EE9FA9400A93466 /* main_02_MENT.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
6590866E1EE9F98F00A93466 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
659086701EE9F98F00A93466 /* 02_MENT_gg */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = 02_MENT_gg; sourceTree = BUILT_PRODUCTS_DIR; };
6590867C1EE9FA9400A93466 /* main_02_MENT.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main_02_MENT.cpp; path = ../../../02_MENT/main_02_MENT.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
6590866D1EE9F98F00A93466 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
659086671EE9F98F00A93466 = {
isa = PBXGroup;
children = (
659086721EE9F98F00A93466 /* 02_MENT_gg */,
659086711EE9F98F00A93466 /* Products */,
);
sourceTree = "<group>";
};
659086711EE9F98F00A93466 /* Products */ = {
isa = PBXGroup;
children = (
659086701EE9F98F00A93466 /* 02_MENT_gg */,
);
name = Products;
sourceTree = "<group>";
};
659086721EE9F98F00A93466 /* 02_MENT_gg */ = {
isa = PBXGroup;
children = (
6590867C1EE9FA9400A93466 /* main_02_MENT.cpp */,
);
path = 02_MENT_gg;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
6590866F1EE9F98F00A93466 /* 02_MENT_gg */ = {
isa = PBXNativeTarget;
buildConfigurationList = 659086771EE9F98F00A93466 /* Build configuration list for PBXNativeTarget "02_MENT_gg" */;
buildPhases = (
6590866C1EE9F98F00A93466 /* Sources */,
6590866D1EE9F98F00A93466 /* Frameworks */,
6590866E1EE9F98F00A93466 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = 02_MENT_gg;
productName = 02_MENT_gg;
productReference = 659086701EE9F98F00A93466 /* 02_MENT_gg */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
659086681EE9F98F00A93466 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = HSEL;
TargetAttributes = {
6590866F1EE9F98F00A93466 = {
CreatedOnToolsVersion = 8.2.1;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 6590866B1EE9F98F00A93466 /* Build configuration list for PBXProject "02_MENT_gg" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 659086671EE9F98F00A93466;
productRefGroup = 659086711EE9F98F00A93466 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
6590866F1EE9F98F00A93466 /* 02_MENT_gg */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
6590866C1EE9F98F00A93466 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6590867D1EE9FA9400A93466 /* main_02_MENT.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
659086751EE9F98F00A93466 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
};
name = Debug;
};
659086761EE9F98F00A93466 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
};
name = Release;
};
659086781EE9F98F00A93466 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
659086791EE9F98F00A93466 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
6590866B1EE9F98F00A93466 /* Build configuration list for PBXProject "02_MENT_gg" */ = {
isa = XCConfigurationList;
buildConfigurations = (
659086751EE9F98F00A93466 /* Debug */,
659086761EE9F98F00A93466 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
659086771EE9F98F00A93466 /* Build configuration list for PBXNativeTarget "02_MENT_gg" */ = {
isa = XCConfigurationList;
buildConfigurations = (
659086781EE9F98F00A93466 /* Debug */,
659086791EE9F98F00A93466 /* Release */,
);
defaultConfigurationIsVisible = 0;
};
/* End XCConfigurationList section */
};
rootObject = 659086681EE9F98F00A93466 /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:02_MENT_gg.xcodeproj">
</FileRef>
</Workspace>

View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590866F1EE9F98F00A93466"
BuildableName = "02_MENT_gg"
BlueprintName = "02_MENT_gg"
ReferencedContainer = "container:02_MENT_gg.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590866F1EE9F98F00A93466"
BuildableName = "02_MENT_gg"
BlueprintName = "02_MENT_gg"
ReferencedContainer = "container:02_MENT_gg.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590866F1EE9F98F00A93466"
BuildableName = "02_MENT_gg"
BlueprintName = "02_MENT_gg"
ReferencedContainer = "container:02_MENT_gg.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590866F1EE9F98F00A93466"
BuildableName = "02_MENT_gg"
BlueprintName = "02_MENT_gg"
ReferencedContainer = "container:02_MENT_gg.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>02_MENT_gg.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>6590866F1EE9F98F00A93466</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,255 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
659086601EE9EC6200A93466 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6590865F1EE9EC6200A93466 /* main.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
6590865A1EE9EC6200A93466 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
6590865C1EE9EC6200A93466 /* limits2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = limits2; sourceTree = BUILT_PRODUCTS_DIR; };
6590865F1EE9EC6200A93466 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
659086661EE9ECC100A93466 /* println.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = println.hpp; path = ../../../helpers/println.hpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
659086591EE9EC6200A93466 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
659086531EE9EC6200A93466 = {
isa = PBXGroup;
children = (
6590865E1EE9EC6200A93466 /* limits2 */,
6590865D1EE9EC6200A93466 /* Products */,
);
sourceTree = "<group>";
};
6590865D1EE9EC6200A93466 /* Products */ = {
isa = PBXGroup;
children = (
6590865C1EE9EC6200A93466 /* limits2 */,
);
name = Products;
sourceTree = "<group>";
};
6590865E1EE9EC6200A93466 /* limits2 */ = {
isa = PBXGroup;
children = (
659086661EE9ECC100A93466 /* println.hpp */,
6590865F1EE9EC6200A93466 /* main.cpp */,
);
path = limits2;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
6590865B1EE9EC6200A93466 /* limits2 */ = {
isa = PBXNativeTarget;
buildConfigurationList = 659086631EE9EC6200A93466 /* Build configuration list for PBXNativeTarget "limits2" */;
buildPhases = (
659086581EE9EC6200A93466 /* Sources */,
659086591EE9EC6200A93466 /* Frameworks */,
6590865A1EE9EC6200A93466 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = limits2;
productName = limits2;
productReference = 6590865C1EE9EC6200A93466 /* limits2 */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
659086541EE9EC6200A93466 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = HSEL;
TargetAttributes = {
6590865B1EE9EC6200A93466 = {
CreatedOnToolsVersion = 8.2.1;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 659086571EE9EC6200A93466 /* Build configuration list for PBXProject "limits2" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 659086531EE9EC6200A93466;
productRefGroup = 6590865D1EE9EC6200A93466 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
6590865B1EE9EC6200A93466 /* limits2 */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
659086581EE9EC6200A93466 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
659086601EE9EC6200A93466 /* main.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
659086611EE9EC6200A93466 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
};
name = Debug;
};
659086621EE9EC6200A93466 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
};
name = Release;
};
659086641EE9EC6200A93466 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
659086651EE9EC6200A93466 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
659086571EE9EC6200A93466 /* Build configuration list for PBXProject "limits2" */ = {
isa = XCConfigurationList;
buildConfigurations = (
659086611EE9EC6200A93466 /* Debug */,
659086621EE9EC6200A93466 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
659086631EE9EC6200A93466 /* Build configuration list for PBXNativeTarget "limits2" */ = {
isa = XCConfigurationList;
buildConfigurations = (
659086641EE9EC6200A93466 /* Debug */,
659086651EE9EC6200A93466 /* Release */,
);
defaultConfigurationIsVisible = 0;
};
/* End XCConfigurationList section */
};
rootObject = 659086541EE9EC6200A93466 /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:limits2.xcodeproj">
</FileRef>
</Workspace>

View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590865B1EE9EC6200A93466"
BuildableName = "limits2"
BlueprintName = "limits2"
ReferencedContainer = "container:limits2.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590865B1EE9EC6200A93466"
BuildableName = "limits2"
BlueprintName = "limits2"
ReferencedContainer = "container:limits2.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590865B1EE9EC6200A93466"
BuildableName = "limits2"
BlueprintName = "limits2"
ReferencedContainer = "container:limits2.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6590865B1EE9EC6200A93466"
BuildableName = "limits2"
BlueprintName = "limits2"
ReferencedContainer = "container:limits2.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>limits2.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>6590865B1EE9EC6200A93466</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,26 @@
// main.cpp
// limits2
#include "../../../helpers/println.hpp"
#include <limits>
int main()
{
println("type\t sizeof\t\t lowest\t\t\t\t highest\n");
println("float \t ",sizeof(float), "\t\t\t",
std::numeric_limits<float>::lowest(), "\t\t",
std::numeric_limits<float>::max());
println("double\t ",sizeof(double), "\t\t\t",
std::numeric_limits<double>::lowest(), "\t\t",
std::numeric_limits<double>::max());
println("int \t ",sizeof(int), "\t\t\t",
std::numeric_limits<int>::lowest(), "\t\t\t",
std::numeric_limits<int>::max());
println("long \t ",sizeof(long), " \t\t",
std::numeric_limits<long>::lowest(), '\t',
std::numeric_limits<long>::max());
}