Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
BaseParameterManager.h
1/*
2 * Copyright 2020 IFPEN-CEA
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 */
18
19#pragma once
20
21#include <map>
22#include <sstream>
23#include <vector>
24
25#include <alien/utils/Precomp.h>
26#include <string>
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Alien
32{
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37class BaseParameterMng
38{
39 public:
40 BaseParameterMng()
41 : m_child_mng(nullptr)
42 , m_nb_child(0)
43 , m_parameters_have_changed(false)
44 {}
45
46 virtual ~BaseParameterMng()
47 {
48 if (m_nb_child == 1)
49 delete m_child_mng;
50 else
51 delete[] m_child_mng;
52 }
53
54 BaseParameterMng* getChild()
55 {
56 if (!m_child_mng) {
57 m_nb_child = 1;
58 m_child_mng = new BaseParameterMng();
59 }
60 return m_child_mng;
61 }
62
63 [[nodiscard]] BaseParameterMng const* getChild() const { return m_child_mng; }
64
65 [[nodiscard]] bool hasChild() const { return (m_nb_child != 0); }
66
67 [[nodiscard]] const BaseParameterMng* getChild(Arccore::Integer child) const
68 {
69 ALIEN_ASSERT(m_nb_child > 0 && child < m_nb_child, "Inconsistent child values");
70 return &m_child_mng[child];
71 }
72
73 BaseParameterMng* getChild(Integer child)
74 {
75 ALIEN_ASSERT(m_nb_child > 0 && child < m_nb_child, "Inconsistent child values");
76 return &m_child_mng[child];
77 }
78
79 void setNbChild(Integer nbChild)
80 {
81 if (m_nb_child == 1 || m_nb_child == 0)
82 delete m_child_mng;
83 else
84 delete[] m_child_mng;
85 m_nb_child = nbChild;
86 m_child_mng = new BaseParameterMng[nbChild];
87 }
88
89 template <typename ValueT>
90 std::map<std::string, ValueT> const& getParams() const;
91
92 template <typename ValueT>
93 std::map<std::string, ValueT>& getParams();
94
95 template <typename ValueT>
96 void setParameter(std::string const& key, ValueT const& value)
97 {
98 getParams<ValueT>()[key] = value;
99 }
100
101 template <typename ValueT>
102 ValueT getParameter(std::string const& key, ValueT default_value) const
103 {
104 auto iter = getParams<ValueT>().find(key);
105 if (iter == getParams<ValueT>().end())
106 return default_value;
107 else
108 return iter->second;
109 }
110
111 template <typename ValueT>
112 void addOptions(
113 std::vector<std::string>& options, std::map<std::string, ValueT> const& params)
114 {
115 for (auto iter = params.begin(); iter != params.end(); ++iter) {
116 options.push_back(iter->first);
117 std::stringstream value_str;
118 value_str << iter->second;
119 options.push_back(value_str.str());
120 }
121 }
122
123 void addOptions(std::vector<std::string>& options)
124 {
125 addOptions(options, m_int_params);
126 addOptions(options, m_double_params);
127 addOptions(options, m_string_params);
128 }
129
130 void notifyParamChangesObserver() { m_parameters_have_changed = true; }
131
132 public:
133 void resetParamChangesObserver() { m_parameters_have_changed = false; }
134
135 bool needUpdate() { return m_parameters_have_changed; }
136
137 protected:
138 std::map<std::string, int> m_int_params;
139 std::map<std::string, double> m_double_params;
140 std::map<std::string, std::string> m_string_params;
141 BaseParameterMng* m_child_mng;
142 Integer m_nb_child;
143 bool m_parameters_have_changed;
144};
145
146/*---------------------------------------------------------------------------*/
147/*---------------------------------------------------------------------------*/
148
149template <>
150[[nodiscard]] ALIEN_EXPORT std::map<std::string, int> const&
151BaseParameterMng::getParams() const;
152
153template <>
154ALIEN_EXPORT std::map<std::string, int>& BaseParameterMng::getParams();
155
156template <>
157[[nodiscard]] ALIEN_EXPORT std::map<std::string, double> const&
158BaseParameterMng::getParams() const;
159
160template <>
161ALIEN_EXPORT std::map<std::string, double>& BaseParameterMng::getParams();
162
163template <>
164[[nodiscard]] ALIEN_EXPORT std::map<std::string, std::string> const&
165BaseParameterMng::getParams() const;
166
167template <>
168ALIEN_EXPORT std::map<std::string, std::string>& BaseParameterMng::getParams();
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
172
173} // namespace Alien
174
175/*---------------------------------------------------------------------------*/
176/*---------------------------------------------------------------------------*/
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17