29 #define _unused(x) ((void)(x)) 35 #include "SUAPI-CppWrapper/model/AttributeDictionary.hpp" 37 #include "SUAPI-CppWrapper/model/TypedValue.hpp" 38 #include "SUAPI-CppWrapper/String.hpp" 42 extern size_t SU_VERSION_MAJOR;
47 SUAttributeDictionaryRef AttributeDictionary::create_attribute_dictionary(
const std::string& name) {
48 if (SU_VERSION_MAJOR < 18) {
49 throw std::logic_error(
"AttributeDictionary::create_attribute_dictionary(): Cannot use function prior to SU version 2018");
51 SUAttributeDictionaryRef dict = SU_INVALID;
52 SUResult res = SUAttributeDictionaryCreate(&dict, name.c_str());
53 assert(res == SU_ERROR_NONE);
59 SUAttributeDictionaryRef AttributeDictionary::copy_reference(
const AttributeDictionary& other) {
60 if (SU_VERSION_MAJOR < 18) {
61 throw std::logic_error(
"AttributeDictionary::create_attribute_dictionary(): Cannot use function prior to SU version 2018");
63 if (other.m_attached || !other) {
67 SUAttributeDictionaryRef new_dict = create_attribute_dictionary(other.get_name());
86 Entity(SUAttributeDictionaryToEntity(dict_ref), attached)
92 Entity(other, SUAttributeDictionaryToEntity(copy_reference(other)))
96 std::vector<std::string> keys = other.
get_keys();
97 for (
size_t i=0; i < keys.size(); i++) {
105 if (SU_VERSION_MAJOR >= 18) {
107 SUAttributeDictionaryRef dict = this->
ref();
108 SUResult res = SUAttributeDictionaryRelease(&dict);
109 assert(res == SU_ERROR_NONE);
121 SUAttributeDictionaryRef dict = this->
ref();
122 SUResult res = SUAttributeDictionaryRelease(&dict);
123 assert(res == SU_ERROR_NONE);
126 SUAttributeDictionaryRef dict = copy_reference(other);
127 m_entity = SUAttributeDictionaryToEntity(dict);
134 return SUAttributeDictionaryFromEntity(
m_entity);
138 AttributeDictionary::operator SUAttributeDictionaryRef()
const {
145 throw std::logic_error(
"CW::AttributeDictionary::get_attribute(): AttributeDictionary is null");
147 SUTypedValueRef val = SU_INVALID;
148 SUResult res = SUTypedValueCreate(&val);
149 assert(res == SU_ERROR_NONE);
150 const char* key_char = key.c_str();
151 res = SUAttributeDictionaryGetValue(this->
ref(), &key_char[0], &val);
152 if (res == SU_ERROR_NO_DATA) {
153 return default_value;
155 assert(res == SU_ERROR_NONE); _unused(res);
161 throw std::logic_error(
"CW::AttributeDictionary::set_attribute(): AttributeDictionary is null");
163 SUResult res = SUAttributeDictionarySetValue(this->
ref(), key.data(), value.ref());
164 if (res == SU_ERROR_NONE) {
174 throw std::logic_error(
"CW::AttributeDictionary::get_keys(): AttributeDictionary is null");
177 SUResult res = SUAttributeDictionaryGetNumKeys(this->
ref(), &num_keys);
178 assert(res == SU_ERROR_NONE);
179 std::vector<SUStringRef> keys_ref(num_keys, SU_INVALID);
180 std::for_each(keys_ref.begin(), keys_ref.end(),
181 [](SUStringRef& value) {
182 SUResult res = SUStringCreate(&value);
183 assert(res == SU_ERROR_NONE); _unused(res);
185 res = SUAttributeDictionaryGetKeys(this->
ref(), num_keys, keys_ref.data(), &num_keys);
186 assert(res == SU_ERROR_NONE); _unused(res);
187 std::vector<std::string> keys(num_keys);
188 std::transform(keys_ref.begin(), keys_ref.end(), keys.begin(),
189 [](
const SUStringRef& value) {
190 return String(value).std_string();
201 SUStringRef *string_ref = string;
202 SUResult res = SUAttributeDictionaryGetName(this->
ref(), string_ref);
203 assert(res == SU_ERROR_NONE);
bool m_attached
Indicates whether the Entity has been attached to a model.
SUAttributeDictionaryRef ref() const
std::string get_name() const
AttributeDictionary & operator=(const AttributeDictionary &other)
bool attached() const
Returns true if the entity is attached to another object.
TypedValue get_attribute(const std::string &key, const TypedValue &default_value) const
TypedValue get_value(const std::string &key) const
SUEntityRef m_entity
The C SUEntityRef that this class wraps.
std::vector< std::string > get_keys() const
bool set_attribute(const std::string &key, const TypedValue &value)
Entity & operator=(const Entity &other)
Copy assignment operator.