Bug 1320752 - remove mozilla/Function.h; r=gerald

We have std::function available now, which is likely to be somewhat more
efficient.
This commit is contained in:
Nathan Froyd 2016-11-28 11:03:53 -05:00
Родитель afbfd32d9b
Коммит 2ebbd09068
39 изменённых файлов: 69 добавлений и 503 удалений

Просмотреть файл

@ -1,5 +1,4 @@
#include <functional>
#include "mozilla/Function.h"
#define MOZ_STRONG_REF __attribute__((annotate("moz_strong_ref")))
struct RefCountedBase {
@ -220,107 +219,6 @@ void b() {
});
}
void c() {
R* ptr;
SmartPtr<R> sp;
mozilla::function<void(R*)>([&](R* argptr) {
R* localptr;
ptr->method();
argptr->method();
localptr->method();
});
mozilla::function<void(SmartPtr<R>)>([&](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
sp->method();
argsp->method();
localsp->method();
});
mozilla::function<void(R*)>([&](R* argptr) {
R* localptr;
take(ptr);
take(argptr);
take(localptr);
});
mozilla::function<void(SmartPtr<R>)>([&](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
take(sp);
take(argsp);
take(localsp);
});
mozilla::function<void(R*)>([=](R* argptr) {
R* localptr;
ptr->method(); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}}
argptr->method();
localptr->method();
});
mozilla::function<void(SmartPtr<R>)>([=](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
sp->method();
argsp->method();
localsp->method();
});
mozilla::function<void(R*)>([=](R* argptr) {
R* localptr;
take(ptr); // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}}
take(argptr);
take(localptr);
});
mozilla::function<void(SmartPtr<R>)>([=](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
take(sp);
take(argsp);
take(localsp);
});
mozilla::function<void(R*)>([ptr](R* argptr) { // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}}
R* localptr;
ptr->method();
argptr->method();
localptr->method();
});
mozilla::function<void(SmartPtr<R>)>([sp](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
sp->method();
argsp->method();
localsp->method();
});
mozilla::function<void(R*)>([ptr](R* argptr) { // expected-error{{Refcounted variable 'ptr' of type 'R' cannot be captured by a lambda}} expected-note{{Please consider using a smart pointer}}
R* localptr;
take(ptr);
take(argptr);
take(localptr);
});
mozilla::function<void(SmartPtr<R>)>([sp](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
take(sp);
take(argsp);
take(localsp);
});
mozilla::function<void(R*)>([&ptr](R* argptr) {
R* localptr;
ptr->method();
argptr->method();
localptr->method();
});
mozilla::function<void(SmartPtr<R>)>([&sp](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
sp->method();
argsp->method();
localsp->method();
});
mozilla::function<void(R*)>([&ptr](R* argptr) {
R* localptr;
take(ptr);
take(argptr);
take(localptr);
});
mozilla::function<void(SmartPtr<R>)>([&sp](SmartPtr<R> argsp) {
SmartPtr<R> localsp;
take(sp);
take(argsp);
take(localsp);
});
}
// These tests would check c++14 deduced return types, if they were supported in
// our codebase. They are being kept here for convenience in the future if we do
// add support for c++14 deduced return types

Просмотреть файл

@ -613,7 +613,7 @@ AudioChannelService::RefreshAgentsVolumeAndPropagate(AudioChannel aAudioChannel,
void
AudioChannelService::RefreshAgents(nsPIDOMWindowOuter* aWindow,
mozilla::function<void(AudioChannelAgent*)> aFunc)
std::function<void(AudioChannelAgent*)> aFunc)
{
MOZ_ASSERT(aWindow);
MOZ_ASSERT(aWindow->IsOuterWindow());

Просмотреть файл

@ -16,7 +16,8 @@
#include "AudioChannelAgent.h"
#include "nsAttrValue.h"
#include "mozilla/dom/AudioChannelBinding.h"
#include "mozilla/Function.h"
#include <functional>
class nsIRunnable;
class nsPIDOMWindowOuter;
@ -195,7 +196,7 @@ private:
~AudioChannelService();
void RefreshAgents(nsPIDOMWindowOuter* aWindow,
mozilla::function<void(AudioChannelAgent*)> aFunc);
std::function<void(AudioChannelAgent*)> aFunc);
static void CreateServiceIfNeeded();

Просмотреть файл

@ -6,7 +6,6 @@
#include "IdleRequest.h"
#include "mozilla/Function.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/dom/IdleDeadline.h"
#include "mozilla/dom/Performance.h"

Просмотреть файл

@ -9,7 +9,6 @@
#include "nsITimeoutHandler.h"
#include "nsTArray.h"
#include "js/TypeDecls.h"
#include "mozilla/Function.h"
#include "mozilla/Maybe.h"
namespace mozilla {

Просмотреть файл

@ -7,7 +7,6 @@
#include <algorithm>
#include "mozilla/Attributes.h"
#include "mozilla/Function.h"
#include "mozilla/Likely.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/FunctionBinding.h"

Просмотреть файл

@ -9,9 +9,10 @@
#include "ImageContainer.h"
#include "libyuv.h"
#include "mozilla/dom/ImageBitmapBinding.h"
#include "mozilla/Function.h"
#include "mozilla/gfx/2D.h"
#include <functional>
using namespace libyuv;
using namespace mozilla::gfx;
@ -325,7 +326,7 @@ static UniquePtr<ImagePixelLayout>
CvtSimpleImgToSimpleImg(Utils* aSrcUtils, const SrcType* aSrcBuffer,
const ImagePixelLayout* aSrcLayout, DstType* aDstBuffer,
ImageBitmapFormat aDstFormat, int aDstChannelCount,
mozilla::function<int (const SrcType*, int, DstType*, int, int, int)> converter)
std::function<int (const SrcType*, int, DstType*, int, int, int)> converter)
{
MOZ_ASSERT(aSrcUtils, "Convert color from a null utility object.");
MOZ_ASSERT(aSrcBuffer, "Convert color from a null buffer.");
@ -353,7 +354,7 @@ static UniquePtr<ImagePixelLayout>
CvtYUVImgToSimpleImg(Utils* aSrcUtils, const uint8_t* aSrcBuffer,
const ImagePixelLayout* aSrcLayout, uint8_t* aDstBuffer,
ImageBitmapFormat aDstFormat, int aDstChannelCount,
mozilla::function<int (const uint8_t*, int, const uint8_t*, int, const uint8_t*, int, uint8_t*, int, int, int)> converter)
std::function<int (const uint8_t*, int, const uint8_t*, int, const uint8_t*, int, uint8_t*, int, int, int)> converter)
{
MOZ_ASSERT(aSrcUtils, "Convert color from a null utility object.");
MOZ_ASSERT(aSrcBuffer, "Convert color from a null buffer.");
@ -383,7 +384,7 @@ static UniquePtr<ImagePixelLayout>
CvtNVImgToSimpleImg(Utils* aSrcUtils, const uint8_t* aSrcBuffer,
const ImagePixelLayout* aSrcLayout, uint8_t* aDstBuffer,
ImageBitmapFormat aDstFormat, int aDstChannelCount,
mozilla::function<int (const uint8_t*, int, const uint8_t*, int, uint8_t*, int, int, int)> converter)
std::function<int (const uint8_t*, int, const uint8_t*, int, uint8_t*, int, int, int)> converter)
{
MOZ_ASSERT(aSrcUtils, "Convert color from a null utility object.");
MOZ_ASSERT(aSrcBuffer, "Convert color from a null buffer.");
@ -412,7 +413,7 @@ static UniquePtr<ImagePixelLayout>
CvtSimpleImgToYUVImg(Utils* aSrcUtils, const uint8_t* aSrcBuffer,
const ImagePixelLayout* aSrcLayout, uint8_t* aDstBuffer,
ImageBitmapFormat aDstFormat,
mozilla::function<int (const uint8_t*, int, uint8_t*, int, uint8_t*, int, uint8_t*, int, int, int)> converter)
std::function<int (const uint8_t*, int, uint8_t*, int, uint8_t*, int, uint8_t*, int, int, int)> converter)
{
MOZ_ASSERT(aSrcUtils, "Convert color from a null utility object.");
MOZ_ASSERT(aSrcBuffer, "Convert color from a null buffer.");
@ -444,7 +445,7 @@ static UniquePtr<ImagePixelLayout>
CvtSimpleImgToNVImg(Utils* aSrcUtils, const uint8_t* aSrcBuffer,
const ImagePixelLayout* aSrcLayout, uint8_t* aDstBuffer,
ImageBitmapFormat aDstFormat,
mozilla::function<int (const uint8_t*, int, uint8_t*, int, uint8_t*, int, int, int)> converter)
std::function<int (const uint8_t*, int, uint8_t*, int, uint8_t*, int, int, int)> converter)
{
MOZ_ASSERT(aSrcUtils, "Convert color from a null utility object.");
MOZ_ASSERT(aSrcBuffer, "Convert color from a null buffer.");

Просмотреть файл

@ -7,7 +7,6 @@
#include "mozilla/Base64.h"
#include "mozilla/TaskQueue.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Function.h"
#include "MediaContentType.h"
#include "MediaPrefs.h"
@ -26,6 +25,7 @@
#include "nsCharSeparatedTokenizer.h"
#include "nsContentTypeParser.h"
#include <functional>
#include <stdint.h>
namespace mozilla {

Просмотреть файл

@ -52,6 +52,8 @@
#include "mp4_demuxer/H264.h"
#include <functional>
namespace mozilla {
extern already_AddRefed<PlatformDecoderModule> CreateAgnosticDecoderModule();
@ -155,7 +157,7 @@ public:
void Clear() { mCheckerList.Clear(); }
private:
nsTArray<mozilla::function<CheckResult()>> mCheckerList;
nsTArray<std::function<CheckResult()>> mCheckerList;
}; // SupportChecker
PDMFactory::PDMFactory()

Просмотреть файл

@ -8,7 +8,6 @@
#define PDMFactory_h_
#include "PlatformDecoderModule.h"
#include "mozilla/Function.h"
#include "mozilla/StaticMutex.h"
class CDMProxy;

Просмотреть файл

@ -7,7 +7,6 @@
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/HTMLIFrameElementBinding.h"
#include "mozilla/dom/TabParent.h"
#include "mozilla/Function.h"
#include "mozilla/Logging.h"
#include "mozilla/Move.h"
#include "mozilla/Preferences.h"

Просмотреть файл

@ -7,11 +7,12 @@
#ifndef mozilla_gfx_SFNTNameTable_h
#define mozilla_gfx_SFNTNameTable_h
#include "mozilla/Function.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"
#include "u16string.h"
#include <functional>
namespace mozilla {
namespace gfx {
@ -19,7 +20,7 @@ struct NameHeader;
struct NameRecord;
enum ENameDecoder : int;
typedef Vector<function<ENameDecoder(const NameRecord*)>> NameRecordMatchers;
typedef Vector<std::function<ENameDecoder(const NameRecord*)>> NameRecordMatchers;
class SFNTNameTable final
{

Просмотреть файл

@ -6,10 +6,10 @@
#ifndef mozilla_gfx_config_gfxConfig_h
#define mozilla_gfx_config_gfxConfig_h
#include <functional>
#include "gfxFeature.h"
#include "gfxFallback.h"
#include "mozilla/Assertions.h"
#include "mozilla/Function.h"
namespace mozilla {
namespace gfx {
@ -168,13 +168,13 @@ public:
static void EnableFallback(Fallback aFallback, const char* aMessage);
// Run a callback for each initialized FeatureState.
typedef mozilla::function<void(const char* aName,
typedef std::function<void(const char* aName,
const char* aDescription,
FeatureState& aFeature)> FeatureIterCallback;
static void ForEachFeature(const FeatureIterCallback& aCallback);
// Run a callback for each enabled fallback.
typedef mozilla::function<void(const char* aName, const char* aMsg)>
typedef std::function<void(const char* aName, const char* aMsg)>
FallbackIterCallback;
static void ForEachFallback(const FallbackIterCallback& aCallback);

Просмотреть файл

@ -6,10 +6,10 @@
#ifndef mozilla_gfx_config_gfxFeature_h
#define mozilla_gfx_config_gfxFeature_h
#include <functional>
#include <stdint.h>
#include "gfxTelemetry.h"
#include "mozilla/Assertions.h"
#include "mozilla/Function.h"
#include "nsString.h"
namespace mozilla {
@ -64,7 +64,7 @@ class FeatureState
// aType is "base", "user", "env", or "runtime".
// aMessage may be null.
typedef mozilla::function<void(const char* aType,
typedef std::function<void(const char* aType,
FeatureStatus aStatus,
const char* aMessage)> StatusIterCallback;
void ForEachStatusChange(const StatusIterCallback& aCallback) const;

Просмотреть файл

@ -7,7 +7,6 @@
#ifndef _include_mozilla_gfx_ipc_GPUProcessHost_h_
#define _include_mozilla_gfx_ipc_GPUProcessHost_h_
#include "mozilla/Function.h"
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"

Просмотреть файл

@ -9,10 +9,11 @@
#include "FrameMetrics.h"
#include "InputData.h"
#include "mozilla/EventForwards.h"
#include "mozilla/Function.h"
#include "mozilla/layers/APZUtils.h"
#include "nsIDOMWindowUtils.h"
#include <functional>
class nsIContent;
class nsIDocument;
class nsIPresShell;
@ -24,7 +25,7 @@ template<class T> class nsCOMPtr;
namespace mozilla {
namespace layers {
typedef function<void(uint64_t, const nsTArray<TouchBehaviorFlags>&)>
typedef std::function<void(uint64_t, const nsTArray<TouchBehaviorFlags>&)>
SetAllowedTouchBehaviorCallback;
/* This class contains some helper methods that facilitate implementing the

Просмотреть файл

@ -11,13 +11,14 @@
#include "FrameMetrics.h" // for ScrollableLayerGuid
#include "Units.h"
#include "mozilla/EventForwards.h"
#include "mozilla/Function.h"
#include "mozilla/layers/GeckoContentController.h" // for APZStateChange
#include "mozilla/RefPtr.h"
#include "nsCOMPtr.h"
#include "nsISupportsImpl.h" // for NS_INLINE_DECL_REFCOUNTING
#include "nsIWeakReferenceUtils.h" // for nsWeakPtr
#include <functional>
template <class> class nsCOMPtr;
class nsIDocument;
class nsIPresShell;
@ -28,7 +29,7 @@ namespace layers {
class ActiveElementManager;
typedef function<void(const ScrollableLayerGuid&,
typedef std::function<void(const ScrollableLayerGuid&,
uint64_t /* input block id */,
bool /* prevent default */)>
ContentReceivedInputBlockCallback;

Просмотреть файл

@ -11,6 +11,7 @@
#include "mozilla/gfx/GPUChild.h" // for GPUChild
#include "mozilla/gfx/GPUProcessManager.h" // for GPUProcessManager
#include <functional>
#include <utility> // for std::make_pair
namespace mozilla {
@ -70,7 +71,7 @@ LayerTreeOwnerTracker::IsMapped(uint64_t aLayersId, base::ProcessId aProcessId)
}
void
LayerTreeOwnerTracker::Iterate(function<void(uint64_t aLayersId, base::ProcessId aProcessId)> aCallback)
LayerTreeOwnerTracker::Iterate(std::function<void(uint64_t aLayersId, base::ProcessId aProcessId)> aCallback)
{
MutexAutoLock lock(mLayerIdsLock);

Просмотреть файл

@ -9,8 +9,8 @@
#include "base/process.h" // for base::ProcessId
#include "mozilla/Mutex.h" // for mozilla::Mutex
#include "mozilla/Function.h"
#include <functional>
#include <map>
namespace mozilla {
@ -56,7 +56,7 @@ public:
*/
bool IsMapped(uint64_t aLayersId, base::ProcessId aProcessId);
void Iterate(function<void(uint64_t aLayersId, base::ProcessId aProcessId)> aCallback);
void Iterate(std::function<void(uint64_t aLayersId, base::ProcessId aProcessId)> aCallback);
private:
LayerTreeOwnerTracker();

Просмотреть файл

@ -9,7 +9,7 @@
#include "nsIGfxInfo.h"
#include "nsIFile.h"
#include "nsString.h"
#include "mozilla/Function.h"
#include <functional>
#include <string>
namespace mozilla {
@ -83,7 +83,7 @@ public:
Proxy
};
typedef mozilla::function<void(const char* aName, const char* aPrefName)>
typedef std::function<void(const char* aName, const char* aPrefName)>
CrashGuardCallback;
static void ForEachActiveCrashGuard(const CrashGuardCallback& aCallback);

Просмотреть файл

@ -10,7 +10,6 @@
#include <stdint.h>
#include <string>
#include "mozilla/Assertions.h"
#include "mozilla/Function.h"
#include "mozilla/gfx/LoggingConstants.h"
#include "nsTArray.h"

Просмотреть файл

@ -992,7 +992,7 @@ MessageChannel::OnMessageReceivedFromLink(Message&& aMsg)
}
void
MessageChannel::PeekMessages(mozilla::function<bool(const Message& aMsg)> aInvoke)
MessageChannel::PeekMessages(std::function<bool(const Message& aMsg)> aInvoke)
{
// FIXME: We shouldn't be holding the lock for aInvoke!
MonitorAutoLock lock(*mMonitor);

Просмотреть файл

@ -11,7 +11,6 @@
#include "base/basictypes.h"
#include "base/message_loop.h"
#include "mozilla/Function.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Monitor.h"
#include "mozilla/Vector.h"
@ -26,6 +25,7 @@
#include "MessageLink.h"
#include <deque>
#include <functional>
#include <stack>
#include <math.h>
@ -130,7 +130,7 @@ class MessageChannel : HasResultCodes
// Call aInvoke for each pending message until it returns false.
// XXX: You must get permission from an IPC peer to use this function
// since it requires custom deserialization and re-orders events.
void PeekMessages(mozilla::function<bool(const Message& aMsg)> aInvoke);
void PeekMessages(std::function<bool(const Message& aMsg)> aInvoke);
// Misc. behavioral traits consumers can request for this channel
enum ChannelFlags {

Просмотреть файл

@ -10,7 +10,6 @@
#include "MainThreadUtils.h"
#include "mozilla/Attributes.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Function.h"
#include "mozilla/mscom/COMApartmentRegion.h"
#include "mozilla/mscom/Utils.h"
#include "nsCOMPtr.h"

Просмотреть файл

@ -9,10 +9,10 @@
#include "nsGridContainerFrame.h"
#include <algorithm> // for std::stable_sort
#include <functional>
#include <limits>
#include "mozilla/CSSAlignUtils.h"
#include "mozilla/dom/GridBinding.h"
#include "mozilla/Function.h"
#include "mozilla/Maybe.h"
#include "mozilla/PodOperations.h" // for PodZero
#include "mozilla/Poison.h"
@ -1467,7 +1467,7 @@ struct nsGridContainerFrame::Tracks
}
using FitContentClamper =
function<bool(uint32_t aTrack, nscoord aMinSize, nscoord* aSize)>;
std::function<bool(uint32_t aTrack, nscoord aMinSize, nscoord* aSize)>;
/**
* Grow the planned size for tracks in aGrowableTracks up to their limit
* and then freeze them (all aGrowableTracks must be unfrozen on entry).

Просмотреть файл

@ -53,6 +53,7 @@
#include "gfxPrefs.h"
#include <algorithm>
#include <functional>
using namespace mozilla::layers;
using namespace mozilla::gfx;
@ -1268,7 +1269,7 @@ protected:
struct MaskLayerKey;
already_AddRefed<ImageLayer>
CreateOrRecycleMaskImageLayerFor(const MaskLayerKey& aKey,
mozilla::function<void(Layer* aLayer)> aSetUserData);
std::function<void(Layer* aLayer)> aSetUserData);
/**
* Grabs all PaintedLayers and ColorLayers from the ContainerLayer and makes them
* available for recycling.
@ -2216,7 +2217,7 @@ ContainerState::CreateOrRecycleImageLayer(PaintedLayer *aPainted)
already_AddRefed<ImageLayer>
ContainerState::CreateOrRecycleMaskImageLayerFor(const MaskLayerKey& aKey,
mozilla::function<void(Layer* aLayer)> aSetUserData)
std::function<void(Layer* aLayer)> aSetUserData)
{
RefPtr<ImageLayer> result = mRecycledMaskImageLayers.Get(aKey);
if (result) {

Просмотреть файл

@ -10,11 +10,11 @@
*/
#include <algorithm>
#include <functional>
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Function.h"
#include "mozilla/dom/AnimationEffectReadOnlyBinding.h" // for PlaybackDirection
#include "mozilla/Likely.h"
#include "mozilla/LookAndFeel.h"
@ -116,7 +116,7 @@ nsConditionalResetStyleData::GetConditionalStyleData(nsStyleStructID aSID,
// (The expectation is then that aCallback will set the resulting
// imgRequestProxy in a style struct somewhere.)
static void
SetImageRequest(function<void(imgRequestProxy*)> aCallback,
SetImageRequest(std::function<void(imgRequestProxy*)> aCallback,
nsPresContext* aPresContext,
const nsCSSValue& aValue)
{
@ -127,7 +127,7 @@ SetImageRequest(function<void(imgRequestProxy*)> aCallback,
}
static void
SetStyleImageRequest(function<void(nsStyleImageRequest*)> aCallback,
SetStyleImageRequest(std::function<void(nsStyleImageRequest*)> aCallback,
nsPresContext* aPresContext,
const nsCSSValue& aValue,
nsStyleImageRequest::Mode aModeFlags =

Просмотреть файл

@ -1,223 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* A type-erased callable wrapper. */
#ifndef mozilla_Function_h
#define mozilla_Function_h
#include "mozilla/Attributes.h" // for MOZ_IMPLICIT
#include "mozilla/Move.h"
#include "mozilla/RefCounted.h"
#include "mozilla/RefPtr.h"
// |function<Signature>| is a wrapper that can hold any type of callable
// object that can be invoked in a way that's compatible with |Signature|.
// The standard "type erasure" technique is used to avoid the type of the
// wrapper depending on the concrete type of the wrapped callable.
//
// Supported callable types include non-member functions, static member
// functions, and function objects (that is to say, objects with an overloaded
// call operator; this includes C++11 lambdas). Member functions aren't
// directly supported; they first need to be wrapped into a function object
// using |std::mem_fn()| or an equivalent.
//
// |Signature| is a type of the form |ReturnType(Arguments...)|. Syntactically,
// this is a function type; it's not used in any way other than serving as a
// vehicle to encode the return and argument types into a single type.
//
// |function| is default-constructible. A default-constructed instance is
// considered "empty". Invoking an empty instance is undefined behaviour.
// An empty instance can be populated with a callable by assigning to it.
//
// This class is intended to provide functionality similar to the C++11
// standard library class |std::function|.
namespace mozilla {
namespace detail {
template<typename ReturnType, typename... Arguments>
class FunctionImplBase : public mozilla::RefCounted<FunctionImplBase<ReturnType, Arguments...>>
{
public:
MOZ_DECLARE_REFCOUNTED_TYPENAME(FunctionImplBase)
virtual ~FunctionImplBase() {}
virtual ReturnType call(Arguments... aArguments) = 0;
};
// Normal Callable Object.
template <typename Callable, typename ReturnType, typename... Arguments>
class FunctionImpl : public FunctionImplBase<ReturnType, Arguments...>
{
public:
explicit FunctionImpl(const Callable& aCallable)
: mCallable(aCallable) {}
ReturnType call(Arguments... aArguments) override
{
return mCallable(Forward<Arguments>(aArguments)...);
}
private:
Callable mCallable;
};
// Base class for passing pointer to member function.
template <typename Callable, typename ReturnType, typename... Arguments>
class MemberFunctionImplBase : public FunctionImplBase<ReturnType, Arguments...>
{
public:
explicit MemberFunctionImplBase(const Callable& aCallable)
: mCallable(aCallable) {}
ReturnType call(Arguments... aArguments) override
{
return callInternal(Forward<Arguments>(aArguments)...);
}
private:
template<typename ThisType, typename... Args>
ReturnType callInternal(ThisType* aThis, Args&&... aArguments)
{
return (aThis->*mCallable)(Forward<Args>(aArguments)...);
}
template<typename ThisType, typename... Args>
ReturnType callInternal(ThisType&& aThis, Args&&... aArguments)
{
return (aThis.*mCallable)(Forward<Args>(aArguments)...);
}
Callable mCallable;
};
// For non-const member function specialization of FunctionImpl.
template <typename ThisType, typename... Args, typename ReturnType, typename... Arguments>
class FunctionImpl<ReturnType(ThisType::*)(Args...),
ReturnType, Arguments...>
: public MemberFunctionImplBase<ReturnType(ThisType::*)(Args...),
ReturnType, Arguments...>
{
public:
explicit FunctionImpl(ReturnType(ThisType::*aMemberFunc)(Args...))
: MemberFunctionImplBase<ReturnType(ThisType::*)(Args...),
ReturnType, Arguments...>(aMemberFunc)
{}
};
// For const member function specialization of FunctionImpl.
template <typename ThisType, typename... Args, typename ReturnType, typename... Arguments>
class FunctionImpl<ReturnType(ThisType::*)(Args...) const,
ReturnType, Arguments...>
: public MemberFunctionImplBase<ReturnType(ThisType::*)(Args...) const,
ReturnType, Arguments...>
{
public:
explicit FunctionImpl(ReturnType(ThisType::*aConstMemberFunc)(Args...) const)
: MemberFunctionImplBase<ReturnType(ThisType::*)(Args...) const,
ReturnType, Arguments...>(aConstMemberFunc)
{}
};
} // namespace detail
// The primary template is never defined. As |Signature| is required to be
// of the form |ReturnType(Arguments...)|, we only define a partial
// specialization that matches this form. This allows us to use |ReturnType|
// and |Arguments| in the definition of the specialization without having to
// introspect |Signature|.
template<typename Signature>
class function;
template<typename ReturnType, typename... Arguments>
class function<ReturnType(Arguments...)>
{
public:
function() {}
// This constructor is implicit to match the interface of |std::function|.
template <typename Callable>
MOZ_IMPLICIT function(const Callable& aCallable)
: mImpl(new detail::FunctionImpl<Callable, ReturnType, Arguments...>(aCallable))
{}
MOZ_IMPLICIT function(const function& aFunction)
: mImpl(aFunction.mImpl)
{}
MOZ_IMPLICIT function(decltype(nullptr))
{}
// Move constructor and move assingment operator.
// These should be generated automatically, but MSVC doesn't do that yet.
function(function&& aOther) : mImpl(Move(aOther.mImpl)) {}
function& operator=(function&& aOther) {
mImpl = Move(aOther.mImpl);
return *this;
}
template <typename Callable>
function& operator=(const Callable& aCallable)
{
mImpl = new detail::FunctionImpl<Callable, ReturnType, Arguments...>(aCallable);
return *this;
}
function& operator=(const function& aFunction)
{
mImpl = aFunction.mImpl;
return *this;
}
function& operator=(decltype(nullptr))
{
mImpl = nullptr;
return *this;
}
template<typename... Args>
ReturnType operator()(Args&&... aArguments) const
{
MOZ_ASSERT(mImpl);
return mImpl->call(Forward<Args>(aArguments)...);
}
explicit operator bool() const
{
return bool(mImpl);
}
private:
// TODO: Consider implementing a small object optimization.
RefPtr<detail::FunctionImplBase<ReturnType, Arguments...>> mImpl;
};
template<typename Signature>
bool
operator==(const function<Signature>& aX, decltype(nullptr))
{
return !aX;
}
template<typename Signature>
bool
operator==(decltype(nullptr), const function<Signature>& aX)
{
return !aX;
}
template<typename Signature>
bool
operator!=(const function<Signature>& aX, decltype(nullptr))
{
return bool(aX);
}
template<typename Signature>
bool
operator!=(decltype(nullptr), const function<Signature>& aX)
{
return bool(aX);
}
} // namespace mozilla
#endif /* mozilla_Function_h */

Просмотреть файл

@ -42,7 +42,6 @@ EXPORTS.mozilla = [
'EnumTypeTraits.h',
'FastBernoulliTrial.h',
'FloatingPoint.h',
'Function.h',
'GuardObjects.h',
'HashFunctions.h',
'IndexSequence.h',

Просмотреть файл

@ -1,115 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Assertions.h"
#include "mozilla/Function.h"
using mozilla::function;
#define CHECK(c) \
do { \
bool cond = !!(c); \
MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \
} while (false)
struct ConvertibleToInt
{
operator int() const { return 42; }
};
int increment(int arg) { return arg + 1; }
struct S {
S() {}
static int increment(int arg) { return arg + 1; }
int decrement(int arg) { return arg - 1;}
int sum(int arg1, int arg2) const { return arg1 + arg2;}
};
struct Incrementor {
int operator()(int arg) { return arg + 1; }
};
static void
TestNonmemberFunction()
{
function<int(int)> f = &increment;
CHECK(f(42) == 43);
}
static void
TestStaticMemberFunction()
{
function<int(int)> f = &S::increment;
CHECK(f(42) == 43);
}
static void
TestFunctionObject()
{
function<int(int)> f = Incrementor();
CHECK(f(42) == 43);
}
static void
TestLambda()
{
// Test non-capturing lambda
function<int(int)> f = [](int arg){ return arg + 1; };
CHECK(f(42) == 43);
// Test capturing lambda
int one = 1;
function<int(int)> g = [one](int arg){ return arg + one; };
CHECK(g(42) == 43);
}
static void
TestDefaultConstructionAndAssignmentLater()
{
function<int(int)> f; // allowed
// Would get an assertion if we tried calling f now.
f = &increment;
CHECK(f(42) == 43);
}
static void
TestReassignment()
{
function<int(int)> f = &increment;
CHECK(f(42) == 43);
f = [](int arg){ return arg + 2; };
CHECK(f(42) == 44);
}
static void
TestMemberFunction()
{
function<int(S&, int)> f = &S::decrement;
S s;
CHECK((f(s, 1) == 0));
}
static void
TestConstMemberFunction()
{
function<int(const S*, int, int)> f = &S::sum;
const S s;
CHECK((f(&s, 1, 1) == 2));
}
int
main()
{
TestNonmemberFunction();
TestStaticMemberFunction();
TestFunctionObject();
TestLambda();
TestDefaultConstructionAndAssignmentLater();
TestReassignment();
TestMemberFunction();
TestConstMemberFunction();
return 0;
}

Просмотреть файл

@ -22,7 +22,6 @@ CppUnitTests([
'TestEnumTypeTraits',
'TestFastBernoulliTrial',
'TestFloatingPoint',
'TestFunction',
'TestIntegerPrintfMacros',
'TestIntegerRange',
'TestJSONWriter',

Просмотреть файл

@ -20,7 +20,6 @@ skip-if = os != 'win'
[TestEnumTypeTraits]
[TestFastBernoulliTrial]
[TestFloatingPoint]
[TestFunction]
[TestIntegerPrintfMacros]
[TestIntegerRange]
[TestJSONWriter]

Просмотреть файл

@ -13,7 +13,7 @@ using mozilla::TimeStamp;
namespace mozilla {
void GTestBench(const char* aSuite, const char* aName,
const mozilla::function<void()>& aTest)
const std::function<void()>& aTest)
{
#ifdef DEBUG
// Run the test to make sure that it doesn't fail but don't log

Просмотреть файл

@ -6,11 +6,11 @@
#ifndef GTEST_MOZGTESTBENCH_H
#define GTEST_MOZGTESTBENCH_H
#include "mozilla/Function.h"
#include <functional>
namespace mozilla {
void GTestBench(const char* aSuite, const char* aName, const mozilla::function<void()>& aTest);
void GTestBench(const char* aSuite, const char* aName, const std::function<void()>& aTest);
} //mozilla

Просмотреть файл

@ -15,7 +15,7 @@ namespace mozilla {
void
NSPRLogModulesParser(const char* aLogModules,
function<void(const char*, LogLevel, int32_t)> aCallback)
std::function<void(const char*, LogLevel, int32_t)> aCallback)
{
if (!aLogModules) {
return;

Просмотреть файл

@ -5,7 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Logging.h"
#include "mozilla/Function.h"
#include <functional>
namespace mozilla {
@ -17,6 +18,6 @@ namespace mozilla {
* @param aCallback The callback to invoke for each log module config entry.
*/
void NSPRLogModulesParser(const char* aLogModules,
function<void(const char*, LogLevel, int32_t)> aCallback);
std::function<void(const char*, LogLevel, int32_t)> aCallback);
} // namespace mozilla

Просмотреть файл

@ -13,7 +13,6 @@
#include "mozilla/Attributes.h"
#include "mozilla/BinarySearch.h"
#include "mozilla/fallible.h"
#include "mozilla/Function.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Move.h"
@ -29,6 +28,7 @@
#include "nsDebug.h"
#include "nsISupportsImpl.h"
#include "nsRegionFwd.h"
#include <functional>
#include <initializer_list>
#include <new>
@ -737,6 +737,12 @@ DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::indexedDB::SerializedStructuredClone
DECLARE_USE_COPY_CONSTRUCTORS(JSStructuredCloneData)
DECLARE_USE_COPY_CONSTRUCTORS(mozilla::dom::MessagePortMessage)
template<typename T>
struct nsTArray_CopyChooser<std::function<T>>
{
typedef nsTArray_CopyWithConstructors<std::function<T>> Type;
};
//
// Base class for nsTArray_Impl that is templated on element type and derived

Просмотреть файл

@ -11,6 +11,8 @@
#include "nsTArray.h"
#include "nsCycleCollectionNoteChild.h"
#include <functional>
/**
* An array of observers. Like a normal array, but supports iterators that are
* stable even if the array is modified during iteration.
@ -249,7 +251,7 @@ public:
}
// See nsTArray::RemoveElementsBy.
void RemoveElementsBy(mozilla::function<bool(const elem_type&)> aPredicate)
void RemoveElementsBy(std::function<bool(const elem_type&)> aPredicate)
{
index_type i = 0;
mArray.RemoveElementsBy([&](const elem_type& aItem) {

Просмотреть файл

@ -5,7 +5,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Attributes.h"
#include "mozilla/Function.h"
#include "mozilla/mozalloc.h"
#include "mozilla/ScopeExit.h"
#include "nsCOMPtr.h"