Latest / CyberSecurity Summary / Practical C++ Design: From Programming to Architecture
Transcript
- 0:00Speaker 1: If you write C + + code today, you have
- 0:02Speaker 1: access to, you know, some of the most powerful, high-level
- 0:04Speaker 1: features in computing.
- 0:06Speaker 2: Oh, absolutely. Things like smart pointers, lambda expressions, variadic templates.
- 0:12Speaker 1: Right. Tools that make your code incredibly robust. But what
- 0:16Speaker 1: if I told you that the second you try to
- 0:18Speaker 1: share that modern, beautiful code as a runtime plugin, you
- 0:22Speaker 1: just have to strip all.
- 0:23Speaker 2: Of it away? You basically have to pretend you're coding
- 0:26Speaker 2: in 1985. Right.
- 0:27Speaker 1: Yeah, exactly. Welcome to the brutal, uncompromising realities of software architecture.
- 0:32Speaker 2: It really is a harsh transition for a lot of developers.
- 0:35Speaker 1: I mean, the gap between writing a script that technically
- 0:38Speaker 1: functions and engineering a system that survives changing requirements, it's massive.
- 0:44Speaker 2: It's huge, yeah. You have to stop focusing purely on
- 0:47Speaker 2: syntax and start thinking about load-bearing walls and isolation boundaries.
- 0:51Speaker 1: And bridging that gap is exactly our mission today. Welcome
- 0:55Speaker 1: to our deep dive into Practical C + + Design
- 0:58Speaker 1: from Programming to Architecture by Adam B. Singer.
- 1:01Speaker 2: It's a great book.
- 1:02Speaker 1: It really is. So for you, the listener, whether you're
- 1:05Speaker 1: a cybersecurity student, a programming undergrad, or just a young professional,
- 1:09Speaker 1: we are going to break down how to architect maintainable,
- 1:12Speaker 1: secure software.
- 1:14Speaker 2: And the source material anchors this entire journey on one
- 1:17Speaker 2: single case study, which is building PDCalc. Right, PDCalc. It's
- 1:22Speaker 2: a stack-based... Reverse Polish Notation Calculator.
- 1:26Speaker 1: Which honestly serves as a brilliant Goldilocks case study. But
- 1:30Speaker 1: before we get to the architecture, we should probably define
- 1:33Speaker 1: what a reverse Polish notation or RPN calculator actually is.
- 1:36Speaker 2: Yeah, good idea, because it dictates all our design constraints.
- 1:40Speaker 1: Right. An RPN calculator doesn't use standard equations with parentheses
- 1:44Speaker 1: like we're used to.
- 1:45Speaker 2: No, the mechanics are entirely different. Instead of typing, say,
- 1:48Speaker 2: 3 plus 4 equals, you enter the operands first and
- 1:52Speaker 2: then the operator.
- 1:54Speaker 1: So you type 3, then you push it to a stack.
- 1:56Speaker 2: Exactly. You type 4, push it to the stack, then
- 2:00Speaker 2: you press plus.
- 2:01Speaker 1: And then the calculator just pops the last two numbers
- 2:03Speaker 1: off the stack, adds them, and pushes the result, 7,
- 2:06Speaker 1: back onto the stack.
- 2:07Speaker 2: Yeah, it sounds a bit retro, but, you know, I
- 2:10Speaker 2: see why it's the perfect testbed.
- 2:12Speaker 1: It's complex enough to require real architectural heavy lifting, right?
- 2:15Speaker 1: Like unlimited undo and redo, graphical user interface, runtime plugins.
- 2:21Speaker 2: But the domain itself is just basic math.
- 2:24Speaker 1: Exactly. We don't have to waste time learning obscure financial
- 2:26Speaker 1: algorithms just to understand the C + + design.
- 2:29Speaker 2: Right. The focus remains purely on structural patterns. And the
- 2:33Speaker 2: first step in any robust architecture is decomposition.
- 2:37Speaker 1: Okay, let's unpack this. Before we write a single line
- 2:40Speaker 1: of C + +, we have to build our blueprint. Yes.
- 2:43Speaker 2: You have to responsibly decide what pieces you actually need
- 2:46Speaker 2: to build this calculator.
- 2:48Speaker 1: So how do we do that?
- 2:49Speaker 2: Well, a professional decomposition relies on four principles. You've got modularity. encapsulation, cohesion,
- 2:56Speaker 2: and low coupling.
- 2:58Speaker 1: Okay, so you break the system into independent parts. Right.
- 3:01Speaker 2: You hide their internal states. You ensure each module does
- 3:05Speaker 2: exactly one thing, which is cohesion.
- 3:08Speaker 1: And you minimize dependencies, so low coupling.
- 3:11Speaker 2: Exactly. And for a cybersecurity student, you can map this
- 3:15Speaker 2: directly to isolating subsystems.
- 3:17Speaker 1: Oh, right, because if an attacker breaches the UI layer,
- 3:20Speaker 1: low coupling and strict encapsulation prevent them from easily pivoting
- 3:23Speaker 1: into the core data models.
- 3:25Speaker 2: You're limiting the blast radius right from day one.
- 3:27Speaker 1: That makes a lot of sense.
- 3:28Speaker 2: So looking at PDCalc, the text evaluates two main architectural models, right?
- 3:34Speaker 1: Yeah, the multi-tiered architecture and the model view controller, or MVC.
- 3:38Speaker 2: Let's break those down.
- 3:39Speaker 1: Okay.
- 3:40Speaker 2: Well, in a standard multi-tiered architecture, communication is strictly linear.
- 3:44Speaker 2: The view, which is your user interface, talks to the
- 3:47Speaker 2: logic layer.
- 3:48Speaker 1: And then the logic layer talks to the data layer.
- 3:51Speaker 2: Exactly. The UI and the data are completely walled off
- 3:54Speaker 2: from one another.
- 3:55Speaker 1: Got it. And MVC?
- 3:58Speaker 2: MVC, by contrast, introduces triangular communication. The view in the model,
- 4:02Speaker 2: which is our data, can interact directly while the controller
- 4:05Speaker 2: handles the business logic.
- 4:07Speaker 1: So a multi-tiered architecture feels kind of like a traditional restaurant.
- 4:11Speaker 1: You sit at the table, you give your order to
- 4:13Speaker 1: the waiter, who's the logic layer, and the waiter goes
- 4:16Speaker 1: to the kitchen to talk to the chef, the data layer. Right.
- 4:19Speaker 2: You never step foot in the kitchen yourself.
- 4:22Speaker 1: But MVC is more like a sushi bar. You can
- 4:24Speaker 1: just talk to the chef directly when it makes sense.
- 4:27Speaker 2: That's a really good way to visualize it.
- 4:29Speaker 1: But bypassing the logic layer seems risky, doesn't it? If
- 4:32Speaker 1: the UI can just reach into the data stack, doesn't
- 4:35Speaker 1: that create a massive vulnerability for state corruption?
- 4:38Speaker 2: What's fascinating here is that MVC accommodates this safely by
- 4:42Speaker 2: enforcing strict, use-case-driven interfaces.
- 4:46Speaker 1: How so?
- 4:47Speaker 2: Think about how an RPN calculator is actually used. The
- 4:50Speaker 2: user constantly needs to see the intermediate numbers sitting on
- 4:53Speaker 2: the stack, not just the final result.
- 4:56Speaker 1: Oh, right. They need to see all the numbers they've pushed.
- 4:59Speaker 2: Exactly. If we used a linear, multi-tiered approach, the controller
- 5:03Speaker 2: would be forced to constantly fetch, translate, and pass that
- 5:07Speaker 2: stack data to the UI.
- 5:08Speaker 1: Which turns the controller into a glorified messenger.
- 5:12Speaker 2: Yes, and it clutters it with tasks totally unrelated to
- 5:15Speaker 2: its main job of executing math.
- 5:17Speaker 1: It creates a bottleneck.
- 5:19Speaker 2: It really does. So instead, the architecture defines a read-only,
- 5:23Speaker 2: event-driven interface.
- 5:25Speaker 1: Okay, meaning the view is allowed to observe the model,
- 5:28Speaker 1: but it cannot alter it.
- 5:30Speaker 2: Exactly. It safely monitors the data without compromising the integrity
- 5:34Speaker 2: of the stack.
- 5:35Speaker 1: So our MVC sushi bar is open now. The blueprint
- 5:38Speaker 1: is set, but now we have to actually pour the foundation.
- 5:42Speaker 2: And for an RPN calculator, the model is the stack itself.
- 5:45Speaker 1: Right, and this presents our first major C + +
- 5:48Speaker 1: design decision.
- 5:49Speaker 2: Yeah, it does. The standard template library, the STL, it
- 5:53Speaker 2: provides a perfectly functional container called standard.stack.
- 5:57Speaker 1: So we just use that, right?
- 5:59Speaker 2: Well, the amateur move is to just inherit from it
- 6:02Speaker 2: or use it directly. But Singer's design specifically avoids this.
- 6:06Speaker 1: Wait, why?
- 6:07Speaker 2: Instead, we implement the adapter pattern. We build our own
- 6:11Speaker 2: custom stack class that internally wraps a completely different container.
- 6:15Speaker 1: Which container?
- 6:16Speaker 2: Standard.deck, which is a double-ended queue.
- 6:18Speaker 1: I'd assume standard stack containers are highly optimized. Why choose
- 6:22Speaker 1: a double-ended queue?
- 6:23Speaker 2: It goes back to our core use case. The standard standard.stack
- 6:26Speaker 2: in C + + only allows you to view the
- 6:29Speaker 2: top element.
- 6:30Speaker 1: Oh, you can't see the rest of the items.
- 6:31Speaker 2: No, you cannot iterate through the rest of them. But
- 6:34Speaker 2: our UI needs to display the last five or ten
- 6:37Speaker 2: intermediate results.
- 6:38Speaker 1: So a standard. dec allows for non-destructive iteration through all
- 6:42Speaker 1: the elements? Yes.
- 6:43Speaker 2: Well, our custom wrapper class mathematically enforces stack-like push and
- 6:47Speaker 2: pop behavior on top of it.
- 6:49Speaker 1: Okay, that covers the data structure. But how are we
- 6:51Speaker 1: managing this instance?
- 6:53Speaker 2: We implement this custom stack as a singleton.
- 6:55Speaker 1: Because there should only ever be one mathematical core state, right?
- 6:59Speaker 2: By definition, a singleton restricts the instantiation of a class
- 7:02Speaker 2: to one single object. So, yes.
- 7:05Speaker 1: And then we pair this with the observer pattern.
- 7:08Speaker 2: We do. Instead of the UI constantly polling the stack
- 7:11Speaker 2: and asking, did you change? Did you change? The stack
- 7:14Speaker 2: maintains a list of its observers.
- 7:16Speaker 1: Ah, so whenever a number is pushed or popped, the
- 7:20Speaker 1: stack just broadcasts an event to the UI.
- 7:22Speaker 2: Exactly. It's much more efficient.
- 7:24Speaker 1: And from a memory management perspective, how are we keeping
- 7:27Speaker 1: this secure? Because in C + +, memory leaks are just,
- 7:30Speaker 1: you know, the stuff of nightmares.
- 7:31Speaker 2: The architecture leans heavily into modern C + + paradigms,
- 7:35Speaker 2: specifically unique underscore PTR.
- 7:38Speaker 1: That's crucial for security, isn't it?
- 7:40Speaker 2: Absolutely. A unique underscore PTR enforces strict, exclusive ownership of
- 7:45Speaker 2: dynamically allocated memory. It cannot be copied, only moved.
- 7:49Speaker 1: So when the pointer goes out of scope, the memory
- 7:51Speaker 1: is automatically destroyed.
- 7:53Speaker 2: Right. You eliminate dangling pointers and memory leaks just by design.
- 7:57Speaker 1: I want to push back on the adapter pattern we
- 7:59Speaker 1: used earlier, though.
- 8:00Speaker 2: Okay, go ahead.
- 8:01Speaker 1: If the standard library provides well-tested containers, isn't wrapping them
- 8:06Speaker 1: in our own custom class just adding boilerplate? It feels
- 8:10Speaker 1: like we're adding a layer of abstraction just for the
- 8:12Speaker 1: sake of it.
- 8:13Speaker 2: It is not boilerplate, actually. It acts as an interface guard.
- 8:17Speaker 1: What do you mean by that?
- 8:18Speaker 2: When you wrap vendor code, even the C + +
- 8:20Speaker 2: standard library, you are protecting the rest of your application
- 8:23Speaker 2: from future turbulence.
- 8:25Speaker 1: Okay, give me an example.
- 8:26Speaker 2: Imagine that five years from now, you discover std.dq just
- 8:30Speaker 2: isn't performant enough for a new feature, and you want
- 8:33Speaker 2: to swap it out for a proprietary data structure.
- 8:36Speaker 1: Oh, I see, because you use the adapter pattern, you
- 8:39Speaker 1: only modify the code hidden inside your wrapper.
- 8:41Speaker 2: Exactly. The rest of the application remains completely untouched because
- 8:45Speaker 2: it only knows about your custom interface. You limit the
- 8:48Speaker 2: blast radius of change.
- 8:50Speaker 1: It's a firewall against future technical debt. I see the
- 8:53Speaker 1: value in that.
- 8:53Speaker 2: Yeah, it's really powerful.
- 8:55Speaker 1: So the model is locked down. It is safely storing data.
- 8:58Speaker 1: But we have a new problem.
- 8:59Speaker 2: What's that?
- 9:00Speaker 1: How does the user actually hand their math operations to
- 9:04Speaker 1: the engine without messing up the underlying state, especially when
- 9:07Speaker 1: they make a mistake and need to hit undo?
- 9:09Speaker 2: That dilemma is exactly what the command dispatcher solves, utilizing
- 9:14Speaker 2: the command pattern.
- 9:15Speaker 1: How does the command pattern work here?
- 9:17Speaker 2: Instead of treating a math operation as a transient function call,
- 9:21Speaker 2: like firing off an addition function and just forgetting about it,
- 9:25Speaker 2: we encapsulate the request as an object.
- 9:28Speaker 1: Here's where it gets really interesting. The command pattern essentially
- 9:31Speaker 1: turns a verb into a noun.
- 9:34Speaker 2: Yes.
- 9:34Speaker 1: It's like a restaurant order ticket. The ticket is a
- 9:37Speaker 1: physical object that can be passed to the kitchen, delayed,
- 9:40Speaker 1: stored on a spindle, or just completely voided.
- 9:43Speaker 2: That analogy hits the mark perfectly. Because every action, addition, subtraction,
- 9:48Speaker 2: sine wave, is instantiated as an object, we can push
- 9:51Speaker 2: them into a history list.
- 9:53Speaker 1: Which is the mechanical secret behind unlimited undo and redo functionality.
- 9:58Speaker 2: Exactly. If a user wants to undo an addition, the
- 10:01Speaker 2: dispatcher retrieves the addition command object from the history list
- 10:05Speaker 2: and calls its internal undo method.
- 10:07Speaker 1: And that just reverses the operation on the stack. Right.
- 10:10Speaker 1: But the execution of those commands has to be foolproof.
- 10:14Speaker 1: If a user tries to divide by zero, the.
- 10:18Speaker 2: Brings us to the non-virtual interface or NVI pattern. It
- 10:22Speaker 2: operates like a strict gatekeeper.
- 10:25Speaker 1: How does that work in practice?
- 10:27Speaker 2: In the base command class, you define a public non-virtual
- 10:31Speaker 2: execute function. This is the only doorway the outside world
- 10:34Speaker 2: can use to trigger a command.
- 10:36Speaker 1: Okay, but internally?
- 10:38Speaker 2: Internally, that public function first calls a private virtual function
- 10:42Speaker 2: named check preconditions IMPL.
- 10:46Speaker 1: So the derived classes, like the division command, override that
- 10:49Speaker 1: private function with their specific checks.
- 10:51Speaker 2: Yes, exactly. The base class forces the preconditions to run.
- 10:56Speaker 2: The division command checks for a zero denominator.
- 10:59Speaker 1: And only if those preconditions pass does the base class
- 11:02Speaker 1: allow the actual mathematical execution to proceed. Right.
- 11:06Speaker 2: It guarantees a secure execution sequence that derived classes literally
- 11:10Speaker 2: cannot accidentally bypass or forget to implement.
- 11:14Speaker 1: That's super smart. But why not just use standard C
- 11:18Speaker 1: + + exceptions?
- 11:20Speaker 2: What do you mean?
- 11:21Speaker 1: Like if the division command sees a zero, just throw
- 11:23Speaker 1: an exception and let the UI catch it and display
- 11:26Speaker 1: an error box.
- 11:27Speaker 2: Because of the architectural boundaries we set up with MVC,
- 11:31Speaker 2: the user interface and the controller communicate via asynchronous events,
- 11:34Speaker 2: not synchronous function calls.
- 11:37Speaker 1: Oh, I see. Yeah.
- 11:38Speaker 2: If a command throws a standard C + + exception
- 11:41Speaker 2: deep in the logic layer... It has no safe trajectory
- 11:44Speaker 2: across those asynchronous boundaries.
- 11:46Speaker 1: The event loop would lose context and the application would
- 11:49Speaker 1: likely just terminate.
- 11:50Speaker 2: Exactly. Event-driven error handling where the command catches its own
- 11:54Speaker 2: failure and broadcasts an error event for the UI to
- 11:57Speaker 2: interpret is vastly safer in a decoupled architecture.
- 12:01Speaker 1: You keep the failure contained locally and just send a
- 12:03Speaker 1: message saying, hey, something went wrong.
- 12:06Speaker 2: Precisely.
- 12:07Speaker 1: That makes complete sense. All right. We have a safe engine.
- 12:11Speaker 1: We have data. We have commands. Now we need to
- 12:13Speaker 1: expose this to the user visually.
- 12:15Speaker 2: Right, the GUI.
- 12:17Speaker 1: But we need to do it without exposing our private
- 12:19Speaker 1: code structures to the compiler or to other modules.
- 12:22Speaker 2: Well, the source text builds the graphical user interface entirely
- 12:26Speaker 2: programmatically using the QT framework.
- 12:29Speaker 1: So no visual drag-and-drop builders.
- 12:31Speaker 2: No, none of that. But the standout technique used to
- 12:34Speaker 2: protect the application's internal structure is the PIMPL idiom.
- 12:39Speaker 1: PIMPL, P-I-M-P-L. pointer to implementation.
- 12:44Speaker 2: That's the one.
- 12:45Speaker 1: I've heard developers talk about this as a way to
- 12:47Speaker 1: speed up massive code bases, but how does it actually
- 12:50Speaker 1: work under the hood?
- 12:51Speaker 2: It solves one of the biggest headaches in C + +,
- 12:54Speaker 2: compilation dependencies. Normally, you declare your private class variables in
- 12:58Speaker 2: your header file.
- 12:59Speaker 1: Right.
- 13:00Speaker 2: But here's the flaw. If you add or change a
- 13:03Speaker 2: private variable, the compiler sees that the header file has changed.
- 13:07Speaker 1: Which means every single other file in your entire project
- 13:10Speaker 1: that includes that header must be recompiled.
- 13:12Speaker 2: And in a project with millions of lines of code,
- 13:15Speaker 2: that ripple effect could trigger a compilation that takes hours
- 13:18Speaker 2: just because you changed one private integer.
- 13:20Speaker 1: That sounds miserable.
- 13:22Speaker 2: So how does Pimple fix it? To stop that, you
- 13:26Speaker 2: use the Pimple idiom. In your header file, you don't
- 13:29Speaker 2: list any of your private variables.
- 13:31Speaker 1: What do you list instead?
- 13:32Speaker 2: Instead, you declare a single opaque pointer that points to
- 13:35Speaker 2: a forward-declared struct called the implementation.
- 13:39Speaker 1: Oh, okay.
- 13:40Speaker 2: You hide all the actual private variables inside that struct,
- 13:43Speaker 2: which lives exclusively in your. cpp source file.
- 13:47Speaker 1: So the compiler looks at the header file, sees a pointer,
- 13:50Speaker 1: knows exactly how much memory a pointer takes up, and
- 13:52Speaker 1: just moves on.
- 13:53Speaker 2: Exactly. It doesn't need to know what's inside the struct
- 13:57Speaker 2: until it compiles the source file.
- 13:58Speaker 1: You've completely isolated the private data.
- 14:02Speaker 2: When you change a private variable inside the. cpp file,
- 14:05Speaker 2: the header remains completely unchanged. Compilation times just plummet because
- 14:10Speaker 2: the ripple effect is eliminated.
- 14:12Speaker 1: And from a cybersecurity and encapsulation mindset, your private data
- 14:16Speaker 1: is entirely invisible to the rest of the system.
- 14:18Speaker 2: Yes. The header file reveals absolutely nothing about how you
- 14:22Speaker 2: manage your internal state.
- 14:23Speaker 1: So we've hidden the engine, but building the actual GUI
- 14:26Speaker 1: window brings up another notorious C + + issue.
- 14:29Speaker 2: Oh, you mean the multiple inheritance?
- 14:32Speaker 1: Yeah. The implementation here uses multiple inheritance. It inherits from
- 14:36Speaker 1: both Qt's Q main window and our custom user interface
- 14:40Speaker 1: base class.
- 14:41Speaker 2: It does.
- 14:42Speaker 1: Isn't multiple inheritance the ultimate taboo? I thought it leads
- 14:45Speaker 1: to the diamond of death.
- 14:47Speaker 2: It's considered taboo when used recklessly. The diamond of death
- 14:51Speaker 2: occurs when a class derives from two different base classes
- 14:54Speaker 2: that themselves share a common ancestor.
- 14:57Speaker 1: Right, so if both intermediate classes modify a variable from
- 15:00Speaker 1: the root class, the compiler gets confused about which version
- 15:03Speaker 1: of the variable the final derived class should use.
- 15:06Speaker 2: Exactly. The ambiguity causes a fatal compilation error.
- 15:09Speaker 1: But we avoid that here.
- 15:11Speaker 2: Completely. We avoid it because the classes we're inheriting from
- 15:15Speaker 2: are strictly orthogonal.
- 15:17Speaker 1: Meaning they don't overlap in functionality at all. Right.
- 15:21Speaker 2: QMainWindow is a massive framework class that exclusively handles OS-level windowing, rendering,
- 15:27Speaker 2: and mouse clicks.
- 15:28Speaker 1: And our UserInterfaceBase class?
- 15:31Speaker 2: It's a pure abstract interface. It contains no state, no variables,
- 15:34Speaker 2: no implementation. It is merely a list of pure virtual functions,
- 15:40Speaker 2: defining how math callbacks should look.
- 15:43Speaker 1: Oh, I see. Because they don't share any state, variables,
- 15:45Speaker 1: or common ancestry, there's no ambiguity.
- 15:48Speaker 2: Exactly. It's the exact right tool for combining framework plumbing
- 15:51Speaker 2: with domain logic.
- 15:53Speaker 1: Okay, so the core app is built, it's fast, it's decoupled,
- 15:56Speaker 1: and it's secure.
- 15:58Speaker 2: But we're not done.
- 15:59Speaker 1: No, because now the stakeholders want to open it up.
- 16:02Speaker 1: They want third-party developers to be able to add new features, maybe...
- 16:05Speaker 1: a trigonometry module, without recompiling the core application.
- 16:08Speaker 2: We need runtime plugins.
- 16:11Speaker 1: This is the ultimate test of C + + architecture,
- 16:13Speaker 1: isn't it?
- 16:14Speaker 2: It really is. Loading dynamically linked libraries,. dll files on
- 16:19Speaker 2: Windows or. so files on POSIX systems, dynamically, at runtime.
- 16:24Speaker 1: And the rules for crossing that application binary interface, the ABI,
- 16:29Speaker 1: boundary between a compiled host application and a compiled plugin,
- 16:33Speaker 1: they are brutally restrictive.
- 16:35Speaker 2: Because the host app and the plugin might be built
- 16:37Speaker 2: in entirely different environments, right?
- 16:39Speaker 1: Yeah. They might be built by different compilers, different versions
- 16:42Speaker 1: of the same compiler, or using completely different standard library implementations.
- 16:47Speaker 2: Because of this chaos, you have to follow strict rules.
- 16:51Speaker 2: Rule number one, memory allocated in a plugin must be
- 16:54Speaker 2: deallocated in the same plugin.
- 16:57Speaker 1: What happens if you don't?
- 16:58Speaker 2: If the plugin uses its memory manager to allocate an object,
- 17:02Speaker 2: and the main app uses its memory manager to delete it,
- 17:05Speaker 2: The heap can become corrupted and the system crashes.
- 17:08Speaker 1: But wait, earlier we said we were passing command objects
- 17:11Speaker 1: around using unique PTR.
- 17:13Speaker 2: We did.
- 17:13Speaker 1: When the main app's unique PTR goes out of scope,
- 17:16Speaker 1: it automatically calls delete. How do we stop it from
- 17:19Speaker 1: crashing the system if that object came.
- 17:20Speaker 2: From a plugin? By utilizing a custom deleter.
- 17:23Speaker 1: How does that work?
- 17:24Speaker 2: When we create the unique PTR, we inject a specific
- 17:27Speaker 2: deallocation function provided by the plugin. Oh, wow.
- 17:31Speaker 1: So when the main app's pointer goes out of scope,
- 17:33Speaker 1: it doesn't use the standard delete.
- 17:35Speaker 2: Nope. It calls the plug-in's function, ensuring the memory is
- 17:39Speaker 2: cleaned up on the correct side of the fence.
- 17:41Speaker 1: That's a really clever workaround. Okay, so rule number one
- 17:45Speaker 1: is memory isolation. What about the actual data we're passing
- 17:48Speaker 1: back and forth? I assume I can't just toss a
- 17:51Speaker 1: standard C + + vector or string over the wall?
- 17:54Speaker 2: You absolutely cannot. That is rule number two. No STL
- 17:58Speaker 2: containers can cross the boundary.
- 17:59Speaker 1: Because of the different memory layouts?
- 18:01Speaker 2: Exactly. The memory layout of a standard. vector in the
- 18:05Speaker 2: Microsoft compiler is completely different from the GNU compiler on Linux.
- 18:10Speaker 1: If you pass one across the boundary, the application will
- 18:12Speaker 1: misinterpret the memory and psych fault.
- 18:14Speaker 2: You must drop down to raw C-style arrays and character pointers.
- 18:18Speaker 1: So we're just abandoning all the modern conveniences.
- 18:20Speaker 2: We have to. Rule 3 requires all functions crossing the
- 18:24Speaker 2: boundary to use extern C linkage.
- 18:27Speaker 1: That prevents C + + name mangling, right? Where the
- 18:29Speaker 1: compiler scrambles function names to support overloading?
- 18:32Speaker 2: Exactly. It stops that from hiding the function signatures. And
- 18:36Speaker 2: rule four, no C + + exceptions can cross the boundary.
- 18:40Speaker 1: So what do you do if there's an error?
- 18:43Speaker 2: You must use the noexcept keyword, catch all errors internally
- 18:47Speaker 2: within the plugin, and return standard integer error codes.
- 18:51Speaker 1: To actually load these wildly different. dll and. so files
- 18:55Speaker 1: across operating systems, the architecture uses the abstract factory pattern, correct?
- 19:01Speaker 2: Yes. The abstract factory hides the platform's specific API calls.
- 19:06Speaker 2: It dynamically loads the correct file type based on the
- 19:08Speaker 2: OS at runtime.
- 19:10Speaker 1: Which keeps the core code base clean and free of messy,
- 19:13Speaker 1: hard-to-read preprocessor directives.
- 19:16Speaker 2: Exactly.
- 19:17Speaker 1: So what does this all mean for the modern developer?
- 19:20Speaker 1: I mean, we have all these beautiful, high-level, modern C
- 19:22Speaker 1: + + features, and at the boundary, we have to
- 19:25Speaker 1: strip it all away and write code like it's 1985.
- 19:27Speaker 2: If we connect this to the bigger picture... You have
- 19:32Speaker 2: to conceptualize the boundaries between compiled binaries exactly like international borders.
- 19:37Speaker 1: Okay. I like this. Go on.
- 19:40Speaker 2: Inside your own country, inside your plug-in, you can speak
- 19:43Speaker 2: whatever local language you want. You can use lambda expressions,
- 19:46Speaker 2: smart pointers, complex template metaprogramming.
- 19:49Speaker 1: Bye. at the border checkpoint.
- 19:51Speaker 2: At the checkpoint, the API boundary. Everyone has to put
- 19:54Speaker 2: away their local dialects and use the agreed-upon international standard.
- 19:58Speaker 1: And in software architecture, that universal standard is C linkage
- 20:02Speaker 1: and raw memory pointers.
- 20:04Speaker 2: It is the only way to ensure safe passage between
- 20:07Speaker 2: sovereign territories.
- 20:08Speaker 1: International borders for code. I will definitely remember that. Okay,
- 20:13Speaker 1: our Plurgan system proves the architecture can handle external extensions.
- 20:17Speaker 2: It does.
- 20:17Speaker 1: But what happens when the inevitable happens? A client asks
- 20:21Speaker 1: for a feature that fundamentally changes how we thought the
- 20:24Speaker 1: system would be used.
- 20:25Speaker 2: They always do.
- 20:26Speaker 1: Right. They want stored procedures, the ability to execute a
- 20:30Speaker 1: whole batch of commands at once.
- 20:33Speaker 2: This is where rigid architecture shatters, and a robust one flexes.
- 20:38Speaker 1: If the initial design was heavily coupled, adding batch processing
- 20:42Speaker 1: would require rewriting the entire command dispatcher, wouldn't it?
- 20:45Speaker 2: Yes. But because we encapsulated every action inside the command pattern,
- 20:50Speaker 2: we can seamlessly introduce the composite pattern.
- 20:53Speaker 1: Walk us through how the composite pattern solves this.
- 20:57Speaker 2: We create a new stored procedure class that inherits from
- 21:00Speaker 2: our base command class. To the dispatcher, it looks and
- 21:03Speaker 2: acts exactly like a single addition or subtraction command.
- 21:07Speaker 1: It has an execute function and an undo function?
- 21:10Speaker 2: Exactly. But internally, this class contains a list of other
- 21:14Speaker 2: command objects.
- 21:16Speaker 1: Oh, so it's a container masquerading as a single command. Right.
- 21:21Speaker 1: It reminds me of a macro key on a gaming keyboard.
- 21:23Speaker 1: You press one key, but it rapidly executes an entire
- 21:27Speaker 1: complex combo of moves as if it were a single keystroke.
- 21:30Speaker 2: And because of that nested structure, implementing the undo function
- 21:34Speaker 2: for a stored procedure is almost effortless.
- 21:37Speaker 1: How so?
- 21:37Speaker 2: The undo function simply loops through its internal list of
- 21:40Speaker 2: commands in reverse order, calling undo on each one. The
- 21:44Speaker 2: architecture naturally absorbs the complexity.
- 21:47Speaker 1: But constructing these stored procedures mechanically sounds incredibly difficult. A
- 21:52Speaker 1: stored procedure could contain literally any combination of commands. Right.
- 21:56Speaker 2: Two additions, a sine wave, a division.
- 21:58Speaker 1: Normally, a constructor needs a fixed number of arguments of
- 22:01Speaker 1: known types. How do you write a factory that can
- 22:04Speaker 1: dynamically build commands from unknown parameters?
- 22:08Speaker 2: This is where Singer leverages one of the most powerful
- 22:10Speaker 2: features in modern C + +, the generic perfect forwarding constructor,
- 22:15Speaker 2: utilizing variadic templates.
- 22:17Speaker 1: Variadic templates.
- 22:19Speaker 2: Explain the mechanics of that, because it sounds intimidating. Think
- 22:24Speaker 2: of variadic templates like a set of Matryoshka nesting dolls. Okay.
- 22:28Speaker 2: A variadic template allows a single function to accept an unknown,
- 22:31Speaker 2: arbitrary number of arguments of entirely different types.
- 22:35Speaker 1: So you pass these arguments into the factory, and then what?
- 22:38Speaker 2: The compiler recursively unpacks them, one by one at compile time.
- 22:43Speaker 2: It peels back a layer, perfectly preserving the exact type
- 22:46Speaker 2: of each argument, whether it's an integer, a string, or
- 22:49Speaker 2: a custom object.
- 22:51Speaker 1: And it hands those exact pieces directly to the constructor
- 22:54Speaker 1: of whatever specific command object you're building.
- 22:56Speaker 2: Exactly. It gives you the dynamic flexibility to build endless
- 23:00Speaker 2: combinations of commands without sacrificing C + +' s strict
- 23:03Speaker 2: compile-time type safety.
- 23:05Speaker 1: That is just phenomenal. You get the flexibility of a
- 23:08Speaker 1: dynamic language, but with the armor of a statically typed one.
- 23:11Speaker 2: It's incredible.
- 23:13Speaker 1: Let's step back and recap this journey. We started with
- 23:16Speaker 1: high-level decomposition, selecting the MVC architecture to ensure our UI
- 23:20Speaker 1: could safely observe the stack without cluttering our logic layer.
- 23:23Speaker 2: Right.
- 23:24Speaker 1: We dropped down into the trenches to enforce memory safety
- 23:27Speaker 1: using the adapter pattern and smart pointers. We locked down
- 23:30Speaker 1: our interfaces with the pimple idiom. And then we navigated
- 23:34Speaker 1: the absolute minefield of the C + + ABI using
- 23:38Speaker 1: strict international border rules.
- 23:40Speaker 2: And finally, we future-proofed the entire engine with a composite
- 23:43Speaker 2: pattern and the recursive power of variadic templates.
- 23:46Speaker 1: It's a master class in treating code not just as
- 23:49Speaker 1: a set of instructions, but as a structural engineering discipline.
- 23:52Speaker 2: It really is.
- 23:53Speaker 1: So, as an educational exercise for you, the learner, I
- 23:57Speaker 1: want you to pose a scenario to yourself based on
- 23:59Speaker 1: what we've covered today.
- 24:01Speaker 2: This is a good exercise.
- 24:02Speaker 1: If you were asked to add a completely new graphing
- 24:05Speaker 1: module to PDCalc today, which architectural pattern would you use
- 24:09Speaker 1: to ensure the core mathematical engine doesn't get completely tangled
- 24:12Speaker 1: up in the pixel rendering logic?
- 24:14Speaker 2: Think deeply about how the observer pattern we discussed earlier
- 24:18Speaker 2: might keep those rendering layers safely decoupled from the core
- 24:21Speaker 2: data model.
- 24:22Speaker 1: And as you map out that graphing module, this raises
- 24:25Speaker 1: an important question.
- 24:26Speaker 2: Yeah, we saw how our architecture evolved naturally absorb the
- 24:30Speaker 2: surprise requirement of stored procedures. But if software requirements always
- 24:36Speaker 2: change and users always demand features you didn't anticipate, is
- 24:40Speaker 2: it ever truly possible to have a perfect initial design?
- 24:44Speaker 1: That's a tough one.
- 24:45Speaker 2: Or is software architecture simply the fine art of managing, mitigating,
- 24:49Speaker 2: and predicting future technical debt?
- 24:52Speaker 1: That is the real challenge of the job. Because at
- 24:55Speaker 1: the end of the day, you can always hack together
- 24:57Speaker 1: a script that technically works, But if you want your
- 24:59Speaker 1: code to stand tall, weather the storms of changing requirements,
- 25:02Speaker 1: and not collapse under its own weight, you have to
- 25:06Speaker 1: respect the blueprint.
- 25:07Speaker 2: Well said.
- 25:08Speaker 1: Thanks for joining this deep dive. Keep building and keep designing.