TkN 2.7
Toolkit for Nuclei
Loading...
Searching...
No Matches
tkproperty_list.cpp
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#include "tkproperty_list.h"
15
16#include <array>
17#include <charconv>
18
19namespace tkn {
30}
31
32using namespace tkn;
33
34bool tkproperty_list::has_property(const tkstring &_property) const
35{
36 return fProperties.find(_property) != fProperties.end();
37}
38
40{
41 const auto property = fProperties.find(_property);
42 return property == fProperties.end() ? tkstring("") : property->second.value;
43}
44
46{
47 const auto property = fProperties.find(_property);
48 return property == fProperties.end() ? tkstring("") : property->second.unit;
49}
50
52{
53 const auto property = fProperties.find(_property);
54 return property == fProperties.end() ? tkstring("") : property->second.type;
55}
56
58{
59 glog << info << fname << " properties:" << do_endl;
60 for(const auto &prop : fProperties)
61 if (prop.first.match(_opt))
62 glog << info << left << setw(35) << prop.first << setw(35) << prop.second.value + " " + prop.second.unit << "[" << prop.second.type<< "]" << do_endl;
63}
64
66{
67 glog << info << fname << " data properties:" << do_endl;
68 for(const auto &prop : fDataProperties)
69 if (prop.first.match(_opt))
70 glog << info << left << setw(35) << prop.first << prop.second << do_endl;
71}
72
73vector<shared_ptr<tkmeasure>> tkproperty_list::get_data_properties(const tkstring &_opt)
74{
75 vector<shared_ptr<tkmeasure>> list;
76 for (auto &data : fDataProperties) {
77 if (data.first.match(_opt)) list.push_back(data.second);
78 }
79 return list;
80}
81
82shared_ptr<tkmeasure> tkproperty_list::get(const tkstring &_property) const
83{
84 const auto property = fDataProperties.find(_property);
85 return property == fDataProperties.end() ? nullptr : property->second;
86}
87
88void tkproperty_list::add_property_str(const tkstring &_name, const tkstring &_val, const tkstring &_unit, const tkstring &_type)
89{
90 const auto property = fProperties.try_emplace(_name).first;
91 property->second.value = _val;
92 property->second.unit = _unit;
93 property->second.type = _type;
94}
95
96void tkproperty_list::add_property(const tkstring &_name, const shared_ptr<tkmeasure> &_property, const tkstring &_value_str, const tkstring &_unit_str)
97{
98 std::array<char, 64> buffer{};
99 const auto conversion = std::to_chars(
100 buffer.data(), buffer.data() + buffer.size(), _property->get_value(),
101 std::chars_format::fixed, 6);
102 tkstring val;
103 if (conversion.ec == std::errc())
104 val.assign(buffer.data(), conversion.ptr);
105 else
106 val = tkstring::form("%f", _property->get_value());
107 tkstring unit = _property->get_unit();
108 if(!_value_str.is_empty()) val = _value_str;
109 if(_unit_str!="!") unit = _unit_str;
110 add_property_str(_name,val,unit,"FLOAT");
111 fDataProperties.insert_or_assign(_name, _property);
112}
113
114#ifdef HAS_ROOT
115ClassImp(tkproperty_list);
116#endif
Contains list of properties.
tkstring get_property_unit(const tkstring &_property) const
get the property unnit as a string
void list_data_properties(const tkstring &_opt="*") const
list the available tkmeasure properties
bool has_property(const tkstring &_property) const
to check if the property is available
shared_ptr< tkmeasure > get(const tkstring &_property) const
get the property as tkmeasure
tkstring get_property_type(const tkstring &_property) const
get the property type as a string
tkstring get_property(const tkstring &_property) const
get the property value as a string
vector< shared_ptr< tkmeasure > > get_data_properties(const tkstring &_opt="*")
get a vector of data properties according to a regular expression
void list_properties(const tkstring &_opt="*") const
list the available properties
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:33
static const char * form(const char *_format,...)
Definition tkstring.cpp:438
bool is_empty() const
Definition tkstring.h:145
Definition tklog.cpp:16
tklog & info(tklog &log)
Definition tklog.h:313
tklog & do_endl(tklog &log)
Definition tklog.h:212