parse/enum: return a cookie we could use for cleanup on setup

This commit is contained in:
Danny Robson 2019-06-01 10:06:15 +10:00
parent 6a77ed074a
commit 822243f86d
4 changed files with 24 additions and 3 deletions

View File

@ -409,6 +409,7 @@ list (
parallel/queue.hpp parallel/queue.hpp
parallel/stack.cpp parallel/stack.cpp
parallel/stack.hpp parallel/stack.hpp
parse/fwd.hpp
parse/enum.cpp parse/enum.cpp
parse/enum.hpp parse/enum.hpp
parse/time.cpp parse/time.cpp

View File

@ -8,6 +8,8 @@
#pragma once #pragma once
#include "fwd.hpp"
#include "../view.hpp" #include "../view.hpp"
#include "../introspection.hpp" #include "../introspection.hpp"
#include "../log.hpp" #include "../log.hpp"
@ -90,7 +92,7 @@ namespace cruft::parse::enumeration {
template <typename EnumT> template <typename EnumT>
void setup (std::map<std::string_view, EnumT> mapping) cookie setup [[nodiscard]] (std::map<std::string_view, EnumT> mapping)
{ {
auto &cache = detail::cache (); auto &cache = detail::cache ();
@ -100,7 +102,9 @@ namespace cruft::parse::enumeration {
auto [pos, success] = cache.insert ({ index, std::move (lookup) }); auto [pos, success] = cache.insert ({ index, std::move (lookup) });
if (!success) if (!success)
LOG_WARN ("duplicate parse setup for %! was ignored", cruft::type_name<EnumT> ()); LOG_WARN ("duplicate parse setup for %! was ignored", cruft::type_name<EnumT> ());
}
return cookie {};
};
template <typename EnumT> template <typename EnumT>

15
parse/fwd.hpp Normal file
View File

@ -0,0 +1,15 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2019, Danny Robson <danny@nerdcruft.net>
*/
#pragma once
namespace cruft::parse {
namespace enumeration {
struct cookie {};
}
}

View File

@ -7,11 +7,12 @@ enum enumeration_t : u16 { FOO, BAR = 2, QUX = 257 };
int main () int main ()
{ {
cruft::parse::enumeration::setup<enumeration_t> ({ auto const cookie = cruft::parse::enumeration::setup<enumeration_t> ({
{ "FOO", FOO }, { "FOO", FOO },
{ "BAR", BAR }, { "BAR", BAR },
{ "QUX", QUX }, { "QUX", QUX },
}); });
(void)cookie;
cruft::TAP::logger tap; cruft::TAP::logger tap;
tap.expect_eq (FOO, cruft::parse::from_string<enumeration_t> ("FOO"), "enumeration, FOO"); tap.expect_eq (FOO, cruft::parse::from_string<enumeration_t> ("FOO"), "enumeration, FOO");