Skip to content

Commit

Permalink
Feature/hoarau (#30)
Browse files Browse the repository at this point in the history
* Allow chaining -> with pointer-like value types.

* Remove default types.

* Use last argument to construct the mutex.

* Fix typo in license.

* More readable file names.

* Typo in readme.

* New default locks.

* Revert "Allow chaining -> with pointer-like value types."

This reverts commit 2321c7d.

* Clean-up.

* Better specialization example.

* Home-made index_sequence.

* Fix auto return type.

* readme revamp.

* Fix typos.

---------

Co-authored-by: Louis-Charles Caron <louis-charles.caron@sensefly.com>
  • Loading branch information
LouisCharlesC and Louis-Charles Caron authored Oct 6, 2024
1 parent 4c33c79 commit e1821a5
Show file tree
Hide file tree
Showing 13 changed files with 480 additions and 611 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Louis-Charles Caron
Copyright (c) 2019-2023 Louis-Charles Caron

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
279 changes: 129 additions & 150 deletions README.md

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions include/safe/accessmode.h → include/safe/access_mode.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright(c) 2019-2022 Louis-Charles Caron
// Copyright (c) 2019-2023 Louis-Charles Caron

// This file is part of the safe library.
// This file is part of the safe library (https://github.com/LouisCharlesC/safe).

// (https://github.com/LouisCharlesC/safe). Use of this source code is governed by an MIT-style license that can be
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file or at https://opensource.org/licenses/MIT.

#pragma once
Expand All @@ -20,18 +20,25 @@ enum class AccessMode
ReadWrite
};

// Base template: most LockTypes are not read only.
// Specialize this template if you want to force a LockType to be read only.
// If you specialize this template, consider using the pattern described in the
// test_default_locks.cpp file to avoid ill-formed programs!
template <typename LockType> struct AccessTraits
{
static constexpr bool IsReadOnly = false;
};
// Partial specialization for lock_guard: not read only.
template <typename MutexType> struct AccessTraits<std::lock_guard<MutexType>>
{
static constexpr bool IsReadOnly = false;
};
// Partial specialization for unique_lock: not read only.
template <typename MutexType> struct AccessTraits<std::unique_lock<MutexType>>
{
static constexpr bool IsReadOnly = false;
};
// Partial specialization for shared_lock: read only!
#if __cplusplus >= 201402L
template <typename MutexType> struct AccessTraits<std::shared_lock<MutexType>>
{
Expand Down
33 changes: 33 additions & 0 deletions include/safe/default_locks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2019-2022 Louis-Charles Caron

// This file is part of the safe library (https://github.com/LouisCharlesC/safe).

// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file or at https://opensource.org/licenses/MIT.

#pragma once

#include <mutex>

namespace safe
{
namespace impl
{
// Base template defining default lock types for all mutex types.
// Specialize this template as shown in the ReadMe and tests to define your own default locks.
// This struct only uses the first template parameter (MutexType), the parameter pack is there
// only to allow one to partially specialize the template using a single template parameter. Doing
// so overrides the default types for any MutexType. See the test_default_locks.cpp file for examples.
template<typename MutexType, typename...>
struct DefaultLocks
{
using ReadOnly = std::lock_guard<MutexType>;
using ReadWrite = std::lock_guard<MutexType>;
};
} // namespace impl

template<typename MutexType>
using DefaultReadOnlyLockType = typename impl::DefaultLocks<MutexType>::ReadOnly;
template<typename MutexType>
using DefaultReadWriteLockType = typename impl::DefaultLocks<MutexType>::ReadWrite;
} // namespace safe
18 changes: 0 additions & 18 deletions include/safe/defaulttypes.h

This file was deleted.

52 changes: 52 additions & 0 deletions include/safe/meta.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023 Louis-Charles Caron

// This file is part of the safe library (https://github.com/LouisCharlesC/safe).

// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file or at https://opensource.org/licenses/MIT.

#pragma once

#include <cstddef>

namespace safe
{
namespace impl
{
// This set of template and specializations is used to extract the type of the last argument of a paramter pack.
template <typename... Ts> struct Last; // Base template, specializations cover all uses.
template <typename First, typename Second, typename... Others> struct Last<First, Second, Others...>
{
using type = typename Last<Second, Others...>::type;
};
template <typename T> struct Last<T>
{
using type = T;
};
template <> struct Last<>
{
using type = void;
};

template<std::size_t... Is>
struct index_sequence {};
template<std::size_t N, std::size_t... Is>
struct index_sequence<N, Is...>
{
using type = typename index_sequence<N-1, N-1, Is...>::type;
};
template<std::size_t... Is>
struct index_sequence<0, Is...>
{
using type = index_sequence<Is...>;
};

template<std::size_t N>
constexpr typename index_sequence<N>::type make_index_sequence()
{
return {};
}
} // namespace impl

template <typename... Ts> using Last = typename impl::Last<Ts...>::type;
} // namespace safe
6 changes: 3 additions & 3 deletions include/safe/mutableref.h → include/safe/mutable_ref.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright(c) 2019-2022 Louis-Charles Caron
// Copyright (c) 2019-2022 Louis-Charles Caron

// This file is part of the safe library.
// This file is part of the safe library (https://github.com/LouisCharlesC/safe).

// (https://github.com/LouisCharlesC/safe). Use of this source code is governed by an MIT-style license that can be
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file or at https://opensource.org/licenses/MIT.

#pragma once
Expand Down
Loading

0 comments on commit e1821a5

Please sign in to comment.