aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsa/blob.h
blob: 7c9dbbed318598b834a410956167f4f4579d5714 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/08/20
 * @version $Id$
 * @file    blob.h
 * @brief   Definition of Blob class
 *
 */

#pragma once

#include <string.h>
#include <stdlib.h>

#include <string>

namespace fsa {

// {{{ class Blob

/**
 * @class Blob
 * @brief %Blob (binary large object) class.
 *
 * Representation of a blob (binary large object). Supports assign
 * method, access to size and data, and comparison operators.
 */
class Blob {
private:
  /** Size of data. */
  unsigned int  _size;
  /** Pointer to the data. */
  void*   _data;
public:

  /**
   * @brief Default constructor
   *
   * Creates an empty blob.
   */
  Blob() : _size(0), _data(NULL) {}

  /**
   * @brief Constructor
   *
   * Creates a blob from a character string. The string must be zero
   * terminated.
   *
   * @param str Pointer to input string.
   */
  Blob(const char *str) : _size(strlen(str)+1), _data((void*)strdup(str)) {}

  /**
   * @brief Constructor
   *
   * Creates a blob from arbitrary data.
   *
   * @param data Pointer to data.
   * @param size Size of the data.
   */
  Blob(const void *data, unsigned int size) : _size(size), _data(malloc(size))
  { memcpy(_data,data,_size); }

  /**
   * @brief Copy constructor
   *
   * @param b Blob to copy.
   */
  Blob(const Blob& b) : _size(b._size), _data(malloc(_size))
  { memcpy(_data,b._data,_size); }

  /**
   * @brief Constructor
   *
   * Creates a blob from std::string.
   *
   * @param s Reference to input string.
   */
  Blob(const std::string &s) : _size(s.size()), _data(malloc(_size))
  { s.copy((char*)_data,_size); }

  /** Destructor */
  ~Blob() { if(_data!=NULL) free(_data); }

  /**
   * @brief Get data size.
   *
   * @return Data size.
   */
  unsigned int size() const { return _size; }

  /**
   * @brief Get data.
   *
   * @return Pointer to data. Valid as long as the blob object exists
   *         and is not modified.
   */
  const void*  data() const { return _data; }

  /**
   * @brief Reassign the blob.
   *
   * @param s Input string
   */
  void assign(const std::string &s)
  {
    if(_data!=NULL) free(_data);
    _size=s.size();
    _data=malloc(s.size());
    s.copy((char*)_data,_size);
  }

  /**
   * @brief Less-than operator.
   *
   * @param b Blob to compare.
   */
  bool operator<(const Blob& b) const;

  /**
   * @brief Greater-than operator.
   *
   * @param b Blob to compare.
   */
  bool operator>(const Blob& b) const;

  /**
   * @brief Equals operator.
   *
   * @param b Blob to compare.
   */
  bool operator==(const Blob& b) const;

};

// }}}

} // namespace fsa