[vtk-developers] PROPOSAL: vtkVariant ostream << operator

David Thompson dcthomp at sandia.gov
Thu Jul 16 15:31:30 EDT 2009


Hi all,

I would like to use vtkSetMacro with an ivar declared as a vtkVariant.
Currently this won't work because vtkVariant doesn't provide an ostream
operator (<<). There are at least 2 ways around this:

1. A special vtkSetVariantMacro() that does not attempt to print the
variant in any debug/warning/error messages.

2. Implement ostream& operator << ( ostream& os, const vtkVariant& val )

I propose to implement #2. A patch against the CVS trunk is attached. It
works for me on gcc 4.4/Linux. Does anyone have any objections,
comments, or hints on what problems the attached patch might have on
other platforms?

	Thanks,
	David

-------------- next part --------------
Index: vtkVariant.h
===================================================================
RCS file: /cvsroot/ParaView3/ParaView3/VTK/Common/vtkVariant.h,v
retrieving revision 1.24
diff -C 5 -r1.24 vtkVariant.h
*** vtkVariant.h	9 Jul 2009 22:38:05 -0000	1.24
--- vtkVariant.h	16 Jul 2009 19:28:16 -0000
***************
*** 344,354 ****
    bool operator<(const vtkVariant &other) const;
    bool operator>(const vtkVariant &other) const;
    bool operator<=(const vtkVariant &other) const;
    bool operator>=(const vtkVariant &other) const;
  
! 
  
  
  private:
    union
    {
--- 344,354 ----
    bool operator<(const vtkVariant &other) const;
    bool operator>(const vtkVariant &other) const;
    bool operator<=(const vtkVariant &other) const;
    bool operator>=(const vtkVariant &other) const;
  
!   friend ostream& operator << ( ostream& os, const vtkVariant& val );
  
  
  private:
    union
    {
***************
*** 418,423 ****
--- 418,425 ----
  {
  public:
    bool operator()(const vtkVariant &s1, const vtkVariant &s2) const;
  };
  
+ ostream& VTK_COMMON_EXPORT operator << ( ostream& os, const vtkVariant& val );
+ 
  #endif
Index: vtkVariant.cxx
===================================================================
RCS file: /cvsroot/ParaView3/ParaView3/VTK/Common/vtkVariant.cxx,v
retrieving revision 1.37
diff -C 5 -r1.37 vtkVariant.cxx
*** vtkVariant.cxx	9 Jul 2009 22:38:05 -0000	1.37
--- vtkVariant.cxx	16 Jul 2009 19:28:16 -0000
***************
*** 976,985 ****
--- 976,1077 ----
  bool vtkVariant::IsEqual(const vtkVariant& other) const
  {
    return this->operator==(other);
  }
  
+ ostream& operator << ( ostream& os, const vtkVariant& val )
+ {
+   if ( ! val.Valid )
+     {
+     os << "(invalid)";
+     return os;
+     }
+   switch ( val.Type )
+     {
+   case VTK_STRING:
+     if ( val.Data.String )
+       {
+       os << "\"" << val.Data.String->c_str() << "\"";
+       }
+     else
+       {
+       os << "\"\"";
+       }
+     break;
+   case VTK_UNICODE_STRING:
+     if ( val.Data.UnicodeString )
+       {
+       os << "\"" << val.Data.UnicodeString->utf8_str() << "\"";
+       }
+     else
+       {
+       os << "\"\"";
+       }
+     break;
+   case VTK_FLOAT:
+     os << val.Data.Float;
+     break;
+   case VTK_DOUBLE:
+     os << val.Data.Double;
+     break;
+   case VTK_CHAR:
+     os << val.Data.Char;
+     break;
+   case VTK_UNSIGNED_CHAR:
+     os << val.Data.UnsignedChar;
+     break;
+   case VTK_SIGNED_CHAR:
+     os << val.Data.SignedChar;
+     break;
+   case VTK_SHORT:
+     os << val.Data.Short;
+     break;
+   case VTK_UNSIGNED_SHORT:
+     os << val.Data.UnsignedShort;
+     break;
+   case VTK_INT:
+     os << val.Data.Int;
+     break;
+   case VTK_UNSIGNED_INT:
+     os << val.Data.UnsignedInt;
+     break;
+   case VTK_LONG:
+     os << val.Data.Long;
+     break;
+   case VTK_UNSIGNED_LONG:
+     os << val.Data.UnsignedLong;
+     break;
+ #if defined(VTK_TYPE_USE___INT64)
+   case VTK___INT64:
+     os << val.Data.__Int64;
+     break;
+   case VTK_UNSIGNED___INT64:
+     os << val.Data.Unsigned__Int64;
+     break;
+ #endif
+ #if defined(VTK_TYPE_USE_LONG_LONG)
+   case VTK_LONG_LONG:
+     os << val.Data.LongLong;
+     break;
+   case VTK_UNSIGNED_LONG_LONG:
+     os << val.Data.UnsignedLongLong;
+     break;
+ #endif
+   case VTK_OBJECT:
+     if ( val.Data.VTKObject )
+       {
+       os << "(" << val.Data.VTKObject->GetClassName() << ")" << hex << val.Data.VTKObject;
+       }
+     else
+       {
+       os << "(vtkObjectBase)0x0";
+       }
+     break;
+     }
+   return os;
+ }
+ 
  //----------------------------------------------------------------------------
  // Definition of ToNumeric if not already included above:
  
  #if !defined(VTK_VARIANT_TO_NUMERIC_CXX_INCLUDED)
  # define VTK_VARIANT_TO_NUMERIC_CXX_INCLUDED


More information about the vtk-developers mailing list