2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-07-24 09:11:15 -04:00
|
|
|
// MSVC 2013 doesn't allow "= default" on move constructors, but since we are
|
|
|
|
|
// (currently) only actually using the move constructors in non-MSVC situations
|
|
|
|
|
// (ie, wpilibC++Devices), we can just ignore it in MSVC.
|
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
|
#define DEFAULT_MOVE_CONSTRUCTOR(ClassName) \
|
|
|
|
|
ClassName(ClassName &&) = default
|
|
|
|
|
#else
|
|
|
|
|
#define DEFAULT_MOVE_CONSTRUCTOR(ClassName)
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-04-26 19:19:57 -04:00
|
|
|
#if (__cplusplus < 201103L)
|
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
|
#define nullptr NULL
|
|
|
|
|
#endif
|
|
|
|
|
#define constexpr const
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
|
#define noexcept throw()
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-09-23 23:44:54 -07:00
|
|
|
// [[deprecated(msg)]] is a C++14 feature not supported by MSVC or GCC < 4.9.
|
|
|
|
|
// We provide an equivalent warning implementation for those compilers here.
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
|
#define DEPRECATED(msg) __declspec(deprecated(msg))
|
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 8)
|
|
|
|
|
#define DEPRECATED(msg) [[deprecated(msg)]]
|
|
|
|
|
#else
|
|
|
|
|
#define DEPRECATED(msg) __attribute__((deprecated(msg)))
|
|
|
|
|
#endif
|
|
|
|
|
#elif __cplusplus > 201103L
|
|
|
|
|
#define DEPRECATED(msg) [[deprecated(msg)]]
|
|
|
|
|
#else
|
|
|
|
|
#define DEPRECATED(msg) /*nothing*/
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-22 00:13:28 -07:00
|
|
|
// Provide std::decay_t when using GCC < 4.9
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
#if __GNUC__ == 4 && __GNUC_MINOR__ < 9
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
namespace std {
|
|
|
|
|
template <class T> using decay_t = typename decay<T>::type;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
// A struct to use as a deleter when a std::shared_ptr must wrap a raw pointer
|
|
|
|
|
// that is being deleted by someone else.
|
|
|
|
|
template<class T>
|
2015-04-26 19:19:57 -04:00
|
|
|
struct
|
|
|
|
|
NullDeleter {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
void operator()(T *) const noexcept {};
|
|
|
|
|
};
|
2015-07-24 09:11:15 -04:00
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
// Use this for determining whether the default move constructor has been
|
|
|
|
|
// called on a containing object. This serves the purpose of allowing us to
|
|
|
|
|
// use the default move constructor of an object for moving all the data around
|
|
|
|
|
// while being able to use this to, for instance, chose not to de-allocate
|
|
|
|
|
// a PWM port in a destructor.
|
|
|
|
|
struct HasBeenMoved {
|
|
|
|
|
HasBeenMoved(HasBeenMoved&& other) {
|
|
|
|
|
other.moved = true;
|
|
|
|
|
moved = false;
|
|
|
|
|
}
|
|
|
|
|
HasBeenMoved() = default;
|
|
|
|
|
std::atomic<bool> moved{false};
|
|
|
|
|
operator bool() const { return moved; }
|
|
|
|
|
};
|
2015-09-16 21:41:35 -07:00
|
|
|
|
|
|
|
|
// Define make_unique for C++11-only compilers
|
|
|
|
|
#if __cplusplus == 201103L
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <utility>
|
|
|
|
|
namespace std {
|
|
|
|
|
template <class T>
|
|
|
|
|
struct _Unique_if {
|
|
|
|
|
typedef unique_ptr<T> _Single_object;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
struct _Unique_if<T[]> {
|
|
|
|
|
typedef unique_ptr<T[]> _Unknown_bound;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class T, size_t N>
|
|
|
|
|
struct _Unique_if<T[N]> {
|
|
|
|
|
typedef void _Known_bound;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class T, class... Args>
|
|
|
|
|
typename _Unique_if<T>::_Single_object make_unique(Args &&... args) {
|
|
|
|
|
return unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
typename _Unique_if<T>::_Unknown_bound make_unique(size_t n) {
|
|
|
|
|
typedef typename remove_extent<T>::type U;
|
|
|
|
|
return unique_ptr<T>(new U[n]());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class... Args>
|
|
|
|
|
typename _Unique_if<T>::_Known_bound make_unique(Args &&...) = delete;
|
|
|
|
|
} // namespace std
|
|
|
|
|
#endif
|