xtd 0.2.0
__enum_definition_to_enum_collection.hpp
Go to the documentation of this file.
1
3#pragma once
5#if !defined(__XTD_CORE_INTERNAL__)
6#error "Do not include this file: Internal use only"
7#endif
9#pragma once
11#include "../int64.hpp"
12#include "../string.hpp"
14
16template<class enum_t>
17xtd::enum_collection<enum_t> __enum_definition_to_enum_collection__(const xtd::string& enum_definition) {
19 #if !defined(__XTD_DO_NOT_USE_ENUMERATION_INTROSPECTION__)
20 for (size_t num_state = 0; num_state < __enumeration_introspection::num_states<enum_t>; ++num_state)
21 entries.emplace_back(static_cast<enum_t>(__enumeration_introspection::enum_values<enum_t>[num_state]), __enumeration_introspection::enum_names<enum_t>[num_state]);
22 #else
23 using namespace xtd;
24 if (enum_definition.empty()) return entries;
25 xtd::int64 current_value = 0;
26 for (auto entry : enum_definition.split({','})) {
27 auto key_value = entry.trim().split({'='});
28 if (key_value.size() < 1 || key_value.size() > 2 || (key_value.size() == 2 && xtd::string::is_empty(key_value[1])))
30 xtd::int64 value = current_value;
31 if (key_value.size() == 2) {
33 auto iterator = std::find_if(entries.begin(), entries.end(), [&](auto item)->bool {return item.second == key_value[1].trim();});
34 if (iterator != entries.end()) value = static_cast<xtd::int64>(iterator->first);
35
38 }
39 }
40 current_value = value;
41 entries.emplace_back(static_cast<enum_t>(value), key_value[0].trim());
42 current_value++;
43 }
44 #endif
45 return entries;
46}
bool is_empty() const noexcept
Definition basic_string.hpp:1471
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Contains xtd::enum_collection.
Contains xtd::format_exception exception.
@ format
The format is not valid.
Definition exception_case.hpp:49
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::vector< std::pair< enum_t, xtd::string > > enum_collection
Represents a pair of an enum_t value and a string of an enum of type enum_t.
Definition enum_collection.hpp:24
bool try_parse(const std::basic_string< char > &str, value_t &value) noexcept
Convert a string into a type.
Definition parse.hpp:416
@ binary_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_binary_specifier styles are u...
Definition number_styles.hpp:56
@ octal_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_octal_specifier styles are us...
Definition number_styles.hpp:58
@ number
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_trailing_sign...
Definition number_styles.hpp:46
@ hex_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_hex_specifier styles are used...
Definition number_styles.hpp:54
Contains xtd::int64 type.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::string alias.