Cogs.Core
AnnotationComponent.h
1#pragma once
2
3#include "Components/Core/DynamicComponent.h"
4
5#include "Resources/Resources.h"
6
7namespace Cogs::RationalReducerExtension
8{
15 {
16 std::vector<std::string> annotations;
17
18 static void registerType();
19
20 public:
22 static constexpr auto npos { static_cast<size_t>(-1) };
23
30 size_t indexOfKey(const Cogs::StringView key)
31 {
32 for (size_t i = 0; i < annotations.size(); ++i)
33 {
34 const auto & annot = annotations[i];
35 if (annot.find(key.data(), 0) == 0 && annot.size() > key.size() && annot[key.size()] == ':') {
36 return i;
37 }
38 }
39
40 return npos;
41 }
42
50 {
51 return indexOfKey(key) != npos;
52 }
53
60 std::string getValue(const Cogs::StringView key)
61 {
62 const auto index = indexOfKey(key);
63 if (index == npos)
64 return std::string();
65 else
66 return annotations[index].substr(key.size() + 1);
67 }
68
75 void setValue(const Cogs::StringView key, const Cogs::StringView value)
76 {
77 const auto index = indexOfKey(key);
78 if (index == npos)
79 annotations.emplace_back(key.to_string() + ':' + value.to_string());
80 else
81 annotations[index] = annotations[index].substr(0, key.size() + 1) + value.to_string();
82 }
83 };
84}
85
86template<> inline Cogs::StringView getName<Cogs::RationalReducerExtension::AnnotationComponent>() { return "AnnotationComponent"; }
Base class for components implementing dynamic behavior.
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
Definition: StringView.h:171
constexpr size_t size() const noexcept
Get the size of the string.
Definition: StringView.h:178
std::string to_string() const
String conversion method.
Definition: StringView.cpp:9
A component that serves as storage for a vector of text annotations.
bool containsKey(const Cogs::StringView key)
Check if the annotations vector contains the key using ':' for key/value separator.
size_t indexOfKey(const Cogs::StringView key)
Get index of the key in the annotations vector using ':' for key/value separator.
void setValue(const Cogs::StringView key, const Cogs::StringView value)
Add or replace key value using ':' for key/value separator.
std::string getValue(const Cogs::StringView key)
Get key value using ':' for key/value separator.
static constexpr auto npos
Undefined index.