Move io_tests to folly/io/async/test
[hiphop-php.git] / hphp / runtime / base / container-functions.h
blobf04de8f436c1a85c59ba679d8a554ef3a71d8a85
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #pragma once
18 #include "hphp/runtime/base/type-variant.h"
19 #include "hphp/runtime/base/collections.h"
20 #include "hphp/runtime/ext/collections/ext_collections.h"
22 namespace HPHP {
24 //////////////////////////////////////////////////////////////////////
26 inline bool isContainer(const TypedValue c) {
27 assertx(tvIsPlausible(c));
28 return isArrayLikeType(c.m_type) ||
29 (c.m_type == KindOfObject && c.m_data.pobj->isCollection());
32 inline bool isContainer(const Variant& v) {
33 return isContainer(*v.asTypedValue());
36 inline bool isContainerOrNull(const TypedValue c) {
37 assertx(tvIsPlausible(c));
38 return isNullType(c.m_type) || isArrayLikeType(c.m_type) ||
39 (c.m_type == KindOfObject && c.m_data.pobj->isCollection());
42 inline bool isContainerOrNull(const Variant& v) {
43 return isContainerOrNull(*v.asTypedValue());
46 inline bool isMutableContainer(const TypedValue c) {
47 assertx(tvIsPlausible(c));
48 return isArrayLikeType(c.m_type) ||
49 (c.m_type == KindOfObject && c.m_data.pobj->isMutableCollection());
52 inline bool isMutableContainer(const Variant& v) {
53 return isMutableContainer(*v.asTypedValue());
56 inline size_t getContainerSize(const TypedValue c) {
57 assertx(isContainer(c));
58 if (isArrayLikeType(c.m_type)) {
59 return c.m_data.parr->size();
61 assertx(c.m_type == KindOfObject && c.m_data.pobj->isCollection());
62 return collections::getSize(c.m_data.pobj);
65 inline size_t getContainerSize(const Variant& v) {
66 return getContainerSize(*v.asTypedValue());
69 ALWAYS_INLINE
70 const TypedValue container_as_tv(const Variant& container) {
71 const auto& cellContainer = *container.asTypedValue();
72 if (UNLIKELY(!isContainer(cellContainer))) {
73 SystemLib::throwInvalidArgumentExceptionObject(
74 "Parameter must be a container (array or collection)");
76 return cellContainer;
79 //////////////////////////////////////////////////////////////////////
82 * clsmeth compact container helpers.
84 inline bool isClsMethCompactContainer(const TypedValue c) {
85 return isContainer(c);
88 inline bool isClsMethCompactContainer(const Variant& v) {
89 return isClsMethCompactContainer(*v.asTypedValue());
92 inline size_t getClsMethCompactContainerSize(const TypedValue c) {
93 assertx(!isClsMethType(c.m_type));
94 return isClsMethType(c.m_type) ? 2 : getContainerSize(c);
97 inline size_t getClsMethCompactContainerSize(const Variant& v) {
98 return getClsMethCompactContainerSize(*v.asTypedValue());
101 //////////////////////////////////////////////////////////////////////