Setting variable typename after declaration

Started by
7 comments, last by Acar 8 years, 9 months ago

Hi guys, so here's what i got:

a Table class with std::vector<Row> variable
a Row class with std::vector<Data> variable
a Data template class

Now as you might have noticed std::vector<Data> won't work without adding <anytypename> after Data. What i wanna do is to read table data type from file and create a table with according data type such as unsigned long int/string/string or string/char or whatever is in the file. Is it possible ? Thanks.

Advertisement

Now as you might have noticed std::vector<Data> won't work without adding <anytypename> after Data. What i wanna do is to read table data type from file and create a table with according data type such as unsigned long int/string/string or string/char or whatever is in the file. Is it possible ? Thanks.


If you're wanting a variable type ("Data") to be able to hold multiple different types of data, you'll need to create a special class/struct that can handle those types of data.
You could look into boost::any, or similar classes.

a Table class with std::vector<Row> variable
a Row class with std::vector<Data> variable
a Data template class

A 2D table is usually a two-dimensional square/rectangle shape. You can store that in a 2D array.

A vector of vectors is not a 2D array - it's not a rectangle. If you do a vector of vectors, what's stopping each 'row' of the table from being different lengths?

I far prefer storing 2D arrays as 1D arrays, and just indexing into them as if they are 2D:


std::vector<CellData> table;
table.resize(width * height);

table[(y * width) + x] = blah; //Indexing a 2D index (x,y) into a 1D array.

Thanks for the reply. I was refering to database table by that :) Anyways i had this idea while i was eating. What if i create a pointer for each type that i possibly can use and delete the ones i didn't use after reading file ? Would it be not safe or optimal somehow?

Thanks for the reply. I was refering to database table by that smile.png Anyways i had this idea while i was eating. What if i create a pointer for each type that i possibly can use and delete the ones i didn't use after reading file ? Would it be not safe or optimal somehow?

Yes, that's how it's done. Except you don't need to "delete" anything except the single pointer you 'new' - you just leave the unused pointers null the entire time.

However, the usual way is to use a void* pointer (which can point to any variable type), and reinterpret the void pointer when you need to use it - perhaps using a template member function for convenience.

The difficulty, for safety purposes, is some way of ID'ing what type is stored in the pointer, so you don't store a string and try to cast it to a float (which would give gibberish data).

If you have a limited set of types, you can ID them manually, but if supporting ANY type (which your project isn't), you need an automated way of doing that - such as using typeid() - but last time I tried using typeid() a few years back I ran into a problem, though I forget what it was.

Also, for basic variable types (ints, floats, bools), you don't actually need to allocate anything - just store them into the 4 or 8 bytes that your pointer is being stored in.

Thanks again for the fast reply. It's been really helpful.

It is important to understand the distinction between compile time, link time and run time. Templates are resolved at compile time, though some toolchains delegate to the linker to generate the actual machine code. At run time all this has already been decided, you can't go back and change things now (aside from things like dynamically generating machine code or loading dynamic libraries).

I'd be interested in knowing what the actual contents of the file is as there might be alternative designs. For example, configuration data appears to have types but you can get away loading / passing a dictionary of strings to strings as the data structure, deferring the type conversion until you're actually applying the configuration to some internal state. Depending on how dynamic this data is supposed to be, maybe a scripting language is what you're really looking for.

Here's what table data looks like:


tname:Accounts
datatypes:luint:string:string
columnnames:user_id:username:password
segmentinfo:a:5:s:6:t:7
1:account1:password2
2:ssdsad:password3
0:tsdasdad:passwor1

I'm trying to make this application serve as database server and send data to game/world/login servers. So eventually before i send the requested data over tcp socket i'll have to cast it to char array. I tried using boost::any and seems like it's working but i'm opened for better solutions.

Any particular reason not to use an existing database solution? They've already solved a lot of the correctness / scalability issues you are likely to run into if you try to put a hand-rolled solution into production, along with coming with suites of monitoring, mining and backup tools.

Well when i made some research about online multiplayer games and what kind of database should i use i saw that common idea was to create own database system that suits your needs the most. Also people seemed to not be happy about sql. By the way please don't think that i'm a kid who wants to create an mmorpg in 2 weeks. I know that this project of mine could take years and i'm not trying to rush anything. Also i'm not trying to make a complete database application, it doesn't have to exceed needs of the game.

This topic is closed to new replies.

Advertisement