Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strong types #1309

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e2a6cd7
started an implementation for strong types
Davknapp Nov 21, 2024
8ca1ae7
Provide access to underlying operators via crtp and pickable interface
Davknapp Nov 21, 2024
102da46
Add documentation and the operator-file
Davknapp Nov 21, 2024
c1d0ebc
More documentation
Davknapp Nov 21, 2024
adf10fd
More documentation
Davknapp Nov 21, 2024
d241eee
Add swapable competence
Davknapp Nov 21, 2024
6129d1a
Make strong types hashable if desired
Davknapp Nov 21, 2024
32f7b0b
add documentation
Davknapp Nov 21, 2024
1e33703
Merge branch 'main' into strong-types
Davknapp Nov 21, 2024
7b0460f
Access to default constructor.
Davknapp Nov 22, 2024
8565b54
Merge remote-tracking branch 'origin/strong-types' into strong-types
Davknapp Nov 22, 2024
ed7442f
More documentation
Davknapp Nov 22, 2024
9214757
Check sizes
Davknapp Nov 22, 2024
2a27f9e
Merge branch 'main' into strong-types
Davknapp Dec 11, 2024
39a357f
Merge branch 'main' into strong-types
Davknapp Dec 16, 2024
f0783a1
Merge branch 'main' into strong-types
Davknapp Dec 17, 2024
eb4af4b
Merge branch 'main' into strong-types
Davknapp Jan 13, 2025
9b33975
Move crtp implementation
Davknapp Jan 14, 2025
240b4f6
Change template parameter name T -> TUnderlying
Davknapp Jan 14, 2025
0029923
Undo git mf
Davknapp Jan 14, 2025
f7f8fe3
Merge branch 'main' into strong-types
Davknapp Jan 14, 2025
a0257b6
More fine grained tests
Davknapp Jan 14, 2025
606a29b
Merge remote-tracking branch 'origin/strong-types' into strong-types
Davknapp Jan 14, 2025
9c72850
Merge branch 'main' into strong-types
Davknapp Jan 24, 2025
dd0af3e
let scheme use t8_crtp_operator
sandro-elsweijer Jan 31, 2025
019c92b
Merge branch 'main' into strong-types
sandro-elsweijer Jan 31, 2025
82d7fc0
remove old crtp implementation
sandro-elsweijer Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ install( DIRECTORY t8_forest DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_M
install( DIRECTORY t8_geometry DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_MATCHING PATTERN "*.hxx" )
install( DIRECTORY t8_schemes DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_MATCHING PATTERN "*.hxx" )
install( DIRECTORY t8_vtk DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_MATCHING PATTERN "*.hxx" )
install( DIRECTORY t8_types DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_MATCHING PATTERN "*.hxx" )

install( TARGETS T8 DESTINATION ${CMAKE_INSTALL_PREFIX}/lib )
install( TARGETS T8 EXPORT ${PROJECT_NAME}-targets )
Expand Down
48 changes: 0 additions & 48 deletions src/t8_schemes/t8_crtp.hxx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define T8_DEFAULT_COMMON_HXX

#include <t8_element.h>
#include <t8_schemes/t8_crtp.hxx>
#include <t8_types/t8_operators.hxx>
#include <sc_functions.h>
#include <sc_containers.h>

Expand Down Expand Up @@ -84,7 +84,7 @@ count_leaves_from_level (const int element_level, const int refinement_level, co
}

template <class TUnderlyingEclassScheme>
class t8_default_scheme_common: public t8_crtp<TUnderlyingEclassScheme> {
class t8_default_scheme_common: public t8_crtp_operator<TUnderlyingEclassScheme, t8_default_scheme_common> {
private:
friend TUnderlyingEclassScheme;
/** Private constructor which can only be used by derived schemes.
Expand Down
239 changes: 239 additions & 0 deletions src/t8_types/t8_operators.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
This file is part of t8code.
t8code is a C library to manage a collection (a forest) of multiple
connected adaptive space-trees of general element classes in parallel.

Copyright (C) 2024 the developers

t8code 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 2 of the License, or
(at your option) any later version.

t8code 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 t8code; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef T8_OPERATORS_HXX
#define T8_OPERATORS_HXX

#include <iostream>
#include <t8_types/t8_type.hxx>

/**
* \file This file provides the CRTP pattern for operators.
* The operators can be used by a \a T8Type to extend the functionality of the type.
*/

/**
* \brief The CRTP pattern for operators.
*
* \tparam TUnderlying
* \tparam crtpType
*/
template <typename TUnderlying, template <typename> class crtpType>
struct t8_crtp_operator
{
inline TUnderlying&
underlying ()
{
return static_cast<TUnderlying&> (*this);
}

inline TUnderlying const&
underlying () const
{
return static_cast<TUnderlying const&> (*this);
}
};

/*
* The following is a list of competences that can be added to a type.
* Each competence provides access to an operator of the underlying type. That way instead of
* typing `my_int.get() + my_other_int.get()` you can type `my_int + my_other_int`.
*/

/**
* \brief A template for addable types. Provides the + operator.
*
* \tparam TUnderlying
*/
template <typename TUnderlying>
struct Addable: t8_crtp_operator<TUnderlying, Addable>
{

TUnderlying
operator+ (TUnderlying const& other)
{
return TUnderlying (this->underlying ().get () + other.get ());
}
};

/**
* \brief A template for subtractable types. Provides the - operator.
*
* \tparam TUnderlying
*/
template <typename TUnderlying>
struct Subtractable: t8_crtp_operator<TUnderlying, Subtractable>
{
TUnderlying
operator- (TUnderlying const& other)
{
return TUnderlying (this->underlying ().get () - other.get ());
}
};

/**
* \brief A template for multipliable types. Provides the * operator.
*
* \tparam TUnderlying
*/
template <typename TUnderlying>
struct Multipliable: t8_crtp_operator<TUnderlying, Multipliable>
{
TUnderlying
operator* (TUnderlying const& other)
{
return TUnderlying (this->underlying ().get () * other.get ());
}
};

/**
* \brief A template for dividable types. Provides the / operator.
*
* \tparam TUnderlying
*/
template <typename TUnderlying>
struct Dividable: t8_crtp_operator<TUnderlying, Dividable>
{
TUnderlying
operator/ (TUnderlying const& other)
{
return TUnderlying (this->underlying ().get () / other.get ());
}
};

/**
* \brief A template for add-assignable types. Provides the += operator.
*
* \tparam TUnderlying
*/
template <typename TUnderlying>
struct AddAssignable: t8_crtp_operator<TUnderlying, AddAssignable>
{
TUnderlying&
operator+= (TUnderlying const& other)
{
this->underlying ().get () += other.get ();
return this->underlying ();
}
};

/**
* \brief A template for incrementable types. Provides the ++ operator.
*
* \tparam TUnderlying
*
* \note The operator is a prefix operator.
*/
template <typename TUnderlying>
struct PrefixIncrementable: t8_crtp_operator<TUnderlying, PrefixIncrementable>
{
TUnderlying&
operator++ ()
{
this->underlying ().get ()++;
return this->underlying ();
}
};

/**
* \brief A template for decrementable types. Provides the -- operator.
*
* \tparam TUnderlying
*
* \note The operator is a prefix operator.
*/
template <typename TUnderlying>
struct PrefixDecrementable: t8_crtp_operator<TUnderlying, PrefixDecrementable>
{
TUnderlying&
operator-- ()
{
this->underlying ().get ()--;
return this->underlying ();
}
};

template <typename TUnderlying>
struct Printable: t8_crtp_operator<TUnderlying, Printable>
{
void
print (std::ostream& os) const
{
os << this->underlying ().get ();
}
};

/**
* \brief A template for swapping types. Used to make a type swappable.
*
* \tparam TUnderlying
*/
template <typename TUnderlying>
struct Swapable: t8_crtp_operator<TUnderlying, Swapable>
{
void
swap (TUnderlying& other)
{
std::swap (this->underlying ().get (), other.get ());
}
};

/**
* \brief A template for equality comparable types. Provides the == operator.
*
* \tparam TUnderlying
*/
template <typename TUnderlying>
struct EqualityComparable: t8_crtp_operator<TUnderlying, EqualityComparable>
{
bool
operator== (TUnderlying const& other) const
{
return this->underlying ().get () == other.get ();
}
};

/**
* \brief A template for << types. Provides the << operator.
*
* \tparam T
*/
template <typename TUnderlying, typename Parameter>
std::ostream&
operator<< (std::ostream& os, T8Type<TUnderlying, Parameter> const& p)
{
p.print (os);
return os;
}

/**
* \brief A template for hashable types. Used to make a type hashable.
*
* \tparam T
*/
template <typename TUnderlying>
struct Hashable
{
static constexpr bool is_hashable = true;
};

#endif /* T8_OPERATORS_HXX */
Loading