Skip to content

Commit

Permalink
Add tag property.
Browse files Browse the repository at this point in the history
  • Loading branch information
pasuka committed Sep 3, 2019
1 parent bf3ca27 commit 97431d1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ if(MSVC)
else()
add_compile_options(-Wall -Wextra -Werror -pedantic $<$<CONFIG:Release>:-O3>)
endif()
if($<CONFIG:Release>)
message(STATUS "Enable Release mode")
endif()

enable_testing()
# include(CTest)
set(CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/contrib ${CMAKE_MODULE_PATH})
Expand Down
21 changes: 21 additions & 0 deletions src/include/cafea/base/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ class ObjectBase {
assert(0 < n && n <= 10);
for (int i = 0; i < n; i++) group_[i] = y[i];
}
//! Set object's tags.
template <class T>
void set_tags(init_list_<T> vals) {
assert(0 < n && n <= 10);
std::transform(vals.begin(), vals.end(), tags_.begin(),
[] (T a)->std::string { return fmt::format("{}", a);});
}
//! Set object's tag by index.
template <class T>
void set_tag_by_index(T val, int indx=0) {
assert(0 <= indx && indx <=9);
tags_[indx] = fmt::format("{}", val);
}
//! Get object's tags.
std::array<std::string, 10> get_tags() const { return tags_;}
//! Get object's tag by index.
std::string get_tag_by_index(int indx=0) {
assert(0 <= indx && indx <= 9);
return tags_[indx];
}
//! Get object's name.
std::string get_name() const { return name_;}
//! Get object's id.
Expand All @@ -106,6 +126,7 @@ class ObjectBase {
protected:
int id_{-1};//!< Object's id.
std::array<int, 10> group_;//!< Object's group array.
std::array<std::string, 10> tags_;//!< Object's tags array.
std::string name_{"Empty"};//!< Object's name.
};
} // namespace cafea
Expand Down
17 changes: 17 additions & 0 deletions test/basic/a01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "cafea/base/base.hpp"

using namespace std::string_literals;
using cafea::ObjectBase;

TEST_CASE("init", "[ObjectBase]") {
Expand Down Expand Up @@ -53,3 +54,19 @@ TEST_CASE("group", "[ObjectBase]") {
for (int i = 0; i < 9; i++) REQUIRE(gp[i] == a[i]);
}
}

TEST_CASE("tags", "[ObjectBase]") {
ObjectBase obj;
obj.set_tags({2, 4, 6, 8, 10});
auto tags = obj.get_tags();
int i{0};
for(auto x: {2, 4, 6, 8, 10}) REQUIRE(std::to_string(x) == tags[i++]);
obj.set_tag_by_index(10, 1);
REQUIRE(std::to_string(10) == obj.get_tag_by_index(1));
obj.set_tags({"apple", "pear", "banana", "peach", "grape"});
auto newTags = obj.get_tags();
i = 0;
for(auto x: {"apple"s, "pear"s, "banana"s, "peach"s, "grape"s}) REQUIRE(x == newTags[i++]);
obj.set_tag_by_index("Pineapple", 3);
REQUIRE("Pineapple"s == obj.get_tag_by_index(3));
}

0 comments on commit 97431d1

Please sign in to comment.