Change m_schema in Parser to a shared pointer
Shared pointers are reference counted, so this will prevent segmentation
faults in some cases.
Change-Id: Ie180cf6a639c8e73e70f395d19a6082b4fce73d4
diff --git a/src/main.cpp b/src/main.cpp
index e498db0..2835600 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -46,23 +46,23 @@
true);
std::cout << "Welcome to netconf-cli" << std::endl;
- Schema schema;
- schema.addContainer("", "a", yang::ContainerTraits::Presence);
- schema.addContainer("", "b");
- schema.addLeaf("", "leafString", yang::LeafDataTypes::String);
- schema.addLeaf("", "leafDecimal", yang::LeafDataTypes::Decimal);
- schema.addLeaf("", "leafBool", yang::LeafDataTypes::Bool);
- schema.addLeaf("", "leafInt", yang::LeafDataTypes::Int);
- schema.addLeaf("", "leafUint", yang::LeafDataTypes::Uint);
- schema.addLeafEnum("", "leafEnum", {"lol", "data", "coze"});
- schema.addContainer("a", "a2");
- schema.addLeaf("a", "leafa", yang::LeafDataTypes::String);
- schema.addContainer("b", "b2", yang::ContainerTraits::Presence);
- schema.addContainer("a/a2", "a3", yang::ContainerTraits::Presence);
- schema.addContainer("b/b2", "b3");
- schema.addList("", "list", {"number"});
- schema.addContainer("list", "contInList", yang::ContainerTraits::Presence);
- schema.addList("", "twoKeyList", {"number", "name"});
+ auto schema = std::make_shared<Schema>();
+ schema->addContainer("", "a", yang::ContainerTraits::Presence);
+ schema->addContainer("", "b");
+ schema->addLeaf("", "leafString", yang::LeafDataTypes::String);
+ schema->addLeaf("", "leafDecimal", yang::LeafDataTypes::Decimal);
+ schema->addLeaf("", "leafBool", yang::LeafDataTypes::Bool);
+ schema->addLeaf("", "leafInt", yang::LeafDataTypes::Int);
+ schema->addLeaf("", "leafUint", yang::LeafDataTypes::Uint);
+ schema->addLeafEnum("", "leafEnum", {"lol", "data", "coze"});
+ schema->addContainer("a", "a2");
+ schema->addLeaf("a", "leafa", yang::LeafDataTypes::String);
+ schema->addContainer("b", "b2", yang::ContainerTraits::Presence);
+ schema->addContainer("a/a2", "a3", yang::ContainerTraits::Presence);
+ schema->addContainer("b/b2", "b3");
+ schema->addList("", "list", {"number"});
+ schema->addContainer("list", "contInList", yang::ContainerTraits::Presence);
+ schema->addList("", "twoKeyList", {"number", "name"});
Parser parser(schema);
while (true) {
@@ -74,7 +74,7 @@
try {
command_ cmd = parser.parseCommand(input, std::cout);
- boost::apply_visitor(Interpreter(parser, schema), cmd);
+ boost::apply_visitor(Interpreter(parser, *schema), cmd);
} catch (InvalidCommandException& ex) {
std::cerr << ex.what() << std::endl;
}
diff --git a/src/parser.cpp b/src/parser.cpp
index e2d0330..0c62358 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -13,7 +13,7 @@
InvalidCommandException::~InvalidCommandException() = default;
-Parser::Parser(const Schema& schema)
+Parser::Parser(const std::shared_ptr<const Schema> schema)
: m_schema(schema)
{
}
@@ -21,7 +21,7 @@
command_ Parser::parseCommand(const std::string& line, std::ostream& errorStream)
{
command_ parsedCommand;
- ParserContext ctx(m_schema, m_curDir);
+ ParserContext ctx(*m_schema, m_curDir);
auto it = line.begin();
boost::spirit::x3::error_handler<std::string::const_iterator> errorHandler(it, line.end(), errorStream);
diff --git a/src/parser.hpp b/src/parser.hpp
index 0a253c1..05e636e 100644
--- a/src/parser.hpp
+++ b/src/parser.hpp
@@ -26,12 +26,12 @@
class Parser {
public:
- Parser(const Schema& schema);
+ Parser(const std::shared_ptr<const Schema> schema);
command_ parseCommand(const std::string& line, std::ostream& errorStream);
void changeNode(const path_& name);
std::string currentNode() const;
private:
- const Schema& m_schema;
+ const std::shared_ptr<const Schema> m_schema;
path_ m_curDir;
};