Lecture thumbnail 0:01 / 6:38 Take assessment So one of those really interesting uses of the memento pattern is actually using it for interop.
So you might be wondering why exactly you would use this particular pattern for interop as opposed to
some of the other patterns.
So let me explain.
Let me show you a very simple schematic.
So we’re not going to do any code here.
We’re just going to explain.
I’m going to explain how this whole thing works.
Let’s suppose you have some unmanaged cplusplus code.
Let’s suppose this code could be virtually anything.
It could be native code, it could be something like Cuda, it can be something else.
It doesn’t really matter.
So you want to consume this code on the c sharp side and there are different ways of doing it.
There are actually tons of ways, for example, you can use.
Com, but the typical way is something called P invoke parallel invocation services.
And the reason for this is to allow people to very easily call C or C plus plus functions out of C sharp.
So the way this works is as follows.
Let’s suppose that you have some function f here.
Now what happens on the C sharp side is you have this external keyword, so you have extern static.
Void.
F with the same arguments as you would here.
So I’ll just put a dot here for now.
And then you would be able to.
You don’t put a body for this function, but you will be able to actually invoke this function.
And if it’s decorated with the appropriate DLL import, for example, if you’re on Windows, the import
attribute, what you can do is you can actually call into this function and pass parameters.
Now there is a bit of a problem in the C sharp C plus plus interop.
And this problem is that you can only pass in things like pointers, for example.
So if you have an array of data, say you have an array of 32 bit integers, then in C sharp if you
want to pass it into C plus plus, for example, what you do is you use the fixed keyword.
So you say something like, I’m going to have a fixed integer pointer called P initialized to whatever
actual array you’re working with.
And then you would pass this argument, P And you could pass it in here into the arguments which would
kind of forward it into the arguments here.
And then you can work with this memory.
And this only works for very simple types.
Now the problem is that sometimes you want to return an object.
So for example, here you might want to construct a person.
Which is a C plus plus class.
So it’s class person with, you know, some public members.
Some public members like age, for example, and you want to actually pass this and return it from some
function.
G So you have a function G which returns a person and you want to pass it here.
Unfortunately, you cannot do it directly.
You cannot simply make a class here.
Last person.
You cannot simply make a class which would replicate this class and have binary compatibility one with
the other.
So there are different approaches to this.
And one of them is you can try to get the structure layout just right, so you can try to build a struct
here which replicates all of the fields.
But then of course you might have something like a string or a W string here and then you’re really
in trouble because we cannot really put a string in here.
And have some sort of equivalence between the two.
That’s simply not how it works.
So what you can do as an alternative is you can use the memento pattern.
So whenever somebody calls G on this side, so whenever somebody has this, we still have this function
G.
It might be a person.
Token.
What you can do is you can actually store G on this side.
So if you have some cache of these GS, let’s say you have a vector or maybe a map, a map from, let’s
say person token to person, and that’s called cache.
What you do here is you make a new person.
You person.
You stick that person into the cash.
So you say something like cash at and then calculate some sort of hash code maybe for the person or
some sort of essentially the memento.
So here you make a new token.
So cash at token equals whatever this thing is.
So this goes in here and then what you do is whenever you have this invocation, let me just extend
the line here.
You actually return the token.
So you return the memento and that memento can go in here and that memento can be as simple as an integer.
So now the c sharp part has some sort of integer which refers to the person, but you cannot really
operate on it, meaning you don’t have the fields directly, you don’t have anything directly.
But once again, the upside of this is that if you want to perform some operations on the person, I
suppose at some point you want to pass the person back into the C plus plus side of things.
What you can do is you can take that token, you can take that token and here I’m saying it’s an integer
can be virtually anything that is serializable and easy to send across the boundary.
And you can actually pass that token in here and call some sort of API.
So if we have some sort of other API, let’s call it function Z, which actually takes a person token,
then what you can do is you can have a platform invocation here for Z, which takes exactly this thing,
whatever it is, and it actually invokes the appropriate DLL.
So the memento pattern is actually rather useful when you do interop because it lets you operate on
objects without actually having those objects as a kind of construct that you need to serialize across
the different boundaries.
So in this case, it’s on the one hand very inconvenient because you don’t have direct access to members
of person.
Like for example, if it has an H here, you cannot really access the H here.
But we can do is you can try and get the API to maybe return to you the H, so maybe you have more global
functions.
And unfortunately, yeah, these have to be global functions, all of them.
But if they are, then they can return certain information about the person.
That’s the best that we can do because unfortunately we are unable to serialize and send across the
boundaries entire objects because they’re binary incompatible.
Play Play Play Stop Start Play information alert