Add MarkerSection

Summary:
@public

Adds a class for triggering markers.

This calls `startMarker()` on construction, and `endMarker()` on destruction, thus being usable like a "scope guard": the object is instantiated, and automatically destroyed when going out of scope.

Reviewed By: SidharthGuglani

Differential Revision: D13817589

fbshipit-source-id: fd88884af970c1c0933d9ca6843f3f8f5d28b9e6
This commit is contained in:
David Aurelio
2019-01-29 10:30:11 -08:00
committed by Facebook Github Bot
parent 3de3575ac4
commit 58f0cca7c7
3 changed files with 190 additions and 0 deletions

View File

@@ -37,3 +37,25 @@ typedef struct {
void YGConfigSetMarkerCallbacks(YGConfigRef, YGMarkerCallbacks);
YG_EXTERN_C_END
#ifdef __cplusplus
namespace facebook {
namespace yoga {
namespace marker {
namespace detail {
template <YGMarker M>
struct MarkerData;
template <>
struct MarkerData<YGMarkerLayout> {
using type = YGMarkerLayoutData;
};
} // namespace detail
} // namespace marker
} // namespace yoga
} // namespace facebook
#endif // __cplusplus

44
yoga/instrumentation.h Normal file
View File

@@ -0,0 +1,44 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
#include "YGConfig.h"
#include "YGMarker.h"
#include "YGNode.h"
namespace facebook {
namespace yoga {
namespace marker {
template <YGMarker MarkerType>
class MarkerSection {
public:
MarkerSection(YGNodeRef node) : MarkerSection{node, node->getConfig()} {}
~MarkerSection() {
if (endMarker_) {
endMarker_(MarkerType, node_, {&data}, userData_);
}
}
typename detail::MarkerData<MarkerType>::type data = {};
private:
decltype(YGMarkerCallbacks{}.endMarker) endMarker_;
YGNodeRef node_;
void* userData_;
MarkerSection(YGNodeRef node, YGConfigRef config)
: MarkerSection{node, config ? &config->markerCallbacks : nullptr} {}
MarkerSection(YGNodeRef node, YGMarkerCallbacks* callbacks)
: endMarker_{callbacks ? callbacks->endMarker : nullptr},
node_{node},
userData_{callbacks && callbacks->startMarker
? callbacks->startMarker(MarkerType, node, {&data})
: nullptr} {}
};
} // namespace marker
} // namespace yoga
} // namespace facebook