~drizzle-trunk/drizzle/development

2318.8.1 by Olaf van der Spek
Add data_ref.h
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>
2385.1.6 by Olaf van der Spek
Use str_ref
22
#include <ostream>
2430.1.7 by Olaf van der Spek
Use size()
23
#include <string>
2318.8.1 by Olaf van der Spek
Add data_ref.h
24
25
template <class T>
26
class data_ref_basic
27
{
28
public:
29
  data_ref_basic()
30
  {
2433.1.1 by Olaf van der Spek
Refactor
31
    clear();
2318.8.1 by Olaf van der Spek
Add data_ref.h
32
  }
33
34
  template <class U>
2385.1.7 by Olaf van der Spek
Rename set_db to set_schema
35
  data_ref_basic(const U& c)
2318.8.1 by Olaf van der Spek
Add data_ref.h
36
  {
2385.1.3 by Olaf van der Spek
Refactor getTableName
37
    if (c.begin() == c.end())
2318.8.1 by Olaf van der Spek
Add data_ref.h
38
      clear();
39
    else
40
      assign(&*c.begin(), &*c.end());
41
  }
42
43
  data_ref_basic(const void* b, const void* e)
44
  {
45
    assign(b, e);
46
  }
47
48
  data_ref_basic(const void* b, size_t sz)
49
  {
50
    assign(b, sz);
51
  }
52
53
  explicit data_ref_basic(const char* b)
54
  {
55
    assign(b, strlen(b));
56
  }
57
2433.1.5 by Olaf van der Spek
Use assign(), data() and size()
58
  void clear()
2318.8.1 by Olaf van der Spek
Add data_ref.h
59
  {
2440.2.28 by Olaf van der Spek
Refactor
60
    begin_ = end_ = reinterpret_cast<T>("");
2318.8.1 by Olaf van der Spek
Add data_ref.h
61
  }
62
63
  void assign(const void* b, const void* e)
64
  {
2318.8.9 by Olaf van der Spek
Use boost::to_upper
65
    begin_ = reinterpret_cast<T>(b);
66
    end_ = reinterpret_cast<T>(e);
2318.8.1 by Olaf van der Spek
Add data_ref.h
67
  }
68
69
  void assign(const void* b, size_t sz)
70
  {
2318.8.9 by Olaf van der Spek
Use boost::to_upper
71
    begin_ = reinterpret_cast<T>(b);
72
    end_ = begin_ + sz;
2318.8.1 by Olaf van der Spek
Add data_ref.h
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
private:
100
  T begin_;
101
  T end_;
102
};
103
104
typedef data_ref_basic<const unsigned char*> data_ref;
105
typedef data_ref_basic<const char*> str_ref;
2430.1.7 by Olaf van der Spek
Use size()
106
107
inline std::ostream& operator<<(std::ostream& os, str_ref v)
108
{
2440.2.10 by Olaf van der Spek
Use data_ref
109
  return os.write(v.data(), v.size());
2430.1.7 by Olaf van der Spek
Use size()
110
}
111
112
inline std::string to_string(str_ref v)
113
{
114
  return std::string(v.data(), v.size());
115
}