Skip to content

Commit

Permalink
Merge branch 'development' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsleepyhead authored Dec 12, 2023
2 parents e5beb64 + 594cd3b commit 98da5f6
Show file tree
Hide file tree
Showing 54 changed files with 2,023 additions and 2,231 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BasedOnStyle: google
ColumnLimit: 100
AccessModifierOffset: -2

11 changes: 11 additions & 0 deletions in_class/emscripten/Hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
#include <string>

int main() {
std::cout << "Hello World" << std::endl;

std::string text;
std::cin >> text;

std::cout << "You Entered: " << text << std::endl;
}
1 change: 1 addition & 0 deletions in_class/emscripten/build-drawSquare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emcc drawSquare.cpp -o drawSquare.js -s "EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']"
1 change: 1 addition & 0 deletions in_class/emscripten/build-factorial.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emcc factorial.cpp -s WASM=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" -o factorial.js
45 changes: 45 additions & 0 deletions in_class/emscripten/drawSquare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <iostream>

int X_pos=50, Y_pos=50;

extern "C" {
// Function to be called from JavaScript
EMSCRIPTEN_KEEPALIVE
void drawSquare() {
EM_ASM_({
var canvas = document.getElementById('canvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect($0, $1, 100, 100); // Draw a 100x100 red square
}
}, X_pos, Y_pos);
X_pos += 10;
Y_pos += 10;
}
}

int main() {
// Use EM_ASM to inject HTML for the button
EM_ASM({
var button = document.createElement('button');
button.innerHTML = 'Draw Square';
button.onclick = function() {
// Call the C++ function drawSquare()
_drawSquare();
};
document.body.appendChild(button);

// Create a canvas element
var canvas = document.createElement('canvas');
canvas.id = 'canvas';
canvas.width = 300;
canvas.height = 300;
canvas.style.border = '1px solid black';
document.body.appendChild(canvas);
});

return 0;
}
9 changes: 9 additions & 0 deletions in_class/emscripten/drawSquare.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Draw Squares with Emscripten</title>
</head>
<body>
<script src="drawSquare.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions in_class/emscripten/factorial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <emscripten/emscripten.h>
#include <iostream>

extern "C" {
EMSCRIPTEN_KEEPALIVE
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
}
23 changes: 23 additions & 0 deletions in_class/emscripten/factorial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h1>Factorial Calculator</h1>
Enter a number: <input id="number" type="number" min="0" value="5"/>
<button onclick="calculateFactorial()">Calculate Factorial</button>
<div id="result"></div>

<script>
function calculateFactorial() {
var num = document.getElementById('number').value;
var result = Module.ccall('factorial', 'number', ['number'], [num]);
document.getElementById('result').innerText = 'Factorial: ' + result;
}
</script>

<!-- Load the compiled JavaScript file -->
<script src="factorial.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions in_class/emscripten/power.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <emscripten/emscripten.h>
#include <iostream>
#include <cmath>

extern "C" {
EMSCRIPTEN_KEEPALIVE
double power(double base, double exponent) {
return std::pow(base, exponent);
}
}
25 changes: 25 additions & 0 deletions in_class/emscripten/power.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Power Calculator</title>
</head>
<body>
<h1>Power Calculator</h1>
Enter a base: <input id="base" type="number" min="0" value="2.0"/>
Enter an exponent: <input id="exponent" type="number" min="0" value="10.0"/>
<button onclick="calculatePower()">Calculate Power</button>
<div id="result"></div>

<script>
function calculatePower() {
var base = document.getElementById('base').value;
var exp = document.getElementById('exponent').value;
var result = Module.ccall('power', 'number', ['number', 'number'], [base, exp]);
document.getElementById('result').innerText = 'Result: ' + result;
}
</script>

<!-- Load the compiled JavaScript file -->
<script src="power.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions in_class/emscripten/start-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 -m http.server
67 changes: 67 additions & 0 deletions in_class/meta/meta.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <string>
//#include <type_traits>

// static size_t NextID() {
// static size_t next_id = 0;
// return ++next_id;
// }

// template <typename T>
// static size_t ToID() {
// static size_t this_id = NextID();
// return this_id;
// }

template <typename T1, typename T2>
constexpr bool SameType(T1, T2) {
return false;
}

template <typename T1>
constexpr bool SameType(T1, T1) {
return true;
}

template <typename T1>
constexpr bool SameType(T1) {
return true;
}

template <typename T1, typename T2, typename... extraTs>
constexpr bool SameType(T1 v1, T2 v2, extraTs... extra_values) {
return SameType(v1,v2) && SameType(v1, extra_values...);
}

void TestFun(int) { std::cout << "int version!" << std::endl; }
void TestFun(bool) { std::cout << "bool version!" << std::endl; }
void TestFun(...) { std::cout << "extra version!" << std::endl; }

int main()
{
int u = 5, v = 11, w = 22, x = 10, y = 1;
double z = 3.14159265358979;
std::string s("test");

std::cout << "SameType(x,y) = " << SameType(x,y) << std::endl;
std::cout << "SameType(x,z) = " << SameType(x,z) << std::endl;
std::cout << "SameType(x,s) = " << SameType(x,s) << std::endl;
std::cout << "SameType(x,1) = " << SameType(x,1) << std::endl;
std::cout << "SameType(x) = " << SameType(x) << std::endl;
std::cout << "SameType(u,v,w,x,y) = " << SameType(u,v,w,x,y) << std::endl;
std::cout << "SameType(u,v,w,x,y,z) = " << SameType(u,v,w,x,y,z) << std::endl;

// TestFun(5);
// TestFun(true);
// std::string str("Hello.");
// TestFun(str);
// TestFun("what will this do?");
// TestFun('a');

// std::cout << "int: " << ToID<int>() << std::endl;
// std::cout << "int: " << ToID<int>() << std::endl;
// std::cout << "double: " << ToID<double>() << std::endl;
// std::cout << "std::string: " << ToID<std::string>() << std::endl;
// std::cout << "char: " << ToID<char>() << std::endl;
// std::cout << "int: " << ToID<int>() << std::endl;
}
68 changes: 68 additions & 0 deletions in_class/meta/meta2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <type_traits>

struct Animal {
std::string name;
void PrintName() const { std::cout << name << std::endl; };
};

struct Dog : Animal {
void Herd() { }
void Hunt() { }
int GetStrength() { return 10; }
};
struct Sheep : Animal {
void Graze() { }
int GetStrength() { return 5; }
};
struct Lion : Animal {
void Roar() { }
int Hunt() { return 1; }
int GetStrength() { return 100; }
};

template <typename T>
concept CanHunt = requires(T animal) {
{ animal.Hunt() } -> std::same_as<int>;
};

template <CanHunt T>
void ManageHunt(T & hunter) {
hunter.Hunt();
}

template <typename... Ts>
void PrintNames(Ts &... animals) {
(std::cout << ... << animals.name) << std::endl;
// (..., animals.PrintName());
}

template <typename... Ts>
int TotalStrength(Ts &... animals) {
return (animals.GetStrength() + ...);
}

template <typename... Ts>
auto Multiply(Ts... values) {
return (... * values);
}

template <typename... Ts>
int MultStrength(Ts &... animals) {
return Multiply(animals.GetStrength() ...);
}

int main()
{
Lion l; l.name = "Oscar";
Dog d; d.name = "Spot";
Sheep s; s.name = "Baaaa";

PrintNames(l, d, s, l, l);
std::cout << "Total = " << TotalStrength(l, d, s, l, l) << std::endl;
std::cout << "Product = " << MultStrength(l, d, s, l, l) << std::endl;

ManageHunt(l);
// ManageHunt(d);
// ManageHunt(s);
}
22 changes: 22 additions & 0 deletions in_class/other/adl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <string>

namespace mynamespace {
struct MyClass { int x = 5; };

template <typename T>
int fun(T in) { return in.x; }
}

struct MyClass2 { int x = 10; };

template <typename T>
int fun(T in) { return in.x + 1; }

int main()
{
mynamespace::MyClass obj;
MyClass2 obj2;

std::cout << fun(obj) << std::endl;
}
30 changes: 30 additions & 0 deletions in_class/other/crtp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <string>

struct VehicleBase {

};

template <typename DERIVED_T>
struct Vehicle : public VehicleBase {
std::string name;
std::string owner;
virtual double GetSpeed() const { return 0; }
DERIVED_T & Derived() { return *static_cast<DERIVED_T*>(this); }

DERIVED_T & SetName(std::string in) { name = in; return Derived(); }
DERIVED_T & SetOwner(std::string in) { owner = in; return Derived(); }
};

struct Quinjet : public Vehicle<Quinjet> {
int num_missiles = 100;
double GetSpeed() const override { return 1000000.0; }

Quinjet & SetNumMissiles(int in) { num_missiles = in; return *this; }
};

int main()
{
Quinjet q1;
q1.SetName("q1").SetOwner("Avengers").SetNumMissiles(2000);
}
9 changes: 9 additions & 0 deletions in_class/other/downto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

int main()
{
int x = 10;
while (x --> 0) {
std::cout << x << std::endl;
}
}
42 changes: 42 additions & 0 deletions in_class/other/multitype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

struct BaseNode {
virtual void Print() = 0;
};

template <typename T>
struct Node : public BaseNode {
T value;

Node(T value) : value(value) { }

void Print() override {
std::cout << value << std::endl;
}
};

struct Container {
std::vector<BaseNode *> values;

template <typename T>
void AddValue(T in) {
values.push_back( new Node<T>(in) );
}

void Print() {
for (auto ptr : values) ptr->Print();
}
};


int main()
{
Container c;
c.AddValue(10);
c.AddValue("Test");
c.AddValue('x');
c.Print();
}
Loading

0 comments on commit 98da5f6

Please sign in to comment.