track total size of static array and Unit/Class/Func
[hiphop-php.git] / hphp / runtime / base / container-functions.h
blobb6c94bf720ab3cef7e8c0b078fcb7288629f9968
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 #ifndef incl_HPHP_CONTAINER_FUNCTIONS_H_
17 #define incl_HPHP_CONTAINER_FUNCTIONS_H_
19 #include "hphp/runtime/base/type-variant.h"
20 #include "hphp/runtime/base/collections.h"
21 #include "hphp/runtime/ext/collections/ext_collections.h"
23 namespace HPHP {
25 //////////////////////////////////////////////////////////////////////
27 inline bool isContainer(const TypedValue c) {
28 assertx(tvIsPlausible(c));
29 return isArrayLikeType(c.m_type) ||
30 (c.m_type == KindOfObject && c.m_data.pobj->isCollection());
33 inline bool isContainer(const Variant& v) {
34 return isContainer(*v.asTypedValue());
37 inline bool isContainerOrNull(const TypedValue c) {
38 assertx(tvIsPlausible(c));
39 return isNullType(c.m_type) || isArrayLikeType(c.m_type) ||
40 (c.m_type == KindOfObject && c.m_data.pobj->isCollection());
43 inline bool isContainerOrNull(const Variant& v) {
44 return isContainerOrNull(*v.asTypedValue());
47 inline bool isMutableContainer(const TypedValue c) {
48 assertx(tvIsPlausible(c));
49 return isArrayLikeType(c.m_type) ||
50 (c.m_type == KindOfObject && c.m_data.pobj->isMutableCollection());
53 inline bool isMutableContainer(const Variant& v) {
54 return isMutableContainer(*v.asTypedValue());
57 inline size_t getContainerSize(const TypedValue c) {
58 assertx(isContainer(c));
59 if (isArrayLikeType(c.m_type)) {
60 return c.m_data.parr->size();
62 assertx(c.m_type == KindOfObject && c.m_data.pobj->isCollection());
63 return collections::getSize(c.m_data.pobj);
66 inline size_t getContainerSize(const Variant& v) {
67 return getContainerSize(*v.asTypedValue());
70 inline bool isPackedContainer(const TypedValue c) {
71 assertx(isContainer(c));
72 if (isArrayLikeType(c.m_type)) {
73 return c.m_data.parr->hasVanillaPackedLayout();
76 return isVectorCollection(c.m_data.pobj->collectionType());
79 ALWAYS_INLINE
80 const TypedValue container_as_tv(const Variant& container) {
81 const auto& cellContainer = *container.asTypedValue();
82 if (UNLIKELY(!isContainer(cellContainer))) {
83 SystemLib::throwInvalidArgumentExceptionObject(
84 "Parameter must be a container (array or collection)");
86 return cellContainer;
89 //////////////////////////////////////////////////////////////////////
92 * clsmeth compact container helpers.
94 inline bool isClsMethCompactContainer(const TypedValue c) {
95 return isContainer(c) || isClsMethType(c.m_type);
98 inline bool isClsMethCompactContainer(const Variant& v) {
99 return isClsMethCompactContainer(*v.asTypedValue());
102 inline size_t getClsMethCompactContainerSize(const TypedValue c) {
103 return isClsMethType(c.m_type) ? 2 : getContainerSize(c);
106 inline size_t getClsMethCompactContainerSize(const Variant& v) {
107 return getClsMethCompactContainerSize(*v.asTypedValue());
110 inline TypedValue* castClsmethToContainerInplace(TypedValue* c) {
111 if (isClsMethType(c->m_type)) {
112 if (RuntimeOption::EvalHackArrDVArrs) {
113 tvCastToVecInPlace(c);
114 } else {
115 tvCastToVArrayInPlace(c);
118 return c;
121 //////////////////////////////////////////////////////////////////////
124 #endif