0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-26 08:42:34 +01:00

ircd::util: Move more utils into util; minor cleanup.

This commit is contained in:
Jason Volk 2018-01-12 02:52:45 -08:00
parent ebb48d96a9
commit d566e90402
6 changed files with 86 additions and 98 deletions

View file

@ -191,11 +191,9 @@ namespace ircd
#include "vector_view.h" #include "vector_view.h"
#include "array_view.h" #include "array_view.h"
#include "byte_view.h" #include "byte_view.h"
#include "tuple.h"
#include "allocator.h" #include "allocator.h"
#include "buffer.h" #include "buffer.h"
#include "date.h" #include "date.h"
#include "timer.h"
#include "logger.h" #include "logger.h"
#include "nacl.h" #include "nacl.h"
#include "rand.h" #include "rand.h"
@ -203,7 +201,6 @@ namespace ircd
#include "ed25519.h" #include "ed25519.h"
#include "info.h" #include "info.h"
#include "localee.h" #include "localee.h"
#include "life_guard.h"
#include "color.h" #include "color.h"
#include "lex_cast.h" #include "lex_cast.h"
#include "stringops.h" #include "stringops.h"

View file

@ -20,27 +20,10 @@
*/ */
#pragma once #pragma once
#define HAVE_IRCD_LIFE_GUARD_H #define HAVE_IRCD_UTIL_LIFE_GUARD_H
namespace ircd namespace ircd::util
{ {
// Tests if type inherits from std::enable_shared_from_this<>
template<class T>
constexpr typename std::enable_if<is_complete<T>::value, bool>::type
is_shared_from_this()
{
return std::is_base_of<std::enable_shared_from_this<T>, T>();
}
// Unconditional failure for fwd-declared incomplete types, which
// obviously don't inherit from std::enable_shared_from_this<>
template<class T>
constexpr typename std::enable_if<!is_complete<T>::value, bool>::type
is_shared_from_this()
{
return false;
}
// Convenience functions for types shared_from_this // Convenience functions for types shared_from_this
template<class T> std::shared_ptr<const T> shared_from(const T &t); template<class T> std::shared_ptr<const T> shared_from(const T &t);
template<class T> std::shared_ptr<T> shared_from(T &t); template<class T> std::shared_ptr<T> shared_from(T &t);
@ -70,7 +53,7 @@ namespace ircd
/// } /// }
/// ///
template<class T> template<class T>
struct ircd::life_guard struct ircd::util::life_guard
:std::shared_ptr<T> :std::shared_ptr<T>
{ {
// This constructor is used when the templated type inherits from std::enable_shared_from_this<> // This constructor is used when the templated type inherits from std::enable_shared_from_this<>
@ -103,28 +86,28 @@ struct ircd::life_guard
template<class T> template<class T>
std::weak_ptr<T> std::weak_ptr<T>
ircd::weak_from(T &t) ircd::util::weak_from(T &t)
{ {
return shared_from(t); return shared_from(t);
}; };
template<class T> template<class T>
std::weak_ptr<const T> std::weak_ptr<const T>
ircd::weak_from(const T &t) ircd::util::weak_from(const T &t)
{ {
return shared_from(t); return shared_from(t);
}; };
template<class T> template<class T>
std::shared_ptr<T> std::shared_ptr<T>
ircd::shared_from(T &t) ircd::util::shared_from(T &t)
{ {
return dynamic_pointer_cast<T>(t.shared_from_this()); return dynamic_pointer_cast<T>(t.shared_from_this());
}; };
template<class T> template<class T>
std::shared_ptr<const T> std::shared_ptr<const T>
ircd::shared_from(const T &t) ircd::util::shared_from(const T &t)
{ {
return dynamic_pointer_cast<const T>(t.shared_from_this()); return dynamic_pointer_cast<const T>(t.shared_from_this());
}; };

View file

@ -1,24 +1,21 @@
/* // Copyright (C) Matrix Construct Developers, Authors & Contributors
* Copyright (C) 2016 Charybdis Development Team // Copyright (C) 2016-2018 Jason Volk
* Copyright (C) 2016 Jason Volk <jason@zemos.net> //
* // Permission to use, copy, modify, and/or distribute this software for any
* Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above
* purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice is present in all copies.
* copyright notice and this permission notice is present in all copies. //
* // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE.
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#pragma once #pragma once
#define HAVE_IRCD_UTIL_TIMER_H #define HAVE_IRCD_UTIL_TIMER_H

View file

@ -1,37 +1,31 @@
/* // Copyright (C) Matrix Construct Developers, Authors & Contributors
* charybdis: 21st Century IRC++d // Copyright (C) 2016-2018 Jason Volk
* util.h: Miscellaneous utilities //
* // Permission to use, copy, modify, and/or distribute this software for any
* Copyright (C) 2016 Charybdis Development Team // purpose with or without fee is hereby granted, provided that the above
* Copyright (C) 2016 Jason Volk <jason@zemos.net> // copyright notice and this permission notice is present in all copies.
* //
* Permission to use, copy, modify, and/or distribute this software for any // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* purpose with or without fee is hereby granted, provided that the above // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* copyright notice and this permission notice is present in all copies. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* // DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // POSSIBILITY OF SUCH DAMAGE.
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#pragma once #pragma once
#define HAVE_IRCD_TUPLE_H #define HAVE_IRCD_UTIL_TUPLE_H
// //
// Utilities for std::tuple // Utilities for std::tuple
// //
namespace ircd::util namespace ircd {
{ namespace util {
template<class tuple> template<class tuple>
constexpr bool constexpr bool
@ -431,4 +425,5 @@ tuple_offset(const tuple &t)
}; };
} }
} // namespace ircd::util } // namespace util
} // namespace ircd

View file

@ -45,7 +45,6 @@ namespace util {
#define IRCD_USING_OVERLOAD(ALIAS, ORIGIN) \ #define IRCD_USING_OVERLOAD(ALIAS, ORIGIN) \
static constexpr const auto &ALIAS{ORIGIN} static constexpr const auto &ALIAS{ORIGIN}
// //
// Typedef macros // Typedef macros
// //
@ -82,9 +81,8 @@ struct NAME \
#define IRCD_STRONG_T(TYPE) \ #define IRCD_STRONG_T(TYPE) \
IRCD_STRONG_TYPEDEF(TYPE, IRCD_UNIQUE(strong_t)) IRCD_STRONG_TYPEDEF(TYPE, IRCD_UNIQUE(strong_t))
// //
// Debug size of structure at compile time. // Debug sizeof structure at compile time
// //
/// Internal use only /// Internal use only
@ -100,7 +98,6 @@ struct _TEST_SIZEOF_;
#define IRCD_TEST_SIZEOF(name) \ #define IRCD_TEST_SIZEOF(name) \
ircd::util::_TEST_SIZEOF_<sizeof(name)> _test_; ircd::util::_TEST_SIZEOF_<sizeof(name)> _test_;
// //
// Test if type is forward declared or complete // Test if type is forward declared or complete
// //
@ -116,7 +113,6 @@ struct is_complete<T, decltype(void(sizeof(T)))>
:std::true_type :std::true_type
{}; {};
// //
// Test if type is a specialization of a template // Test if type is a specialization of a template
// //
@ -135,6 +131,26 @@ struct is_specialization_of<T<args...>, T>
:std::true_type :std::true_type
{}; {};
//
// Test if type is shared_from_this
//
/// Tests if type inherits from std::enable_shared_from_this<>
template<class T>
constexpr typename std::enable_if<is_complete<T>::value, bool>::type
is_shared_from_this()
{
return std::is_base_of<std::enable_shared_from_this<T>, T>();
}
/// Unconditional failure for fwd-declared incomplete types, which
/// obviously don't inherit from std::enable_shared_from_this<>
template<class T>
constexpr typename std::enable_if<!is_complete<T>::value, bool>::type
is_shared_from_this()
{
return false;
}
// //
// Misc type testing boilerplates // Misc type testing boilerplates
@ -171,7 +187,6 @@ is_integer()
return is_number<T>() && !is_floating<T>(); return is_number<T>() && !is_floating<T>();
} }
// //
// Convenience constexprs for iterators // Convenience constexprs for iterators
// //
@ -197,6 +212,19 @@ is_input_iterator()
return std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<It>::iterator_category>::value; return std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<It>::iterator_category>::value;
} }
/// Convenience loop to test std::is* on a character sequence
template<int (&test)(int) = std::isprint>
ssize_t
ctype(const char *begin,
const char *const &end)
{
size_t i(0);
for(; begin != end; ++begin, ++i)
if(!test(static_cast<unsigned char>(*begin)))
return i;
return -1;
}
/// Zero testing functor (work in progress) /// Zero testing functor (work in progress)
/// ///
@ -244,20 +272,5 @@ struct is_zero
} }
}; };
/// Convenience loop to test std::is* on a character sequence
template<int (&test)(int) = std::isprint>
ssize_t
ctype(const char *begin,
const char *const &end)
{
size_t i(0);
for(; begin != end; ++begin, ++i)
if(!test(static_cast<unsigned char>(*begin)))
return i;
return -1;
}
} // namespace ircd
} // namespace util } // namespace util
} // namespace ircd

View file

@ -49,6 +49,9 @@ namespace ircd
#include "unique_iterator.h" #include "unique_iterator.h"
#include "instance_list.h" #include "instance_list.h"
#include "bswap.h" #include "bswap.h"
#include "tuple.h"
#include "timer.h"
#include "life_guard.h"
// Unsorted section // Unsorted section
namespace ircd { namespace ircd {