-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,566 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
project(template_miniseries) | ||
add_subdirectory(episode1) | ||
add_subdirectory(episode2) | ||
add_subdirectory(episode3) | ||
add_subdirectory(episode4) |
13 changes: 13 additions & 0 deletions
13
Qt-Widgets-and-more/template-mini-series/episode1/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.9) | ||
project(episode1) | ||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_INCLUDE_CURRENT_DIR ON) | ||
add_executable(episode1 | ||
main.cpp | ||
basic_min_method.cpp | ||
template_min_method.cpp | ||
template_stack.cpp | ||
template_stackV2.cpp | ||
stack_container_type.cpp | ||
stack_container_typeV2.cpp | ||
) |
40 changes: 40 additions & 0 deletions
40
Qt-Widgets-and-more/template-mini-series/episode1/basic_min_method.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* MIT License | ||
Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
namespace { | ||
int min(int x, int y) | ||
{ | ||
return x < y ? x : y; | ||
} | ||
} // end namespace | ||
|
||
void basic_min_method() | ||
{ | ||
cout << "========== episode1 - basic min method ==========\n"; | ||
cout << "min(1,2)= " << min(1, 2) << endl; | ||
cout << "min(2,1)= " << min(2, 1) << endl; | ||
cout << "min(1,1)=" << min(1, 1) << endl; | ||
} |
38 changes: 38 additions & 0 deletions
38
Qt-Widgets-and-more/template-mini-series/episode1/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* MIT License | ||
Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
void basic_min_method(); | ||
void template_min_method(); | ||
void template_stack(); | ||
void template_stackV2(); | ||
void stack_container_type(); | ||
void stack_container_typeV2(); | ||
int main() | ||
{ | ||
basic_min_method(); | ||
template_min_method(); | ||
template_stack(); | ||
template_stackV2(); | ||
stack_container_type(); | ||
stack_container_typeV2(); | ||
} |
79 changes: 79 additions & 0 deletions
79
Qt-Widgets-and-more/template-mini-series/episode1/stack_container_type.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* MIT License | ||
Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
#include <deque> | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
namespace { | ||
template <typename ItemType, typename Container = std::vector<ItemType>> | ||
class Stack | ||
{ | ||
public: | ||
using item_type = ItemType; | ||
void push(const ItemType &t) { m_items.push_back(t); } | ||
ItemType pop() | ||
{ | ||
auto item = m_items.back(); | ||
m_items.pop_back(); | ||
return item; | ||
} | ||
size_t size() const { return m_items.size(); } | ||
void printStack() | ||
{ | ||
bool first = true; | ||
cout << "Stack: ["; | ||
for (ItemType &item : m_items) { | ||
if (!first) | ||
cout << ", "; | ||
first = false; | ||
cout << item; | ||
} | ||
cout << "]" << endl; | ||
} | ||
|
||
private: | ||
Container m_items; | ||
}; | ||
} // end namespace | ||
|
||
void stack_container_type() | ||
{ | ||
cout << "========== episode1 - stack container type ==========\n"; | ||
Stack<int> intStack; | ||
intStack.push(1); | ||
intStack.push(2); | ||
intStack.printStack(); | ||
cout << "intStack pop: " << intStack.pop() << endl; | ||
|
||
Stack<int, std::deque<int>> dequeStack; | ||
dequeStack.push(1); | ||
dequeStack.push(2); | ||
dequeStack.printStack(); | ||
cout << "dequeStack pop: " << dequeStack.pop() << endl; | ||
|
||
// Ouch this is possible too.... | ||
// Stack<int, std::deque<string>> brokenStack; | ||
// brokenStack.push(1); | ||
} |
75 changes: 75 additions & 0 deletions
75
Qt-Widgets-and-more/template-mini-series/episode1/stack_container_typeV2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* MIT License | ||
Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
#include <deque> | ||
#include <vector> | ||
|
||
namespace { | ||
template <typename ItemType, template <typename...> class Container = std::vector> | ||
class Stack | ||
{ | ||
public: | ||
using item_type = ItemType; | ||
void push(const ItemType &t) { m_items.push_back(t); } | ||
ItemType pop() | ||
{ | ||
auto item = m_items.back(); | ||
m_items.pop_back(); | ||
return item; | ||
} | ||
size_t size() const { return m_items.size(); } | ||
void printStack() | ||
{ | ||
bool first = true; | ||
cout << "Stack: ["; | ||
for (ItemType &item : m_items) { | ||
if (!first) | ||
cout << ", "; | ||
first = false; | ||
cout << item; | ||
} | ||
cout << "]" << endl; | ||
} | ||
|
||
private: | ||
Container<ItemType> m_items; | ||
}; | ||
} // end namespace | ||
|
||
void stack_container_typeV2() | ||
{ | ||
cout << "========== episode1 - stack container type V2 ==========\n"; | ||
Stack<int> intStack; | ||
intStack.push(1); | ||
intStack.push(2); | ||
intStack.printStack(); | ||
cout << "intStack pop: " << intStack.pop() << endl; | ||
|
||
Stack<int, std::deque> dequeStack; | ||
dequeStack.push(1); | ||
dequeStack.push(2); | ||
dequeStack.printStack(); | ||
cout << "dequeStack pop: " << dequeStack.pop() << endl; | ||
} |
66 changes: 66 additions & 0 deletions
66
Qt-Widgets-and-more/template-mini-series/episode1/template_min_method.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* MIT License | ||
Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
namespace { | ||
template <typename T> | ||
const T &myMin(const T &x, const T &y) | ||
{ | ||
return x < y ? x : y; | ||
} | ||
|
||
class Test | ||
{ | ||
public: | ||
Test(int x, int y) | ||
: x(x) | ||
, y(y) | ||
{ | ||
} | ||
friend bool operator<(const Test &a, const Test &b) | ||
{ | ||
if (a.x != b.x) | ||
return a.x < b.x; | ||
return a.y < b.y; | ||
} | ||
friend ostream &operator<<(ostream &os, const Test &t) | ||
{ | ||
os << "Test(" << t.x << ", " << t.y << ")"; | ||
return os; | ||
} | ||
|
||
private: | ||
int x; | ||
int y; | ||
}; | ||
} // end namespace | ||
|
||
void template_min_method() | ||
{ | ||
std::cout << "========== episode1 - template min method ==========\n"; | ||
std::cout << "integers " << myMin(1, 2) << endl; | ||
std::cout << "strings " << myMin(std::string("abc"), std::string("def")) << endl; | ||
std::cout << "classes " << myMin(Test(2, 5), Test(2, 4)) << endl; | ||
} |
Oops, something went wrong.