torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EncapsulatedInteger.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the EncapsulatedInteger template.
18 
19 #ifndef TORC_COMMON_ENCAPSULATEDINTEGER_HPP
20 #define TORC_COMMON_ENCAPSULATEDINTEGER_HPP
21 
22 #include "boost/functional/hash.hpp"
23 
24 namespace torc {
25 namespace common {
26 
27  /// \brief Template base for encapsulated integers, to enforce strong typing
28  template <typename T> class EncapsulatedInteger {
29  protected:
30  // members
31  T m; ///< \brief Encapsulated integer.
32  public:
33  // types
34  typedef EncapsulatedInteger<T> type; ///< \brief Alias for the instantiated class type.
35  typedef T pod; ///< \brief Alias for the encapsulated Plain-Old-Data type.
36  // constructors
37  /// \brief Null constructor.
38  EncapsulatedInteger(void) { m = 0; }
39  /// \brief Copy constructor.
40  EncapsulatedInteger(const type& rhs) { m = rhs.m; }
41  /// \brief Copy constructor.
42  EncapsulatedInteger(const T& rhs) { m = rhs; }
43  // operators
44  /// \brief Assignment operator (from encapsulated type).
45  type& operator =(const type& rhs) { m = rhs.m; return *this; }
46  /// \brief Assignment operator.
47  type& operator =(const T& rhs) { m = rhs; return *this; }
48  /// \brief Equality operator (against encapsulated type).
49  bool operator ==(const type& rhs) const { return m == rhs.m; }
50  /// \brief Equality operator.
51  bool operator ==(const T& rhs) const { return m == rhs; }
52  /// \brief Constant cast operator.
53  operator const T&(void) const { return m; }
54  /// \brief Non-constant cast operator.
55  operator T&(void) { return m; }
56  // friends
57  /// \brief Return a hash value for the specified encapsulated integer.
58  friend std::size_t hash_value(const type& rhs) { return boost::hash_value(rhs.m); }
59  // functions
60  static inline pod undefined(void) { return static_cast<pod>(-1); }
61  inline bool isUndefined(void) const { return m == static_cast<pod>(-1); }
62  /// \brief Cast operator.
63  //operator const T(void) const { return m; }
64  //operator T(void) { return m; }
65  };
66 
67 } // namespace common
68 } // namespace torc
69 
70 #endif // TORC_COMMON_ENCAPSULATEDINTEGER_HPP
EncapsulatedInteger(void)
Null constructor.
friend std::size_t hash_value(const type &rhs)
Return a hash value for the specified encapsulated integer.
EncapsulatedInteger(const T &rhs)
Copy constructor.
type & operator=(const type &rhs)
Assignment operator (from encapsulated type).
EncapsulatedInteger(const type &rhs)
Copy constructor.
Template base for encapsulated integers, to enforce strong typing.
bool operator==(const type &rhs) const
Equality operator (against encapsulated type).
std::size_t hash_value(const Arc &inArc)
Definition: Arc.cpp:25
EncapsulatedInteger< T > type
Alias for the instantiated class type.
T pod
Alias for the encapsulated Plain-Old-Data type.