Skip to content

[pull] master from parallel101:master #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 16/00/.tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[+]
build_type=Release
build_target=pybmain
build_dir=build
build_generator=Ninja
build_configs=-DENABLE_SOME_FEATURE:BOOL=ON
run_target="build/pybmain/pybmain"
16 changes: 16 additions & 0 deletions 16/00/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.18)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake;${CMAKE_MODULE_PATH}")

project(CppCMakeDemo LANGUAGES CXX)

include(MyUsefulFuncs)

add_subdirectory(pybmain)
add_subdirectory(biology)
3 changes: 3 additions & 0 deletions 16/00/biology/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file(GLOB_RECURSE srcs CONFIGURE_DEPENDS src/*.cpp include/*.h)
add_library(biology STATIC ${srcs})
target_include_directories(biology PUBLIC include)
20 changes: 20 additions & 0 deletions 16/00/biology/include/biology/Animal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <ostream>

namespace biology {

struct Animal {
virtual void speak(std::ostream &os) const = 0;
virtual ~Animal() = default;
};

struct Cat : Animal {
virtual void speak(std::ostream &os) const override;
};

struct Dog : Animal {
virtual void speak(std::ostream &os) const override;
};

}
13 changes: 13 additions & 0 deletions 16/00/biology/include/biology/Carer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <string>

namespace biology {

struct Animal;

struct Carer {
std::string care(Animal *a) const;
};

}
13 changes: 13 additions & 0 deletions 16/00/biology/src/Animal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <biology/Animal.h>

namespace biology {

void Cat::speak(std::ostream &os) const {
os << "Meow~";
}

void Dog::speak(std::ostream &os) const {
os << "Wang!";
}

}
13 changes: 13 additions & 0 deletions 16/00/biology/src/Carer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <biology/Carer.h>
#include <biology/Animal.h>
#include <sstream>

namespace biology {

std::string Carer::care(Animal *a) const {
std::ostringstream ss;
a->speak(ss);
return ss.str();
}

}
13 changes: 13 additions & 0 deletions 16/00/cmake/MyUsefulFuncs.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
macro (my_add_target name type)
# 用法: my_add_target(pybmain EXECUTABLE)
file(GLOB_RECURSE srcs CONFIGURE_DEPENDS src/*.cpp src/*.h)
if ("${type}" MATCHES "EXECUTABLE")
add_executable(${name} ${srcs})
else()
add_library(${name} ${type} ${srcs})
endif()
target_include_directories(${name} PUBLIC include)
endmacro()

set(SOME_USEFUL_GLOBAL_VAR ON)
set(ANOTHER_USEFUL_GLOBAL_VAR OFF)
5 changes: 5 additions & 0 deletions 16/00/pybmain/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
file(GLOB_RECURSE srcs CONFIGURE_DEPENDS src/*.cpp include/*.h)
add_executable(pybmain ${srcs})
target_include_directories(pybmain PUBLIC include)

target_link_libraries(pybmain PUBLIC biology)
16 changes: 16 additions & 0 deletions 16/00/pybmain/include/pybmain/myutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <string>
#include <cctype>

namespace pybmain {

static std::string alluppercase(std::string s) {
std::string ret;
for (char c: s) {
ret.push_back(std::toupper(c));
}
return ret;
}

}
17 changes: 17 additions & 0 deletions 16/00/pybmain/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <string>
#include <pybmain/myutils.h>
#include <biology/Animal.h>
#include <biology/Carer.h>

int main() {
std::cout << "This is biomain!\n";
biology::Animal *a = new biology::Cat();
biology::Carer *c = new biology::Carer();
std::string res = c->care(a);
res = pybmain::alluppercase(res);
std::cout << res << '\n';
delete c;
delete a;
return 0;
}
Binary file modified 16/slides.pptx
Binary file not shown.
16 changes: 16 additions & 0 deletions 17/00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
map<string, int> items = {
{"hello", 1},
{"world", 2},
};

items["time"] = 4;
int time = items.at("time");
cout << time << endl;
}
8 changes: 8 additions & 0 deletions 17/00.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
items = dict([
("hello", 1),
("world", 2),
])

items["time"] = 4
time = items["time"]
print(time)
15 changes: 15 additions & 0 deletions 17/00a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
map<string, int> items = {
{"hello", 1},
{"world", 2},
};

int time = items["hello"];
cout << time << endl;
}
15 changes: 15 additions & 0 deletions 17/00b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
map<string, int> items = {
{"hello", 1},
{"world", 2},
};

int time = items["hell"];
cout << time << endl;
}
13 changes: 13 additions & 0 deletions 17/01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <map>
#include "printer.h"

using namespace std;

int main() {
map<string, int> m;
m["hello"] = 1;
m["world"] = 2;
cout << m << endl;
return 0;
}
13 changes: 13 additions & 0 deletions 17/02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <map>
#include "printer.h"

using namespace std;

int main() {
map<string, int> m;
m.at("hello") = 1;
m.at("world") = 2;
cout << m << endl;
return 0;
}
39 changes: 39 additions & 0 deletions 17/printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <iostream>
#include <utility>
#include <type_traits>

namespace std {

template <class T, class = const char *>
struct __printer_test_c_str {
using type = void;
};

template <class T>
struct __printer_test_c_str<T, decltype(std::declval<T>().c_str())> {};

template <class T, int = 0, int = 0, int = 0,
class = decltype(std::declval<std::ostream &>() << *++std::declval<T>().begin()),
class = decltype(std::declval<T>().begin() != std::declval<T>().end()),
class = typename __printer_test_c_str<T>::type>
std::ostream &operator<<(std::ostream &os, T const &v) {
os << '{';
auto it = v.begin();
if (it != v.end()) {
os << *it;
for (++it; it != v.end(); ++it)
os << ',' << *it;
}
os << '}';
return os;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> const &v) {
os << '{' << v.first << ',' << v.second << '}';
return os;
}

}
Binary file added 17/slides.pptx
Binary file not shown.
2 changes: 2 additions & 0 deletions 17/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target("exec")
add_files("01.cpp")