xtd 0.2.0
Loading...
Searching...
No Matches
__enum_definition_to_enum_collection.h
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
10#include "../format_exception.h"
11#include "../types.h"
12#include "../string.h"
13#include "../enum_collection.h"
14
16template<typename 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])))
29 throw xtd::format_exception("Not a valid enum declaration"_t);
30 xtd::int64 value = current_value;
31 if (key_value.size() == 2) {
32 if (!xtd::try_parse<xtd::int64>(key_value[1].trim(), value, xtd::number_styles::number) && xtd::try_parse<xtd::int64>(key_value[1].trim(), value, xtd::number_styles::hex_number) && xtd::try_parse<xtd::int64>(key_value[1].trim(), value, xtd::number_styles::binary_number) && xtd::try_parse<xtd::int64>(key_value[1].trim(), value, xtd::number_styles::octal_number)) {
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
37 throw xtd::format_exception("Not a valid enum declaration"_t);
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}
Represents text as a sequence of character units.
Definition basic_string.h:79
bool is_empty() const noexcept
Indicates whether this basic_string is an empty basic_string ("").
Definition basic_string.h:1511
bool empty() const noexcept
Checks if the string has no characters, i.e. whether begin() == end().
Definition basic_string.h:904
The exception that is thrown when the format of an argument does not meet the parameter specification...
Definition format_exception.h:19
int64_t int64
Represents a 64-bit signed integer.
Definition int64.h:23
std::vector< xtd::collections::generic::key_value_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.h:22
@ binary_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_binary_specifier styles are u...
@ octal_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_octal_specifier styles are us...
@ number
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_trailing_sign...
@ hex_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_hex_specifier styles are used...
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10