it_bus/nillable_value_base.h

00001 #ifndef _IT_BUS_NILLABLE_VALUE_BASE_H_
00002 #define _IT_BUS_NILLABLE_VALUE_BASE_H_
00003 
00004 // @Copyright 2003 IONA Technologies, Plc. All Rights Reserved.
00005 //
00006 
00007 #include <it_bus/any_type.h>
00008 #include <it_bus/nillable.h>
00009 #include <it_bus/complex_type_reader.h>
00010 #include <it_bus/complex_type_writer.h>
00011 #include <it_bus/no_data_exception.h>
00012 #include <it_bus/schema_type_traits.h>
00013 
00014 namespace IT_Reflect
00015 {
00016     template <class NillableT, class T>
00017     class NillableValueImplT;
00018 }
00019 
00020 namespace IT_Bus
00021 {
00032     template <class T>
00033     class NillableValueBase : public Nillable
00034     {
00035       public:
00036         typedef T ValueType;
00037 
00038         typedef IT_Reflect::NillableValueImplT<NillableValueBase<T>, T>
00039         IT_ReflectionType;
00040 
00041         friend class IT_Reflect::NillableValueImplT<NillableValueBase<T>, T>;
00042 
00043         static const QName&
00044         get_static_type();
00045         
00046         virtual ~NillableValueBase();
00047 
00048         virtual AnyType&
00049         copy(
00050             const AnyType& other
00051         );
00052 
00053         virtual const QName&
00054         get_type() const;
00055 
00056         virtual Boolean
00057         is_nil() const;
00058 
00059         virtual void
00060         set_nil();
00061 
00062         virtual void 
00063         write_value(
00064             const QName& name,
00065             ComplexTypeWriter& writer
00066         ) const throw((SerializationException));
00067 
00068         virtual void
00069         read_value(
00070             const QName& name,
00071             ComplexTypeReader& reader
00072         ) throw((DeserializationException));
00073 
00078         virtual const T&
00079         get() const throw((NoDataException));
00080         
00085         virtual T&
00086         get() throw((NoDataException));
00087         
00091         virtual void
00092         set(
00093             const T& data
00094         );
00095 
00099         virtual void
00100         set(
00101             const T *data
00102         );
00103 
00107         virtual void
00108         reset();
00109 
00110         NillableValueBase();
00111 
00112         NillableValueBase(
00113             const NillableValueBase& other
00114         );
00115 
00116         NillableValueBase(
00117             const T& other
00118         );
00119 
00120         NillableValueBase(
00121             const T *other
00122         );
00123 
00124         NillableValueBase&
00125         operator=(
00126             const NillableValueBase& other
00127         );
00128 
00129       private:
00130 
00131         typedef NillableValueBase ThisType; // needed for DYNAMIC_CAST macro
00132         Boolean m_is_nil;
00133         T       m_data;
00134     };
00135 
00136     template<class T>
00137     const QName&
00138     NillableValueBase<T>::get_static_type()
00139     {
00140         return SchemaTypeTraits<T>::type_name();
00141     }
00142 
00143     template <class T>
00144     NillableValueBase<T>::NillableValueBase() 
00145         :
00146         m_is_nil(IT_TRUE)
00147     {
00148         // Complete.
00149     }
00150 
00151     template <class T>
00152     NillableValueBase<T>::NillableValueBase(
00153         const NillableValueBase<T>& other
00154     ) 
00155         :
00156         m_is_nil(other.m_is_nil),
00157         m_data(other.m_data)
00158     {
00159         // Complete.
00160     }
00161 
00162 
00163     template <class T>
00164     NillableValueBase<T>::NillableValueBase(
00165         const T& data
00166     ) 
00167         :
00168         m_is_nil(IT_FALSE),
00169         m_data(data)
00170     {
00171         // Complete.
00172     }
00173     
00174     template <class T>
00175     NillableValueBase<T>::NillableValueBase(
00176         const T *data
00177     ) 
00178     {
00179         set(data);
00180     }
00181 
00182     template <class T>
00183     NillableValueBase<T>::~NillableValueBase()
00184     {
00185         // Complete.
00186     }
00187 
00188     template <class T>
00189     NillableValueBase<T>&
00190     NillableValueBase<T>::operator=(
00191         const NillableValueBase<T>& other
00192     ) 
00193     {
00194         if (other.is_nil())
00195         {
00196             reset();
00197         }
00198         else
00199         {
00200             set(other.get());
00201         }
00202         return *this;
00203     }
00204 
00205     template <class T>
00206     AnyType&
00207     NillableValueBase<T>::copy(
00208         const AnyType& rhs
00209     ) 
00210     {
00211         if (dynamic_cast<const Nillable&>(rhs).is_nil())
00212         {
00213             set_nil();
00214         }
00215         else
00216         {
00217             IT_Reflect::Copier<ThisType>::copy(*this, rhs);
00218         }
00219         return *this;
00220     }
00221 
00222     template <class T>
00223     IT_Bus::Boolean
00224     NillableValueBase<T>::is_nil() const
00225     {
00226         return m_is_nil;
00227     }
00228 
00229     template <class T>
00230     void
00231     NillableValueBase<T>::set_nil() 
00232     {
00233         reset();
00234     }
00235 
00236     template <class T>
00237     void
00238     NillableValueBase<T>::write_value(
00239         const QName& element_name,
00240         ComplexTypeWriter& writer
00241     ) const throw((SerializationException)) 
00242     {
00243         if (is_nil())
00244         {
00245             throw SerializationException("Attempt to write nil value");
00246         }
00247         writer.write(element_name, m_data);
00248     }
00249 
00250     template <class T>
00251     void
00252     NillableValueBase<T>::read_value(
00253         const QName& name,
00254         ComplexTypeReader& reader
00255     ) throw((DeserializationException))
00256     {
00257         reader.read(name, m_data);
00258         m_is_nil = IT_FALSE;
00259     }
00260 
00261     template <class T>    
00262     T&
00263     NillableValueBase<T>::get() throw((NoDataException))
00264     {
00265         if (is_nil())
00266         {
00267             throw NoDataException("Nillable value has no data");
00268         }
00269         return m_data;
00270     }
00271 
00272     template <class T>    
00273     const T&
00274     NillableValueBase<T>::get() const throw((NoDataException))
00275     {
00276         if (is_nil())
00277         {
00278             throw NoDataException("Nillable value has no data");
00279         }
00280         return m_data;
00281     }
00282 
00283     template <class T>
00284     void
00285     NillableValueBase<T>::set(
00286         const T& data
00287     )
00288     {
00289         m_is_nil = IT_FALSE;
00290         m_data = data;
00291     }
00292     
00293     template <class T>
00294     void
00295     NillableValueBase<T>::set(
00296         const T *data
00297     )
00298     {
00299         if (data == 0)
00300         {
00301             m_is_nil = IT_TRUE;
00302         }
00303         else
00304         {
00305             m_is_nil = IT_FALSE;
00306             m_data = *data;
00307         }
00308     }
00309  
00310     template <class T>
00311     void
00312     NillableValueBase<T>::reset()
00313     {
00314         m_is_nil = IT_TRUE;
00315     }
00316 
00317     template <class T>
00318     const QName&
00319     NillableValueBase<T>::get_type() const
00320     {
00321         return get_static_type();
00322     }
00323 }
00324 
00325 #endif  

Generated on Tue Mar 20 15:27:42 2007 for Artix by  doxygen 1.5.1-p1