| helics 3.6.1

JsonBuilder.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 #include <map>
9 #include <memory>
10 #include <nlohmann/json_fwd.hpp>
11 #include <string>
12 #include <utility>
13 #include <vector>
14
15 namespace helics::fileops {
16
18 class JsonMapBuilder {
19 private:
20 std::unique_ptr<nlohmann::json> jMap;
21 std::map<int, std::pair<std::string, int32_t>> missing_components;
22 int counterCode{0}; // a code for the user to include for various purposes
23 public:
24 JsonMapBuilder() noexcept;
25 ~JsonMapBuilder();
26 JsonMapBuilder(JsonMapBuilder&& map) noexcept = default;
27 JsonMapBuilder& operator=(JsonMapBuilder&& map) = default;
29 nlohmann::json& getJValue();
31 bool isCompleted() const;
32 // check whether a map is currently completed or under construction
33 bool isActive() const { return static_cast<bool>(jMap); }
39 bool addComponent(const std::string& info, int index) noexcept;
42 int generatePlaceHolder(const std::string& location, int32_t code);
45 bool clearComponents(int32_t code);
48 bool clearComponents();
50 std::string generate();
52 void reset();
54 void setCounterCode(int code) { counterCode = code; }
55 int getCounterCode() const { return counterCode; }
56 };
57
59 class JsonBuilder {
60 private:
61 std::unique_ptr<nlohmann::json> jMap;
62
63 public:
64 JsonBuilder() noexcept;
65 ~JsonBuilder();
67 nlohmann::json& getJValue();
69 void addElement(const std::string& path, const std::string& value);
71 void addElement(const std::string& path, double value);
73 void addElement(const std::string& path, const std::vector<double>& value);
75 std::string generate();
77 void reset();
78 };
79
80 } // namespace helics::fileops
helics::fileops::JsonBuilder
Definition: JsonBuilder.hpp:59
helics::fileops::JsonBuilder::addElement
void addElement(const std::string &path, const std::string &value)
Definition: JsonBuilder.cpp:103
helics::fileops::JsonBuilder::reset
void reset()
Definition: JsonBuilder.cpp:170
helics::fileops::JsonBuilder::generate
std::string generate()
Definition: JsonBuilder.cpp:162
helics::fileops::JsonBuilder::getJValue
nlohmann::json & getJValue()
Definition: JsonBuilder.cpp:154
helics::fileops::JsonMapBuilder
Definition: JsonBuilder.hpp:18
helics::fileops::JsonMapBuilder::isCompleted
bool isCompleted() const
Definition: JsonBuilder.cpp:33
helics::fileops::JsonMapBuilder::setCounterCode
void setCounterCode(int code)
Definition: JsonBuilder.hpp:54
helics::fileops::JsonMapBuilder::addComponent
bool addComponent(const std::string &info, int index) noexcept
Definition: JsonBuilder.cpp:45
helics::fileops::JsonMapBuilder::generatePlaceHolder
int generatePlaceHolder(const std::string &location, int32_t code)
Definition: JsonBuilder.cpp:38
helics::fileops::JsonMapBuilder::clearComponents
bool clearComponents()
Definition: JsonBuilder.cpp:79
helics::fileops::JsonMapBuilder::generate
std::string generate()
Definition: JsonBuilder.cpp:85
helics::fileops::JsonMapBuilder::getJValue
nlohmann::json & getJValue()
Definition: JsonBuilder.cpp:25
helics::fileops::JsonMapBuilder::reset
void reset()
Definition: JsonBuilder.cpp:93