cheshirekow  v0.1.0
attr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of cpp-pthreads.
5  *
6  * cpp-pthreads is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * cpp-pthreads is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with cpp-pthreads. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef CPP_PTHREADS_ATTR_H_
28 #define CPP_PTHREADS_ATTR_H_
29 
30 
31 namespace pthreads
32 {
33 
34 // forward declarations
35 template<class Base> class Attr;
36 template<class Base, typename T> struct Assignment;
37 template<class Base, typename T> struct Access;
38 template<class Base, typename T> struct Delegate;
39 
42 template<class Base> struct AttrType;
43 
46 template<class Base, typename T>
47 struct Assignment {
48  T m_value;
49 
51  Assignment(const T& value)
52  : m_value(value) {
53  }
54 
56  int set(Attr<Base>& attr) const;
57 };
58 
61 template<class Base, typename T>
62 struct Access {
63  Access() {
64  }
65 
67  int get(Attr<Base>& attr, T& value) const;
68 };
69 
72 template<class T>
73 struct Friendly {
74  typedef T type;
75 };
76 
78 
82 template<class Base>
83 class Attr {
84  private:
87 
88  public:
89  template<class Base2, typename T> friend class Assignment;
90  template<class Base2, typename T> friend class Access;
91  template<class Base2, typename T> friend class Delegate;
92  friend class Friendly<Base>::type;
93 
95  int init();
96 
98  int destroy();
99 
102 
113  template<typename T>
114  int set(const T& value) {
115  Assignment<Base, T> assignment(value);
116  return assignment.set(*this);
117  }
118 
121 
138  template<typename T>
139  int get(T& value) {
140  Access<Base, T> access;
141  return access.get(*this, value);
142  }
143 
146 
166  template<typename T, typename U>
167  int get(const Access<Base, T>& access, U& value) {
168  T tVal;
169  int return_val = access.get(*this, tVal);
170  value = tVal;
171  return return_val;
172  }
173 
175 
188  template<typename T>
189  Attr<Base>& operator <<(const T& value) {
190  Assignment<Base, T> assignment(value);
191  assignment.set(*this);
192  return *this;
193  }
194 
197 
211  template<typename T>
213  return Delegate<Base, T>(*this);
214  }
215 
217  template<typename T>
218  T operator[](const Access<Base, T>& access) const {
219  T value;
220  access.get(*this, value);
221  return value;
222  }
223 };
224 
227 
232 template<class Base, typename T>
233 struct Delegate {
235 
237  : m_attr(attr) {
238  }
239 
240  operator T() {
241  T value;
242  Access<Base, T> access;
243  m_attr.get(access, value);
244  return value;
245  }
246 
247  int operator=(T value) {
248  return m_attr.set(value);
249  }
250 };
251 
253 
260 template<typename T, unsigned int ID>
261 struct TypeWrap {
263 
266  }
267 
269  TypeWrap(const T& data)
270  : m_data(data) {
271  }
272 
274  TypeWrap<T, ID>& operator=(const T& data) {
275  m_data = data;
276  return *this;
277  }
278 
281  operator T&() {
282  return m_data;
283  }
284 
287  operator const T&() const {
288  return m_data;
289  }
290 
291 };
292 
293 } // namespace pthreads
294 
295 #endif // ATTR_H_
TypeWrap(const T &data)
initialize the stored data to data
Definition: attr.h:269
TypeWrap()
does nothing
Definition: attr.h:265
template used to resolve the pthreads_xxx_t object corresponding to the Attr<Base> object ...
Definition: attr.h:42
T operator[](const Access< Base, T > &access) const
map operator for retrieval, ignores error values returned
Definition: attr.h:218
Delegate< Base, T > operator[](const Access< Base, T > &access)
map operator, unsafe assignment and retrieval, ignores error values returned
Definition: attr.h:212
int operator=(T value)
Definition: attr.h:247
two-way delegate, may assign or retrieve (unsafely) an attribute value of an attribute object ...
Definition: attr.h:38
Assignment(const T &value)
constructs an assignment object from a value
Definition: attr.h:51
Attr< Base > & operator<<(const T &value)
stream assignment, unsafe, ignores error values returned
Definition: attr.h:189
Attr< Base > & m_attr
Definition: attr.h:234
TypeWrap< T, ID > & operator=(const T &data)
assigns the value to data
Definition: attr.h:274
Provides a unique type that acts like a native type.
Definition: attr.h:261
int init()
initialize the attribute object
since a template parameter cannot be a friend (until C++-11) this template simply allows us to work a...
Definition: attr.h:73
AttrType< Base >::type m_data
the wrapped pthreads_XXXattr_t object
Definition: attr.h:86
Delegate(Attr< Base > &attr)
Definition: attr.h:236
Attributes object for type Base.
Definition: attr.h:35
T m_value
storage for the value to be assigned
Definition: attr.h:48
int set(Attr< Base > &attr) const
sets the attribute value of attr to the value stored in this object
assignment delegate, an object which can assign an attribute value to an attribute object ...
Definition: attr.h:36
int set(const T &value)
safe assignment of an attribute by value, use only when type is implied
Definition: attr.h:114
int destroy()
destroy the attribute object
access delegate, an object which can access an attribute value of an attribute object ...
Definition: attr.h:37