Wallaroo  0.8
attribute.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * wallaroo - A library for configurable creation and wiring of C++ classes.
3  * Copyright (C) 2012 Daniele Pallastrelli
4  *
5  * This file is part of wallaroo.
6  * For more information, see http://wallaroolib.sourceforge.net/
7  *
8  * Boost Software License - Version 1.0 - August 17th, 2003
9  *
10  * Permission is hereby granted, free of charge, to any person or organization
11  * obtaining a copy of the software and accompanying documentation covered by
12  * this license (the "Software") to use, reproduce, display, distribute,
13  * execute, and transmit the Software, and to prepare derivative works of the
14  * Software, and to permit third-parties to whom the Software is furnished to
15  * do so, all subject to the following:
16  *
17  * The copyright notices in the Software and this entire statement, including
18  * the above license grant, this restriction and the following disclaimer,
19  * must be included in all copies of the Software, in whole or in part, and
20  * all derivative works of the Software, unless such copies or derivative
21  * works are solely in the form of machine-executable object code generated by
22  * a source language processor.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
27  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
28  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
29  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  * DEALINGS IN THE SOFTWARE.
31  ******************************************************************************/
32 
33 #ifndef WALLAROO_ATTRIBUTE_H_
34 #define WALLAROO_ATTRIBUTE_H_
35 
36 #include <string>
37 #include <sstream>
38 #include <iostream>
39 #include "cxx0x.h"
40 #include "part.h"
41 #include "deserializable_value.h"
42 #include "exceptions.h"
43 
44 
45 namespace wallaroo
46 {
47 
48 namespace detail
49 {
50  // Template function that converts a string into a value of type T
51  // It provides a specialization to manage the case of type string
52 
53  // Generic conversion: try to convert v to type T and put the result in value
54  // throw WrongType if v cannot be converted to T
55  template < typename T >
56  inline void String2Value( const std::string& v, T& value )
57  {
58  std::istringstream istream( v );
59  if ( !( istream >> std::boolalpha >> value ) )
60  {
61  // error: didn't convert to T
62  throw WrongType();
63  }
64  }
65 
66  // T is an unsigned char
67  template <>
68  inline void String2Value< unsigned char >( const std::string& v, unsigned char& value )
69  {
70  unsigned int x;
71  String2Value( v, x );
72  value = x;
73  }
74 
75  // T is a string. No conversion needed
76  template <>
77  inline void String2Value< std::string >( const std::string& v, std::string& value )
78  {
79  value = v;
80  }
81 }
82 
92 template < typename T >
94 {
95 public:
96 
104  Attribute( const std::string& name, const RegToken& token )
105  {
106  Part* owner = token.GetPart();
107  owner -> Register( name, this );
108  }
109 
114  virtual void Value( const std::string& v )
115  {
116  detail::String2Value( v, value );
117  }
118 
122  operator T () { return value; }
123 
127  operator T () const { return value; }
128 
133  const char* c_str() const { return value.c_str(); }
134 
135  Attribute& operator = ( const Attribute& a ) { value = a.value; return *this; }
136  Attribute& operator = ( const T& v ) { value = v; return *this; }
137 
138  Attribute& operator++( ) { ++value; return *this; }
139  T operator++( int ) { T tmp( value ); operator++(); return tmp; }
140 
141  Attribute& operator--() { --value; return *this; }
142  T operator--( int ) { T tmp( value ); operator--(); return tmp; }
143 
144  Attribute& operator+=( const T& rhs ) { value += rhs; return *this; }
145  Attribute& operator-=( const T& rhs ) { value -= rhs; return *this; }
146  Attribute& operator*=( const T& rhs ) { value *= rhs; return *this; }
147  Attribute& operator/=( const T& rhs ) { value /= rhs; return *this; }
148 
149 private:
150  T value; // here we store the real attribute value
151 
152  // copy ctor disabled
153  Attribute( const Attribute& );
154 };
155 
156 
157 // binary rel operators with lhs Attribute<T> and rhs T:
158 
159 template < typename T > bool operator == ( const Attribute< T >& lhs, const T& rhs ) { return static_cast< T >( lhs ) == rhs; }
160 template < typename T > bool operator != ( const Attribute< T >& lhs, const T& rhs ) { return !operator==( lhs, rhs ); }
161 
162 template < typename T > bool operator < ( const Attribute< T >& lhs, const T& rhs ){ return static_cast< T >( lhs ) < rhs; }
163 template < typename T > bool operator > ( const Attribute< T >& lhs, const T& rhs ){ return !operator<=( lhs, rhs ); }
164 template < typename T > bool operator <= ( const Attribute< T >& lhs, const T& rhs ){ return operator<( lhs, rhs ) || operator==( lhs, rhs ); }
165 template < typename T > bool operator >= ( const Attribute< T >& lhs, const T& rhs ){ return !operator<( lhs, rhs ); }
166 
167 
168 // binary rel operators with lhs T and rhs Attribute<T>:
169 
170 template < typename T > bool operator == ( const T& lhs, const Attribute< T >& rhs ) { return operator==( rhs, lhs ); }
171 template < typename T > bool operator != ( const T& lhs, const Attribute< T >& rhs ) { return operator!=( rhs, lhs ); }
172 
173 template < typename T > bool operator < ( const T& lhs, const Attribute< T >& rhs ){ return operator>( rhs, lhs ); }
174 template < typename T > bool operator > ( const T& lhs, const Attribute< T >& rhs ){ return operator<( rhs, lhs ); }
175 template < typename T > bool operator <= ( const T& lhs, const Attribute< T >& rhs ){ return !operator>( lhs, rhs ); }
176 template < typename T > bool operator >= ( const T& lhs, const Attribute< T >& rhs ){ return !operator<( lhs, rhs ); }
177 
178 // concat operators
179 template < typename T > T operator + ( const Attribute< T >& lhs, const Attribute< T >& rhs ) { return static_cast< T >( lhs ) + static_cast< T >( rhs ); }
180 template < typename T > T operator + ( const Attribute< T >& lhs, const T& rhs ) { return static_cast< T >( lhs ) + rhs; }
181 template < typename T > T operator + ( const T& lhs, const Attribute< T >& rhs ) { return lhs + static_cast< T >( rhs ); }
182 
183 // stream output operator
184 template < typename T >
185 std::ostream& operator << ( std::ostream& os, const Attribute< T >& att )
186 {
187  os << static_cast< T >( att );
188  return os;
189 }
190 
191 }
192 
193 #endif
virtual void Value(const std::string &v)
Definition: attribute.h:114
Attribute & operator--()
Definition: attribute.h:141
T operator++(int)
Definition: attribute.h:139
Part * GetPart() const
Definition: part.h:60
Definition: attribute.h:93
Attribute & operator++()
Definition: attribute.h:138
T operator+(const Attribute< T > &lhs, const Attribute< T > &rhs)
Definition: attribute.h:179
Attribute & operator*=(const T &rhs)
Definition: attribute.h:146
const char * c_str() const
Definition: attribute.h:133
void String2Value< unsigned char >(const std::string &v, unsigned char &value)
Definition: attribute.h:68
void String2Value(const std::string &v, T &value)
Definition: attribute.h:56
Definition: part.h:76
Attribute & operator/=(const T &rhs)
Definition: attribute.h:147
bool operator<(const Attribute< T > &lhs, const T &rhs)
Definition: attribute.h:162
void String2Value< std::string >(const std::string &v, std::string &value)
Definition: attribute.h:77
Definition: part.h:57
bool operator>(const Attribute< T > &lhs, const T &rhs)
Definition: attribute.h:163
Attribute & operator+=(const T &rhs)
Definition: attribute.h:144
bool operator<=(const Attribute< T > &lhs, const T &rhs)
Definition: attribute.h:164
Attribute(const std::string &name, const RegToken &token)
Definition: attribute.h:104
Attribute & operator=(const Attribute &a)
Definition: attribute.h:135
bool operator>=(const Attribute< T > &lhs, const T &rhs)
Definition: attribute.h:165
T operator--(int)
Definition: attribute.h:142
bool operator!=(const Attribute< T > &lhs, const T &rhs)
Definition: attribute.h:160
Definition: attribute.h:45
Attribute & operator-=(const T &rhs)
Definition: attribute.h:145
Definition: deserializable_value.h:47
Definition: exceptions.h:110
bool operator==(const Attribute< T > &lhs, const T &rhs)
Definition: attribute.h:159