Skip to content

Commit

Permalink
Add template mini series
Browse files Browse the repository at this point in the history
  • Loading branch information
dangelog committed Nov 28, 2022
1 parent 563fa6d commit b818b93
Show file tree
Hide file tree
Showing 28 changed files with 1,566 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Qt-Widgets-and-more/template-mini-series/CMakeLists.txt
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 Qt-Widgets-and-more/template-mini-series/episode1/CMakeLists.txt
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
)
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 Qt-Widgets-and-more/template-mini-series/episode1/main.cpp
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();
}
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);
}
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;
}
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;
}
Loading

0 comments on commit b818b93

Please sign in to comment.