home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / Any.py < prev    next >
Text File  |  1999-06-28  |  7KB  |  256 lines

  1. #!/usr/bin/env python
  2. #############################################################################
  3. # Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  4. # All Rights Reserved.
  5. #
  6. # The software contained on this media is the property of the DSTC Pty
  7. # Ltd.  Use of this software is strictly in accordance with the
  8. # license agreement in the accompanying LICENSE.HTML file.  If your
  9. # distribution of this software does not contain a LICENSE.HTML file
  10. # then you have no rights to use this software in any manner and
  11. # should contact DSTC at the address below to determine an appropriate
  12. # licensing arrangement.
  13. #      DSTC Pty Ltd
  14. #      Level 7, GP South
  15. #      Staff House Road
  16. #      University of Queensland
  17. #      St Lucia, 4072
  18. #      Australia
  19. #      Tel: +61 7 3365 4310
  20. #      Fax: +61 7 3365 4311
  21. #      Email: enquiries@dstc.edu.au
  22. # This software is being provided "AS IS" without warranty of any
  23. # kind.  In no event shall DSTC Pty Ltd be liable for damage of any
  24. # kind arising out of or in connection with the use or performance of
  25. # this software.
  26. #
  27. # Project:      Fnorb
  28. # File:         $Source: /units/arch/src/Fnorb/orb/RCS/Any.py,v $
  29. # Version:      @(#)$RCSfile: Any.py,v $ $Revision: 1.12 $
  30. #
  31. #############################################################################
  32. """ Implementation of CORBA Any type. """
  33.  
  34.  
  35. # Fnorb modules.
  36. import CORBA
  37.  
  38.  
  39. class Any:
  40.     """ Implementation of CORBA Any type. """
  41.  
  42.     def __init__(self, typecode, value):
  43.     """ Constructor.
  44.     
  45.     typecode -> TypeCode
  46.     value -> A value of the appropriate type
  47.  
  48.     """
  49.     self.__typecode = typecode
  50.     self.__value = value
  51.  
  52.     return
  53.  
  54.     #########################################################################
  55.     # CORBA interface.
  56.     #########################################################################
  57.  
  58.     def typecode(self):
  59.     """ Return the TypeCode of the Any. """
  60.  
  61.     return self.__typecode
  62.  
  63.     def value(self):
  64.     """ Return the value of the Any. """
  65.  
  66.     return self.__value
  67.  
  68.     #########################################################################
  69.     # Fnorb-specific interface.
  70.     #########################################################################
  71.     
  72.     def _fnorb_marshal(self, cursor):
  73.     """ Marshal myself onto an octet stream. """
  74.  
  75.     self.__typecode._fnorb_marshal(cursor)
  76.     self.__typecode._fnorb_marshal_value(cursor, self.__value)
  77.  
  78.     return
  79.  
  80.     def _fnorb_unmarshal(self, cursor):
  81.     """ Unmarshal myself from an octet stream. """
  82.  
  83.     self.__typecode = CORBA.TypeCodeFactory_init().unmarshal(cursor)
  84.     self.__value = self.__typecode._fnorb_unmarshal_value(cursor)
  85.  
  86.     return
  87.  
  88.     #########################################################################
  89.     # Methods for 'Any' arithmetic!
  90.     # 
  91.     # Arithmetic can only be performed on numeric (integer and floating point)
  92.     # 'Any's.
  93.     #
  94.     # Mixed mode arithmetic is not allowed.
  95.     #
  96.     #########################################################################
  97.  
  98.     def __add__(self, other):
  99.     """ Addition ;^) """
  100.  
  101.     # Type checking.
  102.     result_type = self.__check_numeric_types(other)
  103.  
  104.     return Any(result_type, self.__value + other.__value)
  105.  
  106.     def __sub__(self, other):
  107.     """ Subtraction ;^) """
  108.  
  109.     # Type checking.
  110.     result_type = self.__check_numeric_types(other)
  111.  
  112.     return Any(result_type, self.__value - other.__value)
  113.  
  114.     def __mul__(self, other):
  115.     """ Multiplication ;^) """
  116.  
  117.     # Type checking.
  118.     result_type = self.__check_numeric_types(other)
  119.  
  120.     return Any(result_type, self.__value * other.__value)
  121.  
  122.     def __div__(self, other):
  123.     """ Division ;^) """
  124.  
  125.     # Type checking.
  126.     result_type = self.__check_numeric_types(other)
  127.  
  128.     return Any(result_type, self.__value / other.__value)
  129.  
  130.     def __lshift__(self, other):
  131.     """ Left shift ;^) """
  132.  
  133.     # Type checking.
  134.     result_type = self.__check_integer_types(other)
  135.  
  136.     return Any(result_type, self.__value << other.__value)
  137.  
  138.     def __rshift__(self, other):
  139.     """ Right shift ;^) """
  140.  
  141.     # Type checking.
  142.     result_type = self.__check_integer_types(other)
  143.  
  144.     return Any(result_type, self.__value >> other.__value)
  145.  
  146.     def __and__(self, other):
  147.     """ And ;^) """
  148.  
  149.     # Type checking.
  150.     result_type = self.__check_integer_types(other)
  151.  
  152.     return Any(result_type, self.__value & other.__value)
  153.  
  154.     def __xor__(self, other):
  155.     """ Exclusive Or ;^) """
  156.  
  157.     # Type checking.
  158.     result_type = self.__check_integer_types(other)
  159.  
  160.     return Any(result_type, self.__value ^ other.__value)
  161.  
  162.     def __or__(self, other):
  163.     """ Or ;^) """
  164.  
  165.     # Type checking.
  166.     result_type = self.__check_integer_types(other)
  167.  
  168.     return Any(result_type, self.__value | other.__value)
  169.  
  170.     def __neg__(self):
  171.     """ Unary '-' """
  172.  
  173.     # Type checking.
  174.     if self.__typecode.kind() not in Any.__TK_INT:
  175.         raise TypeError, "Operation not allowed on this type."
  176.  
  177.     return Any(self.__typecode, -self.__value)
  178.  
  179.     def __pos__(self):
  180.     """ Unary '+' """
  181.  
  182.     # Type checking.
  183.     if self.__typecode.kind() not in Any.__TK_INT:
  184.         raise TypeError, "Operation not allowed on this type."
  185.  
  186.     return Any(self.__typecode, +self.__value)
  187.  
  188.     def __invert__(self):
  189.     """ Unary '~' """
  190.  
  191.     # Type checking.
  192.     if self.__typecode.kind() not in Any.__TK_INT:
  193.         raise TypeError, "Operation not allowed on this type."
  194.  
  195.     return Any(self.__typecode, ~self.__value)
  196.  
  197.     #########################################################################
  198.     # Private interface.
  199.     #########################################################################
  200.  
  201.     # Integer typecode kinds.
  202.     __TK_INT = [CORBA.tk_short,  CORBA.tk_long,  CORBA.tk_longlong,
  203.         CORBA.tk_ushort, CORBA.tk_ulong, CORBA.tk_ulonglong]
  204.  
  205.     # Floating point typecode kinds.
  206.     __TK_FP  = [CORBA.tk_float, CORBA.tk_double]
  207.     
  208.     def __check_numeric_types(self, other):
  209.     """ Type checking for 'Any' arithmetic. """
  210.  
  211.     if self.__typecode.kind() in Any.__TK_FP:
  212.         if other.__typecode.kind() in Any.__TK_FP:
  213.         result = CORBA.TC_double
  214.  
  215.         else:
  216.         raise TypeError, "Mixed mode arithmetic not allowed."
  217.  
  218.     elif self.__typecode.kind() in Any.__TK_INT:
  219.         if other.__typecode.kind() in Any.__TK_INT:
  220.         result = CORBA.TC_longlong
  221.  
  222.         else:
  223.         raise TypeError, "Mixed mode arithmetic not allowed."
  224.  
  225.     elif self.__typecode.kind() == CORBA.tk_fixed:
  226.         if other.__typecode.kind() == CORBA.tk_fixed:
  227.         result = CORBA.FixedTypeCode(0, 0)
  228.  
  229.         else:
  230.         raise TypeError, "Mixed mode arithmetic not allowed."
  231.     
  232.     else:
  233.         raise TypeError, "Operation not allowed on these types."
  234.  
  235.     return result
  236.  
  237.     def __check_integer_types(self, other):
  238.     """ Type checking for 'Any' arithmetic. """
  239.  
  240.     if self.__typecode.kind() in Any.__TK_INT:
  241.         if other.__typecode.kind() in Any.__TK_INT:
  242.         result = CORBA.TC_long
  243.  
  244.         else:
  245.         raise TypeError, "Mixed mode arithmetic not allowed."
  246.  
  247.     else:
  248.         raise TypeError, "Operation not allowed on these types."
  249.  
  250.     return result
  251.  
  252. #############################################################################
  253.  
  254.