aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/blob.h
blob: 6ce0c67c00ddef8fca9e4820c4ab401ba5155b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2007-2019 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 <stdint.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.
     *
     * If you're dealing with data that contains language, such as human text,
     * then use the Bu::Text class instead.
     */
    class Blob
    {
    public:
        Blob();
        Blob( const Blob &rSrc );
        Blob( const char *pSrc );
        Blob( const void *pSrc, int32_t iSize );
        virtual ~Blob();

        int32_t getSize() const;
        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;

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

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

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

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

            iterator &operator=( 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 );

        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++( int );
            const_iterator &operator++();
            const_iterator &operator--( int );
            const_iterator &operator--();
            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=( iterator &rRhs );
            const_iterator &operator=( const_iterator &rRhs );

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

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

    private:
        char *pData;
        int32_t iSize;
    };

    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 );
}

#endif