Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_sg_states 4 #ifndef tools_sg_states 5 #define tools_sg_states 5 #define tools_sg_states 6 6 7 #include "state" 7 #include "state" 8 8 9 //#define TOOLS_SG_STATES_CUR 9 //#define TOOLS_SG_STATES_CUR 10 10 11 namespace tools { 11 namespace tools { 12 namespace sg { 12 namespace sg { 13 13 14 class states { 14 class states { 15 public: 15 public: 16 #ifdef TOOLS_SG_STATES_CUR 16 #ifdef TOOLS_SG_STATES_CUR 17 states(unsigned int a_ww,unsigned int a_wh) 17 states(unsigned int a_ww,unsigned int a_wh) 18 :m_states(8) 18 :m_states(8) 19 ,m_state(&(m_states[0])) 19 ,m_state(&(m_states[0])) 20 ,m_cur_state(0) 20 ,m_cur_state(0) 21 { 21 { 22 m_state->m_ww = a_ww; 22 m_state->m_ww = a_ww; 23 m_state->m_wh = a_wh; 23 m_state->m_wh = a_wh; 24 } 24 } 25 #else 25 #else 26 states(unsigned int a_ww,unsigned int a_wh): 26 states(unsigned int a_ww,unsigned int a_wh):m_states() { 27 m_state.m_ww = a_ww; 27 m_state.m_ww = a_ww; 28 m_state.m_wh = a_wh; 28 m_state.m_wh = a_wh; 29 } 29 } 30 #endif 30 #endif 31 virtual ~states(){} 31 virtual ~states(){} 32 protected: 32 protected: 33 #ifdef TOOLS_SG_STATES_CUR 33 #ifdef TOOLS_SG_STATES_CUR 34 states(const states& a_from) 34 states(const states& a_from) 35 :m_states(a_from.m_states) 35 :m_states(a_from.m_states) 36 ,m_state(&(m_states[a_from.m_cur_state])) 36 ,m_state(&(m_states[a_from.m_cur_state])) 37 ,m_cur_state(a_from.m_cur_state) 37 ,m_cur_state(a_from.m_cur_state) 38 ,m_saved_state(a_from.m_saved_state) 38 ,m_saved_state(a_from.m_saved_state) 39 {} 39 {} 40 states& operator=(const states& a_from){ 40 states& operator=(const states& a_from){ 41 m_states = a_from.m_states; 41 m_states = a_from.m_states; 42 m_state = &(m_states[a_from.m_cur_state]); 42 m_state = &(m_states[a_from.m_cur_state]); 43 m_cur_state = a_from.m_cur_state; 43 m_cur_state = a_from.m_cur_state; 44 m_saved_state = a_from.m_saved_state; 44 m_saved_state = a_from.m_saved_state; 45 return *this; 45 return *this; 46 } 46 } 47 #else 47 #else 48 states(const states& a_from) 48 states(const states& a_from) 49 :m_states(a_from.m_states) 49 :m_states(a_from.m_states) 50 ,m_state(a_from.m_state) 50 ,m_state(a_from.m_state) 51 ,m_saved_state(a_from.m_saved_state) 51 ,m_saved_state(a_from.m_saved_state) 52 {} 52 {} 53 states& operator=(const states& a_from){ 53 states& operator=(const states& a_from){ 54 m_states = a_from.m_states; 54 m_states = a_from.m_states; 55 m_state = a_from.m_state; 55 m_state = a_from.m_state; 56 m_saved_state = a_from.m_saved_state; 56 m_saved_state = a_from.m_saved_state; 57 return *this; 57 return *this; 58 } 58 } 59 #endif 59 #endif 60 public: 60 public: 61 #ifdef TOOLS_SG_STATES_CUR 61 #ifdef TOOLS_SG_STATES_CUR 62 const sg::state& state() const {return *m_st 62 const sg::state& state() const {return *m_state;} 63 sg::state& state() {return *m_state;} 63 sg::state& state() {return *m_state;} 64 void pop_state() { 64 void pop_state() { 65 m_state = &(m_states[m_cur_state]); 65 m_state = &(m_states[m_cur_state]); 66 m_cur_state--; 66 m_cur_state--; 67 } 67 } 68 void push_state() { 68 void push_state() { 69 if((m_cur_state+1)>=(int)m_states.size()) 69 if((m_cur_state+1)>=(int)m_states.size()) { 70 m_states.resize(m_states.size()+5); 70 m_states.resize(m_states.size()+5); 71 } 71 } 72 m_cur_state++; 72 m_cur_state++; 73 m_states[m_cur_state] = *m_state; 73 m_states[m_cur_state] = *m_state; 74 } 74 } 75 #else 75 #else 76 const sg::state& state() const {return m_sta 76 const sg::state& state() const {return m_state;} 77 sg::state& state() {return m_state;} 77 sg::state& state() {return m_state;} 78 void pop_state() { 78 void pop_state() { 79 //if(!m_states.size()) return; //throw. 79 //if(!m_states.size()) return; //throw. 80 m_state = m_states.back(); 80 m_state = m_states.back(); 81 m_states.pop_back(); 81 m_states.pop_back(); 82 } 82 } 83 void push_state() {m_states.push_back(m_stat 83 void push_state() {m_states.push_back(m_state);} 84 #endif 84 #endif 85 85 86 void save_state(const sg::state& a_state) {m 86 void save_state(const sg::state& a_state) {m_saved_state = a_state;} 87 const sg::state& saved_state() const {return 87 const sg::state& saved_state() const {return m_saved_state;} 88 sg::state& saved_state() {return m_saved_sta 88 sg::state& saved_state() {return m_saved_state;} 89 protected: 89 protected: 90 std::vector<sg::state> m_states; 90 std::vector<sg::state> m_states; 91 #ifdef TOOLS_SG_STATES_CUR 91 #ifdef TOOLS_SG_STATES_CUR 92 sg::state* m_state; 92 sg::state* m_state; 93 int m_cur_state; 93 int m_cur_state; 94 #else 94 #else 95 sg::state m_state; 95 sg::state m_state; 96 #endif 96 #endif 97 sg::state m_saved_state; 97 sg::state m_saved_state; 98 }; 98 }; 99 99 100 }} 100 }} 101 101 102 #endif 102 #endif