diff --git a/CMakeLists.txt b/CMakeLists.txt index e8ab442..107b6f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,7 @@ set(SOURCE_FILES src/Phone.cpp src/Shape.cpp src/centiseconds.cpp + src/enumTools.cpp src/mouthAnimation.cpp src/phoneExtraction.cpp src/platformTools.cpp diff --git a/src/Phone.cpp b/src/Phone.cpp index a348764..1bf75c1 100644 --- a/src/Phone.cpp +++ b/src/Phone.cpp @@ -2,39 +2,78 @@ #include "Phone.h" using std::string; +using std::vector; +using std::tuple; +using std::make_tuple; -template -boost::bimap -makeBimap(std::initializer_list::value_type> list) { - return boost::bimap(list.begin(), list.end()); +template <> +const string& getEnumTypeName() { + static const string name = "Shape"; + return name; } -boost::bimap phonesByName = makeBimap({ - { "None", Phone::None }, - { "Unknown", Phone::Unknown }, - { "AO", Phone::AO }, { "AA", Phone::AA }, { "IY", Phone::IY }, { "UW", Phone::UW }, - { "EH", Phone::EH }, { "IH", Phone::IH }, { "UH", Phone::UH }, { "AH", Phone::AH }, - { "AE", Phone::AE }, { "EY", Phone::EY }, { "AY", Phone::AY }, { "OW", Phone::OW }, - { "AW", Phone::AW }, { "OY", Phone::OY }, { "ER", Phone::ER }, { "P", Phone::P }, - { "B", Phone::B }, { "T", Phone::T }, { "D", Phone::D }, { "K", Phone::K }, - { "G", Phone::G }, { "CH", Phone::CH }, { "JH", Phone::JH }, { "F", Phone::F }, - { "V", Phone::V }, { "TH", Phone::TH }, { "DH", Phone::DH }, { "S", Phone::S }, - { "Z", Phone::Z }, { "SH", Phone::SH }, { "ZH", Phone::ZH }, { "HH", Phone::HH }, - { "M", Phone::M }, { "N", Phone::N }, { "NG", Phone::NG }, { "L", Phone::L }, - { "R", Phone::R }, { "Y", Phone::Y }, { "W", Phone::W }, -}); +template <> +const vector>& getEnumMembers() { + static const vector> values = { + make_tuple(Phone::None, "None"), + make_tuple(Phone::Unknown, "Unknown"), + make_tuple(Phone::AO, "AO"), + make_tuple(Phone::AA, "AA"), + make_tuple(Phone::IY, "IY"), + make_tuple(Phone::UW, "UW"), + make_tuple(Phone::EH, "EH"), + make_tuple(Phone::IH, "IH"), + make_tuple(Phone::UH, "UH"), + make_tuple(Phone::AH, "AH"), + make_tuple(Phone::AE, "AE"), + make_tuple(Phone::EY, "EY"), + make_tuple(Phone::AY, "AY"), + make_tuple(Phone::OW, "OW"), + make_tuple(Phone::AW, "AW"), + make_tuple(Phone::OY, "OY"), + make_tuple(Phone::ER, "ER"), + make_tuple(Phone::P, "P"), + make_tuple(Phone::B, "B"), + make_tuple(Phone::T, "T"), + make_tuple(Phone::D, "D"), + make_tuple(Phone::K, "K"), + make_tuple(Phone::G, "G"), + make_tuple(Phone::CH, "CH"), + make_tuple(Phone::JH, "JH"), + make_tuple(Phone::F, "F"), + make_tuple(Phone::V, "V"), + make_tuple(Phone::TH, "TH"), + make_tuple(Phone::DH, "DH"), + make_tuple(Phone::S, "S"), + make_tuple(Phone::Z, "Z"), + make_tuple(Phone::SH, "SH"), + make_tuple(Phone::ZH, "ZH"), + make_tuple(Phone::HH, "HH"), + make_tuple(Phone::M, "M"), + make_tuple(Phone::N, "N"), + make_tuple(Phone::NG, "NG"), + make_tuple(Phone::L, "L"), + make_tuple(Phone::R, "R"), + make_tuple(Phone::Y, "Y"), + make_tuple(Phone::W, "W") + }; + return values; +} -Phone stringToPhone(const string& s) { +template<> +Phone parseEnum(const string& s) { if (s == "SIL") return Phone::None; - auto it = phonesByName.left.find(s); - return (it != phonesByName.left.end()) ? it->second : Phone::Unknown; + Phone result; + return tryParseEnum(s, result) ? result : Phone::Unknown; } -string phoneToString(Phone phone) { - auto it = phonesByName.right.find(phone); - return (it != phonesByName.right.end()) ? it->second : phoneToString(Phone::Unknown); +std::ostream& operator<<(std::ostream& stream, Phone value) { + return stream << enumToString(value); } -std::ostream &operator<<(std::ostream &stream, const Phone phone) { - return stream << phoneToString(phone); +std::istream& operator>>(std::istream& stream, Phone& value) { + string name; + stream >> name; + value = parseEnum(name); + return stream; } diff --git a/src/Phone.h b/src/Phone.h index e88b1c7..0e14629 100644 --- a/src/Phone.h +++ b/src/Phone.h @@ -1,5 +1,7 @@ #pragma once +#include "enumTools.h" + // Defines a subset of the Arpabet enum class Phone { None, @@ -69,8 +71,15 @@ enum class Phone { W // [w] as in [w]ay }; -Phone stringToPhone(const std::string& s); +template<> +const std::string& getEnumTypeName(); -std::string phoneToString(Phone phone); +template<> +const std::vector>& getEnumMembers(); -std::ostream& operator <<(std::ostream& stream, const Phone phone); +template<> +Phone parseEnum(const std::string& s); + +std::ostream& operator<<(std::ostream& stream, Phone value); + +std::istream& operator>>(std::istream& stream, Phone& value); diff --git a/src/Shape.cpp b/src/Shape.cpp index 0c6a51f..4870fbe 100644 --- a/src/Shape.cpp +++ b/src/Shape.cpp @@ -1,10 +1,40 @@ #include "Shape.h" -std::string shapeToString(Shape shape) { - char c = 'A' + static_cast(shape); - return std::string(&c, 1); +#include + +using std::string; +using std::vector; +using std::tuple; +using std::make_tuple; + +template <> +const string& getEnumTypeName() { + static const string name = "Shape"; + return name; } -std::ostream &operator<<(std::ostream &stream, const Shape shape) { - return stream << shapeToString(shape); +template <> +const vector>& getEnumMembers() { + static const vector> values = { + make_tuple(Shape::A, "A"), + make_tuple(Shape::B, "B"), + make_tuple(Shape::C, "C"), + make_tuple(Shape::D, "D"), + make_tuple(Shape::E, "E"), + make_tuple(Shape::F, "F"), + make_tuple(Shape::G, "G"), + make_tuple(Shape::H, "H") + }; + return values; +} + +std::ostream& operator<<(std::ostream& stream, Shape value) { + return stream << enumToString(value); +} + +std::istream& operator>>(std::istream& stream, Shape& value) { + string name; + stream >> name; + value = parseEnum(name); + return stream; } diff --git a/src/Shape.h b/src/Shape.h index 9301de3..c9e74aa 100644 --- a/src/Shape.h +++ b/src/Shape.h @@ -1,7 +1,8 @@ #pragma once #include -#include +#include +#include "enumTools.h" // The classic Hanna-Barbera mouth shapes A-F phus the common supplements G-H // For reference, see http://sunewatts.dk/lipsync/lipsync/article_02.php @@ -18,6 +19,12 @@ enum class Shape { H // L }; -std::string shapeToString(Shape shape); +template<> +const std::string& getEnumTypeName(); -std::ostream& operator <<(std::ostream& stream, const Shape shape); +template<> +const std::vector>& getEnumMembers(); + +std::ostream& operator<<(std::ostream& stream, Shape value); + +std::istream& operator>>(std::istream& stream, Shape& value); diff --git a/src/enumTools.cpp b/src/enumTools.cpp new file mode 100644 index 0000000..ddc47be --- /dev/null +++ b/src/enumTools.cpp @@ -0,0 +1 @@ +#include "enumTools.h" \ No newline at end of file diff --git a/src/enumTools.h b/src/enumTools.h new file mode 100644 index 0000000..d51a7e0 --- /dev/null +++ b/src/enumTools.h @@ -0,0 +1,94 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +template +const std::string& getEnumTypeName(); + +template +const std::vector>& getEnumMembers(); + +namespace detail { + + template + std::map createLowerCaseNameToValueMap() { + std::map map; + for (const auto& pair : getEnumMembers()) { + map[boost::algorithm::to_lower_copy(std::get(pair))] = std::get(pair); + } + return map; + } + + template + std::map createValueToNameMap() { + std::map map; + for (const auto& pair : getEnumMembers()) { + map[std::get(pair)] = std::get(pair); + } + return map; + } + + template + std::vector getEnumValues() { + std::vector result; + for (const auto& pair : getEnumMembers()) { + result.push_back(std::get(pair)); + } + return result; + } + +} + +template +bool tryParseEnum(const std::string& s, T& result) { + static const std::map lookup = detail::createLowerCaseNameToValueMap(); + auto it = lookup.find(boost::algorithm::to_lower_copy(s)); + if (it == lookup.end()) return false; + + result = it->second; + return true; +} + +template +T parseEnum(const std::string& s) { + T result; + if (!tryParseEnum(s, result)) { + throw std::invalid_argument(fmt::format("{} is not a valid {} value.", s, getEnumTypeName())); + } + + return result; +} + +template +bool tryEnumToString(T value, std::string& result) { + static const std::map lookup = detail::createValueToNameMap(); + auto it = lookup.find(value); + if (it == lookup.end()) return false; + + result = it->second; + return true; +} + +template +std::string enumToString(T value) { + std::string result; + if (!tryEnumToString(value, result)) { + throw std::invalid_argument(fmt::format( + "{} is not a valid {} value.", + static_cast::type>(value), + getEnumTypeName())); + } + + return result; +} + +template +const std::vector& getEnumValues() { + static const auto result = detail::getEnumValues(); + return result; +} \ No newline at end of file diff --git a/src/logging.cpp b/src/logging.cpp index cf521fa..6451906 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -1,5 +1,4 @@ #include "logging.h" -#include #include #include #include @@ -10,15 +9,40 @@ using std::lock_guard; using boost::log::sinks::text_ostream_backend; using boost::log::record_view; using boost::log::sinks::unlocked_sink; +using std::vector; +using std::tuple; +using std::make_tuple; namespace expr = boost::log::expressions; -string toString(LogLevel level) { - constexpr size_t levelCount = static_cast(LogLevel::EndSentinel); - static const std::array strings = { - "Trace", "Debug", "Info", "Warning", "Error", "Fatal" +template <> +const string& getEnumTypeName() { + static const string name = "LogLevel"; + return name; +} + +template <> +const vector>& getEnumMembers() { + static const vector> values = { + make_tuple(LogLevel::Trace, "Trace"), + make_tuple(LogLevel::Debug, "Debug"), + make_tuple(LogLevel::Info, "Info"), + make_tuple(LogLevel::Warning, "Warning"), + make_tuple(LogLevel::Error, "Error"), + make_tuple(LogLevel::Fatal, "Fatal") }; - return strings.at(static_cast(level)); + return values; +} + +std::ostream& operator<<(std::ostream& stream, LogLevel value) { + return stream << enumToString(value); +} + +std::istream& operator>>(std::istream& stream, LogLevel& value) { + string name; + stream >> name; + value = parseEnum(name); + return stream; } PausableBackendAdapter::PausableBackendAdapter(boost::shared_ptr backend) : diff --git a/src/logging.h b/src/logging.h index cc717c8..0d32aa7 100644 --- a/src/logging.h +++ b/src/logging.h @@ -9,6 +9,7 @@ #include #include #include "centiseconds.h" +#include "enumTools.h" enum class LogLevel { Trace, @@ -20,12 +21,15 @@ enum class LogLevel { EndSentinel }; -std::string toString(LogLevel level); +template<> +const std::string& getEnumTypeName(); -template -std::basic_ostream& operator<< (std::basic_ostream& stream, LogLevel level) { - return stream << toString(level); -} +template<> +const std::vector>& getEnumMembers(); + +std::ostream& operator<<(std::ostream& stream, LogLevel value); + +std::istream& operator>>(std::istream& stream, LogLevel& value); using LoggerType = boost::log::sources::severity_logger_mt; diff --git a/src/mouthAnimation.cpp b/src/mouthAnimation.cpp index 288288a..37ef836 100644 --- a/src/mouthAnimation.cpp +++ b/src/mouthAnimation.cpp @@ -80,7 +80,7 @@ map animate(const map &phones) { for (auto it = shapes.cbegin(); it != shapes.cend(); ++it) { if (next(it) == shapes.cend()) break; - logTimedEvent("shape", it->first, next(it)->first, shapeToString(it->second)); + logTimedEvent("shape", it->first, next(it)->first, enumToString(it->second)); } return shapes; diff --git a/src/phoneExtraction.cpp b/src/phoneExtraction.cpp index 0c1f442..f7b2261 100644 --- a/src/phoneExtraction.cpp +++ b/src/phoneExtraction.cpp @@ -287,7 +287,7 @@ map getPhoneAlignment(const vector& wordIds, uniqu // Add map entries centiseconds start(startFrame); - result[start] = stringToPhone(phoneName); + result[start] = parseEnum(phoneName); centiseconds end(startFrame + duration); result[end] = Phone::None;