home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / general.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  14.4 KB  |  572 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class GENERAL
  5. --        
  6. -- Platform-independent universal properties.
  7. -- This class is an ancestor to all developer-written classes.
  8. --
  9.  
  10. feature -- Access :
  11.    
  12.    generating_type: STRING is
  13.      -- Name of current object's generating type (type of 
  14.      -- which it is a direct instance).
  15.       external "CSE"
  16.       end;
  17.   
  18.    generator: STRING is
  19.      -- Name of current object's generating class (base class
  20.      -- of the type of witch it is a direct instance).
  21.       external "CSE"
  22.       end;
  23.    
  24.    id_object(id: INTEGER): ANY is
  25.      -- Object for wich `object_id' has returened `id'.
  26.      -- Void if none.
  27.       require
  28.      id /= 0;
  29.       do
  30.      c_inline_c("R=((T0 *)a1);");
  31.       end;
  32.    
  33.    object_id: INTEGER is
  34.      -- Value identifying current reference object.
  35.       require
  36.      not is_expanded_type
  37.       do
  38.      c_inline_c("R=((int)C);")
  39.       end;
  40.    
  41.    stripped(other: GENERAL): like other is
  42.      -- New created object with fields copied from current object, but
  43.      -- limited to attributes of type of `other'.
  44.       require
  45.      conformance: conforms_to(other);
  46.       do
  47.      not_yet_implemented;
  48.       ensure
  49.      stripped_to_other: Result.same_type(other);
  50.       end;
  51.    
  52. feature -- Status report :
  53.    
  54.    frozen conforms_to(other: GENERAL): BOOLEAN is
  55.      -- Does type of current object conform to type of other 
  56.      -- (as per Eiffel: The Language, chapter 13) ?
  57.       require
  58.      other_not_void: other /= Void;
  59.       do
  60.      not_yet_implemented;
  61.       end;
  62.    
  63.    frozen same_type(other: GENERAL): BOOLEAN is
  64.      -- Is type of current object identical to type of other.     
  65.       require
  66.      other_not_void: other /= Void;
  67.       do
  68.      if is_expanded_type then
  69.      else
  70.         c_inline_c("R=((C->id)==(a1->id));");
  71.      end;
  72.       ensure
  73. --     definition: Result = (conforms_to(other) and
  74. --                   other.conforms_to(Current));
  75.       end;
  76.    
  77.    frozen new_same_type(other: GENERAL): BOOLEAN is
  78.      -- Is type of current object identical to type of other.     
  79.       require
  80.      other_not_void: other /= Void;
  81.       do
  82.      if is_expanded_type then
  83.         -- *** Could write not is_expanded !!
  84.      else
  85.         c_inline_c("R=(C->id)==(a1->id);");
  86.      end;
  87.       ensure
  88.      definition: Result = (conforms_to(other) and
  89.                    other.conforms_to(Current));
  90.       end;
  91.    
  92. feature -- Comparison :
  93.    
  94.    frozen deep_equal(some: GENERAL; other: like some): BOOLEAN is
  95.       do
  96.      if some = other then
  97.         Result := true;
  98.      elseif some = Void then
  99.      elseif other = Void then
  100.      elseif standard_equal(some,other) then
  101.         Result := true;
  102.      else
  103.         not_yet_implemented;
  104.      end;
  105.       ensure
  106.      shallow_implies_deep: standard_equal(some,other) 
  107.                    implies Result;
  108.      same_type: Result implies some.same_type(other);
  109.      symmetric: Result implies deep_equal(other,some);
  110.       end;
  111.    
  112.    frozen equal(some: ANY; other: like some): BOOLEAN is
  113.      -- Are `some' and `other' both Void or attached to
  114.      -- objects considered equal ?
  115.       do
  116.      if some = other then
  117.         Result := true;
  118.      elseif some = Void then
  119.      elseif other = Void then
  120.      else
  121.         Result := some.is_equal(other);
  122.      end;
  123.       ensure
  124.      definition: Result = (some = Void and other = Void) or else
  125.              ((some /= Void and other /= Void) and then
  126.               some.is_equal(other));
  127.       end;
  128.  
  129.    is_equal(other: like Current): BOOLEAN is
  130.      -- Is `other' attached to an object considered equal to 
  131.      -- current object ?
  132.       require
  133.      other_not_void: other /= Void
  134.       do
  135.      Result := standard_is_equal(other);
  136.       ensure
  137.      consistent: standard_is_equal(other) implies Result;
  138. -- ***     same_type: Result implies same_type(other);
  139. -- ELKS problem for expanded target.
  140.      symmetric: Result implies other.is_equal(Current);
  141.       end;
  142.    
  143.    frozen standard_equal(some: ANY; other: like some): BOOLEAN is
  144.      -- Are `some' and `other' both Void or attached to
  145.      -- field-by-field objects of the same type ?
  146.      -- Always use the default object comparison criterion.
  147.       do
  148.      if some = other then
  149.         Result := true;
  150.      elseif some = Void then
  151.      elseif other = Void then
  152.      elseif some.same_type(other) then
  153.         Result := some.standard_is_equal(other);
  154.      end;
  155.       ensure
  156.      definition: Result = (some = Void and other = Void) or else
  157.              ((some /= Void and other /= Void) and then
  158.               some.standard_is_equal(other));
  159.       end;
  160.  
  161.    frozen standard_is_equal(other: like Current): BOOLEAN is
  162.      -- Are Current and `other' field-by-field identical?
  163.       require
  164.      other /= Void
  165.       do
  166.      if is_expanded_type then
  167.         Result := other = Current;
  168.      elseif other = Current then
  169.         Result := true;
  170.      else
  171.         c_inline_c("R=!memcmp(C,a1,s[C->id]);");
  172.      end;
  173.       ensure
  174.      same_type: Result implies same_type(other);
  175.      symmetric: Result implies other.standard_is_equal(Current);
  176.       end;
  177.    
  178. feature -- Duplication :
  179.    
  180.    frozen clone(other: ANY): like other is
  181.      -- Void if `other' is Void; otherwise new object 
  182.      -- equal to `other'. 
  183.       do
  184.      if other /= Void then
  185.         c_inline_c("R=(T0 *)new(a1->id);");
  186.         c_inline_c("AF_1");
  187.         Result.copy(other);
  188.         c_inline_c("AF_0");
  189.      end;
  190.       ensure
  191.      equal: equal(Result,other);
  192.       end;
  193.  
  194.    copy(other: like Current) is
  195.      -- Update current object using fields of object attached
  196.      -- to `other', so as to yield equal objects.
  197.       require
  198.      other_not_void: other /= Void;
  199.      type_identity: same_type(other);
  200.       do
  201.      if is_expanded_type then
  202.         c_inline_c("C=a1;");
  203.      else
  204.         c_inline_c("memcpy(C,a1,s[C->id]);");
  205.      end;
  206.       ensure
  207.      is_equal: is_equal(other)
  208.       end;
  209.    
  210.    frozen deep_clone(other: GENERAL): like other is
  211.      -- Void if `other' is Void: otherwise, new object structure 
  212.      -- recursively duplicated from the one attached to other.
  213.       do
  214.      not_yet_implemented;
  215.       ensure
  216.      deep_equal: deep_equal(other,Result);
  217.       end;
  218.    
  219.    frozen standard_clone(other: ANY): like other is
  220.      -- Void if `other' is Void; otherwise new object 
  221.      -- field-by-field identical to `other'. 
  222.      -- Always use the default copying semantics.
  223.       do
  224.      if other /= Void then
  225.         c_inline_c("R=(T0 *)new(a1->id);%N%
  226.                %memcpy(R,a1,s[a1->id]);");
  227.      end;
  228.       ensure
  229.      equal: standard_equal(Result,other);
  230.       end;
  231.  
  232.     frozen standard_copy(other: like Current) is
  233.      -- Copy every field of `other' onto corresponding 
  234.      -- field of curent object.
  235.       require
  236.      other_not_void: other /= Void;
  237.      type_identity: same_type(other);
  238.       do
  239.      c_inline_c("memcpy(C,a1,s[a1->id]);");
  240.       ensure
  241.      is_standard_equal: standard_is_equal(other);
  242.       end;
  243.    
  244. feature -- Basic operations :
  245.    
  246.    frozen default: like Current is
  247.      -- Default value of current type.
  248.       do
  249.       end;
  250.    
  251.    frozen default_pointer: POINTER is
  252.      -- Default value of type POINTER (avoid the need to
  253.       -- write p.default for some `p' of type POINTER).
  254.       do
  255.       ensure
  256.      Result = Result.default;
  257.       end;
  258.    
  259.    default_rescue is
  260.      -- Handle exception if no Rescue clause (default do
  261.      -- nothing).
  262.       do
  263.       end;
  264.    
  265.    frozen do_nothing is
  266.      -- Execute a null action.
  267.       do
  268.       end;
  269.    
  270.    frozen Void: NONE is 
  271.       -- Void reference.
  272.       external "CSE" 
  273.       end;
  274.  
  275. feature -- Output :
  276.    
  277.    frozen io: STD_INPUT_OUTPUT is
  278.      -- Handle to standard file setup.
  279.      -- To use the standard input/output file.
  280.       once
  281.      !!Result.make;
  282.       ensure
  283.      Result /= Void;
  284.       end; 
  285.    
  286.    frozen std_input: STD_INPUT is
  287.      -- To use the standard input file.
  288.       once
  289.      !!Result.make;
  290.       end; 
  291.    
  292.    frozen std_output: STD_OUTPUT is
  293.      -- To use the standard output file.
  294.       once
  295.      !!Result.make;
  296.       end; 
  297.    
  298.    frozen std_error: STD_ERROR is
  299.      -- To use the standard error file.
  300.       once
  301.      !!Result.make;
  302.       end; 
  303.    
  304.    out: STRING is
  305.      -- New string containing terse printable representation 
  306.      -- of current object;
  307.       do
  308.      not_yet_implemented;
  309.       end;
  310.    
  311.    frozen print(some: GENERAL) is
  312.      -- Write terse external representation of `some' on
  313.      -- `standard_output'. This routine is called to print
  314.      -- the stack when a `crash' occurs. Thus, user can redefine
  315.      -- `print_on' or `print_attributes_on' to adapt printing 
  316.      -- of run time stack. 
  317.       do
  318.      if some = Void then
  319.         std_output.put_string("Void");
  320.      else
  321.         some.print_on(std_output);
  322.      end;
  323.       end;
  324.    
  325.    print_on(file: STD_FILE_WRITE) is
  326.      -- Default printing for reference target.
  327.      --     
  328.      -- Note : this routine is used to print stack when `crash'.
  329.       do
  330.      file.put_string(generating_type);
  331.      file.put_character('#');
  332.      file.put_integer(object_id);
  333.      file.put_character('[');
  334.      print_attributes_on(file);
  335.      file.put_character(']');
  336.       end;
  337.    
  338.    print_attributes_on(file: STD_FILE_WRITE) is
  339.       do 
  340.            tagged_out_memory.clear;
  341.      fill_tagged_out_memory;
  342.      file.put_string(tagged_out_memory);
  343.       end;
  344.    
  345.    frozen tagged_out: STRING is
  346.      -- New string containing printable representation of current 
  347.      -- object, each field preceded by its attribute name, a 
  348.      -- colon and a space.
  349.       do
  350.      tagged_out_memory.clear;
  351.      fill_tagged_out_memory;
  352.      Result := clone(tagged_out_memory);
  353.       end;
  354.    
  355.    frozen tagged_out_memory: STRING is
  356.       once
  357.      !!Result.make(1024);
  358.       end;
  359.    
  360.    fill_tagged_out_memory is
  361.       external "CSE"
  362.       end;
  363.    
  364. feature -- Named file handling :
  365.    
  366.    file_exists(path: STRING): BOOLEAN is
  367.       require
  368.      path /= Void;
  369.       do
  370.      path.extend('%U');
  371.      c_inline_c(
  372.          "{FILE *f=fopen(((T7 *)a1)->_storage,%"r%");%N%
  373.      %R=(f != NULL);%N%
  374.      %fclose(f);}");
  375.      path.remove_last(1);
  376.       end;
  377.    
  378.    remove_file(path: STRING) is
  379.       require
  380.      path /= Void;
  381.       do
  382.      path.extend('%U');
  383.      c_inline_c("remove(((T7 *)a1)->_storage);");
  384.      path.remove_last(1);
  385.       end;
  386.    
  387.    rename_file(old_path, new_path: STRING) is
  388.       require
  389.      old_path /= Void;
  390.      new_path /= Void;
  391.       do
  392.      old_path.extend('%U');
  393.      new_path.extend('%U');
  394.      c_inline_c("rename(((T7 *)a1)->_storage,((T7 *)a2)->_storage);");
  395.      old_path.remove_last(1);
  396.      new_path.remove_last(1);
  397.       end;
  398.  
  399. feature -- Access to command-line arguments :
  400.    
  401.    argument_count: INTEGER is
  402.      -- Number of arguments given to command that started
  403.      -- system execution (command name does not count).
  404.       external "CSE"
  405.       ensure
  406.      Result >= 0;
  407.       end;
  408.    
  409.    argument(i: INTEGER): STRING is
  410.      -- `i' th argument of command that started system execution 
  411.      -- Gives the command name if `i' is 0.
  412.       require
  413.      i >= 0;
  414.      i <= argument_count;
  415.       external "CSE"
  416.       ensure
  417.      Result /= Void
  418.       end;
  419.    
  420.    get_environment_variable(name: STRING): STRING is
  421.      -- To get the value of a system environment variable
  422.      -- (like "PATH" on Unix for example).
  423.      -- Gives Void when system variable `name' is undefined.
  424.       require
  425.      name /= Void
  426.       do
  427.      name.extend('%U');
  428.      c_inline_c("R=((T0 *)getenv(((T7 *)a1)->_storage));%N%
  429.             %if (R) R=((T0 *)e2s((char *)R));")
  430.      name.remove_last(1);
  431.       ensure
  432.      Result /= Void implies not Result.empty
  433.       end;
  434.    
  435. feature -- System calls and crashing :
  436.    
  437.    system(cmd: STRING) is
  438.      -- To execute a `cmd' at system level.
  439.      -- For example, "ls -l" on UNIX.
  440.       do
  441.      cmd.extend('%U');
  442.      c_inline_c("system(((T7 *)a1)->_storage);");
  443.      cmd.remove_last(1);
  444.       end;
  445.    
  446.    crash is
  447.      -- Print Run Time Stack (unless "-boost" mode) 
  448.      -- and then exit with `exit_failure_code'.
  449.      -- See also `print'.
  450.       do
  451.      c_inline_c("rsp();");
  452.      die_with_code(exit_failure_code);
  453.       end;
  454.    
  455.    die_with_code(code:INTEGER) is
  456.      -- Terminate execution with exit status code `code'.
  457.      -- Do not print any message.
  458.       require
  459.      code = exit_success_code or else
  460.      code = exit_failure_code;
  461.       do
  462.      c_inline_c("exit(a1);");
  463.       end;
  464.    
  465.    exit_success_code: INTEGER is 0;
  466.    
  467.    exit_failure_code: INTEGER is 1;
  468.    
  469. feature -- Maths constants :
  470.  
  471.    Pi: DOUBLE is 3.1415926535897932384626; 
  472.      
  473.    Evalue: DOUBLE is  2.71828182845904523536;
  474.    
  475.    Deg: DOUBLE is 57.295780; -- Deg/Radian.
  476.    
  477.    Phi: DOUBLE is 1.618034; -- Golden Ratio.
  478.    
  479. feature -- Character names :
  480.  
  481.    Ctrl_a: CHARACTER is '%/1/';
  482.    Ctrl_b: CHARACTER is '%/2/';
  483.    Ctrl_c: CHARACTER is '%/3/';
  484.    Ctrl_d: CHARACTER is '%/4/';
  485.    Ctrl_e: CHARACTER is '%/5/';
  486.    Ctrl_f: CHARACTER is '%/6/';
  487.    Ctrl_g: CHARACTER is '%/7/';
  488.    Ch_del: CHARACTER is '%/8/';
  489.    Ch_tab: CHARACTER is '%/9/';
  490.    Ctrl_j: CHARACTER is '%/10/';
  491.    Ctrl_k: CHARACTER is '%/11/';
  492.    Ctrl_l: CHARACTER is '%/12/';
  493.    Ctrl_m: CHARACTER is '%/13/';
  494.    Ctrl_n: CHARACTER is '%/14/';
  495.    Ctrl_o: CHARACTER is '%/15/';
  496.    Ctrl_p: CHARACTER is '%/16/';
  497.    Ctrl_q: CHARACTER is '%/17/';
  498.    Ctrl_r: CHARACTER is '%/18/';
  499.    Ctrl_s: CHARACTER is '%/19/';
  500.    Ctrl_t: CHARACTER is '%/20/';
  501.    Ctrl_u: CHARACTER is '%/21/';
  502.    Ctrl_v: CHARACTER is '%/22/';
  503.    Ctrl_w: CHARACTER is '%/23/';
  504.    Ctrl_x: CHARACTER is '%/24/';
  505.    Ctrl_y: CHARACTER is '%/25/';
  506.    Ctrl_z: CHARACTER is '%/26/';
  507.  
  508. feature -- Hashing :
  509.    
  510.    hash_code: INTEGER is
  511.       external "CSE"
  512.       ensure
  513.      non_negative: Result >= 0
  514.       end;
  515.    
  516. feature -- Should not exist :
  517.    
  518.    not_yet_implemented is
  519.       do
  520.      std_error.put_string(
  521.       "Sorry, Some Feature is Not Yet Implemented.%N%
  522.        %Please, if you can write it by yourself and if you send me%N%
  523.        %the corresponding tested Eiffel code, I may put it in the%N% 
  524.        %standard library!%N%
  525.        %Many Thanks in advance.%N%  
  526.        %D.Colnet e-mail: colnet@loria.fr%N");
  527.      crash;
  528.        end;
  529.        
  530. feature {ANY} -- WARNING: For SmallEiffel Users's only. 
  531.    -- Using following features could makes your Eiffel code not 
  532.    -- portable on other Eiffel compilers.
  533.    
  534.    frozen is_expanded_type: BOOLEAN is
  535.      -- Statically computed by SmallEiffel.
  536.      -- Result is true if target static type is an expanded type.
  537.      -- Target is not evaluated.
  538.      -- Usefull for formal generic type.
  539.       external "CSE"
  540.       end;
  541.    
  542.    object_size: INTEGER is
  543.      -- Gives the size of the current object at first level 
  544.      -- only (pointed sub-object are not concerned).  
  545.      -- The result is the C sizeof of the corresponding
  546.      -- struct or basic object.
  547.       do
  548.      if is_expanded_type then
  549.         c_inline_c("R=sizeof(C);")
  550.      else
  551.         c_inline_c("R=s[C->id];")
  552.      end;
  553.       end;
  554.  
  555. feature {NONE} -- WARNING: For SmallEiffel Gurus's only. 
  556.    -- Using following features makes your Eiffel code not 
  557.    -- portable on other Eiffel compilers.
  558.    
  559.    c_inline_h(c_code: STRING) is
  560.      -- Target must be Current and `c_code' must be a manifest
  561.      -- string. Write `c_code' in the heading C file.
  562.       external "CSE"
  563.       end;
  564.  
  565.    c_inline_c(c_code: STRING) is
  566.      -- Target must be Current and `c_code' must be a manifest
  567.      -- string. Write `c_code' in the stream at current position.
  568.       external "CSE"
  569.       end;
  570.  
  571. end -- GENERAL
  572.