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