Skip to content

Commit

Permalink
polish code
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur committed Dec 28, 2020
1 parent 45af9c2 commit c452b86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
22 changes: 8 additions & 14 deletions common/boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Boolean
{
return m_boolean;
}
operator bool&()
{
return m_boolean;
}
protected:
bool m_boolean;
};
Expand All @@ -31,32 +35,22 @@ class AlphaBoolean : public Boolean

static inline std::ostream &operator<<(std::ostream &out, const AlphaBoolean &b)
{
bool value = b;
out << std::boolalpha << value;
return out;
return out << std::boolalpha << (bool)(b);
}

static inline std::istream &operator>>(std::istream &in, AlphaBoolean &b)
{
bool value = false;
in >> std::boolalpha >> value;
b = value;
return in;
return in >> std::boolalpha >> (bool &)(b);
}

static inline std::ostream &operator<<(std::ostream &out, const Boolean &b)
{
bool value = b;
out << value;
return out;
return out << (bool)(b);
}

static inline std::istream &operator>>(std::istream &in, Boolean &b)
{
bool value = false;
in >> value;
b = value;
return in;
return in >> (bool &)(b);
}

}
27 changes: 27 additions & 0 deletions tests/boolean_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@

#include <sstream>

TEST(BOOLEAN, boolean)
{
swss::Boolean b;

b = true;
std::ostringstream ost;
ost << b;
EXPECT_EQ(ost.str(), "1");

b = false;
std::ostringstream osf;
osf << b;
EXPECT_EQ(osf.str(), "0");

b = false;
std::istringstream ist("1");
ist >> b;
EXPECT_TRUE(b);
EXPECT_FALSE(ist.fail());

b = true;
std::istringstream isf("0");
isf >> b;
EXPECT_FALSE(b);
EXPECT_FALSE(isf.fail());
}

TEST(BOOLEAN, alpha_boolean)
{
swss::AlphaBoolean bt(true);
Expand Down

0 comments on commit c452b86

Please sign in to comment.