Click here to Skip to main content
15,896,111 members
Articles / Mobile Apps

Memory mapped files and flat table primitives.

Rate me:
Please Sign up or sign in to vote.
3.11/5 (18 votes)
6 Apr 2003 209.6K   640   26  
Source C++ classes for persisting flat table data.
//|
//|
//| tl_mm_file.h
//|
//| Copyright (c) 2003
//| Andrew Fedoniouk - andrew@terra-informatica.org
//|
//| CONTENT:
//| class <B>mm_file</B> - access to memory mapped file
//| template table<RECORD> - persistent flat table
//|
//|

#ifndef __tl_mm_file_h
#define __tl_mm_file_h

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <assert.h>

namespace tool 
{

class mm_file 
{
    HANDLE hfile;
    HANDLE hmap;

  protected:
    bool read_only;
    void*  ptr;
    size_t length;
  public:
    mm_file(): hfile(0),hmap(0),ptr(0),length(0),read_only(true) {}
    virtual ~mm_file() { close(); }

    void *open(const char *path, bool to_write = false);
    void  close();

    void * data() { return ptr; }
    size_t size() { return length; }
    void   size(size_t sz) { assert(!read_only); length = sz; }
};

template<class RECORD>
  class table : public mm_file
{
    unsigned int  total_records;
  public:
    table(): total_records(0) {}
    virtual ~table() { close(); }

    bool  open(const char *path, bool to_write = false)
    {
      mm_file::open(path,to_write);
      total_records = size() / sizeof(RECORD);
      return (ptr != 0);
    }
    void  close()
    {
      if(!read_only) size(total_records * sizeof(RECORD));
      mm_file::close();
    }
    unsigned int total() { return total_records; }
    void         total(unsigned int numrecs) { assert(numrecs <= total_records); total_records = numrecs; }

    // "read" record 
    const RECORD& operator()(unsigned int idx) const 
    { 
      assert(idx < total_records);
      return ((RECORD*)ptr)[idx]; 
    }
    
    // "write" record. expand array if necessary 
    RECORD& operator[](unsigned int idx)  
    { 
      assert(!read_only);
      if(idx >= total_records)
      {
        RECORD nullrec;
        while(idx >= total_records)
          ((RECORD*)ptr)[total_records++] = nullrec;
      }
      return ((RECORD*)ptr)[idx]; 
    }
};

}



#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder Terra Informatica Software
Canada Canada
Andrew Fedoniouk.

MS in Physics and Applied Mathematics.
Designing software applications and systems since 1991.

W3C HTML5 Working Group, Invited Expert.

Terra Informatica Software, Inc.
http://terrainformatica.com

Comments and Discussions