BWAPI
trunk/bwapi/Util/Source/Util/SharedStructure.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 //  A memory buffer which can be shared with other processes          //
00004 //                                                                    //
00006 
00007 namespace Util
00008 {
00009   template<typename T> class SharedStructure;
00010 }
00011 
00012 #include "MemoryFrame.h"
00013 #include "RemoteProcess.h"
00014 #include "SharedMemory.h"
00015 #define WIN32_LEAN_AND_MEAN
00016 #include "windows.h"
00017 
00018 namespace Util
00019 {
00020   template<typename T> class SharedStructure
00021   {
00022   public:
00023     //----------------------- TYPES -----------------------------------------------------
00024     template<typename S> struct Pointer
00025     {
00026       friend class SharedStructure;
00027     private:
00028       SharedMemory::Pointer<S> pointer;
00029     };
00030     struct Export
00031     {
00032       friend class SharedStructure;
00033 
00034       Export(){};
00035       bool isValid() const
00036       {
00037         return smemExport.isValid();
00038       }
00039     private:
00040       SharedMemory::Export smemExport;
00041     };
00042     //----------------------- CONSTRUCTION ----------------------------------------------
00043     SharedStructure()
00044       : smem()
00045     {
00046     }
00047     ~SharedStructure()
00048     {
00049     }
00050     //----------------------- CREATE ----------------------------------------------------
00051     void create()
00052     {
00053       smem.create(sizeof(T));
00054     }
00055     bool create(const char* systemName)
00056     {
00057       return smem.create(sizeof(T), systemName);
00058     }
00059     //----------------------- IMPORT ----------------------------------------------------
00060     void import(Export source)
00061     {
00062       smem.import(source.smemExport);
00063     }
00064     //----------------------- RELEASE ---------------------------------------------------
00065     void release()
00066     {
00067       smem.release();
00068     }
00069     //----------------------- EXPORT TO PROCESS -----------------------------------------
00070     Export exportToProcess(RemoteProcess &target, bool readOnly) const
00071     {
00072       Export exp;
00073       exp.smemExport = smem.exportToProcess(target, readOnly);
00074       return exp;
00075     }
00076     //----------------------- UNPACK ----------------------------------------------------
00077     template<typename S>
00078       S *unpack(Pointer<S> p) const // to process address space
00079       {
00080         return smem.unpack(p.pointer);
00081       }
00082     //----------------------- PACK ------------------------------------------------------
00083     template<typename S>
00084       Pointer<S> pack(S *pt) const  // to shared address space
00085       {
00086         Pointer<S> retval;
00087         retval.pointer = smem.pack(pt);
00088         return retval;
00089       }
00090     //----------------------- GET MEMORY ------------------------------------------------
00091     T &get() const
00092     {
00093       return smem.getMemory().getAs<T>();
00094     }
00095     //----------------------- -----------------------------------------------------------
00096   private:
00097     SharedStructure(SharedStructure&);                // no copying
00098 
00099     SharedMemory smem;
00100   };
00101 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines