Since the beginnings of C++, classes are the basic building block of any self-respecting application. The combination of data and its relevant functionality heavily impacts the way we modularize the program, design APIs, construct and reuse the code - think of software in general.
In the previous era, C++ was a hammer for every nail, providing secure high-level abstractions without sacrifice of the superior performance. Yet during the past decades, competing technologies dethroned it out of many domains. As the language evolved to prevail its principal advantages, so did the way classes are programmed.
I am pleased to invite you to a speech about code construction and programming techniques used in programs and libraries now and then. Expect a lot of code samples!
8. 8
• The QObject class is the base class of all Qt objects.
• You can connect a signal to a slot with connect() and destroy the connection with
disconnect(). To avoid never ending notification loops you can temporarily block
signals with blockSignals(). The protected functions connectNotify() and
disconnectNotify() make it possible to track connections.
• QObjects organize themselves in object trees. When you create a QObject with
another object as parent, the object will automatically add itself to the parent's
children() list.
• Every object has an objectName() and its class name can be found via the
corresponding metaObject().
• Notice that the Q_OBJECT macro is mandatory for any object that implements signals,
slots or properties. You also need to run the Meta Object Compiler on the source file.
• Some QObject functions, e.g. children(), return a QObjectList. QObjectList is a
typedef for QList<QObject *>.
10. 10
// We need to call this to set up global state for TensorFlow.
tensorflow::port::InitMain(argv[0], &argc, &argv);
TensorFlow, Google
https://github.com/tensorflow/tensorflow
void InitMain(const char* usage, int* argc, char*** argv) {}
17. • To express optionality
• Some objects/classes mostly appear as pointers in the sources
• Arrays, C-strings, pointer arithmetics
• Explicit delete
• To express that the object is placed on the heap
• Pointers are mutable (unlike references)
• Pointers are small and fast to copy (unlike some objects)
• Because of C
Why *ptr ?
17
const auto* const formatPtr = encodingFormatter.format;
Expects(formatPtr != nullptr);
const auto& format = *formatPtr;
delete &format; // looks strange
45. 45
auto c = std::complex<float>{1.f, 0.f};
c.real(2.f);
assert(std::real(c) == c.real());
struct complex { float re, im; };
complex c = {1.f};
c = {2.f};
51. 51
btDiscreteDynamicsWorld* m_dynamicsWorld =
new btDiscreteDynamicsWorld(...);
...
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, ...);
btRigidBody* body = new btRigidBody(rbInfo);
body->setFriction(.5);
//add the body to the dynamics world
m_dynamicsWorld->addRigidBody(body);
Bullet Physics
https://github.com/bulletphysics/bullet3
52. namespace physics {
using RigidBodyRealmHook = boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
class RigidBody : public RigidBodyRealmHook
{
Realm& _realm;
public:
RigidBody(Realm& realm, bool makePresent = false) : _realm{realm} {
Present(makePresent);
}
bool Present() const { return is_linked(); }
void Present(bool present) {
if (present) {
if (!Present())
_realm._rigidBodies.push_back(*this);
}
else
unlink();
}
void Update() { /*...*/ }
};
class Realm {
using RigidBodyList = boost::intrusive::list<RigidBody,
boost::intrusive::constant_time_size<false>>;
RigidBodyList _rigidBodies;
public:
void UpdateAll() {
for (auto body : _rigidBodies)
body.Update();
}
friend class RigidBody;
};
} 52
auto realm = physics::Realm{};
auto ball = physics::RigidBody{realm};
ball.Present(true);
realm.UpdateAll();
ball.Present(false);
56. 56
2 5 6 8 10
Approach 1:
x is an index in a collection of N slots
Approach 2:
x is a value of an element in a collection
2 5 6 8 10
• Sequencial
• Vector
• List
• Associative
• Tree
• Hash table
62. Variadic templates and auto-lambdas to the rescue. (≥ C++14)
How to iterate over a set of values in compile-time?
62
template<typename IntT, typename Func, IntT... IS>
void foreach_constexpr(std::integer_sequence<IntT, IS...>,
Func func)
{
const IntT zeroes[] {
(func(std::integral_constant<IntT, IS>{}), 0)...
};
}
foreach_constexpr(std::make_integer_sequence<int, 4>{}, [](auto ic) {
constexpr auto i = ic();
...
});
63.
64. Twoja opinia na temat mojej prelekcji jest dla mnie bardzo ważna.
1. Wejdź w mój wykład znajdujący się w agendzie w aplikacji Eventory.
2. Oceń moją prelekcję i dodaj swój komentarz.
Dzięki temu będę wiedział, co Ci się podobało a co
powinienem ulepszyć!