Tawara  0.1.0
prim_element.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, 2012, Geoffrey Biggs, geoffrey.biggs@aist.go.jp
5  * RT-Synthesis Research Group
6  * Intelligent Systems Research Institute,
7  * National Institute of Advanced Industrial Science and Technology (AIST),
8  * Japan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above
18  * copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided
20  * with the distribution.
21  * * Neither the name of Geoffrey Biggs nor AIST, nor the names of its
22  * contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #if !defined(TAWARA_PRIM_ELEMENT_H_)
40 #define TAWARA_PRIM_ELEMENT_H_
41 
42 
43 #include <boost/operators.hpp>
44 #include <stdint.h>
45 #include <string>
46 #include <tawara/element.h>
47 #include <tawara/exceptions.h>
48 #include <tawara/win_dll.h>
49 
52 
53 namespace tawara
54 {
75  template<typename T>
77  public boost::equality_comparable<PrimitiveElement<T> >
78  {
79  public:
86  PrimitiveElement(uint32_t id, T value)
87  : Element(id),
88  value_(value), has_default_(false)
89  {
90  }
91 
99  PrimitiveElement(uint32_t id, T value, T default_value)
100  : Element(id),
101  value_(value), default_(default_value), has_default_(true)
102  {
103  }
104 
106  virtual PrimitiveElement& operator=(T const& rhs)
107  {
108  value_ = rhs;
109  return *this;
110  }
111 
113  virtual uint32_t id() const { return Element::id(); }
114 
120  virtual void id(uint32_t id)
121  {
122  if (id == 0 ||
123  id == 0xFF ||
124  id == 0xFFFF ||
125  id == 0xFFFFFF ||
126  id == 0xFFFFFFFF)
127  {
128  throw InvalidElementID() << err_id(id);
129  }
130  id_ = id;
131  }
132 
134  virtual T value() const { return value_; }
136  virtual void value(T value) { value_ = value; }
138  operator T() const { return value_; }
139 
141  virtual bool has_default() const { return has_default_; }
143  virtual T get_default() const { return default_; }
145  virtual void set_default(T default_value)
146  {
147  default_ = default_value;
148  has_default_ = true;
149  }
154  virtual T remove_default()
155  {
156  has_default_ = false;
157  return default_;
158  }
164  virtual bool is_default() const
165  { return value_ == default_ && has_default_; }
166 
168  friend bool operator==(PrimitiveElement<T> const& lhs,
169  PrimitiveElement<T> const& rhs)
170  {
171  return lhs.value_ == rhs.value_;
172  }
173 
174  protected:
178 
179  virtual bool equal_(PrimitiveElement<T> const& rhs)
180  {
181  return value_ == rhs.value_;
182  }
183  }; // class Element
184 }; // namespace tawara
185 
188 
189 #endif // TAWARA_PRIM_ELEMENT_H_
190 
virtual bool is_default() const
Check if this element is at the default value.
Definition: prim_element.h:164
virtual uint32_t id() const
Get the element&#39;s ID.
Definition: prim_element.h:113
virtual PrimitiveElement & operator=(T const &rhs)
Value assignment operator.
Definition: prim_element.h:106
virtual T value() const
Get the value.
Definition: prim_element.h:134
An invalid Element ID was provided.
Definition: exceptions.h:203
#define TAWARA_EXPORT
Definition: win_dll.h:51
virtual bool has_default() const
Check if a default value is set.
Definition: prim_element.h:141
uint32_t id() const
Get the element&#39;s ID.
Definition: element.h:104
virtual void set_default(T default_value)
Set the default value.
Definition: prim_element.h:145
friend bool operator==(PrimitiveElement< T > const &lhs, PrimitiveElement< T > const &rhs)
Equality operator.
Definition: prim_element.h:168
virtual bool equal_(PrimitiveElement< T > const &rhs)
Definition: prim_element.h:179
virtual T remove_default()
Remove the default value.
Definition: prim_element.h:154
virtual T get_default() const
Get the default value.
Definition: prim_element.h:143
boost::error_info< struct tag_id, ids::ID > err_id
An Element ID.
Definition: exceptions.h:530
virtual void value(T value)
Set the value.
Definition: prim_element.h:136
virtual void id(uint32_t id)
Set the element&#39;s ID.
Definition: prim_element.h:120
The Element interface, a basic interface to an element object.
Definition: element.h:66
PrimitiveElement(uint32_t id, T value)
Create a new element with no default.
Definition: prim_element.h:86
The primitive data element interface.
Definition: prim_element.h:76
PrimitiveElement(uint32_t id, T value, T default_value)
Create a signed integer element with a default value.
Definition: prim_element.h:99