Skip to content

Instantly share code, notes, and snippets.

@nboutin
nboutin / handling_system_for_stream_packet.cpp
Created February 17, 2023 09:12
Handling system for stream packet
enum class MessageType : char {
Add = 'A',
Modify = 'M',
Delete = 'D',
};
void on_message(const MessageHeader& hdr, const void* payload)
{
switch(hdr.type)
@nboutin
nboutin / solve_if-else_indentation_hell.cpp
Last active February 18, 2023 15:26
Solve if-else indentation hell
// Multiple level of nesting
void func()
{
if (...) {
if (...) {
...
} else {
if (...) {
...
} else if (...) {
// https://www.youtube.com/watch?v=nLSm3Haxz0I
class MyWidget
{
void tinker_with(int amount);
public:
class Tinkerable;
Tinkerable get_tinkerable(){
return Tinkerable(*this);
@nboutin
nboutin / raii_subscribe_unsubscribe.cpp
Created February 18, 2023 20:27
RAII subscribe unscribe
// https://www.youtube.com/watch?v=nLSm3Haxz0I
class EventSource
{
void add(Listener& listener);
void remove(Listener& listener);
public:
[[nodiscard]]
Subscription subscribe(Listener& listener) {
return Subscription(*this, listener); }
@nboutin
nboutin / dependency_injection.cpp
Last active February 22, 2023 13:24
Dependency injection
// https://www.youtube.com/watch?v=zYPb7oBU5_E
#include "time_interface.h"
#include "output_interface.h"
class alarm_clock {
public:
alarm_clock(const time_interface&, output_interface&)
// ...
private: