00001 //---------------------------------------------------------------------------- 00002 /** @file SgBWArray.h 00003 Arrays indexed by color. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef SG_BWARRAY_H 00008 #define SG_BWARRAY_H 00009 00010 #include <boost/static_assert.hpp> 00011 #include "SgBlackWhite.h" 00012 00013 //---------------------------------------------------------------------------- 00014 00015 /** An array of two values of type T, indexed by SG_BLACK and SG_WHITE. */ 00016 template <class T> 00017 class SgBWArray 00018 { 00019 public: 00020 /** Constructor. 00021 Constructs elements with the default constructor of type T. 00022 @note Previously, BWArray automatically initialized primitive types 00023 like ints or pointers with 0, and there was a second class 00024 BWConstrArray used for non-primitive types. This has changed, 00025 because it is not the standard semantics for container classes in C++, 00026 and because it does not allow use cases with incremental 00027 initialization after construction. If you want to initialize for 00028 example an SgBWArray<int> with 0, use the constructor that takes a 00029 default value. 00030 */ 00031 SgBWArray(); 00032 00033 SgBWArray(const T& val); 00034 00035 SgBWArray(const T& black, const T& white); 00036 00037 bool operator==(const SgBWArray& bwArray) const; 00038 00039 bool operator!=(const SgBWArray& bwArray) const; 00040 00041 T& operator[](SgBlackWhite color); 00042 00043 const T& operator[](SgBlackWhite color) const; 00044 00045 private: 00046 BOOST_STATIC_ASSERT(SG_BLACK == 0); 00047 BOOST_STATIC_ASSERT(SG_WHITE == 1); 00048 00049 T m_array[2]; 00050 }; 00051 00052 template <class T> 00053 inline SgBWArray<T>::SgBWArray() 00054 { 00055 } 00056 00057 template <class T> 00058 inline SgBWArray<T>::SgBWArray(const T& val) 00059 { 00060 m_array[SG_BLACK] = val; 00061 m_array[SG_WHITE] = val; 00062 } 00063 00064 template <class T> 00065 inline SgBWArray<T>::SgBWArray(const T& black, const T& white) 00066 { 00067 m_array[SG_BLACK] = black; 00068 m_array[SG_WHITE] = white; 00069 } 00070 00071 template <class T> 00072 inline bool SgBWArray<T>::operator==(const SgBWArray& bwArray) const 00073 { 00074 return (m_array[SG_BLACK] == bwArray.m_array[SG_BLACK] 00075 && m_array[SG_WHITE] == bwArray.m_array[SG_WHITE]); 00076 } 00077 00078 template <class T> 00079 inline bool SgBWArray<T>::operator!=(const SgBWArray& bwArray) const 00080 { 00081 return ! operator==(bwArray); 00082 } 00083 00084 template <class T> 00085 inline T& SgBWArray<T>::operator[](SgBlackWhite color) 00086 { 00087 SG_ASSERT_BW(color); 00088 return m_array[color]; 00089 } 00090 00091 template <class T> 00092 inline const T& SgBWArray<T>::operator[](SgBlackWhite color) const 00093 { 00094 SG_ASSERT_BW(color); 00095 return m_array[color]; 00096 } 00097 00098 //---------------------------------------------------------------------------- 00099 00100 #endif // SG_BWARRAY_H