|
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright (C) 2019-2020 Matthew Milton |
| 4 | +
|
| 5 | +This file is part of the LB-LMC Solver C++ Code Generation Library, included in the |
| 6 | +Open Real-Time Simulation (ORTiS) Framework. |
| 7 | +
|
| 8 | +LB-LMC Solver C++ Code Generation Library is free software: you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation, either version 3 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +
|
| 13 | +LB-LMC Solver C++ Code Generation Library is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +GNU General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with LB-LMC Solver C++ Code Generation Library. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +
|
| 21 | +*/ |
| 22 | + |
| 23 | +#ifndef CODEGEN_ARRAYOBJECT_HPP |
| 24 | +#define CODEGEN_ARRAYOBJECT_HPP |
| 25 | + |
| 26 | +#include <string> |
| 27 | +#include <vector> |
| 28 | +#include <stdexcept> |
| 29 | +#include <sstream> |
| 30 | +#include "Object.hpp" |
| 31 | + |
| 32 | +namespace lblmc |
| 33 | +{ |
| 34 | + |
| 35 | +/** |
| 36 | + \brief describes C++ array objects and generates appropriate C++ code for the objects |
| 37 | +
|
| 38 | + This class cannot describe containers such as std::vector, std::list, or even std::array. |
| 39 | +
|
| 40 | + \author Matthew Milton |
| 41 | + \date 2019 |
| 42 | +**/ |
| 43 | +class ArrayObject : public Object |
| 44 | +{ |
| 45 | +protected: |
| 46 | + |
| 47 | + std::vector<unsigned int> dimensions; |
| 48 | + |
| 49 | +public: |
| 50 | + |
| 51 | + ArrayObject() : |
| 52 | + Object(), |
| 53 | + dimensions() |
| 54 | + {} |
| 55 | + |
| 56 | + ArrayObject(std::string type, std::string label, std::string value, std::vector<unsigned int> dimensions): |
| 57 | + Object(type, label, value), |
| 58 | + dimensions(dimensions) |
| 59 | + { |
| 60 | + if(dimensions.size() < 1) throw std::invalid_argument("lblmc::ArrayObject::constructor(): dimensions must have size of 1 or higher"); |
| 61 | + } |
| 62 | + |
| 63 | + ArrayObject(const ArrayObject& base) : |
| 64 | + Object(base), |
| 65 | + dimensions(base.dimensions) |
| 66 | + {} |
| 67 | + |
| 68 | + inline void insertDimension(unsigned int dim) |
| 69 | + { |
| 70 | + dimensions.push_back(dim); |
| 71 | + } |
| 72 | + |
| 73 | + inline const std::vector<unsigned int>& getDimensions() const |
| 74 | + { |
| 75 | + return dimensions; |
| 76 | + } |
| 77 | + |
| 78 | + inline unsigned int getDimensionSize() const |
| 79 | + { |
| 80 | + return dimensions.size(); |
| 81 | + } |
| 82 | + |
| 83 | + std::string generateDeclaration() |
| 84 | + { |
| 85 | + if(type.empty()) throw std::runtime_error("lblmc::ArrayObject::generateDeclaration(): type cannot be empty or null"); |
| 86 | + if(label.empty()) throw std::runtime_error("lblmc::ArrayObject::generateDeclaration(): label cannot be empty or null"); |
| 87 | + if(dimensions.empty()) throw std::runtime_error("lblmc::ArrayObject::generateDeclaration(): dimension size cannot be zero"); |
| 88 | + |
| 89 | + std::stringstream sstrm; |
| 90 | + |
| 91 | + for(auto q : qualifiers) |
| 92 | + { |
| 93 | + |
| 94 | + if(qualiferAsString(q).empty()) continue; |
| 95 | + |
| 96 | + sstrm << qualiferAsString(q) << " "; |
| 97 | + } |
| 98 | + |
| 99 | + sstrm << type << " " << label; |
| 100 | + |
| 101 | + for(auto d : dimensions) |
| 102 | + { |
| 103 | + sstrm << "[" << d << "]"; |
| 104 | + } |
| 105 | + |
| 106 | + sstrm << ";"; |
| 107 | + |
| 108 | + return sstrm.str(); |
| 109 | + } |
| 110 | + |
| 111 | + std::string generateDefinition() |
| 112 | + { |
| 113 | + if(type.empty()) throw std::runtime_error("lblmc::ArrayObject::generateDefinition(): type cannot be empty or null"); |
| 114 | + if(label.empty()) throw std::runtime_error("lblmc::ArrayObject::generateDefinition(): label cannot be empty or null"); |
| 115 | + if(dimensions.empty()) throw std::runtime_error("lblmc::ArrayObject::generateDefinition(): dimension size cannot be zero"); |
| 116 | + |
| 117 | + std::stringstream sstrm; |
| 118 | + |
| 119 | + for(auto q : qualifiers) |
| 120 | + { |
| 121 | + |
| 122 | + if(qualiferAsString(q).empty()) continue; |
| 123 | + |
| 124 | + sstrm << qualiferAsString(q) << " "; |
| 125 | + } |
| 126 | + |
| 127 | + sstrm << type << " " << label; |
| 128 | + |
| 129 | + for(auto d : dimensions) |
| 130 | + { |
| 131 | + sstrm << "[" << d << "]"; |
| 132 | + } |
| 133 | + |
| 134 | + if(!value.empty()) |
| 135 | + { |
| 136 | + sstrm << " = " << value << ";"; |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + sstrm << ";"; |
| 141 | + } |
| 142 | + |
| 143 | + return sstrm.str(); |
| 144 | + } |
| 145 | + |
| 146 | + std::string generateAssignment() |
| 147 | + { |
| 148 | + throw std::logic_error("lblmc::ArrayObject::generateAssignment(void): assignment generation cannot be performed without specifying indices for assignment"); |
| 149 | + |
| 150 | + return std::string(""); |
| 151 | + } |
| 152 | + |
| 153 | + template <typename T> |
| 154 | + std::string generateAssignment(std::vector<T>&& indices, std::string v) |
| 155 | + { |
| 156 | + if(indices.size() != dimensions.size()) |
| 157 | + throw std::invalid_argument("lblmc::ArrayObject::generateAssignment(std::vector<T>&&): number of specified indices must equal array dimension size"); |
| 158 | + if(v.empty()) throw std::invalid_argument("lblmc::ArrayObject::generateAssignment(): argument v cannot be empty or null"); |
| 159 | + |
| 160 | + if(label.empty()) throw std::runtime_error("lblmc::ArrayObject::generateAssignment(): label cannot be empty or null"); |
| 161 | + |
| 162 | + std::stringstream sstrm; |
| 163 | + |
| 164 | + sstrm << label; |
| 165 | + |
| 166 | + for(auto i : indices) |
| 167 | + { |
| 168 | + sstrm << "[" << i << "]"; |
| 169 | + } |
| 170 | + |
| 171 | + sstrm << " = " << v << ";"; |
| 172 | + |
| 173 | + return sstrm.str(); |
| 174 | + } |
| 175 | + |
| 176 | + std::string generateArgument() |
| 177 | + { |
| 178 | + if(type.empty()) throw std::runtime_error("lblmc::ArrayObject::generateArgument(): type cannot be empty or null"); |
| 179 | + if(label.empty()) throw std::runtime_error("lblmc::ArrayObject::generateArgument(): label cannot be empty or null"); |
| 180 | + if(dimensions.empty()) throw std::runtime_error("lblmc::ArrayObject::generateArgument(): dimension size cannot be zero"); |
| 181 | + |
| 182 | + std::stringstream sstrm; |
| 183 | + |
| 184 | + for(auto q : qualifiers) |
| 185 | + { |
| 186 | + |
| 187 | + if(qualiferAsString(q).empty()) continue; |
| 188 | + |
| 189 | + sstrm << qualiferAsString(q) << " "; |
| 190 | + } |
| 191 | + |
| 192 | + sstrm << type << " " << label; |
| 193 | + |
| 194 | + for(auto d : dimensions) |
| 195 | + { |
| 196 | + sstrm << "[" << d << "]"; |
| 197 | + } |
| 198 | + |
| 199 | + return sstrm.str(); |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | +}; |
| 204 | + |
| 205 | +} //namespace codegen |
| 206 | + |
| 207 | +#endif // CODEGEN_ARRAYOBJECT_HPP |
0 commit comments