~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/data_ref.h

  • Committer: Olaf van der Spek
  • Date: 2011-07-05 13:16:34 UTC
  • mto: This revision was merged to the branch mainline in revision 2384.
  • Revision ID: olafvdspek@gmail.com-20110705131634-f7g1fjro5slibmdj
Add data_ref.h
Use str_ref for append_identifier

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Drizzle
 
2
 * Copyright (C) 2011 Olaf van der Spek
 
3
 * 
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation, either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
#pragma once
 
19
 
 
20
#include <cstddef>
 
21
#include <cstring>
 
22
 
 
23
template <class T>
 
24
class data_ref_basic
 
25
{
 
26
public:
 
27
  data_ref_basic()
 
28
  {
 
29
  }
 
30
 
 
31
  template <class U>
 
32
  data_ref_basic(U& c)
 
33
  {
 
34
    if (c.empty())
 
35
      clear();
 
36
    else
 
37
      assign(&*c.begin(), &*c.end());
 
38
  }
 
39
 
 
40
  data_ref_basic(const void* b, const void* e)
 
41
  {
 
42
    assign(b, e);
 
43
  }
 
44
 
 
45
  data_ref_basic(const void* b, size_t sz)
 
46
  {
 
47
    assign(b, sz);
 
48
  }
 
49
 
 
50
  explicit data_ref_basic(const char* b)
 
51
  {
 
52
    assign(b, strlen(b));
 
53
  }
 
54
 
 
55
  void clear()
 
56
  {
 
57
    begin_ = NULL;
 
58
    end_ = NULL;
 
59
  }
 
60
 
 
61
  void assign(const void* b, const void* e)
 
62
  {
 
63
                begin_ = reinterpret_cast<T>(b);
 
64
                end_ = reinterpret_cast<T>(e);
 
65
  }
 
66
 
 
67
  void assign(const void* b, size_t sz)
 
68
  {
 
69
                begin_ = reinterpret_cast<T>(b);
 
70
                end_ = begin_ + sz;
 
71
  }
 
72
 
 
73
  T begin() const
 
74
  {
 
75
    return begin_;
 
76
  }
 
77
 
 
78
  T end() const
 
79
  {
 
80
    return end_;
 
81
  }
 
82
 
 
83
  T data() const
 
84
  {
 
85
    return begin();
 
86
  }
 
87
 
 
88
  size_t size() const
 
89
  {
 
90
    return end() - begin();
 
91
  }
 
92
 
 
93
  bool empty() const
 
94
  {
 
95
    return begin() == end();
 
96
  }
 
97
private:
 
98
  T begin_;
 
99
  T end_;
 
100
};
 
101
 
 
102
typedef data_ref_basic<const unsigned char*> data_ref;
 
103
typedef data_ref_basic<const char*> str_ref;