How to add a reference to table to userdata object? - reference

I have a function in lua that accepts a userdata object.
function Class:AttachToUserdataObject(userdataObject)
userDataObject.tableAttached = self
end
But later on, when I am using the same userdata object, I can't find it - userdataObject.tableAttached is nil. I feel like I don't fully understand the way userdata objects work yet.
Is there any way of binding the object to userdata other than creating a global table that has ids of all userdata objects (they have unique id) and references to tables?
I would like to keep it as elegant as I can, but without the access to C++ code I guess I can sacrifice some elegancy if it just works.

A userdata object does not have fields like a table and Lua has no knowledge whatsoever about internals of the underlying C object. In order to achieve what you want, you'd have to handle the __index and __newindex metamethods.
So, for example, when doing the assignment like userdataObject.tableAttached = self, the __newindex metamethod is triggered. Inside it, you could just store the value in the metatable itself (subject to a possible name collision) or in another table, which itself is stored in the metatable.
To retrieve the data back, you'd have to handle the __index metamethod. It can get a bit tricky with userdata, so let me know, if you run into problems.

You could use a backing weak table instead:
local _data = setmetatable({}, {__mode='k'})
function Class:AttachToUserdataObject(userdataObject)
_data[userDataObject] = self
end

Related

How to define a function to get an field element of a Marshmallow Schema while still serialising through a nested Schema

So I have a Nested Many Schema (eg Users) inside another Schema (eg Computer). My input object to be deserialised by the Schema is complex and does not allow for assignment, and to modify it to allow for assignment is impractical.
The input object (eg ComputerObject) itself does not contain an a value called "Users", but nested in a few other objects is a function that can get the users (eg ComputerObject.OS.Accounts.getUsers()), and I want the output of that function to be used as the value for that field in the schema.
Two possible solutions exist that I know of, I could either define the field as field.Method(#call the function here) or I could do a #post_dump function to call the function and add it to the final output JSON as it can provide both the initial object and the output JSON.
The issue with both of these is that it then doesn't serialise it through the nested Schema Users, which contains more nested Schemas and so on, it would just set that field to be equal to the return value of getUsers, which I don't want.
I have tried to define it in a pre-dump so that it can then be serialised in the dump (note: this schema is used only for dumping and not for loading), but as that takes in the initial object I cannot assign to it.
Basically, I have a thing I am trying to do, and a bunch of hacky workarounds that could make it work but not without breaking other things or missing out on the validation altogether, but no actual solution it seems, anybody know how to do this properly?
For further info, the object that is being input is a complex Django Model, which might give me some avenues Im not aware of, my Django experience is somewhat lacking.
So figured this out myself eventually:
Instead of managing the data-getting in the main schema, you can define the method used in the sub-schema using post_dump with many=True, thus the following code would work correctly:
class User(Schema):
id = fields.UUID
#pre_dump(pass_many=True)
def get_data(self, data, **kwargs):
data = data.Accounts.getUsers()
return data
class Computer(Schema):
#The field will need to be called "OS" in order to correctly look in the "OS" attribute for further data
OS = fields.Nested(User, many=True, data_key="users")

How to avoid creating objects to check or get content from Maps in Java

I am implementing my own Map in Java, using a custom class I made.
I already implemented the hashCode and equals without any problem.
I just have a question more related into performance and stuff like that.
So I will check many times in my application if a specific value is inside the map, for that, for that I have to create a object and then use the methods containsKey of Map.
My question is...
Is there any other way? without being always creating the object???
I cant have all the objects in my context universe, so that isn't a way...
I know I can just point the object to 'null' after using it, but still, it's not so elegant, creating objects just to check if there is the same object inside =S
Are there any other conventions?
Thank you very much in advance!
EDIT:
Stuff typed = new Stuff(stuff1, stuff2, (char) stuff3);
if(StuffWarehouse.containsKey(typed))
{
//do stuff
}
//after this I won't want to use that object again so...
typed = null;

Querying Azure TableServiceContext after adding object gives no result

In my application I am adding an entity to a TableServiceContext through the AddObject method. Later in the proces I want to query the TableServiceContext in order to retrieve this specific entity in order to update some properties, but the query doesn't give me a result. It will only give me a result if I do a SaveChanges immediately after the AddObject. This means that I have an extra roundtrip to the server. I would like to create and update the entity, and then call a SaveChanges to persist the entity to Azure Table Storage.
Does anyone know why I don't get a result when querying the context? Is there a way how to get the entity from the context without the extra call to SaveChanges?
Thanks
Ronald
Sounds like you are trying to Upsert here. Have you seen the support for InsertOrReplace? There is also InsertOrMerge, but I think you are looking to overwrite.
http://msdn.microsoft.com/en-us/library/hh452242.aspx
AddObject just starts tracking the object in local memory, so it makes sense to me that querying table storage for it doesn't return it. (Table storage has no knowledge of it.) I'd say that if you want to do this, you should just keep track of the new entity yourself.
I'm a bit confused as to the scenario. It sounds like you want something like this (won't work):
var foo = new Foo();
context.AddObject("foo", foo);
...
foo = context.CreateQuery<Foo>("foo").Where(...).Single();
foo.bar = "baz";
context.UpdateObject(foo);
context.SaveChanges();
Why not replace with this?
var foo = new Foo();
...
foo.bar = "baz";
context.AddObject("foo", foo);
What about your code makes you have to go through the TableServiceContext to get at the object you created?

Naming Vars with strings

Can a variable be named with a string or character array, in any language? Basically I want something like:
Var_String = "varname"
Var_String as double
And then I could fill the double varname.
If it helps im trying to make a program that can declare variables on the fly, while running. Even if thats not possible, I am open to workarounds even if they're impractical, although I would prefer that workarounds be in VB6, C++, or PHP, because I know those languages already, but they dont have to be.
Javascript is completely capable of declaring variable names on the fly. A javascript object can be treated "associatively" as a dictionary. Observe:
var testyObject = function()
{
Awesome = "hello";
};
var myObject = new testyObject();
alert(myObject.Awesome); // creates an alert window that says hello
alert(myObject['Awesome']); // the same as above
myObject[myObject.Awesome] = "woo!"; // We just created a property on the object with the name "hello"
alert(myObject.hello); // creates an alert window that says "woo!"
I also believe you can add them to your immediate scope rather than as properties on other objects by using this["whatever you want it named"] = "woo!"; but I'm not certain, someone can correct me on that if such does not work.
You can read more about associative arrays at http://www.quirksmode.org/js/associative.html
The usual way to do something like this is called a hash. You store name/value pairs and given the name, can look up its value. You can generally define them to store any sort of object. In fact, in some languages, objects themselves are essentially hashes with a few extra properties.
You can find more information on wikipedia: http://en.wikipedia.org/wiki/Hash_table

Pass a list of types, limiting the types to classes derived from a particular parent, in c#

I have a method on an object oriented database that creates tables based on their type.
I want to be able to send a list of types to get created, but i'm hoping to limit them to only classes derived from a specific base class (MyBase).
Is there a way i can require this in the method signature?
Instead of
CreateTables(IList<Type> tables)
Can i do something that would
CreateTables(IList<TypeWithBaseTypeMyBase> tables)
I know i could check the base class of each type sent over, but if possible i'd like this verified at compile time.
Any suggestions?
You could do the following:
CreateTables(IList<MyBase> tables)
{
// GetType will still return the original (child) type.
foreach(var item in tables)
{
var thisType = item.GetType();
// continue processing
}
}
Have you tried this?
CreateTables(IList<MyBase> tables)
I think that's all you have to do.
Why not just change the signature to:
CreateTables(IList<BaseType> tables)

Resources