home *** CD-ROM | disk | FTP | other *** search
- /*
- * Plugin for REALbasic 2 that adds some new methods to RB's MemoryBlock class
- *
- * This code is freeware, you may use and extend it as you like.
- *
- * The accompanying project file was created using CodeWarrior Pro 3.
- *
- * It was written on June 7, 99 by Thomas Tempelmann.
- *
- * More RB-related stuff can be found here: <http://www.tempel.org/rb/>
- *
- * Enjoy and contribute!
- */
-
- #include <Memory.h>
- #include "rb_plugin.h"
-
-
- //----------------
- // plugin classes
- //----------------
-
- extern REALclassDefinition memBlkClass; // forward declaration
-
- static Ptr getMemBlkPtr (REALobject instance)
- // Returns the ptr to the actual data of a MemoryBlock
- // This is kind of a hack, since I access RB's internal structures directly
- // here, but there's no "safer" alternative currently (as of RB 2.0.2)
- {
- return *(Ptr*)((Ptr)instance+0x1C);
- }
-
- static long getSize (REALobject instance)
- {
- //DebugStr ("\pSize");
- return *(long*)((Ptr)instance+0x18);
- }
-
- static REALstring getString (REALobject instance, long offset, long length)
- {
- //DebugStr ("\pGetString");
- Ptr memBlk = getMemBlkPtr (instance);
- return REALBuildString (memBlk+offset, length);
- }
-
- static void setString (REALobject instance, REALstring str, long offset)
- {
- //DebugStr ("\pSetString");
- Ptr memBlk = getMemBlkPtr (instance);
- BlockMoveData (str->CString(), memBlk+offset, str->Length());
- }
-
- static void copyBytes (REALobject instance, long srcOfs, long length, long destOfs)
- {
- //DebugStr ("\pCopyBytes");
- Ptr memBlk = getMemBlkPtr (instance);
- BlockMoveData (memBlk+srcOfs, memBlk+destOfs, length);
- }
-
- static void copyBytes2 (REALobject instance, long srcOfs, long length, REALobject destMemBlk, long destOfs)
- {
- //DebugStr ("\pCopyBytes2");
- Ptr memBlk = getMemBlkPtr (instance);
- Ptr memBlk2 = getMemBlkPtr (destMemBlk);
- BlockMoveData (memBlk+srcOfs, memBlk2+destOfs, length);
- }
-
- static void toMacPtr (REALobject instance, long offset, long length, Ptr dest)
- {
- //DebugStr ("\pCopyBytesToMacPtr");
- Ptr memBlk = getMemBlkPtr (instance);
- BlockMoveData (memBlk+offset, dest, length);
- }
-
- static void fromMacPtr (REALobject instance, Ptr src, long length, long destOfs)
- {
- //DebugStr ("\pCopyBytesFromMacPtr");
- Ptr memBlk = getMemBlkPtr (instance);
- BlockMoveData (src, memBlk+destOfs, length);
- }
-
- static void toMacHdl (REALobject instance, long offset, long length, Handle dest)
- {
- //DebugStr ("\pCopyBytesToMacHandle");
- Ptr memBlk = getMemBlkPtr (instance);
- BlockMoveData (memBlk+offset, *dest, length); // no need to Lock the Handle here, since this operation is atomic and does not move memory by itself
- }
-
- static void fromMacHdl (REALobject instance, Handle src, long length, long destOfs)
- {
- //DebugStr ("\pCopyBytesFromMacHandle");
- Ptr memBlk = getMemBlkPtr (instance);
- BlockMoveData (*src, memBlk+destOfs, length); // no need to Lock the Handle here, since this operation is atomic and does not move memory by itself
- }
-
- /* currently unused
- static long getPtrSize (REALobject instance)
- {
- //DebugStr ("\pPtrSize");
- Ptr memBlk = getMemBlkPtr (instance);
- long l = GetPtrSize (memBlk);
- if (MemError()) {
- return -1;
- } else {
- return l;
- }
- }
- */
-
- /* currently unused
- static long getHdlSize (REALobject instance)
- {
- //DebugStr ("\pHandleSize");
- Ptr memBlk = getMemBlkPtr (instance);
- long l = GetHandleSize ((Handle)memBlk);
- if (MemError()) {
- return -1;
- } else {
- return l;
- }
- }
- */
-
- #if TARGET_CPU_68K
- typedef Float80 RBdouble;
- #else
- typedef double RBdouble;
- #endif
-
- #if TARGET_CPU_68K
- static void getDouble (RBdouble* f, REALobject instance, long ofs)
- {
- //DebugStr ("\pDoubleValue (get)");
- Ptr memBlk = getMemBlkPtr (instance);
- *f = *(double*)(memBlk+ofs);
- }
- #else
- static RBdouble getDouble (REALobject instance, long ofs)
- {
- //DebugStr ("\pDoubleValue (get)");
- Ptr memBlk = getMemBlkPtr (instance);
- return *(double*)(memBlk+ofs);
- }
- #endif
-
- static void setDouble (REALobject instance, long ofs, RBdouble f)
- {
- //DebugStr ("\pDoubleValue (set)");
- Ptr memBlk = getMemBlkPtr (instance);
- *(double*)(memBlk+ofs) = f;
- }
-
- static REALproperty memBlkClassProperties[] = {
- { nil, "Size", "Integer", 0, (REALproc) getSize },
- };
-
- static REALmethodDefinition memBlkClassMethods[] = {
- { (REALproc) getDouble, (REALproc) setDouble, "DoubleValue(offset as Integer) as Double"},
- { (REALproc) getString, REALnoImplementation, "GetString(offset as Integer, numBytes as Integer) as String"},
- { (REALproc) setString, REALnoImplementation, "SetString(str as String, offset as Integer)"},
- { (REALproc) copyBytes, REALnoImplementation, "CopyBytes(srcOfs as Integer, numBytes as Integer, destOfs as Integer)"},
- { (REALproc) copyBytes2, REALnoImplementation, "CopyBytes(srcOfs as Integer, numBytes as Integer, destBlk as MemoryBlock, destOfs as Integer)"},
- { (REALproc) toMacPtr, REALnoImplementation, "CopyBytesToMacPtr(srcOfs as Integer, numBytes as Integer, destPtr as Integer)"},
- { (REALproc) fromMacPtr, REALnoImplementation, "CopyBytesFromMacPtr(srcPtr as Integer, numBytes as Integer, destOfs as Integer)"},
- { (REALproc) toMacHdl, REALnoImplementation, "CopyBytesToMacHandle(srcOfs as Integer, numBytes as Integer, destHandle as Integer)"},
- { (REALproc) fromMacHdl, REALnoImplementation, "CopyBytesFromMacHandle(srcHandle as Integer, numBytes as Integer, destOfs as Integer)"},
- // { (REALproc) getPtrSize, REALnoImplementation, "PtrSize() as Integer"},
- // { (REALproc) getHdlSize, REALnoImplementation, "HandleSize() as Integer"},
- };
-
- static REALclassDefinition memBlkClass = {
- kCurrentREALControlVersion,
- "MemoryBlock",
- nil,
- 0,
- 0,
- (REALproc) nil,
- (REALproc) nil,
- memBlkClassProperties,
- sizeof(memBlkClassProperties) / sizeof(REALproperty),
- memBlkClassMethods,
- sizeof(memBlkClassMethods) / sizeof(REALmethodDefinition),
- nil,
- 0
- };
-
-
- void PluginEntry (void)
- {
- REALRegisterClassExtension (&memBlkClass);
- }
-