~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/data_ref.h

  • Committer: Mark Atwood
  • Date: 2011-08-12 04:08:33 UTC
  • mfrom: (2385.2.17 refactor5)
  • Revision ID: me@mark.atwood.name-20110812040833-u6j85nc6ahuc0dtz
mergeĀ lp:~olafvdspek/drizzle/refactor5

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
#include <ostream>
 
23
 
 
24
template <class T>
 
25
class data_ref_basic
 
26
{
 
27
public:
 
28
  data_ref_basic()
 
29
  {
 
30
  }
 
31
 
 
32
  template <class U>
 
33
  data_ref_basic(const U& c)
 
34
  {
 
35
    if (c.begin() == c.end())
 
36
      clear();
 
37
    else
 
38
      assign(&*c.begin(), &*c.end());
 
39
  }
 
40
 
 
41
  data_ref_basic(const void* b, const void* e)
 
42
  {
 
43
    assign(b, e);
 
44
  }
 
45
 
 
46
  data_ref_basic(const void* b, size_t sz)
 
47
  {
 
48
    assign(b, sz);
 
49
  }
 
50
 
 
51
  explicit data_ref_basic(const char* b)
 
52
  {
 
53
    assign(b, strlen(b));
 
54
  }
 
55
 
 
56
  const data_ref_basic& clear()
 
57
  {
 
58
    begin_ = NULL;
 
59
    end_ = NULL;
 
60
    return *this;
 
61
  }
 
62
 
 
63
  void assign(const void* b, const void* e)
 
64
  {
 
65
    begin_ = reinterpret_cast<T>(b);
 
66
    end_ = reinterpret_cast<T>(e);
 
67
  }
 
68
 
 
69
  void assign(const void* b, size_t sz)
 
70
  {
 
71
    begin_ = reinterpret_cast<T>(b);
 
72
    end_ = begin_ + sz;
 
73
  }
 
74
 
 
75
  T begin() const
 
76
  {
 
77
    return begin_;
 
78
  }
 
79
 
 
80
  T end() const
 
81
  {
 
82
    return end_;
 
83
  }
 
84
 
 
85
  T data() const
 
86
  {
 
87
    return begin();
 
88
  }
 
89
 
 
90
  size_t size() const
 
91
  {
 
92
    return end() - begin();
 
93
  }
 
94
 
 
95
  bool empty() const
 
96
  {
 
97
    return begin() == end();
 
98
  }
 
99
 
 
100
  friend std::ostream& operator<<(std::ostream& os, data_ref_basic<T> v)
 
101
  {
 
102
    os.write(v.data(), v.size());
 
103
    return os;
 
104
  }
 
105
private:
 
106
  T begin_;
 
107
  T end_;
 
108
};
 
109
 
 
110
typedef data_ref_basic<const unsigned char*> data_ref;
 
111
typedef data_ref_basic<const char*> str_ref;