/*
 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_BLOB_H
#define BU_BLOB_H

#include "bu/config.h"
#include "bu/archivebase.h"

namespace Bu
{
    /**
     * Blob contans a sequence of binary values. This is basically the
     * replacement for the old libbu++ Bu::String, std::string classes, or the
     * tradition C char * string.  The Blob is static and immutable, and a good
     * choice when dealing with any sequence of binary data that you don't
     * need to interpret further.
     *
     * You may note that Blob doesn't have any append, prepend, etc. methods.
     * This is because Blobs are designed to be more or less immutable. If you
     * need that functionality (which is quite likely), check out
     * Bu::BlobBuilder which offers a flexible, fast interface to build Blobs
     * dynamically, piece at a time.
     *
     * If you're dealing with data that contains language, such as human text,
     * then use the Bu::Text class instead.
     */
    class Blob
    {
    friend ArchiveBase &operator<<( ArchiveBase &ar, const Blob &s );
    friend ArchiveBase &operator>>( ArchiveBase &ar, Blob &s );
    public:
        class iterator;
        class const_iterator;

    public:
        Blob();
        Blob( const Blob &rSrc );
        Blob( const class BlobBuilder &rSrc );
        Blob( const char *pSrc );
        Blob( const void *pSrc, int32_t iSize );
        Blob( int32_t iSize );
        Blob( const const_iterator &iStart );
        Blob( const const_iterator &iStart, const const_iterator &iEnd );

        // Obviously temporary
        Blob( const Bu::String &rSrc );
        virtual ~Blob();

        void setSize( int32_t iSize );
        int32_t getSize() const;
        void setData( const char *pSrc, int32_t iSize );
        char *getData() const;
        char *c_str() const;

        char &operator[]( int32_t iIndex );
        char operator[]( int32_t iIndex ) const;

        bool isEmpty() const;
        bool isNull() const;
        bool isNullOrEmpty() const;

        Blob &operator=( const Blob &rRhs );
        Blob &operator=( const char *pRhs );

        bool operator==( const Blob &rRhs ) const;
        bool operator==( const char *rRhs ) const;
        bool operator!=( const Blob &rRhs ) const;
        bool operator!=( const char *rRhs ) const;
        bool operator<( const Blob &rRhs ) const;
        bool operator<( const char *rRhs ) const;
        bool operator<=( const Blob &rRhs ) const;
        bool operator<=( const char *rRhs ) const;
        bool operator>( const Blob &rRhs ) const;
        bool operator>( const char *rRhs ) const;
        bool operator>=( const Blob &rRhs ) const;
        bool operator>=( const char *rRhs ) const;

        iterator begin();
        const_iterator begin() const;
        iterator end();
        const_iterator end() const;
        iterator rbegin();
        const_iterator rbegin() const;
        iterator rend();
        const_iterator rend() const;

        class iterator
        {
        friend class Blob;
        friend class const_iterator;
        private:
            iterator( Blob *pBlob, bool bForward );
            iterator( Blob *pBlob, int32_t iIndex, bool bForward );

        public:
            iterator();
            iterator( const iterator &rSrc );

            bool isValid() const;
            operator bool() const;
            char &operator *();

            iterator &operator++( int32_t );
            iterator &operator++();
            iterator &operator--( int32_t );
            iterator &operator--();
            iterator &operator+=( int32_t iDelta );
            iterator &operator-=( int32_t iDelta );
            iterator operator+( int32_t iDelta ) const;
            iterator operator-( int32_t iDelta ) const;
            bool operator==( const iterator &rRhs );
            bool operator==( const const_iterator &rRhs );
            bool operator!=( const iterator &rRhs );
            bool operator!=( const const_iterator &rRhs );

            iterator &operator=( const iterator &rRhs );

        private:
            Blob *pBlob;
            int32_t iIndex;
            bool bForward;
        };

        class const_iterator
        {
        friend class Blob;
        friend class iterator;
        private:
            const_iterator( const Blob *pBlob, bool bForward );
            const_iterator( const Blob *pBlob, int32_t iIndex, bool bForward );

        public:
            const_iterator();
            const_iterator( const const_iterator &rSrc );
            const_iterator( const iterator &rSrc );
            
            bool isValid() const;
            operator bool() const;
            char operator *() const;

            const_iterator &operator++( int32_t );
            const_iterator &operator++();
            const_iterator &operator--( int32_t );
            const_iterator &operator--();
            const_iterator &operator+=( int32_t iDelta );
            const_iterator &operator-=( int32_t iDelta );
            const_iterator operator+( int32_t iDelta ) const;
            const_iterator operator-( int32_t iDelta ) const;
            bool operator==( const iterator &rRhs );
            bool operator==( const const_iterator &rRhs );
            bool operator!=( const iterator &rRhs );
            bool operator!=( const const_iterator &rRhs );

            const_iterator &operator=( const iterator &rRhs );
            const_iterator &operator=( const const_iterator &rRhs );

        private:
            const Blob *pBlob;
            int32_t iIndex;
            bool bForward;
        };

    private:
        char *pData;
        int32_t iSize;
    };

    ArchiveBase &operator<<( ArchiveBase &ar, const Blob &s );
    ArchiveBase &operator>>( ArchiveBase &ar, Blob &s );

    template<typename T>
    uint32_t __calcHashCode( const T &k );

    template<typename T>
    bool __cmpHashKeys( const T &a, const T &b );

    template<> uint32_t __calcHashCode<Blob>( const Blob &k );
    template<> bool __cmpHashKeys<Blob>(
        const Blob &a, const Blob &b );
    
    template<typename t> void __tracer_format( const t &v );
    template<> void __tracer_format<Blob>( const Blob &v );

    class Formatter;
    Formatter &operator<<( Formatter &rOut, const Blob &b );
    Formatter &operator>>( Formatter &fIn, Blob &b );
}

#endif

