aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-03-17 14:16:33 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-03-21 16:15:35 +0100
commitc3c692d26e2d6976afa226a3f49e1745e94712e7 (patch)
tree2ecb64587c5a2250b9c3f6f95cff1f068c2e0970 /examples
parentec7ad296f48f3f1856c3eb7cc3685249a096d6b8 (diff)
Polish the samplebinding example
- Use a std::shared_ptr for internal storage. - Simplify copy and assignment. - Fix constness of the flavor accessor - Add ostream operator to IceCream Pick-to: 6.2 Change-Id: I814fa14095cbb96ab5642735e16b8b50101d4771 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/samplebinding/icecream.cpp14
-rw-r--r--examples/samplebinding/icecream.h12
-rw-r--r--examples/samplebinding/main.py2
-rw-r--r--examples/samplebinding/truck.cpp35
-rw-r--r--examples/samplebinding/truck.h18
5 files changed, 44 insertions, 37 deletions
diff --git a/examples/samplebinding/icecream.cpp b/examples/samplebinding/icecream.cpp
index 8d40302da..9a65e396f 100644
--- a/examples/samplebinding/icecream.cpp
+++ b/examples/samplebinding/icecream.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2018 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt for Python examples of the Qt Toolkit.
@@ -50,11 +50,13 @@
#include "icecream.h"
+#include <iostream>
+
Icecream::Icecream(const std::string &flavor) : m_flavor(flavor) {}
-Icecream::~Icecream() {}
+Icecream::~Icecream() = default;
-const std::string Icecream::getFlavor()
+std::string Icecream::getFlavor() const
{
return m_flavor;
}
@@ -63,3 +65,9 @@ Icecream *Icecream::clone()
{
return new Icecream(*this);
}
+
+std::ostream &operator<<(std::ostream &str, const Icecream &i)
+{
+ str << i.getFlavor();
+ return str;
+}
diff --git a/examples/samplebinding/icecream.h b/examples/samplebinding/icecream.h
index 1997fdc49..7c1d1cbdb 100644
--- a/examples/samplebinding/icecream.h
+++ b/examples/samplebinding/icecream.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2018 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt for Python examples of the Qt Toolkit.
@@ -51,21 +51,23 @@
#ifndef ICECREAM_H
#define ICECREAM_H
-#include <string>
-
#include "macros.h"
+#include <iosfwd>
+#include <string>
+
class BINDINGS_API Icecream
{
public:
- Icecream(const std::string &flavor);
+ explicit Icecream(const std::string &flavor);
virtual Icecream *clone();
virtual ~Icecream();
- virtual const std::string getFlavor();
+ virtual std::string getFlavor() const;
private:
std::string m_flavor;
};
+std::ostream &operator<<(std::ostream &str, const Icecream &i);
#endif // ICECREAM_H
diff --git a/examples/samplebinding/main.py b/examples/samplebinding/main.py
index bc5e16eec..d5fb4a017 100644
--- a/examples/samplebinding/main.py
+++ b/examples/samplebinding/main.py
@@ -1,7 +1,7 @@
############################################################################
##
-## Copyright (C) 2018 The Qt Company Ltd.
+## Copyright (C) 2022 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing/
##
## This file is part of the Qt for Python examples of the Qt Toolkit.
diff --git a/examples/samplebinding/truck.cpp b/examples/samplebinding/truck.cpp
index c8b0d8988..d23991d9f 100644
--- a/examples/samplebinding/truck.cpp
+++ b/examples/samplebinding/truck.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2018 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt for Python examples of the Qt Toolkit.
@@ -57,18 +57,14 @@ Truck::Truck(bool leaveOnDestruction) : m_leaveOnDestruction(leaveOnDestruction)
Truck::Truck(const Truck &other)
{
- for (size_t i = 0; i < other.m_flavors.size(); ++i) {
- addIcecreamFlavor(other.m_flavors[i]->clone());
- }
+ assign(other);
}
Truck &Truck::operator=(const Truck &other)
{
if (this != &other) {
- clearFlavors();
- for (size_t i = 0; i < other.m_flavors.size(); ++i) {
- addIcecreamFlavor(other.m_flavors[i]->clone());
- }
+ m_flavors.clear();
+ assign(other);
}
return *this;
}
@@ -81,20 +77,18 @@ Truck::~Truck()
{
if (m_leaveOnDestruction)
leave();
- clearFlavors();
}
void Truck::addIcecreamFlavor(Icecream *icecream)
{
- m_flavors.push_back(icecream);
+ m_flavors.push_back(IcecreamPtr(icecream));
}
void Truck::printAvailableFlavors() const
{
std::cout << "It sells the following flavors: \n";
- for (size_t i = 0; i < m_flavors.size(); ++ i) {
- std::cout << " * " << m_flavors[i]->getFlavor() << '\n';
- }
+ for (const auto &flavor : m_flavors)
+ std::cout << " * " << *flavor << '\n';
std::cout << '\n';
}
@@ -123,6 +117,13 @@ std::string Truck::getArrivalMessage() const
return m_arrivalMessage;
}
+void Truck::assign(const Truck &other)
+{
+ m_flavors.reserve(other.m_flavors.size());
+ for (const auto &f : other.m_flavors)
+ m_flavors.push_back(IcecreamPtr(f->clone()));
+}
+
bool Truck::deliver() const
{
std::random_device rd;
@@ -137,11 +138,3 @@ bool Truck::deliver() const
return result;
}
-
-void Truck::clearFlavors()
-{
- for (size_t i = 0; i < m_flavors.size(); ++i) {
- delete m_flavors[i];
- }
- m_flavors.clear();
-}
diff --git a/examples/samplebinding/truck.h b/examples/samplebinding/truck.h
index 742b232eb..e59b365a4 100644
--- a/examples/samplebinding/truck.h
+++ b/examples/samplebinding/truck.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2018 The Qt Company Ltd.
+** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt for Python examples of the Qt Toolkit.
@@ -51,14 +51,16 @@
#ifndef TRUCK_H
#define TRUCK_H
-#include <vector>
-
#include "icecream.h"
#include "macros.h"
-class BINDINGS_API Truck {
+#include <memory>
+#include <vector>
+
+class BINDINGS_API Truck
+{
public:
- Truck(bool leaveOnDestruction = false);
+ explicit Truck(bool leaveOnDestruction = false);
Truck(const Truck &other);
Truck& operator=(const Truck &other);
Truck(Truck &&other);
@@ -79,11 +81,13 @@ public:
std::string getArrivalMessage() const;
private:
- void clearFlavors();
+ using IcecreamPtr = std::shared_ptr<Icecream>;
+
+ void assign(const Truck &other);
bool m_leaveOnDestruction = false;
std::string m_arrivalMessage = "A new icecream truck has arrived!\n";
- std::vector<Icecream *> m_flavors;
+ std::vector<IcecreamPtr> m_flavors;
};
#endif // TRUCK_H