# gdcmVL.h

Go to the documentation of this file.

    00001 /*=========================================================================
    00002
    00003   Program: GDCM (Grassroots DICOM). A DICOM library
    00004   Module:  $URL$
    00005
    00006   Copyright (c) 2006-2009 Mathieu Malaterre
    00007   All rights reserved.
    00008   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
    00009
    00010      This software is distributed WITHOUT ANY WARRANTY; without even
    00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    00012      PURPOSE.  See the above copyright notice for more information.
    00013
    00014 =========================================================================*/
    00015 #ifndef __gdcmVL_h
    00016 #define __gdcmVL_h
    00017
    00018 #include "gdcmTypes.h"
    00019
    00020 #include <iostream>
    00021
    00022 namespace gdcm
    00023 {
    00024
    00030 class GDCM_EXPORT VL
    00031 {
    00032 public:
    00033   VL(uint32_t vl = 0) : ValueLength(vl) { }
    00034
    00035   // FIXME: ugly
    00036   static uint32_t GetVL32Max() { return 0xFFFFFFFF; }
    00037   static uint16_t GetVL16Max() { return 0xFFFF; }
    00038
    00039   bool IsUndefined() const {
    00040     return ValueLength == 0xFFFFFFFF;
    00041   }
    00042   void SetToUndefined() {
    00043     ValueLength = 0xFFFFFFFF;
    00044   }
    00045
    00046   bool IsOdd() const {
    00047     return !IsUndefined() && ValueLength % 2;
    00048   }
    00049
    00050   VL& operator+=(VL const &vl) {
    00051     ValueLength += vl.ValueLength;
    00052     return *this;
    00053   }
    00054   VL& operator++() {
    00055     ++ValueLength;
    00056     return *this;
    00057   }
    00058   VL operator++(int) {
    00059     uint32_t tmp(ValueLength);
    00060     ++ValueLength;
    00061     return tmp;
    00062   }
    00063
    00064   operator uint32_t () const { return ValueLength; }
    00065
    00066   VL GetLength() const {
    00067     // VL cannot know it's length...well in implicit yes...
    00068     // TODO: need to check we cannot call this function from an Explicit element
    00069     return 4;
    00070   }
    00071
    00072   friend std::ostream& operator<<(std::ostream& os, const VL& vl);
    00073
    00074   // PURPOSELY not implemented (could not differenciate 16bits vs 32bits VL)
    00075   //friend std::istream& operator>>(std::istream& is, VL& n);
    00076
    00077   template <typename TSwap>
    00078   std::istream &Read(std::istream &is)
    00079     {
    00080     is.read((char*)(&ValueLength), sizeof(uint32_t));
    00081     TSwap::SwapArray(&ValueLength,1);
    00082     return is;
    00083     }
    00084
    00085   template <typename TSwap>
    00086   std::istream &Read16(std::istream &is)
    00087     {
    00088     uint16_t copy;
    00089     is.read((char*)(&copy), sizeof(uint16_t));
    00090     TSwap::SwapArray(&copy,1);
    00091     ValueLength = copy;
    00092     assert( ValueLength <=  65535 /*UINT16_MAX*/ ); // ?? doh !
    00093     return is;
    00094     }
    00095
    00096   template <typename TSwap>
    00097   const std::ostream &Write(std::ostream &os) const
    00098     {
    00099     uint32_t copy = ValueLength;
    00100 #ifndef GDCM_WRITE_ODD_LENGTH
    00101     if( IsOdd() )
    00102       {
    00103       ++copy;
    00104       }
    00105 #endif
    00106     TSwap::SwapArray(&copy,1);
    00107     return os.write((char*)(&copy), sizeof(uint32_t));
    00108     }
    00109
    00110   template <typename TSwap>
    00111   const std::ostream &Write16(std::ostream &os) const
    00112     {
    00113     assert( ValueLength <=   65535 /*UINT16_MAX*/ );
    00114     uint16_t copy = ValueLength;
    00115 #ifndef GDCM_WRITE_ODD_LENGTH
    00116     if( IsOdd() )
    00117       {
    00118       ++copy;
    00119       }
    00120 #endif
    00121     TSwap::SwapArray(&copy,1);
    00122     return os.write((char*)(&copy), sizeof(uint16_t));
    00123     }
    00124
    00125 private:
    00126   uint32_t ValueLength;
    00127 };
    00128 //----------------------------------------------------------------------------
    00129 inline std::ostream& operator<<(std::ostream& os, const VL& val)
    00130 {
    00131   os << /*std::hex <<*/ val.ValueLength;
    00132   return os;
    00133 }
    00134
    00135 } // end namespace gdcm
    00136
    00137 #endif //__gdcmVL_h