00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/common/EncapsulatedInteger.hpp $ 00003 // $Id: EncapsulatedInteger.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for the EncapsulatedInteger template. 00018 00019 #ifndef TORC_COMMON_ENCAPSULATEDINTEGER_HPP 00020 #define TORC_COMMON_ENCAPSULATEDINTEGER_HPP 00021 00022 namespace torc { 00023 namespace common { 00024 00025 /// \brief Template base for encapsulated integers, to enforce strong typing 00026 template <typename T> class EncapsulatedInteger { 00027 protected: 00028 // members 00029 T m; ///< \brief Encapsulated integer. 00030 public: 00031 // types 00032 typedef EncapsulatedInteger<T> type; ///< \brief Alias for the instantiated class type. 00033 typedef T pod; ///< \brief Alias for the encapsulated Plain-Old-Data type. 00034 // constructors 00035 /// \brief Null constructor. 00036 EncapsulatedInteger(void) { m = 0; } 00037 /// \brief Copy constructor. 00038 EncapsulatedInteger(const type& rhs) { m = rhs.m; } 00039 /// \brief Copy constructor. 00040 EncapsulatedInteger(const T& rhs) { m = rhs; } 00041 // operators 00042 /// \brief Assignment operator (from encapsulated type). 00043 type& operator =(const type& rhs) { m = rhs.m; return *this; } 00044 /// \brief Assignment operator. 00045 type& operator =(const T& rhs) { m = rhs; return *this; } 00046 /// \brief Equality operator (against encapsulated type). 00047 bool operator ==(const type& rhs) const { return m == rhs.m; } 00048 /// \brief Equality operator. 00049 bool operator ==(const T& rhs) const { return m == rhs; } 00050 /// \brief Constant cast operator. 00051 operator const T&(void) const { return m; } 00052 /// \brief Non-constant cast operator. 00053 operator T&(void) { return m; } 00054 // functions 00055 static inline pod undefined(void) { return static_cast<pod>(-1); } 00056 inline bool isUndefined(void) const { return m == static_cast<pod>(-1); } 00057 /// \brief Cast operator. 00058 //operator const T(void) const { return m; } 00059 //operator T(void) { return m; } 00060 }; 00061 00062 } // namespace common 00063 } // namespace torc 00064 00065 #endif // TORC_COMMON_ENCAPSULATEDINTEGER_HPP