PLaSK library
Loading...
Searching...
No Matches
info.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__MATERIAL_INFO_H
15#define PLASK__MATERIAL_INFO_H
16
21#include <string>
22#include <map>
23#include <vector>
24#include <utility>
25
26#include <boost/tokenizer.hpp>
27
28#include "../optional.hpp"
29
30#include <plask/config.hpp> // for PLASK_API
31
32namespace plask {
33
40
98
100 static const char* PROPERTY_NAME_STRING[55];
101
102 static PROPERTY_NAME parsePropertyName(const std::string& name);
103
114
116 static const char* ARGUMENT_NAME_STRING[7];
117
118 static ARGUMENT_NAME parseArgumentName(const std::string& name);
119
125 std::string className;
129 std::string note;
130
131 Link(std::string className, PROPERTY_NAME property, std::string note = std::string())
132 : className(className), property(property), note(note) {}
133
138 Link(const std::string& to_parse);
139
144 std::string str() const;
145 };
146
149
151 std::string _info;
152
160 PropertyInfo& add(const std::string& infoToAdd) {
161 if (_info.empty()) _info = infoToAdd; else (_info += '\n') += infoToAdd;
162 return *this;
163 }
164
165 public:
166
167 boost::tokenizer<boost::char_separator<char>> eachLine() const;
168
169 std::vector<std::string> eachOfType(const std::string& type) const;
170
171 typedef std::pair<double, double> ArgumentRange;
172
174 PropertyInfo(std::string info = ""): _info(std::move(info)) {}
175
178
184 PropertyInfo& set(const std::string& new_info) { this->_info = new_info; return *this; }
185
190 const std::string& get() const { return _info; }
191
196 std::string getSource() const;
197
202 std::string getNote() const;
203
209 ArgumentRange getArgumentRange(ARGUMENT_NAME argument) const;
210
217 std::vector<Link> getLinks() const;
218
224 PropertyInfo& addSource(const std::string& sourceToAdd) {
225 return add("source: " + sourceToAdd);
226 }
227
233 PropertyInfo& addNote(const std::string& sourceToAdd) {
234 return add("note: " + sourceToAdd);
235 }
236
243 PropertyInfo& setArgumentRange(ARGUMENT_NAME argument, ArgumentRange range);
244
251 PropertyInfo& setArgumentRange(ARGUMENT_NAME argument, double from, double to) {
252 return setArgumentRange(argument, ArgumentRange(from, to));
253 }
254
260 PropertyInfo& addLink(const Link& link) { return add("see: " + link.str()); }
261 };
262
264 std::string parent;
265
266 private:
268 std::map<PROPERTY_NAME, PropertyInfo> propertyInfo;
269
270 public:
271
278 void override(const MaterialInfo &to_override);
279
284 PropertyInfo& operator()(PROPERTY_NAME property);
285
291 plask::optional<PropertyInfo> getPropertyInfo(PROPERTY_NAME property) const;
292
294 typedef std::map<PROPERTY_NAME, PropertyInfo>::iterator iterator;
295
297 typedef std::map<PROPERTY_NAME, PropertyInfo>::const_iterator const_iterator;
298
303 iterator begin() { return propertyInfo.begin(); }
304
309 const_iterator begin() const { return propertyInfo.begin(); }
310
315 iterator end() { return propertyInfo.end(); }
316
321 const_iterator end() const { return propertyInfo.end(); }
322
323 //const PropertyInfo& operator()(PROPERTY_NAME property) const;
324
326 class PLASK_API DB {
327
329 std::map<std::string, MaterialInfo> materialInfo;
330
331 public:
332
337 static DB& getDefault();
338
342 void clear() {
343 materialInfo.clear();
344 }
345
350 void update(const DB& src) {
351 for (const auto& item: src.materialInfo) {
352 materialInfo[item.first] = item.second;
353 }
354 }
355
362 MaterialInfo& add(const std::string& materialName, const std::string& parentMaterial);
363
369 MaterialInfo& add(const std::string& materialName);
370
377 plask::optional<MaterialInfo> get(const std::string& materialName, bool with_inherited_info = true) const;
378
385 plask::optional<MaterialInfo::PropertyInfo> get(const std::string& materialName, PROPERTY_NAME propertyName, bool with_inherited_info = true) const;
386
388 typedef std::map<std::string, MaterialInfo>::iterator iterator;
389
391 typedef std::map<std::string, MaterialInfo>::const_iterator const_iterator;
392
397 iterator begin() { return materialInfo.begin(); }
398
403 const_iterator begin() const { return materialInfo.begin(); }
404
409 iterator end() { return materialInfo.end(); }
410
415 const_iterator end() const { return materialInfo.end(); }
416
417 };
418
423
424 void set(PropertyInfo&) {}
425
426 template <typename Setter1, typename ...Setters>
427 void set(PropertyInfo& i, const Setter1& setter1, const Setters&... setters) {
428 setter1.set(i);
429 set(i, setters...);
430 }
431
432 public:
433 Register(const std::string& materialName, const std::string& parentMaterial) {
434 MaterialInfo::DB::getDefault().add(materialName, parentMaterial);
435 }
436
437 template <typename ...PropertySetters>
438 Register(const std::string& materialName, const std::string& parentMaterial, PROPERTY_NAME property, const PropertySetters&... propertySetters) {
439 set(MaterialInfo::DB::getDefault().add(materialName, parentMaterial)(property), propertySetters...);
440 }
441
442 Register(const std::string& materialName) {
443 MaterialInfo::DB::getDefault().add(materialName);
444 }
445
446 template <typename ...PropertySetters>
447 Register(const std::string& materialName, PROPERTY_NAME property, const PropertySetters&... propertySetters) {
448 set(MaterialInfo::DB::getDefault().add(materialName)(property), propertySetters...);
449 }
450
451 /*template <typename MaterialType, typename ParentType, typename ...PropertySetters>
452 Register(const PropertySetters&... propertySetters) {
453 set(MaterialInfo::DB::getDefault().add(MaterialType::NAME, ParentType::NAME), propertySetters...);
454 }
455
456 template <typename MaterialType, typename ...PropertySetters>
457 Register(const PropertySetters&... propertySetters) {
458 set(MaterialInfo::DB::getDefault().add(MaterialType::NAME), propertySetters...);
459 }*/
460
461 };
462
463 /*struct RegisterProperty {
464
465 PropertyInfo& property;
466
467 RegisterProperty(Register& material, PROPERTY_NAME property)
468 : property(material(property)) {}
469
470 RegisterProperty(std::string& material_name, PROPERTY_NAME property)
471 : property(MaterialInfo::DB::getDefault().add(material_name)(property)) {}
472
473 template <typename MaterialType>
474 RegisterProperty(PROPERTY_NAME property)
475 : property(MaterialInfo::DB::getDefault().add(MaterialType::NAME)(property)) {}
476
477 RegisterProperty& source(const std::string& source) { property.addSource(source); return *this; }
478
479 RegisterProperty& info(const std::string& info) { property.add(info); return *this; }
480
481 RegisterProperty& argumentRange(ARGUMENT_NAME argument, PropertyInfo::ArgumentRange range) {
482 property.setArgumentRange(argument, range); return *this;
483 }
484
485 RegisterProperty& argumentRange(ARGUMENT_NAME argument, double from, double to) {
486 property.setArgumentRange(argument, from, to); return *this;
487 }
488
489 };*/
490
491};
492
493struct MISource {
494 std::string value;
495 MISource(const std::string& value): value(value) {}
496 void set(MaterialInfo::PropertyInfo& p) const { p.addSource(value); }
497};
498
499struct MINote {
500 std::string value;
501 MINote(const std::string& value): value(value) {}
502 void set(MaterialInfo::PropertyInfo& p) const { p.addNote(value); }
503};
504
510
511struct MISee {
513 template<typename ...Args> MISee(Args&&... params): value(std::forward<Args>(params)...) {}
514 void set(MaterialInfo::PropertyInfo& p) const { p.addLink(value); }
515};
516
517template <typename materialClass>
520 template<typename ...Args> MISeeClass(Args&&... params): value(materialClass::NAME, std::forward<Args>(params)...) {}
521 void set(MaterialInfo::PropertyInfo& p) const { p.addLink(value); }
522};
523
524}
525
526#define MI_PARENT(material, parent) static plask::MaterialInfo::Register __materialinfo__parent__ ## material(material::NAME, parent::NAME);
527#define MI_PROPERTY(material, property, ...) static plask::MaterialInfo::Register __materialinfo__property__ ## material ## property(material::NAME, plask::MaterialInfo::property, ##__VA_ARGS__);
528#define MI_PARENT_PROPERTY(material, parent, property, ...) static plask::MaterialInfo::Register __materialinfo__parent__property__ ## material ## property(material::NAME, parent::NAME, plask::MaterialInfo::property, ##__VA_ARGS__);
529
530#endif // PLASK__MATERIAL_INFO_H