-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scope and field classes for improved item management
- Loading branch information
1 parent
b536b5a
commit 19fafa0
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <memory> | ||
#include <map> | ||
|
||
namespace riddle | ||
{ | ||
class core; | ||
class type; | ||
|
||
/** | ||
* @brief A field. | ||
*/ | ||
class field | ||
{ | ||
public: | ||
field(type &tp, std::string &&name) noexcept; | ||
|
||
/** | ||
* @brief Retrieves the type of the field. | ||
* | ||
* This function returns a reference to the type of the field. | ||
* | ||
* @return A reference to the type of the field. | ||
*/ | ||
[[nodiscard]] type &get_type() const noexcept { return tp; } | ||
|
||
/** | ||
* @brief Retrieves the name of the field. | ||
* | ||
* This function returns a reference to the name of the field. | ||
* | ||
* @return A reference to the name of the field. | ||
*/ | ||
[[nodiscard]] const std::string &get_name() const noexcept { return name; } | ||
|
||
private: | ||
type &tp; | ||
std::string name; | ||
}; | ||
|
||
class scope | ||
{ | ||
public: | ||
scope(core &c, scope &parent, std::vector<std::unique_ptr<field>> &&args = {}); | ||
virtual ~scope() = default; | ||
|
||
protected: | ||
/** | ||
* @brief Adds a field to the scope. | ||
* | ||
* This function takes ownership of the provided field and adds it to the scope. | ||
* | ||
* @param field A unique pointer to the field to be added. | ||
*/ | ||
void add_field(std::unique_ptr<field> field); | ||
|
||
private: | ||
core &cr; | ||
scope &parent; | ||
|
||
protected: | ||
std::map<std::string, std::unique_ptr<field>, std::less<>> fields; | ||
}; | ||
} // namespace riddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "scope.hpp" | ||
#include <stdexcept> | ||
|
||
namespace riddle | ||
{ | ||
field::field(type &tp, std::string &&name) noexcept : tp(tp), name(std::move(name)) {} | ||
|
||
scope::scope(core &c, scope &parent, std::vector<std::unique_ptr<field>> &&args) : cr(c), parent(parent) | ||
{ | ||
for (auto &arg : args) | ||
add_field(std::move(arg)); | ||
} | ||
|
||
void scope::add_field(std::unique_ptr<field> field) | ||
{ | ||
if (!fields.emplace(field->get_name(), std::move(field)).second) | ||
throw std::runtime_error("Field already exists."); | ||
} | ||
} // namespace riddle |