The code snippet is used to record the status of several cameras.
Since the enum class MODULE_ENABLED should not be used by the customers directly, I marked it as private. And I overload several bit operators for the said scoped enumeration.
Since the enum class MODULE_ENABLED is marked as private, the bit overloading operators for this type can't be invoked by the customers.
How to improve the code snippet below? Is there anything I missed?
#include <iostream>
#include <stdint.h>
#include <type_traits>
class ModuleSwitch
{
private:
enum class MODULE_ENABLED : uint8_t
{
MODULE_ENABLED_NONE = 0,
MODULE_ENABLED_FRONT_CAMERA = 1,
MODULE_ENABLED_MIDDLE_CAMERA = 2,
MODULE_ENABLED_BACK_CAMERA = 4,
};
public:
ModuleSwitch();
bool is_front_camera_enabled() const;
bool is_middle_camera_enabled() const;
bool is_back_camera_enabled() const;
void set_front_camera_enb(bool flag);
void set_middle_camera_enb(bool flag);
void set_back_camera_enb(bool flag);
private:
void set_mod_enb(bool flag, const MODULE_ENABLED& a);
friend MODULE_ENABLED operator|(const MODULE_ENABLED& a, const MODULE_ENABLED& b);
friend MODULE_ENABLED& operator|=(MODULE_ENABLED& a, const MODULE_ENABLED& b);
friend MODULE_ENABLED operator&(const MODULE_ENABLED& a, const MODULE_ENABLED& b);
friend MODULE_ENABLED operator&=(MODULE_ENABLED& a, const MODULE_ENABLED& b);
friend MODULE_ENABLED operator^(const MODULE_ENABLED& a, const MODULE_ENABLED& b);
friend MODULE_ENABLED& operator^=(MODULE_ENABLED& a, const MODULE_ENABLED& b);
friend MODULE_ENABLED operator~(const MODULE_ENABLED& a);
private:
MODULE_ENABLED m_mod_status;
};
ModuleSwitch::ModuleSwitch(): m_mod_status(MODULE_ENABLED::MODULE_ENABLED_NONE){};
ModuleSwitch::MODULE_ENABLED operator|(const ModuleSwitch::MODULE_ENABLED& a, const ModuleSwitch::MODULE_ENABLED& b)
{
using MODULE_ENABLED = ModuleSwitch::MODULE_ENABLED;
return static_cast<MODULE_ENABLED>(static_cast<std::underlying_type<MODULE_ENABLED>::type>(a) | static_cast<std::underlying_type<MODULE_ENABLED>::type>(b));
}
ModuleSwitch::MODULE_ENABLED& operator|=(ModuleSwitch::MODULE_ENABLED& a, const ModuleSwitch::MODULE_ENABLED& b)
{
a=(a|b);
return a;
}
ModuleSwitch::MODULE_ENABLED operator&(const ModuleSwitch::MODULE_ENABLED& a, const ModuleSwitch::MODULE_ENABLED& b)
{
using MODULE_ENABLED = ModuleSwitch::MODULE_ENABLED;
return static_cast<MODULE_ENABLED>(static_cast<std::underlying_type<MODULE_ENABLED>::type>(a) & static_cast<std::underlying_type<MODULE_ENABLED>::type>(b));
}
ModuleSwitch::MODULE_ENABLED operator&=(ModuleSwitch::MODULE_ENABLED& a, const ModuleSwitch::MODULE_ENABLED& b)
{
a = a&b;
return a;
}
ModuleSwitch::MODULE_ENABLED operator^(const ModuleSwitch::MODULE_ENABLED& a, const ModuleSwitch::MODULE_ENABLED& b)
{
using MODULE_ENABLED = ModuleSwitch::MODULE_ENABLED;
return static_cast<MODULE_ENABLED>(static_cast<std::underlying_type<MODULE_ENABLED>::type>(a) ^ static_cast<std::underlying_type<MODULE_ENABLED>::type>(b));
}
ModuleSwitch::MODULE_ENABLED& operator^=(ModuleSwitch::MODULE_ENABLED& a, const ModuleSwitch::MODULE_ENABLED& b)
{
a = a ^ b;
return a;
}
ModuleSwitch::MODULE_ENABLED operator~(const ModuleSwitch::MODULE_ENABLED& a)
{
using MODULE_ENABLED = ModuleSwitch::MODULE_ENABLED;
return static_cast<MODULE_ENABLED>(~static_cast<std::underlying_type<MODULE_ENABLED>::type>(a));
}
bool ModuleSwitch::is_front_camera_enabled() const
{
return (m_mod_status & MODULE_ENABLED::MODULE_ENABLED_FRONT_CAMERA) != MODULE_ENABLED::MODULE_ENABLED_NONE;
}
bool ModuleSwitch::is_middle_camera_enabled() const
{
return (m_mod_status & MODULE_ENABLED::MODULE_ENABLED_MIDDLE_CAMERA) != MODULE_ENABLED::MODULE_ENABLED_NONE;
}
bool ModuleSwitch::is_back_camera_enabled() const
{
return (m_mod_status & MODULE_ENABLED::MODULE_ENABLED_BACK_CAMERA) != MODULE_ENABLED::MODULE_ENABLED_NONE;
}
void ModuleSwitch::set_mod_enb(bool flag, const MODULE_ENABLED& mod_enb)
{
if(flag)
{
m_mod_status |= mod_enb;
}
else
{
m_mod_status &= ~mod_enb;
}
}
void ModuleSwitch::set_front_camera_enb(bool flag)
{
set_mod_enb(flag, MODULE_ENABLED::MODULE_ENABLED_FRONT_CAMERA);
}
void ModuleSwitch::set_middle_camera_enb(bool flag)
{
set_mod_enb(flag, MODULE_ENABLED::MODULE_ENABLED_MIDDLE_CAMERA);
}
void ModuleSwitch::set_back_camera_enb(bool flag)
{
set_mod_enb(flag, MODULE_ENABLED::MODULE_ENABLED_BACK_CAMERA);
}
int main() {
ModuleSwitch mod_swt;
auto print_camera_status = [](const ModuleSwitch& mod_swt){
std::cout << "front camera enabled:" << mod_swt.is_front_camera_enabled() << std::endl;
std::cout << "middle camera enabled:" << mod_swt.is_middle_camera_enabled() << std::endl;;
std::cout << "back camera enabled:" << mod_swt.is_back_camera_enabled() << std::endl;;
};
std::cout <<"#1" << std::endl;
mod_swt.set_front_camera_enb(true);
print_camera_status(mod_swt);
std::cout <<"#2" << std::endl;
mod_swt.set_middle_camera_enb(true);
print_camera_status(mod_swt);
std::cout <<"#3" << std::endl;
mod_swt.set_middle_camera_enb(false);
print_camera_status(mod_swt);
return 0;
}