Skip to content

Commit

Permalink
files: necessary utils
Browse files Browse the repository at this point in the history
  • Loading branch information
skim0119 committed Jul 13, 2024
1 parent 1bfb5d4 commit d92a09c
Show file tree
Hide file tree
Showing 64 changed files with 6,569 additions and 13 deletions.
42 changes: 42 additions & 0 deletions backend/src/Utilities/AsConst.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
#include <type_traits>

namespace cpp17 {

//****************************************************************************
/*!\brief Marks a type as const
* \ingroup utils
*
* \details
* Forms lvalue reference to const type of `t`
*
* \example
* \snippet Test_AsConst.cpp as_const_eg
*
* \param t Parameter to make as const
* \return const T
*/
template <class T>
constexpr auto as_const(T& t) noexcept -> std::add_const_t<T>& {
return t;
}
//****************************************************************************

//****************************************************************************
/*!\cond ELASTICA_INTERNAL */
/*!\brief Marks a type as const
* \ingroup utils
*
* \details
* const rvalue reference overload is deleted to disallow rvalue arguments
*/
template <class T>
void as_const(const T&&) = delete;
/*! \endcond */
//****************************************************************************

} // namespace cpp17
18 changes: 18 additions & 0 deletions backend/src/Utilities/CRTP.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
#include "Utilities/NamedType/CRTP.hpp"

namespace elastica {

//////////////////////////////////////////////////////////////////////////////
//
// Namespace injection of CRTP helper type
//
//////////////////////////////////////////////////////////////////////////////

using named_type::CRTPHelper;

} // namespace elastica
35 changes: 35 additions & 0 deletions backend/src/Utilities/ConvertCase/ConvertCase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "ConvertCase.hpp"

#include <algorithm>
#include <cctype>

namespace convert_case {

// Convert lowerPascalCase and UpperPascalCase strings to
// lower_with_underscore.
std::string convert(std::string const& input_str, From<PascalCase> /*meta*/,
To<SnakeCase> /*meta*/) {
std::size_t const siz(input_str.size());
if (not siz)
return "";

std::string str(1, std::tolower(static_cast<unsigned char>(input_str[0])));
str.reserve(siz);

// First place underscores between contiguous lower and upper case letters.
// For example, `_LowerPascalCase` becomes `_Lower_Pascal_Case`.
for (auto it = std::cbegin(input_str) + 1; it != std::cend(input_str);
++it) {
if (std::isupper(*it) && *(it - 1) != '_')
str += "_";
str += *it;
}

// Then convert it to lower case.
std::transform(std::cbegin(str), std::cend(str), std::begin(str),
[](unsigned char c) { return std::tolower(c); });

return str;
}

} // namespace convert_case
30 changes: 30 additions & 0 deletions backend/src/Utilities/ConvertCase/ConvertCase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************

#include <string>

namespace convert_case {

struct PascalCase {};
struct SnakeCase {};

template <typename Case_>
struct From {
using Case = Case_;
};

template <typename Case_>
struct To {
using Case = Case_;
};

using FromPascalCase = From<PascalCase>;
using ToSnakeCase = To<SnakeCase>;

std::string convert(std::string const& input_str, From<PascalCase> /*meta*/,
To<SnakeCase> /*meta*/);

} // namespace convert_case
200 changes: 200 additions & 0 deletions backend/src/Utilities/End.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
//==============================================================================
/*!
// \file
// \brief A global functor for end of an iterable
//
// Copyright (C) 2020-2020 Tejaswin Parthasarathy - All Rights Reserved
// Copyright (C) 2020-2020 MattiaLab - All Rights Reserved
//
// Distributed under the MIT License.
// See LICENSE.txt for details.
//
// The key idea was taken from the Range v3 library with the following
// copyrights
**
** Copyright Eric Niebler 2013-present
**
** Use, modification and distribution is subject to 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)
**
** Project home: https://github.com/ericniebler/range-v3
**
*/
//==============================================================================

#pragma once

//******************************************************************************
// Includes
//******************************************************************************

#include <cassert>
#include <utility>

#include "Utilities/ForceInline.hpp"
#include "Utilities/StaticConst.hpp"

// end proxy
namespace elastica {

namespace detail {

//==========================================================================
//
// CLASS DEFINITION
//
//==========================================================================

//**************************************************************************
/*! \cond ELASTICA_INTERNAL */
/*!\brief End type
* \ingroup utils
*
* \details
* Type of the global end functor for any iterable
*/
struct impl_end_fn {
//************************************************************************
/*!\brief Get the end of any iterable, even works for std types
*
* \param rng Any iterable
*/
template <class R>
constexpr auto operator()(R&& rng) const
noexcept(noexcept(end(std::forward<R>(rng))))
-> decltype(end(std::forward<R>(rng))) {
return end(std::forward<R>(rng));
}
//************************************************************************
};
/*! \endcond */
//************************************************************************

} // namespace detail

// elastica::end is a global function object!
namespace {
//**************************************************************************
/*!\brief The end free function
* \ingroup utils
*
* Marks the end of any iterable, including those of STL types
*
* \example
* Works for STL types
* \snippet Test_End.cpp end_example
*
* And also for \elastica types
* \snippet Test_End.cpp end_elastica_example TODO!!
*/
constexpr auto const& end = static_const<detail::impl_end_fn>::value;
//**************************************************************************
} // namespace

} // namespace elastica

// from_end
namespace elastica {

//****************************************************************************
/*!\brief Marks the location of an index from the end
* \ingroup utils
*/
struct from_end {
//**Member variables********************************************************
//! The index from the end
std::size_t i;
//**************************************************************************
};
//****************************************************************************

//============================================================================
//
// EQUALITY OPERATORS
//
//============================================================================

//****************************************************************************
/*!\brief Equality operator for from_end
* \ingroup utils
*
* \param lhs The left-hand side from_end.
* \param rhs The right-hand side from_end.
* \return \a true if the from_ends are same, else \a false
*
* \example
* \snippet Test_End.cpp end_equality_comparison_eg
*/
inline constexpr auto operator==(from_end lhs, from_end rhs) noexcept
-> bool {
return (lhs.i == rhs.i);
}
//****************************************************************************

//****************************************************************************
/*!\brief Inequality operator for from_end
* \ingroup utils
*
* \param lhs The left-hand side from_end.
* \param rhs The right-hand side from_end.
* \return \a true if the from_ends are different, else \a false
*
* \example
* \snippet Test_End.cpp end_inequality_comparison_eg
*/
inline constexpr auto operator!=(from_end lhs, from_end rhs) noexcept
-> bool {
return not(lhs == rhs);
}
//****************************************************************************

} // namespace elastica

//==============================================================================
//
// GLOBAL BINARY OPERATORS
//
//==============================================================================

//******************************************************************************
/*!\brief Subtraction operator for marking indices from the end of an iterable
* \ingroup utils
*
* \details
* This operator helps mark the end of an iterable and is useful for slicing.
*
* \param meta The \var end() global function object
* \param i The index from the last location
* \return from_end marking the index from the last location
*
* \example
* \snippet Test_End.cpp end_indexing_example
*/
ELASTICA_ALWAYS_INLINE constexpr elastica::from_end operator-(
decltype(elastica::end) /* meta */, std::size_t i) noexcept {
return {i};
}
//******************************************************************************

//******************************************************************************
/*!\brief Subtraction operator for marking indices from the end of an iterable
* \ingroup utils
*
* \details
* This operator helps mark the end of an iterable and is useful for slicing.
*
* \param meta The \var end() global function object
* \param i The index from the last location
* \return from_end marking the index from the last location
*
* \example
* \snippet Test_End.cpp end_indexing_example
*/
ELASTICA_ALWAYS_INLINE constexpr elastica::from_end operator-(
elastica::from_end end_marker, std::size_t i) noexcept {
end_marker.i += i;
return end_marker;
}
//******************************************************************************
26 changes: 26 additions & 0 deletions backend/src/Utilities/ForceInline.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

// Source :
// https://raw.githubusercontent.com/sxs-collaboration/spectre/develop/src/Utilities/ForceInline.hpp

/// \file
/// Defines macro to always inline a function.

#pragma once

#if ELASTICA_USE_ALWAYS_INLINE
#if defined(__GNUC__)
/// \ingroup UtilitiesGroup
/// Always inline a function. Only use this if you benchmarked the code.
#define ELASTICA_ALWAYS_INLINE __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
/// \ingroup UtilitiesGroup
/// Always inline a function. Only use this if you benchmarked the code.
#define ELASTICA_ALWAYS_INLINE __forceinline
#endif
#else
/// \ingroup UtilitiesGroup
/// Always inline a function. Only use this if you benchmarked the code.
#define ELASTICA_ALWAYS_INLINE inline
#endif
Loading

0 comments on commit d92a09c

Please sign in to comment.