Skip to content

Commit

Permalink
Add retrieval of attribute datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkOates committed Mar 8, 2016
1 parent fa635be commit 124f662
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 2 additions & 0 deletions include/allegro_flare/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class Attributes
static std::vector<std::string> get_known_datatypes();

private:
static std::vector<std::string> denied_custom_types;

int __find_attribute_index(std::string key);
int __find_attribute_index(std::string key, std::string value);
int __find_or_create_attribute_index(std::string key);
Expand Down
19 changes: 13 additions & 6 deletions src/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ std::vector<DatatypeDefinition> DatatypeDefinition::definitions;



std::vector<std::string> Attributes::denied_custom_types = {"int", "float", "bool", "string"};



Attributes::Attributes()
{
Expand Down Expand Up @@ -496,15 +499,17 @@ bool Attributes::pull_value(std::string key)


bool Attributes::create_datatype_definition(
std::string datatype_identifier,
std::string identifier,
bool (*to_val_func)(void *val, std::string str),
std::string (*to_str_func)(void *val))
{
DatatypeDefinition *definition = DatatypeDefinition::find_definition(datatype_identifier);
DatatypeDefinition::definitions.push_back(DatatypeDefinition(datatype_identifier, to_val_func, to_str_func));
std::vector<std::string> &denied_types = Attributes::denied_custom_types;
if (std::find(denied_types.begin(), denied_types.end(), identifier) != denied_types.end()) return false;

DatatypeDefinition *definition = DatatypeDefinition::find_definition(identifier);
if (definition) return false;
DatatypeDefinition::definitions.push_back(DatatypeDefinition(datatype_identifier, to_val_func, to_str_func));

DatatypeDefinition::definitions.push_back(DatatypeDefinition(identifier, to_val_func, to_str_func));
return true;
}

Expand All @@ -521,8 +526,10 @@ bool Attributes::datatype_is_known(std::string datatype)

std::vector<std::string> Attributes::get_known_datatypes()
{
// TODO
std::vector<std::string> result;
std::vector<std::string> result = Attributes::denied_custom_types;
for (unsigned i=0; i<DatatypeDefinition::definitions.size(); i++)
result.push_back(DatatypeDefinition::definitions[i].identifier);
std::sort(result.begin(), result.end());
return result;
}

Expand Down
21 changes: 11 additions & 10 deletions tests/attributes_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,19 +492,20 @@ BOOST_AUTO_TEST_CASE(a_bound_attribute_can_be_unbound)
BOOST_CHECK_EQUAL(attributes.is_bound("val"), false);
}

BOOST_AUTO_TEST_CASE(a_bound_attribute_will_pull_when_getting_from_a_custom_datatype)
BOOST_AUTO_TEST_CASE(user_can_get_the_known_datatypes_in_alphabetical_order)
{
// TODO
}
Attributes attributes;
auto types = attributes.get_known_datatypes();

BOOST_AUTO_TEST_CASE(user_can_get_the_bound_datatype_on_custom_datatypes)
{
// TODO
}
attributes.create_datatype_definition("my_custom_datatype",
my_custom_datatype::to_val_func, my_custom_datatype::to_str_func);

BOOST_AUTO_TEST_CASE(user_can_get_the_known_datatypes)
{
// TODO
BOOST_REQUIRE_EQUAL(types.size(), 5);
BOOST_CHECK_EQUAL(types[0], "bool");
BOOST_CHECK_EQUAL(types[1], "float");
BOOST_CHECK_EQUAL(types[2], "int");
BOOST_CHECK_EQUAL(types[3], "my_custom_datatype");
BOOST_CHECK_EQUAL(types[4], "string");
}


Expand Down

0 comments on commit 124f662

Please sign in to comment.