I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index » Programming » New Module Ideas »

abstraction layer for saving binary data

robsc\′s Photo
24 Sep 05, 6:54AM
(6 replies)
abstraction layer for saving binary data
i frequently encounter the problem that i cannot store all the data (usually lists of structs) i need for my calculation in main memory and have to swap it to a file on my harddisk. since the process of reading and writing data is always the same, one could write a piece of code that can do that for every possible type. One idea is to overload the []-operator for random access, or to construct stl-like iterators. This will only work for non-nested data structures, but maybe there is a way to do it for arbitrary data too.

i am working on a solution for "list <struct whatever>" and will submit it when it works :-)

if you have suggestions how to design the more general case, tell me!

robert
will\′s Photo
24 Sep 05, 12:24PM
Hi,

Thats a great idea. If I understand you correctly, then this shouldn't be too hard. Using the [b:7a32f5d626]sizeof[/b:7a32f5d626] operator, you will know exactly how big a binary variable is, and therefore where in a file any particular item will be into that file.

Something like (sorry I'm used to the C style for files!)

template &lt;class T&gt;
class binaryio
&#123;
  FILE* stream;
 
  binaryio&#40;char* filename&#41;
  &#123;
    stream = fopen&#40;filename, &quot;b&quot;&#41;;
  &#125;

T operator &#91;&#93;&#40;int i&#41; &#123; fseek&#40;stream, i * sizeof&#40;T&#41;, 0&#41;; //&lt;- Can't remember the syntax here. T data; fread&#40;&amp;data, 1, sizeof&#40;T&#41;, stream&#41;; return data; &#125; &#125;;

And something smarter to write back to random locations..

Is that what you're think?

robsc\′s Photo
24 Sep 05, 12:38PM
yes, that's pretty much the same what i had in mind. but this will work only if the data in <class T> is stored sequentially. For simple types (int, double,char) this is definitely true, but what about complex classes, or classes with complex-type members ?
will\′s Photo
24 Sep 05, 12:59PM
This approach will work the same as any C++ container, such as vector or list etc.

Your concern shouldn't about how the data inside a class is stored. In truth that's none of your business. What you're interested in doing is saving the contents of the class and retrieving it with (I suspect) a simple cast that put it back into the same form as it was originally. What I think though you're getting at is the use of the [b:472263e98b]new[/b:472263e98b] operator within that class (say).. Thus two variables of class X, could uses substantially different amounts of dynamic memory, because variable 1, allocated 100k of memory, while variable 2 allocated 200k of memory.

This is a much more complex problem and one you can't solve unless you know exactly how that class is configured. The best you can do is insist that your these 'complex' data types have a common method that ensure that they can be forced to save data and reload it on que.

If you go down that line. Then you probably want to structure your binary file in the following form: ------ [data for each data type] * N ------ [key to additional dynamic data] ------ [additional dynamic data] * N

Hope that make sense. But here how I would image it working, with a request for some data 1) You extract the contents of the dynamic data type given the index, using the overload [] operator we discussed above. 2) You call the 'load' method within that newely reloaded class/struct etc, that reloads any additional dynamic data, give a file offset you provide. The offset you get from the key section (or if you prefer you store it immediately after each individual data type).. This simply means on saving you need to know how many data types you have, so you know how much space to leave for each section, with the additional dynamic bit on the end, since you've no idea how big this is until you get to saving..

Thats the only way I can see this will work..

Hope that helps..
robsc\′s Photo
25 Sep 05, 8:56AM
hi, i thought about what you wrote and concluded that an efficient implementation must define at least two basic classes: one class that will be associated to the file and define a simple interface for reading and writing and another class that will put some constraints on the type of element. Furtheron it might be possible to inherit the first class from "vector" and thus provide a much more powerful tool. Have a look at the pieces of code below.

robert

//that's the save-binary-file-interface
class SaveBinary &#58; std&#58;&#58;vector
&#123;
        public SaveBinary * read&#40;int index&#41;;
        public SaveBinary * write&#40;int index&#41;;
 
        //lots of member functions have to be overwritten
&#125;
class SaveableType
&#123;
        //force the user to define that function
        protected virtual int load&#40;int&#41; = 0;
&#125;

//that's what a user is going to do with the interface
class MyClass &#58; SaveableType
&#123;
        //define the &quot;load&quot; function
        int load&#40;int position&#41;
        &#123;
                //code
        &#125;
 
        //more code
&#125;
int main&#40;&#41;
&#123;
        MyClass mc1,mc2;
        SaveBinary&lt;MyClass&gt; sb;
 
        mc1.do_something&#40;&#41;;
        mc2.do_other&#40;&#41;;
 
 
        sb.push_back&#40;mc1&#41;;
        sb.push_back&#40;mc2&#41;;
 
 
        //probably you will NOT do this, but some other stl-algorithm
        //might perform something really elegant on &quot;b&quot;
        sort&#40;b&#41;;
 
        return &#40;0&#41;;
 
&#125;

will\′s Photo
27 Sep 05, 6:45AM
Thats seems the way to go..

You might want to create a small abstraction to your class. Not sure if they're is anything really neat you can do with templates to solve this. But you essentially want to be able to apply this approach to constant sized variables (classes included) without being dependent on the SaveableType and also to those that dynamically allocate memory..

I also wondered if you could so something dangerously clever by redefining the new operator, so you could catch memory being allocated by a class and allocate disk in your file accordingly. Given that every good class should have a copy constructor, which make an exact copy of anything it finds, I wonder if it might be possible to hijack something in there.. Mega long shot. And probably overtly complex..

Good luck Will.
sajjad125\′s Photo
5 Dec 14, 12:48AM
i frequently encounter the problem that i cannot store all the data (usually lists of structs) i need for my calculation in main memory and have to swap it to a file on my harddisk. since the process of reading and writing data is always the same, one could write a piece of code that can do that for every possible type. One idea is to overload the []-operator for random access, __________________________________________ [url=http://www.pass-4sure.us]pass-4sure.us[/url]
Currently you need to be logged in to leave a message.