# 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-2010 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   typedef uint32_t Type;
    00034   VL(uint32_t vl = 0) : ValueLength(vl) { }
    00035
    00036   // FIXME: ugly
    00037   static uint32_t GetVL32Max() { return 0xFFFFFFFF; }
    00038   static uint16_t GetVL16Max() { return 0xFFFF; }
    00039
    00040   bool IsUndefined() const {
    00041     return ValueLength == 0xFFFFFFFF;
    00042   }
    00043   void SetToUndefined() {
    00044     ValueLength = 0xFFFFFFFF;
    00045   }
    00046
    00048   bool IsOdd() const {
    00049     return !IsUndefined() && ValueLength % 2;
    00050   }
    00051
    00053   VL& operator+=(VL const &vl) {
    00054     ValueLength += vl.ValueLength;
    00055     return *this;
    00056   }
    00057   VL& operator++() {
    00058     ++ValueLength;
    00059     return *this;
    00060   }
    00061   VL operator++(int) {
    00062     uint32_t tmp(ValueLength);
    00063     ++ValueLength;
    00064     return tmp;
    00065   }
    00066
    00067   operator uint32_t () const { return ValueLength; }
    00068
    00069   VL GetLength() const {
    00070     // VL cannot know it's length...well in implicit yes...
    00071     // TODO: need to check we cannot call this function from an Explicit element
    00072     return 4;
    00073   }
    00074
    00075   friend std::ostream& operator<<(std::ostream& os, const VL& vl);
    00076
    00077   // PURPOSELY not implemented (could not differenciate 16bits vs 32bits VL)
    00078   //friend std::istream& operator>>(std::istream& is, VL& n);
    00079
    00080   template <typename TSwap>
    00081   std::istream &Read(std::istream &is)
    00082     {
    00083     is.read((char*)(&ValueLength), sizeof(uint32_t));
    00084     TSwap::SwapArray(&ValueLength,1);
    00085     return is;
    00086     }
    00087
    00088   template <typename TSwap>
    00089   std::istream &Read16(std::istream &is)
    00090     {
    00091     uint16_t copy;
    00092     is.read((char*)(&copy), sizeof(uint16_t));
    00093     TSwap::SwapArray(&copy,1);
    00094     ValueLength = copy;
    00095     assert( ValueLength <=  65535 /*UINT16_MAX*/ ); // ?? doh !
    00096     return is;
    00097     }
    00098
    00099   template <typename TSwap>
    00100   const std::ostream &Write(std::ostream &os) const
    00101     {
    00102     uint32_t copy = ValueLength;
    00103 #ifndef GDCM_WRITE_ODD_LENGTH
    00104     if( IsOdd() )
    00105       {
    00106       ++copy;
    00107       }
    00108 #endif
    00109     TSwap::SwapArray(&copy,1);
    00110     return os.write((char*)(&copy), sizeof(uint32_t));
    00111     }
    00112
    00113   template <typename TSwap>
    00114   const std::ostream &Write16(std::ostream &os) const
    00115     {
    00116     assert( ValueLength <=   65535 /*UINT16_MAX*/ );
    00117     uint16_t copy = ValueLength;
    00118 #ifndef GDCM_WRITE_ODD_LENGTH
    00119     if( IsOdd() )
    00120       {
    00121       ++copy;
    00122       }
    00123 #endif
    00124     TSwap::SwapArray(&copy,1);
    00125     return os.write((char*)(&copy), sizeof(uint16_t));
    00126     }
    00127
    00128 private:
    00129   uint32_t ValueLength;
    00130 };
    00131 //----------------------------------------------------------------------------
    00132 inline std::ostream& operator<<(std::ostream& os, const VL& val)
    00133 {
    00134   os << /*std::hex <<*/ val.ValueLength;
    00135   return os;
    00136 }
    00137
    00138 } // end namespace gdcm
    00139
    00140 #endif //GDCMVL_H