utils: add Result / Option types

Add some Rust-style Result/Option types, based on TartanLlama's
expected, optional classes.

There's std::optional of course, but we can't depend on C++17 yet.
This commit is contained in:
Dirk-Jan C. Binnema
2020-11-09 09:20:54 +02:00
parent 13118ae173
commit 702a1d8452
6 changed files with 4465 additions and 6 deletions

View File

@ -45,17 +45,24 @@ AM_LDFLAGS= \
noinst_LTLIBRARIES= \ noinst_LTLIBRARIES= \
libmu-utils.la libmu-utils.la
third_party= \
optional.hpp \
expected.hpp
libmu_utils_la_SOURCES= \ libmu_utils_la_SOURCES= \
mu-async-queue.hh \ mu-async-queue.hh \
mu-command-parser.cc \
mu-command-parser.hh \
mu-date.c \ mu-date.c \
mu-date.h \ mu-date.h \
mu-error.hh \ mu-error.hh \
mu-logger.cc \ mu-logger.cc \
mu-logger.hh \ mu-logger.hh \
mu-command-parser.cc \ mu-option.hh \
mu-command-parser.hh \
mu-readline.cc \ mu-readline.cc \
mu-readline.hh \ mu-readline.hh \
mu-result.hh \
mu-sexp.cc \ mu-sexp.cc \
mu-sexp.hh \ mu-sexp.hh \
mu-str.c \ mu-str.c \
@ -63,7 +70,8 @@ libmu_utils_la_SOURCES= \
mu-util.c \ mu-util.c \
mu-util.h \ mu-util.h \
mu-utils.cc \ mu-utils.cc \
mu-utils.hh mu-utils.hh \
${third_party}
libmu_utils_la_LIBADD= \ libmu_utils_la_LIBADD= \
$(GLIB_LIBS) \ $(GLIB_LIBS) \

2326
lib/utils/expected.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2019 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2019-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the ** under the terms of the GNU General Public License as published by the
@ -129,8 +129,6 @@ private:
std::string what_; std::string what_;
}; };
} // namespace Mu } // namespace Mu
#endif /* MU_ERROR_HH__ */ #endif /* MU_ERROR_HH__ */

26
lib/utils/mu-option.hh Normal file
View File

@ -0,0 +1,26 @@
/*
* Created on 2020-11-08 by Dirk-Jan C. Binnema <dbinnema@logitech.com>
*
* Copyright (c) 2020 Logitech, Inc. All Rights Reserved
* This program is a trade secret of LOGITECH, and it is not to be reproduced,
* published, disclosed to others, copied, adapted, distributed or displayed
* without the prior authorization of LOGITECH.
*
* Licensee agrees to attach or embed this notice on all copies of the program,
* including partial copies or modified versions thereof.
*
*/
#ifndef MU_OPTION__
#define MU_OPTION__
#include "optional.hpp"
namespace Mu {
/// Either a value of type T, or nothing.
template <typename T> using Option=tl::optional<T>;
}
#endif /*MU_OPTION__*/

39
lib/utils/mu-result.hh Normal file
View File

@ -0,0 +1,39 @@
/*
** Copyright (C) 2019-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#ifndef MU_RESULT_HH__
#define MU_RESULT_HH__
#include "expected.hpp"
#include "utils/mu-error.hh"
namespace Mu {
/**
* A Result is _either_ some value of type T, _or_ an error.
*
*/
template <typename T> using Result = tl::expected<T, Error>;
} // namespace Mu
#endif /* MU_ERROR_HH__ */

2062
lib/utils/optional.hpp Normal file

File diff suppressed because it is too large Load Diff