00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BASEHANDLE_H_
00009 #define BASEHANDLE_H_
00010
00011
00012 #include<sstream>
00013 #include<iostream>
00014 #include<stdexcept>
00015 #include<cassert>
00016
00017 #include "Enums.h"
00018
00019 namespace FemViewer {
00020
00021
00022
00023 class BaseHandle
00024 {
00025 public:
00026
00027 explicit BaseHandle(HandleType htype_=Unknown,int idx_=-1,bool act_=false)
00028 : _htype(htype_), _idx(idx_), _act(act_) {;}
00029
00030 virtual ~BaseHandle(){;}
00031
00032 inline BaseHandle& operator=(const BaseHandle& rhs_) {
00033 _htype = rhs_.type();
00034 _idx = rhs_.idx();
00035 _act = rhs_.is_active();
00036 return *this;
00037 }
00038
00039
00040 HandleType& type() { return _htype; }
00041 const HandleType& type() const { return _htype; }
00042
00043 int& idx() { return _idx; }
00044 const int& idx() const { return _idx; }
00045
00046
00047 bool is_active() const { return _act; }
00048
00049
00050 void activate(const bool flg_) { _act = flg_; }
00051
00052
00053 bool is_valid_idx() const { return (_idx != -1); }
00054
00055
00056 virtual void Reset() { _htype = Unknown; _idx=-1; _act=false; }
00057
00058
00059 bool is_valid_type(const BaseHandle& rhs_) const {
00060 return _htype == rhs_._htype;
00061 }
00062
00063 bool operator==(const BaseHandle& rhs_) const {
00064 return (_idx == rhs_.idx()) && is_valid_type(rhs_); }
00065
00066 bool operator!=(const BaseHandle& rhs_) const
00067 { return (_idx != rhs_.idx()) && is_valid_type(rhs_); }
00068
00069 bool operator <(const BaseHandle& rhs_) const
00070 { return (_idx < rhs_.idx()) && is_valid_type(rhs_); }
00071
00072
00073 protected:
00074 friend std::ostream& operator<<(std::ostream& os_,const BaseHandle& rhs_);
00075 HandleType _htype;
00076 int _idx;
00077 bool _act;
00078
00079 };
00080
00081 inline std::ostream& operator<<(std::ostream& os_,const BaseHandle& rhs_)
00082 {
00083 return (os_ << rhs_._idx);
00084 }
00085
00086 template < typename T,unsigned N >
00087 class TArrayPtrs
00088 {
00089 public: typedef std::size_t size_type;
00090
00091
00092 private:
00093
00094 T* _ptrs;
00095 unsigned int _size;
00096 size_type _count;
00097 static const unsigned int _dim = N;
00098
00099 private:
00100 inline void _check(unsigned n) const {
00101 if (n >= _size) {
00102 std::stringstream ss("Index ");
00103 ss << n << " is over range [" << _size << "]";
00104 throw std::out_of_range(ss.str());
00105 }
00106 }
00107 public:
00108
00109 explicit TArrayPtrs(unsigned int size_= _dim)
00110 : _ptrs(new T[size_]), _size(size_), _count(0) {
00111 assert(_ptrs!=(T*)0);
00112 this->Clear();
00113 }
00114
00115 ~TArrayPtrs() { delete [] _ptrs; }
00116
00117 typedef T* iterator;
00118 typedef const T* const_iterator;
00119
00120
00121 iterator begin() { return _ptrs; }
00122 const_iterator begin()const { return _ptrs; }
00123 iterator end() { return _ptrs + _size; }
00124 const_iterator end() const { return _ptrs + _size; }
00125 unsigned Size() const { return _size; }
00126 unsigned Count() const { return _count; }
00127 void Resize(unsigned new_size_) {
00128 delete _ptrs;
00129 _ptrs = new T[new_size_];
00130 _size = new_size_;
00131 }
00132
00133 T& operator [](unsigned idx_) { return _ptrs[idx_]; }
00134 const T& operator [](unsigned idx_) const { return _ptrs[idx_]; }
00135
00136
00137 T& at(unsigned idx_) {
00138 this->_check(idx_);
00139 return (*this)[idx_];
00140 }
00141
00142 bool Register(T const ptr_) {
00143 if (Exists(ptr_)) return false;
00144 register unsigned i(0);
00145 do {
00146 if (!(*this)[i]) {
00147 (*this)[i] = ptr_;
00148 _count++;
00149
00150 return true;
00151 }
00152 } while(++i<_size);
00153 return false;
00154 }
00155
00156 void Unregister(unsigned idx_) {
00157 this->_check(idx_);
00158 (*this)[idx_] = (T)0;
00159 }
00160
00161 void Clear() { for(unsigned i(0);i<_size;++i) (*this)[i] = (T)0; }
00162
00163 private:
00164
00165 inline bool Exists(T const ptr_)
00166 {
00167 if (_count>0) {
00168 register unsigned int i=0;
00169 while (i<_size) {
00170 if (!(*this)[i]) {i++; continue;}
00171 if (*(_ptrs[i++]) == *ptr_) return true;
00172 }
00173 }
00174 return false;
00175 }
00177 template < typename U,unsigned M >
00178 TArrayPtrs(const TArrayPtrs<U,M>&);
00179 template < typename U,unsigned M >
00180 TArrayPtrs<T,N>& operator=(const TArrayPtrs<U,M>&);
00181
00182 };
00183
00184
00185
00186
00187 }
00188
00189
00190
00191
00192
00193 #endif
00194