Line data Source code
1 : #include <fstream> 2 : #include <sstream> 3 : 4 : #include "Module/Stateful/Source/User/Source_user.hpp" 5 : #include "Tools/Exception/exception.hpp" 6 : 7 : using namespace spu; 8 : using namespace spu::module; 9 : 10 : template<typename B> 11 0 : Source_user<B>::Source_user(const int max_data_size, 12 : const std::string& filename, 13 : const bool auto_reset, 14 : const int start_idx) 15 : : Source<B>(max_data_size) 16 0 : , source() 17 0 : , next_frame_idx(start_idx) 18 0 : , src_counter(0) 19 0 : , auto_reset(auto_reset) 20 0 : , start_idx(start_idx) 21 0 : , done(false) 22 : { 23 0 : const std::string name = "Source_user"; 24 0 : this->set_name(name); 25 : 26 0 : if (filename.empty()) 27 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, "'filename' should not be empty."); 28 : 29 0 : std::ifstream file(filename.c_str(), std::ios::in); 30 : 31 0 : if (file.is_open()) 32 : { 33 0 : int n_src = 0, src_size = 0; 34 : 35 0 : file >> n_src; 36 0 : file >> src_size; 37 : 38 0 : if (n_src <= 0 || src_size <= 0) 39 : { 40 0 : std::stringstream message; 41 0 : message << "'n_src', and 'src_size' have to be greater than 0 ('n_src' = " << n_src 42 0 : << ", 'src_size' = " << src_size << ")."; 43 0 : throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str()); 44 0 : } 45 : 46 0 : this->source.resize(n_src); 47 0 : for (auto i = 0; i < n_src; i++) 48 0 : this->source[i].resize(src_size); 49 : 50 0 : if (src_size == this->max_data_size) 51 : { 52 0 : for (auto i = 0; i < n_src; i++) 53 0 : for (auto j = 0; j < src_size; j++) 54 : { 55 : int bit; 56 0 : file >> bit; 57 : 58 0 : this->source[i][j] = bit != 0; 59 : } 60 : } 61 : else 62 : { 63 0 : file.close(); 64 : 65 0 : std::stringstream message; 66 0 : message << "The size is wrong (read: " << src_size << ", expected: " << this->max_data_size << ")."; 67 0 : throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str()); 68 0 : } 69 : 70 0 : file.close(); 71 : } 72 : else 73 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, "Can't open '" + filename + "' file."); 74 : 75 0 : this->next_frame_idx %= (int)source.size(); 76 0 : } 77 : 78 : template<typename B> 79 0 : Source_user<B>::Source_user(const int max_data_size, const std::string& filename, const int start_idx) 80 0 : : Source_user<B>(max_data_size, filename, true, start_idx) 81 : { 82 0 : } 83 : 84 : template<typename B> 85 : Source_user<B>* 86 0 : Source_user<B>::clone() const 87 : { 88 0 : auto m = new Source_user(*this); 89 0 : m->deep_copy(*this); 90 0 : return m; 91 : } 92 : 93 : template<typename B> 94 : void 95 0 : Source_user<B>::reset() 96 : { 97 0 : this->next_frame_idx = this->start_idx; 98 0 : this->src_counter = 0; 99 0 : this->done = false; 100 0 : } 101 : 102 : template<typename B> 103 : bool 104 0 : Source_user<B>::is_done() const 105 : { 106 0 : return this->done; 107 : } 108 : 109 : template<typename B> 110 : void 111 0 : Source_user<B>::_generate(B* out_data, const size_t /*frame_id*/) 112 : { 113 0 : std::copy(this->source[this->next_frame_idx].begin(), this->source[this->next_frame_idx].end(), out_data); 114 : 115 0 : this->next_frame_idx = (this->next_frame_idx + 1) % (int)this->source.size(); 116 : 117 0 : if (this->auto_reset == false) 118 : { 119 0 : this->src_counter = (this->src_counter + 1) % (int)this->source.size(); 120 0 : if (this->src_counter == 0) this->done = true; 121 : } 122 0 : } 123 : 124 : // ==================================================================================== explicit template instantiation 125 : template class spu::module::Source_user<int8_t>; 126 : template class spu::module::Source_user<uint8_t>; 127 : template class spu::module::Source_user<int16_t>; 128 : template class spu::module::Source_user<uint16_t>; 129 : template class spu::module::Source_user<int32_t>; 130 : template class spu::module::Source_user<uint32_t>; 131 : template class spu::module::Source_user<int64_t>; 132 : template class spu::module::Source_user<uint64_t>; 133 : template class spu::module::Source_user<float>; 134 : template class spu::module::Source_user<double>; 135 : // ==================================================================================== explicit template instantiation