diff --git a/gsmartcontrol/configure.ac b/gsmartcontrol/configure.ac index 8676548..741b002 100644 --- a/gsmartcontrol/configure.ac +++ b/gsmartcontrol/configure.ac @@ -194,7 +194,7 @@ LIBS="$LIBS $PCRECPP_LIBS" # Ideally, gcc should use the "latest" standard, but it just uses the latest option. # TODO For now, since gtkmm3 uses dynamic exception specifications, we have # to enable 14 instead of 17. -CXXFLAGS="$CXXFLAGS -std=c++14" +CXXFLAGS="$CXXFLAGS -std=c++17" # ------------------------------------------------------------------------------------- diff --git a/gsmartcontrol/src/applib/smartctl_executor.cpp b/gsmartcontrol/src/applib/smartctl_executor.cpp index 1614323..873ba6d 100644 --- a/gsmartcontrol/src/applib/smartctl_executor.cpp +++ b/gsmartcontrol/src/applib/smartctl_executor.cpp @@ -9,7 +9,11 @@ /// \weakgroup applib /// @{ +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. #include // Glib::shell_quote() +#undef throw #include "smartctl_executor.h" diff --git a/gsmartcontrol/src/applib/storage_detector_helpers.h b/gsmartcontrol/src/applib/storage_detector_helpers.h index 28baf4b..b24463b 100644 --- a/gsmartcontrol/src/applib/storage_detector_helpers.h +++ b/gsmartcontrol/src/applib/storage_detector_helpers.h @@ -14,12 +14,18 @@ #include #include -#include + +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. +#include // Glib::shell_quote() +#undef throw #include "executor_factory.h" #include "storage_device.h" #include "rconfig/rconfig_mini.h" #include "app_pcrecpp.h" +#include "hz/string_num.h" diff --git a/gsmartcontrol/src/gsc_executor_error_dialog.cpp b/gsmartcontrol/src/gsc_executor_error_dialog.cpp index 0d1403f..def60ac 100644 --- a/gsmartcontrol/src/gsc_executor_error_dialog.cpp +++ b/gsmartcontrol/src/gsc_executor_error_dialog.cpp @@ -9,6 +9,12 @@ /// \weakgroup gsc /// @{ +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. +#include // NOT NEEDED +#undef throw + #include #include "gsc_executor_log_window.h" diff --git a/gsmartcontrol/src/gsc_executor_log_window.cpp b/gsmartcontrol/src/gsc_executor_log_window.cpp index fb43b66..98d8d90 100644 --- a/gsmartcontrol/src/gsc_executor_log_window.cpp +++ b/gsmartcontrol/src/gsc_executor_log_window.cpp @@ -9,6 +9,12 @@ /// \weakgroup gsc /// @{ +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. +#include // NOT NEEDED +#undef throw + #include #include // std::size_t #include diff --git a/gsmartcontrol/src/gsc_info_window.cpp b/gsmartcontrol/src/gsc_info_window.cpp index 3d36a99..cff164e 100644 --- a/gsmartcontrol/src/gsc_info_window.cpp +++ b/gsmartcontrol/src/gsc_info_window.cpp @@ -9,6 +9,12 @@ /// \weakgroup gsc /// @{ +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. +#include // NOT NEEDED +#undef throw + #include #include // GDK_KEY_Escape #include // better use vector, it's needed by others too diff --git a/gsmartcontrol/src/gsc_init.cpp b/gsmartcontrol/src/gsc_init.cpp index 57a4e84..b970e34 100644 --- a/gsmartcontrol/src/gsc_init.cpp +++ b/gsmartcontrol/src/gsc_init.cpp @@ -9,6 +9,12 @@ /// \weakgroup gsc /// @{ +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. +#include // NOT NEEDED +#undef throw + #include // #include // _configthreadlocale (win32) #include // std::runtime_error @@ -18,7 +24,6 @@ #include #include #include // g_, G* -#include // set_application_name #ifdef _WIN32 #include diff --git a/gsmartcontrol/src/gsc_main_window.cpp b/gsmartcontrol/src/gsc_main_window.cpp index 7cb19db..ed4640e 100644 --- a/gsmartcontrol/src/gsc_main_window.cpp +++ b/gsmartcontrol/src/gsc_main_window.cpp @@ -9,10 +9,17 @@ /// \weakgroup gsc /// @{ +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. +#include // NOT NEEDED +#undef throw + #include #include #include "hz/string_algo.h" // string_split +#include "hz/string_num.h" #include "hz/fs_file.h" // hz::File #include "hz/debug.h" #include "hz/scoped_ptr.h" diff --git a/gsmartcontrol/src/gsc_preferences_window.cpp b/gsmartcontrol/src/gsc_preferences_window.cpp index 729a6ec..783755f 100644 --- a/gsmartcontrol/src/gsc_preferences_window.cpp +++ b/gsmartcontrol/src/gsc_preferences_window.cpp @@ -9,6 +9,12 @@ /// \weakgroup gsc /// @{ +// TODO Remove this in gtkmm4. +#include // to avoid throw() macro errors. +#define throw(a) // glibmm uses dynamic exception specifications, remove them. +#include // NOT NEEDED +#undef throw + #include #include #include // GDK_KEY_Escape diff --git a/gsmartcontrol/src/hz/Makefile.am b/gsmartcontrol/src/hz/Makefile.am index f4fafce..ca983d0 100644 --- a/gsmartcontrol/src/hz/Makefile.am +++ b/gsmartcontrol/src/hz/Makefile.am @@ -1,7 +1,7 @@ AM_CPPFLAGS = $(all_includes) METASOURCES = AUTO -noinst_HEADERS = any_convert.h any_type.h any_type_holder.h ascii.h \ +noinst_HEADERS = ascii.h \ bad_cast_exception.h bin2ascii_encoder.h common_types.h cstdint.h cstdint_impl_msvc.h \ data_file.h debug.h env_tools.h errno_string.h error.h error_holder.h \ format_unit.h fs_common.h fs_dir.h fs_dir_platform.h fs_error_holder.h \ @@ -14,7 +14,6 @@ noinst_HEADERS = any_convert.h any_type.h any_type_holder.h ascii.h \ sync_policy_glib.h sync_policy_glibmm.h sync_policy_pthread.h \ sync_policy_win32.h system_specific.h type_categories.h type_properties.h win32_tools.h -any_type_test_SOURCES = any_type_test.cpp bin2ascii_encoder_test_SOURCES = bin2ascii_encoder_test.cpp errno_string_test_SOURCES = errno_string_test.cpp format_unit_test_SOURCES = format_unit_test.cpp @@ -31,7 +30,7 @@ sync_multilock_test_SOURCES = sync_multilock_test.cpp # we don't list them in a separate variable because otherwise kdevelop won't see them. noinst_PROGRAMS = if ENABLE_TESTS -noinst_PROGRAMS += any_type_test bin2ascii_encoder_test errno_string_test \ +noinst_PROGRAMS += bin2ascii_encoder_test errno_string_test \ format_unit_test fs_dir_test fs_file_test fs_path_test string_algo_test string_num_test \ string_wcmatch_test sync_test sync_multilock_test endif diff --git a/gsmartcontrol/src/hz/any_convert.h b/gsmartcontrol/src/hz/any_convert.h deleted file mode 100644 index c203e48..0000000 --- a/gsmartcontrol/src/hz/any_convert.h +++ /dev/null @@ -1,190 +0,0 @@ -/************************************************************************** - Copyright: - (C) 2008 - 2012 Alexander Shaduri - License: See LICENSE_zlib.txt file -***************************************************************************/ -/// \file -/// \author Alexander Shaduri -/// \ingroup hz -/// \weakgroup hz -/// @{ - -#ifndef HZ_ANY_CONVERT_H -#define HZ_ANY_CONVERT_H - -#include "hz_config.h" // feature macros - -#include - -#include "string_num.h" // string_is_numeric(), number_to_string() -#include "type_categories.h" // type_check_category - - -/// \file -/// Loose conversions: -/// Try to convert any type to any other type as long as specialization -/// for any_convert is defined. -/// String conversions to numbers are _not_ strictly checked. -/// Numeric types are converted by static_cast between each other. - - - - -namespace hz { - - - - -/// This can be used for inheritance to provide a const static \c value member -/// that is either true or false. -template -struct AnyConvertibleValue { - static const bool value = v; -}; - - - -/// Convert \c from to \c to using loose conversion methods. -template -inline bool any_convert(From from, To& to) -{ - return false; -} - - -/// Convert \c from to \c to using loose conversion methods. -/// This is a same-type specialization. -template -inline bool any_convert(From from, From& to) -{ - to = from; - return true; -} - - - -/// The structure that says whether type \c From is convertible to type \c To -/// using loose conversion methods. -/// This is the master struct, defaults to non-convertible. -template -struct any_convertible : public AnyConvertibleValue { }; - - -/// The structure that says whether type \c From is convertible to type \c To -/// using loose conversion methods. -// Same-type specialization -template -struct any_convertible : public AnyConvertibleValue { }; - - -// specific type specs - see below. - - -/// Define \c any_convertible specialization for \c from_type and \c to_type, saying that -/// they're convertible. Also print \c any_convert function header. -#define DEFINE_ANY_CONVERT_SPEC(from_type, to_type) \ - template<> \ - struct any_convertible : public AnyConvertibleValue { }; \ - \ - template<> \ - inline bool any_convert(from_type from, to_type& to) \ - - - -/// Define \c any_convertible and \c any_convert specializations using static_cast. -#define DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, to_type) \ - DEFINE_ANY_CONVERT_SPEC(from_type, to_type) \ - { \ - to = static_cast(from); \ - return true; \ - } - -/// Define \c any_convertible and \c any_convert specializations using static_cast using bool. -#define DEFINE_ANY_CONVERT_SPEC_STATIC_TOBOOL(from_type) \ - DEFINE_ANY_CONVERT_SPEC(from_type, bool) \ - { \ - to = static_cast(static_cast(from)); /* this disables gcc warning about unsafe != */ \ - return true; \ - } - -/// Define \c any_convertible and \c any_convert specializations using hz::number_to_string(). -#define DEFINE_ANY_CONVERT_SPEC_NUMTOSTRING(from_type) \ - DEFINE_ANY_CONVERT_SPEC(from_type, std::string) \ - { \ - to = hz::number_to_string(from); \ - return true; \ - } - -/// Define \c any_convertible and \c any_convert specializations using hz::string_is_numeric(). -#define DEFINE_ANY_CONVERT_SPEC_STRINGTONUM(to_type) \ - DEFINE_ANY_CONVERT_SPEC(std::string, to_type) \ - { \ - return hz::string_is_numeric(from, to, false); \ - } \ - DEFINE_ANY_CONVERT_SPEC(const std::string&, to_type) \ - { \ - return hz::string_is_numeric(from, to, false); \ - } - - -/// Define \c any_convertible and \c any_convert specializations for all built-in types as \c To. -#define DEFINE_ANY_CONVERT_SPEC_ALL(from_type) \ - DEFINE_ANY_CONVERT_SPEC_STATIC_TOBOOL(from_type) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, char) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, signed char) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, unsigned char) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, wchar_t) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, short int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, unsigned short int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, unsigned int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, long int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, unsigned long int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, long long int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, unsigned long long int) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, double) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, float) \ - DEFINE_ANY_CONVERT_SPEC_STATIC(from_type, long double) \ - DEFINE_ANY_CONVERT_SPEC_NUMTOSTRING(from_type) \ - DEFINE_ANY_CONVERT_SPEC_STRINGTONUM(from_type) - - - -DEFINE_ANY_CONVERT_SPEC_ALL(bool) - -DEFINE_ANY_CONVERT_SPEC_ALL(char) -DEFINE_ANY_CONVERT_SPEC_ALL(signed char) -DEFINE_ANY_CONVERT_SPEC_ALL(unsigned char) -DEFINE_ANY_CONVERT_SPEC_ALL(wchar_t) - -DEFINE_ANY_CONVERT_SPEC_ALL(short int) -DEFINE_ANY_CONVERT_SPEC_ALL(unsigned short int) -DEFINE_ANY_CONVERT_SPEC_ALL(int) -DEFINE_ANY_CONVERT_SPEC_ALL(unsigned int) -DEFINE_ANY_CONVERT_SPEC_ALL(long int) -DEFINE_ANY_CONVERT_SPEC_ALL(unsigned long int) -DEFINE_ANY_CONVERT_SPEC_ALL(long long int) -DEFINE_ANY_CONVERT_SPEC_ALL(unsigned long long int) - -DEFINE_ANY_CONVERT_SPEC_ALL(double) -DEFINE_ANY_CONVERT_SPEC_ALL(float) -DEFINE_ANY_CONVERT_SPEC_ALL(long double) - - - -#undef DEFINE_ANY_CONVERT_SPEC -#undef DEFINE_ANY_CONVERT_SPEC_STATIC -#undef DEFINE_ANY_CONVERT_SPEC_NUMTOSTRING -#undef DEFINE_ANY_CONVERT_SPEC_STRINGTONUM -#undef DEFINE_ANY_CONVERT_SPEC_ALL - - - - -} // ns - - - -#endif - -/// @} diff --git a/gsmartcontrol/src/hz/any_type.h b/gsmartcontrol/src/hz/any_type.h deleted file mode 100644 index 3dbf8bb..0000000 --- a/gsmartcontrol/src/hz/any_type.h +++ /dev/null @@ -1,424 +0,0 @@ -/************************************************************************** - Copyright: - (C) 2000 - 2010 Kevlin Henney - (C) 2008 - 2012 Alexander Shaduri - License: See LICENSE_boost_1_0.txt file -***************************************************************************/ -/// \file -/// \author Kevlin Henney -/// \author Alexander Shaduri -/// \ingroup hz -/// \weakgroup hz -/// @{ - -#ifndef HZ_ANY_TYPE_H -#define HZ_ANY_TYPE_H - -#include "hz_config.h" // feature macros - -/** -\file -any_type library (any type one-element container), based on boost::any. - -Original notes and copyright info follow: - -// what: variant type boost::any -// who: contributed by Kevlin Henney, -// with features contributed and bugs found by -// Ed Brey, Mark Rodgers, Peter Dimov, and James Curran -// when: July 2001 -// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95 - -// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved. -// -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - - -// Compilation options: -// - Define DISABLE_ANY_CONVERT=1 to disable all .convert functions -// (avoids dependency on any_convert.h) -*/ - - -#include // std::type_info -#include // std::ostream -#include // to enable std::string printing. is included already from any_type_holder.h. - -#include "type_properties.h" -#include "bad_cast_exception.h" -#include "any_type_holder.h" - - - - -namespace hz { - - - -/// An object of this type can contain any variable of any type in it. -class any_type { - public: - - /// Constructor - any_type() - : content(0) - { } - - /// Construct from a value - template - any_type(const ValueType& value) - : content(new internal::AnyHolder(value)) - { } - - /// Copy constructor - any_type(const any_type& other) - : content(other.content ? other.content->clone() : 0) - { } - - /// Destructor - ~any_type() - { - delete content; - } - - /// Swap *this with rhs - inline any_type& swap(any_type& rhs); - - - /// Check whether the wrapped variable is set - bool empty() const - { - return !content; - } - - /// Remove the wrapped variable - void clear() - { - delete content; - content = 0; - } - - - /// Get type info of the wrapped variable - const std::type_info& type() const - { - return (content ? content->type() : typeid(void)); - } - - - /// Check whether the wrapped variable is of type T. - template inline - bool is_type() const; // e.g. is_type - - - - /// Get the value into a variable of _exactly_ the same type. - /// \return false if casting fails - template inline - bool get(T& put_it_here) const; - - - /// Get the value using _exactly_ the same type. - /// \throw bad_any_cast if casting fails - template inline - T get() const; // this may throw if cast fails! - - -#if !(defined DISABLE_ANY_CONVERT && DISABLE_ANY_CONVERT) - /// Convert the value into a castable type using any_convert(). - template inline - bool convert(T& val) const; - - /// Convert the value into a castable type using any_convert(). - /// \throw bad_any_cast if casting fails - template inline - T convert() const; -#endif - - - - /// Assignment operator from specific type - template - any_type& operator= (const ValueType& value) - { - delete content; - content = new internal::AnyHolder(value); - return *this; - } - - - /// Assignment operator from another any_type - any_type& operator= (const any_type& other) - { - delete content; - content = other.content->clone(); - return *this; - } - - - - /// A helper class for easy operator<<() usage. - struct StreamHelper { - StreamHelper(internal::AnyHolderBase* c) : content(c) { } - internal::AnyHolderBase* content; - }; - - - /// A helper function to send any_type to a stream. - /// Example: std::cerr << any_type_object.to_stream() - StreamHelper to_stream() const - { - return StreamHelper(content); - } - - - - // helper classes for enabling <<. - - /// A helper struct for operator<<() - template - struct is_default_printable - : public type_integral_constant::value - || type_is_floating_point::value || type_is_pointer::value)> { }; - - - /// A helper struct for operator<<() - template - struct printable { - static const bool value = b; - }; - - /// A helper struct for operator<<(). - // You may specialize this to , with "public printable" to enable default printing - template - struct set_printable : public printable { }; - - /// A helper struct for operator<<() - template - struct is_printable { - static const bool value = (is_default_printable::value || set_printable::value); - }; - - - - internal::AnyHolderBase* content; ///< Pointer to internal::AnyHolder - -}; - - - -/// Operator<<() for any_type::StreamHelper. See any_type::to_stream(). -inline std::ostream& operator<< (std::ostream& os, const any_type::StreamHelper& sh) -{ - if (sh.content) - sh.content->to_stream(os); - return os; -} - - - - -/// This is thrown in case of bad reference casts -DEFINE_BAD_CAST_EXCEPTION(bad_any_cast, - "Data type mismatch for AnyType. Original type: \"%s\", requested type: \"%s\".", - "Data type mismatch for AnyType."); - - - - -// --------------------------------------------------------- -// any_cast operators - - - -/// Cast a "pointer to any_type" to a "pointer to value_type" -template -ValueType any_cast(any_type* operand) -{ - typedef typename type_remove_pointer::type nopointer; - - if (operand && operand->content - && operand->type() == typeid(nopointer) - ) { - return &static_cast*>(operand->content)->value; - } - - return 0; -} - - -/// Cast a "pointer to any_type" to a "pointer to value_type" -template -const ValueType any_cast(const any_type* operand) -{ - return any_cast(const_cast(operand)); -} - - - - -/// Cast an any_type object to value_type -template -ValueType any_cast(any_type& operand) -{ - typedef typename type_remove_reference::type nonref; - - if (!operand.content) - throw bad_any_cast(operand.type(), typeid(nonref)); - - if (operand.type() != typeid(nonref)) - throw bad_any_cast(operand.type(), typeid(nonref)); - - return static_cast*>(operand.content)->value; -} - - -/// Cast an any_type object to value_type -template -ValueType any_cast(const any_type& operand) -{ - typedef typename type_remove_reference::type nonref; - - if (!operand.content) - throw bad_any_cast(operand.type(), typeid(nonref)); - - if (operand.type() != typeid(nonref)) - throw bad_any_cast(operand.type(), typeid(nonref)); - - return static_cast*>(operand.content)->value; -} - - - - -// --------------------------------------------------------- - - -inline any_type& any_type::swap(any_type& rhs) -{ - // don't use std::swap(), we don't need just for one function. - internal::AnyHolderBase* tmp = rhs.content; - rhs.content = content; - content = tmp; - return *this; -} - - -template inline -bool any_type::is_type() const -{ - // without rtti, any_cast will always return true. - return static_cast(any_cast(this)); -} - - -template inline -bool any_type::get(T& put_it_here) const -{ - T* a = any_cast(this); - if (!a) - return false; - put_it_here = *a; - return true; -} - - -template inline -T any_type::get() const -{ - return any_cast(*this); -} - - - -#if !(defined DISABLE_ANY_CONVERT && DISABLE_ANY_CONVERT) - -template inline -bool any_type::convert(T& val) const -{ - return (content ? content->convert(val) : false); -} - - -template inline -T any_type::convert() const -{ - typedef typename type_remove_reference::type nonref; - nonref result; - if (!this->convert(result)) - throw bad_any_cast(this->type(), typeid(nonref)); - - return result; -} - -#endif - - - -// --------------------------------------------------------- - - -namespace internal { - - /// You may specialize this for to enable custom printing. - template ::value > - struct AnyPrinter { - static void to_stream(std::ostream& os, const ValueType& value) - { - os << "[non-representable]"; - } - }; - - /// Specialization - template - struct AnyPrinter { - static void to_stream(std::ostream& os, const ValueType& value) - { - os << value; - } - }; - - - template inline - void AnyHolder::to_stream(std::ostream& os) const - { - AnyPrinter::to_stream(os, value); - } - - -} - - - - -// Do NOT define operator<< for any_type ! -// Since any C++ type is implicitly convertable to any_type, and it will -// serve all unrelated types and may even end up in an endless loop. - - - -/// Use this to add supported types to any_type::to_stream(). -/// Any type may be added if it supports operator <<. -#define ANY_TYPE_SET_PRINTABLE(type, truefalse) \ - namespace hz { \ - template<> struct any_type::set_printable : public any_type::printable { }; \ - } - - - -} // ns - - - -/// Make std::string printable using any_type::to_stream(). -ANY_TYPE_SET_PRINTABLE(std::string, true) - - - - - -#endif - -/// @} diff --git a/gsmartcontrol/src/hz/any_type_holder.h b/gsmartcontrol/src/hz/any_type_holder.h deleted file mode 100644 index f792fdc..0000000 --- a/gsmartcontrol/src/hz/any_type_holder.h +++ /dev/null @@ -1,169 +0,0 @@ -/************************************************************************** - Copyright: - (C) 2000 - 2010 Kevlin Henney - (C) 2008 - 2012 Alexander Shaduri - License: See LICENSE_boost_1_0.txt file -***************************************************************************/ -/// \file -/// \author Kevlin Henney -/// \author Alexander Shaduri -/// \ingroup hz -/// \weakgroup hz -/// @{ - -#ifndef HZ_ANY_TYPE_HOLDER_H -#define HZ_ANY_TYPE_HOLDER_H - -#include "hz_config.h" // feature macros - -/** -\file -Internal header, do not include manually. -*/ - -#include // std::type_info -#include // std::ostream -#include - -#if !(defined DISABLE_ANY_CONVERT && DISABLE_ANY_CONVERT) - #include "any_convert.h" // any_convert -#endif - - -namespace hz { - - - -namespace internal { - - - - -/// Base class for content of hz::any_type -struct AnyHolderBase { - - /// Virtual destructor - virtual ~AnyHolderBase() { } - - /// Get std::type_info for the wrapped variable - virtual const std::type_info& type() const = 0; - - /// Clone the wrapped variable - virtual AnyHolderBase* clone() const = 0; - - /// Send the wrapped variable to std::ostream - virtual void to_stream(std::ostream& os) const = 0; - - -#if !(defined DISABLE_ANY_CONVERT && DISABLE_ANY_CONVERT) - - /// Convert the wrapped variable to argument type - virtual bool convert(bool& val) const = 0; - - virtual bool convert(char& val) const = 0; - virtual bool convert(signed char& val) const = 0; - virtual bool convert(unsigned char& val) const = 0; - virtual bool convert(wchar_t& val) const = 0; - - virtual bool convert(short int& val) const = 0; - virtual bool convert(unsigned short int& val) const = 0; - virtual bool convert(int& val) const = 0; - virtual bool convert(unsigned int& val) const = 0; - virtual bool convert(long int& val) const = 0; - virtual bool convert(unsigned long int& val) const = 0; - virtual bool convert(long long int& val) const = 0; - virtual bool convert(unsigned long long int& val) const = 0; - - virtual bool convert(double& val) const = 0; - virtual bool convert(float& val) const = 0; - virtual bool convert(long double& val) const = 0; - - virtual bool convert(std::string& val) const = 0; - -#endif // DISABLE_ANY_CONVERT - -}; - - - - - -/// Child class for content, holds the actual value -template -struct AnyHolder : public AnyHolderBase { - - /// Type of the wrapped variable - typedef ValueType value_type; - - /// Constructor - AnyHolder(const ValueType& val) - : value(val) - { } - - - /// Get std::type_info object for the wrapped variable - const std::type_info& type() const - { - return typeid(ValueType); - } - - /// Clone the wrapped variable. Note that this is not a deep clone. - AnyHolderBase* clone() const - { - return new AnyHolder(value); - } - - /// Send the wrapped variable to std::ostream - inline void to_stream(std::ostream& os) const; -// { -// AnyPrinter::to_stream(os, value); -// } - - - -#if !(defined DISABLE_ANY_CONVERT && DISABLE_ANY_CONVERT) - - // Reimplemented from AnyHolderBase - bool convert(bool& val) const { return any_convert(value, val); } - - bool convert(char& val) const { return any_convert(value, val); } - bool convert(signed char& val) const { return any_convert(value, val); } - bool convert(unsigned char& val) const { return any_convert(value, val); } - bool convert(wchar_t& val) const { return any_convert(value, val); } - - bool convert(short int& val) const { return any_convert(value, val); } - bool convert(unsigned short int& val) const { return any_convert(value, val); } - bool convert(int& val) const { return any_convert(value, val); } - bool convert(unsigned int& val) const { return any_convert(value, val); } - bool convert(long int& val) const { return any_convert(value, val); } - bool convert(unsigned long int& val) const { return any_convert(value, val); } - bool convert(long long int& val) const { return any_convert(value, val); } - bool convert(unsigned long long int& val) const { return any_convert(value, val); } - - bool convert(double& val) const { return any_convert(value, val); } - bool convert(float& val) const { return any_convert(value, val); } - bool convert(long double& val) const { return any_convert(value, val); } - - bool convert(std::string& val) const { return any_convert(value, val); } - -#endif // DISABLE_ANY_CONVERT - - - ValueType value; ///< The wrapped data -}; - - - - - - - - -} // ns -} // ns - - - -#endif - -/// @} diff --git a/gsmartcontrol/src/hz/any_type_test.cpp b/gsmartcontrol/src/hz/any_type_test.cpp deleted file mode 100644 index 2e1dfa7..0000000 --- a/gsmartcontrol/src/hz/any_type_test.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/************************************************************************** - Copyright: - (C) 2008 - 2012 Alexander Shaduri - License: See LICENSE_unlicense.txt -***************************************************************************/ -/// \file -/// \author Alexander Shaduri -/// \ingroup hz_tests -/// \weakgroup hz_tests -/// @{ - -// disable libdebug, we don't link to it -#undef HZ_USE_LIBDEBUG -#define HZ_USE_LIBDEBUG 0 -// enable libdebug emulation through std::cerr -#undef HZ_EMULATE_LIBDEBUG -#define HZ_EMULATE_LIBDEBUG 1 - - -// define this to disable any_convert.h dependency -// #define DISABLE_ANY_CONVERT - -// The first header should be then one we're testing, to avoid missing -// header pitfalls. -#include "any_type.h" - -#include -#include -#include - - -using namespace hz; - - -/// TestClass test structure -struct TestClass { - - typedef std::list any_list; - - - void append_int(any_list& values, int value) - { - any_type to_append = value; - values.push_back(to_append); - } - - - void append_string(any_list& values, const std::string& value) - { - values.push_back(value); - } - - - void append_char_ptr(any_list& values, const char* value) - { - values.push_back(value); - } - - - void append_any(any_list& values, const any_type& value) - { - values.push_back(value); - } - - - void append_nothing(any_list& values) - { - values.push_back(any_type()); - } - - - std::string get_string(any_list& values) - { - any_type a = values.back(); - - any_cast(a); - - std::string b; - a.get(b); - - a.get(); - - if (a.is_type()) - return any_cast(a); - return std::string(); - } - - -}; - - - - -/// enable printing TestClass. -/// This needs operator<< and and ANY_TYPE_SET_PRINTABLE specialization. -inline std::ostream& operator<< (std::ostream& os, TestClass a) -{ - return (os << "TestClass\n"); -} - -ANY_TYPE_SET_PRINTABLE(TestClass, true); - - - - -/// Main function for the test -int main() -{ - - any_type a1; - a1 = 4; - std::cerr << a1.to_stream() << "\n"; - - - TestClass b; - any_type b1 = b; -// std::cerr << b; - std::cerr << b1.to_stream() << "\n"; - - - -#if !(defined DISABLE_ANY_CONVERT && DISABLE_ANY_CONVERT) - - any_type a2 = std::string("5.444"); - double a2val = 0; - std::cerr << "conversion " << (a2.convert(a2val) ? "succeeded" : "failed") << ", "; - std::cerr << "value: " << a2val << "\n"; - - any_type a3 = 6.55; - std::string a3val; - std::cerr << "conversion " << (a3.convert(a3val) ? "succeeded" : "failed") << ", "; - std::cerr << "value: " << a3val << "\n"; - - any_type a4 = std::string("7"); - int a4val = 0; - std::cerr << "conversion " << (a4.convert(a4val) ? "succeeded" : "failed") << ", "; - std::cerr << "value: " << a4val << "\n"; - - any_type a5 = 'a'; - double a5val = 0; - std::cerr << "conversion " << (a5.convert(a5val) ? "succeeded" : "failed") << ", "; - std::cerr << "value: " << a5val << "\n"; - - - char b1val = 0; - std::cerr << "conversion " << (b1.convert(b1val) ? "succeeded" : "failed") << ", "; - std::cerr << "value: " << int(b1val) << "\n"; - -#endif - - - return 0; -} - - - - - - -/// @} diff --git a/gsmartcontrol/src/rconfig/rcmain.h b/gsmartcontrol/src/rconfig/rcmain.h index c644dd7..f8fa132 100644 --- a/gsmartcontrol/src/rconfig/rcmain.h +++ b/gsmartcontrol/src/rconfig/rcmain.h @@ -447,39 +447,6 @@ T get_data(const std::string& path) -/// Similar to get_data(), but with looser conversion - can convert between -/// C++ built-in types and std::string. Uses hz::any_convert<>. -/// \return false if casting failed, or empty or invalid type. -template inline -bool convert_data(const std::string& path, T& put_it_here) // returns false if cast failed -{ - ConfigLockPolicy::ScopedLock locker(RootHolder::mutex); - - node_ptr node = get_node(path); - if (!node) - return false; // no such node - return node->convert_data(put_it_here); -} - - -/// Similar to get_data(), but with looser conversion - can convert between -/// C++ built-in types and std::string. Uses hz::any_convert<>. -/// \throw rmn::no_such_node No such node -/// \throw rmn::empty_data_retrieval Data is empty -/// \throw rmn::type_mismatch Type mismatch -template inline -T convert_data(const std::string& path) -{ - ConfigLockPolicy::ScopedLock locker(RootHolder::mutex); - - node_ptr node = get_node(path); - if (!node) - throw rmn::no_such_node(path); - return node->convert_data(); -} - - - diff --git a/gsmartcontrol/src/rconfig/rconfig_test.cpp b/gsmartcontrol/src/rconfig/rconfig_test.cpp index cecf02c..05f5607 100644 --- a/gsmartcontrol/src/rconfig/rconfig_test.cpp +++ b/gsmartcontrol/src/rconfig/rconfig_test.cpp @@ -66,17 +66,16 @@ int main() // loose typing: - int int_var2 = 0; - rconfig::convert_data("app/int_var2", int_var2); // this will do the type conversion - std::cerr << "app/int_var2: " << int_var2 << "\n"; // this will get out 11, not 10 (default) +// int int_var2 = 0; +// rconfig::convert_data("app/int_var2", int_var2); // this will do the type conversion +// std::cerr << "app/int_var2: " << int_var2 << "\n"; // this will get out 11, not 10 (default) + +// std::cerr << "app/some_string1: " << rconfig::convert_data("app/some_string1") << "\n"; - std::cerr << "app/some_string1: " << rconfig::convert_data("app/some_string1") << "\n"; - - - std::string huh; - rconfig::convert_data("app/huh", huh); // float -> string conversion, should work. - std::cerr << "app/huh: " << huh << "\n"; +// std::string huh; +// rconfig::convert_data("app/huh", huh); // float -> string conversion, should work. +// std::cerr << "app/huh: " << huh << "\n"; std::cerr << "\"app/empty\" exists: " << std::boolalpha << rconfig::data_is_empty("app/empty") << "\n"; diff --git a/gsmartcontrol/src/rmn/resource_data_any.h b/gsmartcontrol/src/rmn/resource_data_any.h index 4599135..0a7a4d3 100644 --- a/gsmartcontrol/src/rmn/resource_data_any.h +++ b/gsmartcontrol/src/rmn/resource_data_any.h @@ -31,8 +31,8 @@ #include #include // std::ostream +#include -#include "hz/any_type.h" #include "resource_data_types.h" #include "resource_exception.h" @@ -56,7 +56,11 @@ struct ResourceDataAnyDumper { /// Dump resource data into \c os void dump(std::ostream& os) const { - os << obj->data_.to_stream(); + const std::type_info& type = obj->data_.type(); + if (type == typeid(char)) { + os << std::any_cast(std::any_cast(obj->data_)); + } + // TODO other types. } T* obj; ///< ResourceDataAny object @@ -98,14 +102,14 @@ class ResourceDataAny { /// Check whether data is empty bool data_is_empty() const { - return data_.empty(); + return !data_.has_value(); } /// Clear the data, making it empty void clear_data() { - data_.clear(); + data_.reset(); } @@ -131,7 +135,7 @@ class ResourceDataAny { template inline bool data_is_type() const { - return data_.template is_type(); + return data_.type() == typeid(T); } @@ -140,7 +144,11 @@ class ResourceDataAny { template inline bool get_data(T& put_it_here) const { - return data_.get(put_it_here); // returns false if empty or invalid type + if (const T* value = std::any_cast(&data_)) { + put_it_here = *value; + return true; + } + return false; } @@ -150,52 +158,19 @@ class ResourceDataAny { template T get_data() const { - if (data_.empty()) + if (!data_.has_value()) throw empty_data_retrieval(); try { - // template is needed for gcc 3.3 - return data_.template get(); // won't work if empty or invalid type + return std::any_cast(data_); } - catch (hz::bad_any_cast& e) { // convert any_type exception to rmn exception. + catch (std::bad_any_cast& e) { // convert std::any exception to rmn exception. throw type_mismatch(data_.type(), typeid(T)); } } - /// Similar to get_data(), but with looser conversion - can convert between - /// C++ built-in types and std::string. Uses hz::any_convert<>. - /// \return false if casting failed, or empty or invalid type. - template - inline bool convert_data(T& put_it_here) const - { - return data_.convert(put_it_here); - } - - - /// Similar to get_data(), but with looser conversion - can convert between - /// C++ built-in types and std::string. Uses hz::any_convert<>. - /// \throw rmn::empty_data_retrieval Data is empty - /// \throw rmn::type_mismatch Type mismatch - template - T convert_data() const - { - if (data_.empty()) - throw empty_data_retrieval(); - - try { - // Note: This throws only if RTTI is enabled. - // template is needed for gcc 3.3 - return data_.template convert(); // won't work if empty or invalid type - } - catch (hz::bad_any_cast& e) { - throw type_convert_error(data_.type(), typeid(T)); - } - } - - - template friend struct ResourceDataAnyDumper; @@ -210,7 +185,7 @@ class ResourceDataAny { private: - hz::any_type data_; ///< The data + std::any data_; ///< The data }; diff --git a/gsmartcontrol/src/rmn/resource_data_one.h b/gsmartcontrol/src/rmn/resource_data_one.h index e740e79..49452e7 100644 --- a/gsmartcontrol/src/rmn/resource_data_one.h +++ b/gsmartcontrol/src/rmn/resource_data_one.h @@ -173,26 +173,6 @@ class ResourceDataOne { } - /// Get data (use static_cast conversion). - template - inline bool convert_data(T& put_it_here) const // returns false if cast failed - { - put_it_here = static_cast(data_); - return true; // static_cast will check it at compile-time. - } - - - /// Get a copy of data (use static_cast conversion). - /// \throw rmn::empty_data_retrieval if data is empty - template - T convert_data() const - { - if (empty_) - throw empty_data_retrieval(); - return static_cast(data_); - } - - template friend struct ResourceDataOneDumper;