Results 1 to 8 of 8

Thread: Pass or change TYPE in other unit, possible?

  1. #1

    Pass or change TYPE in other unit, possible?

    Hi.

    Lets say i have simple unit.
    This unit has simple types declared:
    Code:
    unit Unit2;
    
    interface
    
    type
      _TKey= string;
      _TValue= Cardinal;
    
    implementation
    
    end.
    You see in this case there are "string" and "Cardinal" used.

    Is it possible somehow pass these from another unit?
    I mean, from main Unit1 i would like to have that _TKey would become type of "Integer" for example.

    I know there are generics in FPC and newer Delphis, but without generics, is it possible or not?
    Or only possible way is to always change the unit itself, manually?



    Im using Delphi 7 Personal.

  2. #2
    If you want to simply replace this type you can redeclare _TKey in Unit1 as Integer. So whnever you will be refering to _TKey in Unit1 or other unit that uses Unit it will be of Integer type but refering to _TKey from Unit2 will return you variable of String type.
    Note these would be two completly diferent types not interchangable with each other who would only share same name. They can't even exist in same code scope.

    But if you want to have one common type to which you could acces either as Integer or as String then I'm afraid you have to use generics (Type Helpers: http://wiki.freepascal.org/Helper_types). I'm afraid these are not available in Delphi 7.

    Maybe you could make use of classes and their getter/setter methods to do necessary type conversion when you need it. That depends on how your other code is designed.
    If you care to share what exactly you wanna achive we might help you out with a different aproach.

  3. #3
    Thank you for reply.

    Well in my map editor i need to compress map as fast as possible (there is specific algorithm used), in C++ i use std::map, now i have been looking at my archive of different things i have downloaded from the internet and found one zip with some stlpascal. Probably from here: https://code.google.com/p/stlpascal/

    I decided to try if could make the gmap.pas to work with my map block record (struct), because D7 doesnt support generics.
    I successfully modified it to my record (struct) and added comparison functions like: greaterthan, lessthan, equal. They are needed because stl std::map also needs them for specific things. And also did some other small modifications to make it compile in D7.

    It works and its very fast, faster than FPC fgl. And i mean a lot faster, even that stlpascal is very fast on FPC. Dunno why the original fgl included in FPC is so slow.

    I did test with biggest map i found, it had ~14000 blocks and original fgl code just stopped responding and i had to stop the process by myself.
    Now with stlpascal its instant. Just like with C++ std::map. Same with my D7 version.

    So i was thinking about maybe i could make it kinda "generic" for D7.
    And if i succeed i thought i would share it.

    That's why i asked how i could do such thing.

    In FPC all that works because it supports generics and i can use anything, but in D7 i have to manually specify type i want to use.
    But i dont feel like i want to switch to FP completely atm just because of generics..

    Tried to use pointers and typecasting but failed.
    I still dont know much about them..
    Last edited by hwnd; 16-12-2013 at 01:32 AM.

  4. #4
    I quicky checked what std::map is. I haven't used it before.
    If I understand it correctly it is quite similar to word compression. It scans you data to find all posible variations of posible data blocks, builds some kind of a dictionary which contains all posible data block variations and then finally replaces these datablocks with index numbers which represent the block variation saved in this "dictionary".
    In word compression with using 16 bit integer number for dictionary Index this means that you can compress any word which is more than 3 characters long and ocurs athleast twice in the text.

    I'm still puzled why you need ability of direct typecasting from String to Integer and vice versa.

    Also if you might share how you store your map data I might provide you with an alternative solution for compressing map data.

  5. #5
    Its too long to explain. But in short: std::map only stores unique keys.
    It needs custom comparators for custom structs (records). It provides his own comparators for simple types like int, float etc.

    And the primary thing in map compression is unique blocks and columns. And it must be fast.
    I already use std::map for compression and its fast, i just created a DLL in C++ and pass the map array data (actually pointer) to DLL and it compresses it.
    This all works fine and i can live with DLLs, i use SDL and other things anyway that are in DLLs.

    But i just wanted to include it in my code, to get rid of the C++ DLL. Then i can also prove myself that std::map kind of "thingy" is possible in Delphi (Pascal) also.
    Sadly without generics in older versions.

    Take it as an exercise for me. I need one "map" for blocks and one "map" for columns. Currently i created 2 units in Delphi, one for blocks and one for columns.
    As you see, without generics, code is duplicated.

    Dont worry, i will first finish the compressor in FreePascal, to see if it even finds the correct unique columns and compresses map correctly.
    Because i tried many things already in Delphi 7, DeCAL, dcl, etc.. All of them have trouble finding me correct unique blocks and same with columns. All are wrong (ok a bit wrong, but if one is wrong, then whole map is wrong. And will crash the game).

    Probably because most of these container libs to not support (custom) comparators, otherwise even std::map doesnt know whats unique block or column and which is not.
    And of course column struct is a bit complicated also (it contains small static array in it).


    So, i will try to port this to FPC first (actually i already did, but its unfinished).
    If it works, then i will see what i will do next with it.

  6. #6
    not sure if it applies, but have you considered to use variants?

  7. #7
    I guess you could try using includes and macros. Divide the thing into two files: one include file, which will contain the map code, and one (or more) files defining macros and including the map code.

    For example:
    map.inc
    Code:
    Type TMap = class
       { snip }
       Procedure Insert(Key: KEYTYPE ; Val : VALUETYPE );  
       Function Search(Key: KEYTYPE ): VALUETYPE ;
       { snip }
    map_blocks.pas
    Code:
    unit map_blocks;
    
    {$MACRO ON} {$DEFINE KEYTYPE:=TMyCoord} {$DEFINE VALUETYPE:=TMyBlock}
    
    {$INCLUDE map.inc}

  8. #8
    Macros are only in FPC it seems, sadly no such thing in D7.
    In FPC, i will try this of course..

    I once found even some IDE plugin or addon or something, that kinda tried to make things generic in old Delphi. D5 or something.
    You specified type you wanted and it generated another unit with this type and it used it..

    I must find it, it was interesting. Probably from archives, the website is gone.


    Can the "records" work with variants, i mean can they work together?
    Last edited by hwnd; 17-12-2013 at 10:29 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •