timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
vsettings.cpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
26
30
31#include <algorithm>
32#include <cstdint>
33#include <iosfwd>
34#include <map>
35#include <sstream>
36#include <string>
37#include <utility>
38#include <vector>
39
40namespace tim
41{
42//
45 std::vector<std::string> _cmdline, int32_t _count,
46 int32_t _max_count, std::vector<std::string> _choices)
47: m_count(_count)
48, m_max_count(_max_count)
49, m_name(std::move(_name))
50, m_env_name(std::move(_env_name))
51, m_description(std::move(_descript))
52, m_cmdline(std::move(_cmdline))
53, m_choices(std::move(_choices))
54{}
55//
57vsettings::get_display(std::ios::fmtflags fmt, int _w, int _p)
58{
59 display_map_t _data;
60 auto _as_str = [&](auto _val) {
61 std::stringstream _ss;
62 _ss.setf(fmt);
63 if(_w > -1)
64 _ss << std::setw(_w);
65 if(_p > -1)
66 _ss << std::setprecision(_p);
67 _ss << std::boolalpha << _val;
68 return _ss.str();
69 };
70
71 auto _arr_as_str = [&](auto _val) -> std::string {
72 if(_val.empty())
73 return "";
74 std::stringstream _ss;
75 for(size_t i = 0; i < _val.size(); ++i)
76 _ss << ", " << _as_str(_val.at(i));
77 return _ss.str().substr(2);
78 };
79
80 _data["name"] = _as_str(m_name);
81 _data["count"] = _as_str(m_count);
82 _data["max_count"] = _as_str(m_max_count);
83 _data["env_name"] = _as_str(m_env_name);
84 _data["description"] = _as_str(m_description);
85 _data["command_line"] = _arr_as_str(m_cmdline);
86 _data["choices"] = _arr_as_str(m_choices);
87 return _data;
88}
89//
91vsettings::matches(const std::string& inp, bool exact) const
92{
93 // an exact match to name or env-name should always be checked
94 if(inp == m_env_name || inp == m_name)
95 return true;
96
97 if(exact)
98 {
99 // match the command-line option w/ or w/o leading dashes
100 auto _cmd_line_exact = [&](const std::string& itr) {
101 // don't match short-options
102 if(itr.length() == 2)
103 return false;
104 auto _with_dash = (itr == inp);
105 auto _pos = itr.find_first_not_of('-');
106 if(_with_dash || _pos == std::string::npos)
107 return _with_dash;
108 return (itr.substr(_pos) == inp);
109 };
110
111 return std::any_of(m_cmdline.begin(), m_cmdline.end(), _cmd_line_exact);
112 }
113 else
114 {
115 const auto cre = std::regex_constants::icase;
116 const std::regex re(inp, cre);
117
118 if(std::regex_search(m_env_name, re) || std::regex_search(m_name, re))
119 return true;
120
121 auto _cmd_line_regex = [&](const std::string& itr) {
122 // don't match short-options
123 if(itr.length() == 2)
124 return false;
125 return std::regex_search(itr, re);
126 };
127
128 return std::any_of(m_cmdline.begin(), m_cmdline.end(), _cmd_line_regex);
129 }
130}
131//
132//--------------------------------------------------------------------------------------//
133//
135vsettings::clone(std::shared_ptr<vsettings> rhs)
136{
137 m_name = rhs->m_name;
138 m_env_name = rhs->m_env_name;
139 m_description = rhs->m_description;
140 m_cmdline = rhs->m_cmdline;
141 m_count = rhs->m_count;
142 m_max_count = rhs->m_max_count;
143}
144//
145} // namespace tim
STL namespace.
Definition: kokkosp.cpp:39
TIMEMORY_SETTINGS_LINKAGE(vsettings::display_map_t) vsettings
Definition: vsettings.cpp:56
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
#define TIMEMORY_SETTINGS_INLINE
Definition: macros.hpp:51
int32_t m_count
Definition: vsettings.hpp:144
int32_t m_max_count
Definition: vsettings.hpp:145
virtual display_map_t get_display(std::ios::fmtflags fmt={}, int _w=-1, int _p=-1)
virtual bool matches(const std::string &, bool exact=true) const
std::vector< std::string > m_choices
Definition: vsettings.hpp:150
vsettings(std::string _name="", std::string _env_name="", std::string _descript="", std::vector< std::string > _cmdline={}, int32_t _count=-1, int32_t _max_count=-1, std::vector< std::string > _choices={})
Definition: vsettings.cpp:44
std::string m_name
Definition: vsettings.hpp:146
std::map< std::string, std::string > display_map_t
Definition: vsettings.hpp:59
std::string m_description
Definition: vsettings.hpp:148
std::string m_env_name
Definition: vsettings.hpp:147
std::vector< std::string > m_cmdline
Definition: vsettings.hpp:149
virtual std::shared_ptr< vsettings > clone()=0