Blob.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2011 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef BLOB_H
00011 #define BLOB_H
00012 
00013 #include "AppConfig.h"
00014 #include "DataVariantValidator.h"
00015 
00016 #include <string.h>
00017 #include <vector>
00018 
00019 /**
00020  * A Binary Large OBject (BLOB).  This class exists because the serialization/deserialization of
00021  * std::vector<unsigned char> is not recommended for large quantities of data even though this is a natural
00022  * representation of a large quantity of data. XML representations of this class are not generally human readable.
00023  * This class does not provide a set() function and that is by design. The StringUtilities::toDisplayString() and
00024  * StringUtilities::fromDisplayString() specializations of this class are not guaranteed to return useful information.
00025  */
00026 class Blob
00027 {
00028 public:
00029    /**
00030     * Construct a default object.  The value returned from get() will be an empty vector.
00031     * This constructor shouldn't normally be used; you should use the constructor that
00032     * takes an initial value.  This constructor is provided because in order to allow
00033     * this type inside a DataVariant, a default constructor is required.
00034     */
00035    Blob() {}
00036 
00037    /** 
00038     * Construct an object from a std::vector of unsigned char data.
00039     *
00040     * @param value
00041     *        An array containing the data.
00042     */
00043    explicit Blob(const std::vector<unsigned char>& value) : mValue(value) {}
00044 
00045    /**
00046     * Construct a blob from a C array of octets.
00047     *
00048     * @param pValue
00049     *        C array of octets with len bytes. If this is NULL, an empty blob is created.
00050     * @param len
00051     *        The number of octets in the array.
00052     */
00053    explicit Blob(const void* pValue, size_t len)
00054    {
00055       if (pValue == NULL)
00056       {
00057          mValue.clear();
00058       }
00059       else
00060       {
00061          mValue.resize(len);
00062          memcpy(&mValue.front(), pValue, len);
00063       }
00064    }
00065 
00066    /**
00067     * Assignment operator.
00068     * 
00069     * @param right
00070     *        The right hand of the assignment.
00071     *
00072     * @return A reference to \c *this, which has been changed to have a copy of the contents of \em right.
00073     */
00074    BROKEN_INLINE_HINT Blob& operator=(const Blob& right)
00075    {
00076       if (this != &right)
00077       {
00078          mValue = right.mValue;
00079       }
00080       return *this;
00081    }
00082 
00083    /**
00084     * Compares two Blob objects.
00085     *
00086     * @param right
00087     *        The object to compare with.
00088     *
00089     * @return Returns \c true if both Blob objects wrap equivalent data.
00090     */
00091    bool operator==(const Blob& right) const
00092    {
00093       return mValue == right.mValue;
00094    }
00095 
00096    /**
00097     * Access the data.
00098     * This class does not provide a set() function and
00099     * that is by design.  You must use the Blob constructor to set a value into this object.
00100     *
00101     * @returns Returns the wrapped data.
00102     */
00103    const std::vector<unsigned char>& get() const
00104    {
00105       return mValue;
00106    }
00107 
00108    /**
00109     * Access the data.
00110     *
00111     * @returns Returns the data.
00112     */
00113    operator const std::vector<unsigned char>&() const
00114    {
00115       return mValue;
00116    }
00117 
00118 private:
00119    std::vector<unsigned char> mValue;
00120 };
00121 
00122 
00123 /**
00124  * \cond INTERNAL
00125  * These template specializations are required to allow these types to be put into a DataVariant.
00126  */
00127 template <> class VariantTypeValidator<Blob> {};
00128 /// \endcond
00129 
00130 #endif

Software Development Kit - Opticks 4.9.0 Build 16218