~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/data_ref.h

  • Committer: Mark Atwood
  • Date: 2011-12-30 22:59:59 UTC
  • mfrom: (2478.1.3 drizzle-build)
  • Revision ID: me@mark.atwood.name-20111230225959-m3wdvqiymv3a4q2w
mergeĀ lp:~brianaker/drizzle/yacc-merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <cstddef>
21
21
#include <cstring>
22
22
#include <ostream>
 
23
#include <string>
23
24
 
24
25
template <class T>
25
26
class data_ref_basic
27
28
public:
28
29
  data_ref_basic()
29
30
  {
 
31
    clear();
30
32
  }
31
33
 
32
34
  template <class U>
33
35
  data_ref_basic(const U& c)
34
36
  {
35
 
    if (c.begin() == c.end())
36
 
      clear();
37
 
    else
38
 
      assign(&*c.begin(), &*c.end());
 
37
    assign(&*c.begin(), &*c.end());
39
38
  }
40
39
 
41
40
  data_ref_basic(const void* b, const void* e)
53
52
    assign(b, strlen(b));
54
53
  }
55
54
 
56
 
  const data_ref_basic& clear()
 
55
  void clear()
57
56
  {
58
 
    begin_ = NULL;
59
 
    end_ = NULL;
60
 
    return *this;
 
57
    begin_ = end_ = reinterpret_cast<T>("");
61
58
  }
62
59
 
63
60
  void assign(const void* b, const void* e)
97
94
    return begin() == end();
98
95
  }
99
96
 
100
 
  friend std::ostream& operator<<(std::ostream& os, data_ref_basic<T> v)
 
97
  operator std::string() const
101
98
  {
102
 
    os.write(v.data(), v.size());
103
 
    return os;
 
99
    return to_string(*this);
104
100
  }
105
101
private:
106
102
  T begin_;
109
105
 
110
106
typedef data_ref_basic<const unsigned char*> data_ref;
111
107
typedef data_ref_basic<const char*> str_ref;
 
108
 
 
109
inline std::ostream& operator<<(std::ostream& os, str_ref v)
 
110
{
 
111
  return os.write(v.data(), v.size());
 
112
}
 
113
 
 
114
inline std::string to_string(str_ref v)
 
115
{
 
116
  return std::string(v.data(), v.size());
 
117
}