00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef QOLYESTER_UTL_DATA_HH
00020 # define QOLYESTER_UTL_DATA_HH 1
00021
00022 # include <sys/types.h>
00023 # include <cassert>
00024
00025 namespace olsr {
00026
00027 namespace utl {
00028
00029
00030 struct Data;
00031 struct ConstData;
00032
00033 namespace internal {
00034
00035
00037 template <class T, class U>
00038 struct convert {
00039 enum { valid = true };
00040 };
00041
00042 template <>
00043 struct convert<ConstData, Data> {
00044 enum { valid = false };
00045 };
00046
00047 template <class C>
00048 struct other {
00049 typedef ConstData return_type;
00050 };
00051
00052 template <>
00053 struct other<ConstData> {
00054 typedef Data return_type;
00055 };
00057
00059 struct shared_data {
00060 shared_data(unsigned usecount, u_int8_t* buffer, ::size_t size);
00061
00062 unsigned usecount;
00063 u_int8_t* buffer;
00064 ::size_t size;
00065 };
00066
00067 }
00068
00070 template <class Self>
00071 struct Data_
00072 {
00074
00075 Data_();
00076
00077 explicit
00078 inline Data_(const ::size_t size);
00079
00080 inline Data_(const Data_& other);
00081
00082 template <class T>
00083 inline Data_(const Data_<T>& other);
00084
00085 inline ~Data_();
00087
00089 template <class T>
00090 inline Self& operator = (const Data_<T>& other);
00091
00092 inline Self& operator=(const Data_<Self>& other);
00093
00096 inline bool empty() const;
00097
00099 inline ::size_t size() const;
00100
00102 inline void fit(unsigned len);
00103
00105
00106 inline Self shrink_by(unsigned len) const;
00107 inline Self shrink_to(unsigned len) const;
00109
00111
00112 inline Self operator + (unsigned offset) const;
00113 inline Self& operator += (unsigned offset);
00115
00117 template <class T>
00118 inline ::size_t operator - (const Data_<T>& rhs) const;
00119
00121 inline const u_int8_t* raw() const;
00122
00124
00125 inline void dump(u_int8_t* p) const;
00126 inline void dump(Data_<Data>& d) const;
00128
00130
00131 inline Self& exact();
00132 inline const Self& exact() const;
00134
00135 protected:
00137 inline void use();
00139 inline void unuse();
00140
00141 internal::shared_data* data_;
00142 ::size_t instance_size_;
00143 unsigned offset_;
00144
00145 friend
00146 class Data_<typename internal::other<Self>::return_type>;
00147 };
00148
00149 struct Data : public Data_<Data>
00150 {
00151 inline Data();
00152 explicit inline Data(::size_t size);
00153
00155 inline u_int8_t* raw();
00156 };
00157
00158 struct ConstData : public Data_<ConstData>
00159 {
00160 inline ConstData();
00161 explicit inline ConstData(::size_t size);
00162 inline ConstData(const Data& d);
00163
00164 inline ConstData& operator = (const Data& d);
00165 };
00166
00167 }
00168
00169 }
00170
00171 # include "data.hxx"
00172
00173 #endif // ! QOLYESTER_UTL_DATA_HH