Skip to content

Commit

Permalink
countProbeCategories()
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko committed Mar 7, 2018
1 parent 7904825 commit a3567b7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
29 changes: 28 additions & 1 deletion legilimens.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,22 @@ class ProbeCategory
}

public:
/**
* Used for accessing and traversing the global linked list of probes. Normally, the user should not use
* these methods; instead, use the accessor functions defined at the namespace scope.
*/
[[nodiscard]] static const ProbeCategory* getListRoot() { return getMutableListRoot(); }

[[nodiscard]] const ProbeCategory* getNextInstance() const { return next_instance_in_list_; }

/**
* Name of the probe category, i.e. name of all probes belonging to this category.
*/
[[nodiscard]] const ProbeName& getName() const { return name_; }

/**
* Type of the variable pointed to by this probe category, i.e. type of variables of all probes
* belonging to this category.
*/
[[nodiscard]] const TypeDescriptor& getTypeDescriptor() const { return type_descriptor_; }

/**
Expand Down Expand Up @@ -762,6 +772,22 @@ inline const ProbeCategory* findProbeCategoryByName(const ProbeName& name)
return nullptr;
}

/**
* Number of probe category objects registered in the application.
* This function traverses the entire linked list at every invocation.
*/
inline std::size_t countProbeCategories()
{
std::size_t out = 0;
const ProbeCategory* item = ProbeCategory::getListRoot();
while (item != nullptr)
{
++out;
item = item->getNextInstance();
}
return out;
}

/**
* This function traverses the list of probe categories and checks that for every existing name there is only
* one matching probe category. In other words, it ensures that there are no similarly named probe categories
Expand All @@ -770,6 +796,7 @@ inline const ProbeCategory* findProbeCategoryByName(const ProbeName& name)
* Normally one may want to use it in debug builds, if it is important to ensure uniqueness of names:
* assert(legilimens::findFirstNonUniqueProbeCategoryName().isEmpty());
* If you don't care about uniqueness, don't use this function.
* Beware that the complexity is quadratic of the number of probe categories! This operation is very slow.
*/
inline ProbeName findFirstNonUniqueProbeCategoryName()
{
Expand Down
36 changes: 24 additions & 12 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ TEST_CASE("ProbeCategoryRegistration")
<< std::endl;
REQUIRE(findFirstNonUniqueProbeCategoryName().isEmpty());

REQUIRE(countProbeCategories() == 4);

REQUIRE (findProbeCategoryByIndex(0));
REQUIRE (findProbeCategoryByIndex(1));
REQUIRE (findProbeCategoryByIndex(2));
Expand All @@ -110,21 +112,31 @@ TEST_CASE("ProbeCategoryRegistration")
REQUIRE_FALSE(findProbeCategoryByName("0123456789012345678901234567890123456789012345678901234567890123456789"
"012345678901234567890123456789012345678901234567890123456789"));

struct PublicMorozov : public ProbeCategory
{
PublicMorozov() : ProbeCategory(TypeDescriptor(), "conflicting") { }
};

REQUIRE(findFirstNonUniqueProbeCategoryName().isEmpty());
PublicMorozov conflicting_a;

{
PublicMorozov conflicting_c; // This is needed to test linked list removal
PublicMorozov conflicting_d;
struct PublicMorozov : public ProbeCategory
{
PublicMorozov() : ProbeCategory(TypeDescriptor(), "conflicting") {}
};

REQUIRE(findFirstNonUniqueProbeCategoryName().isEmpty());
PublicMorozov conflicting_a;

REQUIRE(countProbeCategories() == 5);

{
PublicMorozov conflicting_c; // This is needed to test linked list removal
REQUIRE(countProbeCategories() == 6);
PublicMorozov conflicting_d;
REQUIRE(countProbeCategories() == 7);
}

REQUIRE(countProbeCategories() == 5);
PublicMorozov conflicting_b;
REQUIRE(countProbeCategories() == 6);
REQUIRE(findFirstNonUniqueProbeCategoryName() == "conflicting");
}

PublicMorozov conflicting_b;
REQUIRE(findFirstNonUniqueProbeCategoryName() == "conflicting");
REQUIRE(countProbeCategories() == 4);
}


Expand Down

0 comments on commit a3567b7

Please sign in to comment.