summaryrefslogtreecommitdiff
path: root/include/value.h
blob: cd3256800ee0e14d1d50d224a88b54136508ec88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
/**
 *  Value.h
 *
 *  Base class for values that are stored in the Zend engine. One instance
 *  of the value class represents a variable that exists in user space in
 *  the PHP environment, for example as global variable, local variable
 *  inside a function or as a member of an object or an array.
 *
 *  A value can be a scalar or a more complicated structure like an object
 *  or an array.
 *
 *  Internally, the Zend engine works with "zval" objects for this. These "zval"
 *  object hold a reference counter and a reference setting. The PHP-CPP Value
 *  class takes care of doing this, so all you need to do is use objects of
 *  this class.
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */

/**
 *  Forward definitions
 */
struct _zval_struct;

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Forward definitions
 */
class Base;
class ValueIterator;
class Parameters;
template <class Type> class HashMember;

/**
 *  Class definition
 */
class PHPCPP_EXPORT Value : private HashParent
{
public:
    /**
     *  Empty constructor (value = NULL)
     */
    Value();

    /**
     *  Constructor for various types
     *  @param  value
     */
    Value(std::nullptr_t value);
    Value(int16_t value);
    Value(int32_t value);
    Value(int64_t value);
    Value(bool value);
    Value(char value);
    Value(const std::string &value);
    Value(const char *value, int size = -1);
    Value(double value);
    Value(const IniValue &value);

    /**
     *  Construct to a specific type
     *  @param  value
     */
    Value(Type type) : Value() { setType(type); }

    /**
     *  Constructors from a vector (this will create an array)
     *  @param  value
     */
    template <typename T>
    Value(const std::vector<T> &input) : Value(Type::Array)
    {
        // index
        int i = 0;

        // set all elements
        for (auto &elem : input) setRaw(i++, elem);
    }

    // old visual c++ environments have no support for initializer lists
#   if !defined(_MSC_VER) || _MSC_VER >= 1800

    /**
     *  Constructor from an initializer list
     *  @param  value
     */
    template <typename T>
    Value(const std::initializer_list<T> &value) : Value(Type::Array)
    {
        // index
        int i = 0;

        // set all elements
        for (auto &elem : value) setRaw(i++, elem);
    }

    // end of visual c++ check
#   endif

    /**
     *  Constructor from a map (this will create an associative array)
     *  @param  value
     */
    template <typename T>
    Value(const std::map<std::string,T> &value) : Value(Type::Array)
    {
        // set all elements
        for (auto &iter : value) setRaw(iter.first.c_str(), iter.first.size(), iter.second);
    }

    /**
     *  Wrap object around zval
     *  @param  zval        Zval to wrap
     *  @param  ref         Force this to be a reference
     */
    Value(struct _zval_struct *zval, bool ref=false);

    /**
     *  Wrap around an object implemented by us
     *  @param  object      Object to be wrapped
     */
    Value(const Base *base);

    /**
     *  Copy constructor
     *  @param  value
     */
    Value(const Value &that);

    /**
     *  Move constructor
     *  @param  value
     */
    Value(Value &&that) _NOEXCEPT;

    /**
     *  Destructor
     */
    virtual ~Value();

    /**
     *  Move assignment
     *  @param  value
     *  @return Value
     */
    Value &operator=(Value &&value) _NOEXCEPT;

    /**
     *  Assignment operator for various types
     *  @param  value
     *  @return Value
     */
    Value &operator=(std::nullptr_t value);
    Value &operator=(const Value &value);
    Value &operator=(int16_t value);
    Value &operator=(int32_t value);
    Value &operator=(int64_t value);
    Value &operator=(bool value);
    Value &operator=(char value);
    Value &operator=(const std::string &value);
    Value &operator=(const char *value);
    Value &operator=(double value);
    Value &operator=(const HashMember<std::string> &value);
    Value &operator=(const HashMember<int> &value);

    /**
     *  Add a value to the object
     *  @param  value
     *  @return Value
     */
    Value &operator+=(const Value &value);
    Value &operator+=(int16_t value);
    Value &operator+=(int32_t value);
    Value &operator+=(int64_t value);
    Value &operator+=(bool value);
    Value &operator+=(char value);
    Value &operator+=(const std::string &value);
    Value &operator+=(const char *value);
    Value &operator+=(double value);

    /**
     *  Subtract a value from the object
     *  @param  value
     *  @return Value
     */
    Value &operator-=(const Value &value);
    Value &operator-=(int16_t value);
    Value &operator-=(int32_t value);
    Value &operator-=(int64_t value);
    Value &operator-=(bool value);
    Value &operator-=(char value);
    Value &operator-=(const std::string &value);
    Value &operator-=(const char *value);
    Value &operator-=(double value);

    /**
     *  Multiply the object with a certain value
     *  @param  value
     *  @return Value
     */
    Value &operator*=(const Value &value);
    Value &operator*=(int16_t value);
    Value &operator*=(int32_t value);
    Value &operator*=(int64_t value);
    Value &operator*=(bool value);
    Value &operator*=(char value);
    Value &operator*=(const std::string &value);
    Value &operator*=(const char *value);
    Value &operator*=(double value);

    /**
     *  Divide the object with a certain value
     *  @param  value
     *  @return Value
     */
    Value &operator/=(const Value &value);
    Value &operator/=(int16_t value);
    Value &operator/=(int32_t value);
    Value &operator/=(int64_t value);
    Value &operator/=(bool value);
    Value &operator/=(char value);
    Value &operator/=(const std::string &value);
    Value &operator/=(const char *value);
    Value &operator/=(double value);

    /**
     *  Divide the object with a certain value, and get the rest
     *  @param  value
     *  @return Value
     */
    Value &operator%=(const Value &value);
    Value &operator%=(int16_t value);
    Value &operator%=(int32_t value);
    Value &operator%=(int64_t value);
    Value &operator%=(bool value);
    Value &operator%=(char value);
    Value &operator%=(const std::string &value);
    Value &operator%=(const char *value);
    Value &operator%=(double value);

    /**
     *  Addition operator
     *  @param  value
     *  @return Value
     */
    Value operator+(const Value &value);
    Value operator+(int16_t value);
    Value operator+(int32_t value);
    Value operator+(int64_t value);
    Value operator+(bool value);
    Value operator+(char value);
    Value operator+(const std::string &value);
    Value operator+(const char *value);
    Value operator+(double value);

    /**
     *  Subtraction operator
     *  @param  value
     *  @return Value
     */
    Value operator-(const Value &value);
    Value operator-(int16_t value);
    Value operator-(int32_t value);
    Value operator-(int64_t value);
    Value operator-(bool value);
    Value operator-(char value);
    Value operator-(const std::string &value);
    Value operator-(const char *value);
    Value operator-(double value);

    /**
     *  Multiplication operator
     *  @param  value
     *  @return Value
     */
    Value operator*(const Value &value);
    Value operator*(int16_t value);
    Value operator*(int32_t value);
    Value operator*(int64_t value);
    Value operator*(bool value);
    Value operator*(char value);
    Value operator*(const std::string &value);
    Value operator*(const char *value);
    Value operator*(double value);

    /**
     *  Division operator
     *  @param  value
     *  @return Value
     */
    Value operator/(const Value &value);
    Value operator/(int16_t value);
    Value operator/(int32_t value);
    Value operator/(int64_t value);
    Value operator/(bool value);
    Value operator/(char value);
    Value operator/(const std::string &value);
    Value operator/(const char *value);
    Value operator/(double value);

    /**
     *  Modulus operator
     *  @param  value
     *  @return Value
     */
    Value operator%(const Value &value);
    Value operator%(int16_t value);
    Value operator%(int32_t value);
    Value operator%(int64_t value);
    Value operator%(bool value);
    Value operator%(char value);
    Value operator%(const std::string &value);
    Value operator%(const char *value);
    Value operator%(double value);

    /**
     *  Comparison operators for hardcoded strings
     *  @param  value
     */
    bool operator==(const char *value) const { return ::strcmp(rawValue(), value) == 0; }
    bool operator!=(const char *value) const { return ::strcmp(rawValue(), value) != 0; }
    bool operator<=(const char *value) const { return ::strcmp(rawValue(), value) <= 0; }
    bool operator>=(const char *value) const { return ::strcmp(rawValue(), value) >= 0; }
    bool operator< (const char *value) const { return ::strcmp(rawValue(), value) <  0; }
    bool operator> (const char *value) const { return ::strcmp(rawValue(), value) >  0; }

    /**
     *  Comparison operators for hardcoded Value
     *  @param  value
     */
    bool operator==(const Value &value) const;
    bool operator!=(const Value &value) const { return !operator==(value); }
    bool operator< (const Value &value) const;
    bool operator> (const Value &value) const { return value.operator<(*this); }
    bool operator<=(const Value &value) const { return !operator>(value); }
    bool operator>=(const Value &value) const { return !operator<(value); }

    /**
     *  Comparison operators
     *  @param  value
     */
    template <typename T> bool operator==(const T &value) const { return (T)*this == value; }
    template <typename T> bool operator!=(const T &value) const { return (T)*this != value; }
    template <typename T> bool operator<=(const T &value) const { return (T)*this <= value; }
    template <typename T> bool operator>=(const T &value) const { return (T)*this >= value; }
    template <typename T> bool operator< (const T &value) const { return (T)*this <  value; }
    template <typename T> bool operator> (const T &value) const { return (T)*this >  value; }

    /**
     *  The type of object
     *  @return Type
     */
    Type type() const;

    /**
     *  Change the internal type of the variable
     *  @param  Type
     */
    virtual Value &setType(Type type) &;

    /**
     *  Make a clone of the value with the same type
     *  @return Value
     */
    Value clone() const;

    /**
     *  Make a clone of the value with a different type
     *  @param  type
     *  @return Value
     */
    Value clone(Type type) const;

    /**
     *  Check if the value is of a certain type
     *  @return bool
     */
    bool isNull()       const { return type() == Type::Null; }
    bool isNumeric()    const { return type() == Type::Numeric; }
    bool isBool()       const { return type() == Type::False || type() == Type::True; }
    bool isString()     const { return type() == Type::String; }
    bool isFloat()      const { return type() == Type::Float; }
    bool isObject()     const { return type() == Type::Object; }
    bool isArray()      const { return type() == Type::Array; }
    bool isScalar()     const { return isNull() || isNumeric() || isBool() || isString() || isFloat(); }
    bool isCallable()   const;

    /**
     *  Get access to the raw buffer - you can use this for direct reading and
     *  writing to and from the buffer. Note that this only works for string
     *  variables - other variables return nullptr.
     *
     *  If you are going to write to the buffer, make sure that you first call
     *  the reserve() method to ensure that the buffer is big enough.
     *
     *  @return char *
     */
    char *buffer() const;

    /**
     *  Get access to the raw buffer for read operations. Note that this
     *  only works for string variables - other variables return nullptr.
     *
     *  @return const char *
     */
    const char *rawValue() const;

    /**
     *  Retrieve the value as number
     *
     *  We force this to be a int64_t because we assume that most
     *  servers run 64 bits nowadays, and because we use int32_t, int64_t
     *  almost everywhere, instead of 'long' and on OSX neither of
     *  these intxx_t types is defined as 'long'...
     *
     *  @return int64_t
     */
    int64_t numericValue() const;

    /**
     *  Retrieve the value as boolean
     *  @return bool
     */
    bool boolValue() const;

    /**
     *  Retrieve the value as a string
     *  @return string
     */
    std::string stringValue() const;

    /**
     *  Retrieve the value as decimal
     *  @return double
     */
    double floatValue() const;

    /**
     *  Convert the object to a vector
     *
     *  This only works for regular arrays that are indexed by a number, start
     *  with position 0 and have no empty spaces.
     *
     *  @return std::vector
     */
    template <typename T>
    std::vector<T> vectorValue() const
    {
        // only works for arrays, other types return an empty vector
        if (!isArray()) return std::vector<T>();

        // allocate a result
        std::vector<T> result;

        // reserve enough space
        size_t count = size();
        result.reserve(count);

        // and fill the result vector
        for (size_t i = 0; i<count; i++)
        {
            // check if the index exists
            if (!contains(i)) continue;

            // get the value and add it to the vector
            result.push_back(get(i));
        }

        // done
        return result;
    }

    /**
     *  Convert the object to a set
     *
     *  This only works for regular arrays that are indexed by a number, start
     *  with position 0 and have no empty spaces.
     *
     *  return  std::vector
     */
    template <typename T>
    std::set<T> setValue() const
    {
        // only works for arrays, other types return an empty set
        if (!isArray()) return std::set<T>();

        // allocate a result
        std::set<T> result;

        // how many elements are we inserting
        size_t count = size();

        // and fill the result set
        for (size_t i = 0; i<count; i++)
        {
            // check if the index exists
            if (!contains(i)) continue;

            // get the value and add it to the vector
            result.insert(get(i));
        }

        // done
        return result;
    }

    /**
     *  Convert the object to a map with string index and Php::Value value
     *  @return std::map
     */
    std::map<std::string,Php::Value> mapValue() const;

    /**
     *  Convert the object to a map with string index and a specific type as value
     *  @return std::map
     */
    template <typename T>
    std::map<std::string,T> mapValue() const
    {
        // must be an array or an object, otherwise the map is empty
        if (!isArray() && !isObject()) return std::map<std::string,T>();

        // result variable
        std::map<std::string,T> result;

        // iterate over the values
        iterate([&result](const Value &key, const Value &value) {

            // first convert the value to the appropriate type (otherwise
            // compiler errors occur)
            T val = value;

            // add the value to the array
            result[key] = val;
        });

        // done
        return result;
    }

    /**
     *  Define the iterator type
     */
    typedef ValueIterator iterator;

    /**
     *  Return an iterator for iterating over the values
     *  This is only meaningful for Value objects that hold an array or an object
     *  @return iterator
     */
    iterator begin() const;

    /**
     *  Return an iterator for iterating over the values
     *  This is only meaningful for Value objects that hold an array or an object
     *  @return iterator
     */
    iterator end() const;

    /**
     *  The number of members in case of an array or object
     *  @return int
     */
    int size() const;

    /**
     *  The number of members in case of an array or object
     *  @return int
     */
    int count() const
    {
        return size();
    }

    /**
     *  The number of members in case of an array or object
     *  @return int
     */
    int length() const
    {
        return size();
    }

    /**
     *  Is a certain index set in the array
     *  @param  index
     *  @return bool
     */
    virtual bool contains(int index) const override;

    /**
     *  Is a certain key set in the array
     *  @param  key
     *  @return bool
     */
    virtual bool contains(const std::string &key) const override
    {
        return contains(key.c_str(), key.size());
    }

    /**
     *  Is a certain key set in the array
     *  @param  key
     *  @param  size
     *  @return bool
     */
    bool contains(const char *key, int size) const;

    /**
     *  Is a certain key set in the array
     *  @param  key
     *  @param  size
     *  @return bool
     */
    bool contains(const char *key) const
    {
        return contains(key, ::strlen(key));
    }

    /**
     *  Is a certain key set in the array, when that key is stored as value object
     *  @param  key
     *  @return bool
     */
    virtual bool contains(const Value &value) const override
    {
        if (value.isNumeric()) return contains(value.numericValue());
        if (value.isString()) return contains(value.rawValue(), value.size());
        return contains(value.stringValue());
    }

    /**
     *  Cast to a number
     *  @return int32_t
     */
    operator int16_t () const
    {
        return (int16_t)numericValue();
    }

    /**
     *  Cast to a number
     *  @return int32_t
     */
    operator int32_t () const
    {
        return (int32_t)numericValue();
    }

    /**
     *  Cast to a number
     *  @return int64_t
     */
    operator int64_t () const
    {
        return numericValue();
    }

    /**
     *  Cast to a boolean
     *  @return boolean
     */
    operator bool () const
    {
        return boolValue();
    }

    /**
     *  Cast to a string
     *  @return string
     */
    operator std::string () const
    {
        return stringValue();
    }

    /**
     *  Cast to byte array
     *
     *  Note that this only works for string values, other
     *  variables return a nullptr.
     *
     *  @return const char *
     */
    operator const char * () const
    {
        return rawValue();
    }

    /**
     *  Cast to a floating point
     *  @return double
     */
    operator double () const
    {
        return floatValue();
    }

    /**
     *  Convert the object to a vector
     *  @return std::vector
     */
    template <typename T>
    operator std::vector<T>() const
    {
        return vectorValue<T>();
    }

    /**
     *  Convert the object to a set
     *  @return std::set
     */
    template <typename T>
    operator std::set<T>() const
    {
        return setValue<T>();
    }

    /**
     *  Convert the object to a map with string index and Php::Value value
     *  @return std::map
     */
    operator std::map<std::string,Php::Value> () const
    {
        return mapValue();
    }

    /**
     *  Convert the object to a map with string index and Php::Value value
     *  @return std::map
     */
    template <typename T>
    operator std::map<std::string,T> () const
    {
        return mapValue<T>();
    }

    /**
     *  Get access to a certain array member
     *  @param  index
     *  @return Value
     */
    virtual Value get(int index) const override;

    /**
     *  Get access to a certain assoc member
     *  @param  key
     *  @param  size
     *  @return Value
     */
    Value get(const char *key, int size=-1) const;

    /**
     *  Get access to a certain assoc member
     *  @param  key
     *  @return Value
     */
    virtual Value get(const std::string &key) const override
    {
        return get(key.c_str(), key.size());
    }

    /**
     *  Get access to a certain variant member
     *  @param  key
     *  @return Value
     */
    virtual Value get(const Value &key) const override
    {
        if (key.isNumeric()) return get(key.numericValue());
        if (key.isString()) return get(key.rawValue(), key.size());
        return get(key.stringValue());
    }

    /**
     *  Set a certain property
     *  Calling this method will turn the value into an array
     *  @param  index       Index of the property to set
     *  @param  value       Value to set
     *  @return Value       The value that was set
     */
    virtual void set(int index, const Value &value) override;

    /**
     *  Set a certain property
     *  Calling this method will turn the value into an array
     *  @param  key         Key of the property to set
     *  @param  size        Size of the key
     *  @param  value       Value to set
     */
    virtual void set(const char *key, int size, const Value &value);

    /**
     *  Set a certain property
     *  Calling this method will turn the object into an array
     *  @param  key         Key to set
     *  @param  value       Value to set
     */
    void set(const char *key, const Value &value)
    {
        set(key, ::strlen(key), value);
    }

    /**
     *  Set a certain property
     *  Calling this method will turn the object into an array
     *  @param  key         Key to set
     *  @param  value       Value to set
     */
    virtual void set(const std::string &key, const Value &value) override
    {
        return set(key.c_str(), key.size(), value);
    }

    /**
     *  Overwrite the value at a certain variant index
     *  @param  key
     *  @param  value
     */
    virtual void set(const Value &key, const Value &value) override
    {
        if (key.isNumeric()) return set(key.numericValue(), value);
        if (key.isString()) return set(key.rawValue(), key.size(), value);
        return set(key.stringValue(), value);
    }

    /**
     *  Unset a member by its index
     *  @param  index
     */
    virtual void unset(int index) override;

    /**
     *  Unset by key name and length of the key
     *  @param  key
     *  @param  size
     */
    void unset(const char *key, int size);

    /**
     *  Unset by key name and length of the key
     *  @param  key
     *  @param  size
     */
    void unset(const char *key)
    {
        unset(key, ::strlen(key));
    }

    /**
     *  Unset a member by its key
     *  @param  key
     */
    virtual void unset(const std::string &key) override
    {
        return unset(key.c_str(), key.size());
    }

    /**
     *  Unset a member by its key
     *  @param  key
     */
    virtual void unset(const Value &key) override
    {
        if (key.isNumeric()) return unset(key.numericValue());
        if (key.isString()) return unset(key.rawValue(), key.size());
        return unset(key.stringValue());
    }

    /**
     *  Array access operator
     *  This can be used for accessing arrays
     *  @param  index
     *  @return Member
     */
    HashMember<int> operator[](int index);

    /**
     *  Array access operator
     *  This can be used for accessing arrays
     *  @param  index
     *  @return Value
     */
    Value operator[](int index) const
    {
        return get(index);
    }

    /**
     *  Array access operator
     *  This can be used for accessing associative arrays
     *  @param  key
     *  @return Member
     */
    HashMember<std::string> operator[](const std::string &key);

    /**
     *  Array access operator
     *  This can be used for accessing associative arrays
     *  @param  key
     *  @return Value
     */
    Value operator[](const std::string &key) const
    {
        return get(key);
    }

    /**
     *  Array access operator
     *  This can be used for accessing associative arrays
     *  @param  key
     *  @return HashMember
     */
    HashMember<std::string> operator[](const char *key);

    /**
     *  Array access operator
     *  This can be used for accessing associative arrays
     *  @param  key
     *  @return Value
     */
    Value operator[](const char *key) const
    {
        return get(key);
    }

    /**
     *  Index by other value object
     *  @param  key
     *  @return HashMember<std::string>
     */
    HashMember<Value> operator[](const Value &key);

    /**
     *  Index by other value object
     *  @param  key
     *  @return HashMember<std::string>
     */
    Value operator[](const Value &key) const
    {
        if (key.isNumeric()) return get(key.numericValue());
        if (key.isString()) return get(key.rawValue(), key.size());
        return get(key.stringValue());
    }

    /**
     *  Call the function in PHP
     *  This call operator is only useful when the variable represents a callable
     *  @return Value
     */
    Value operator()() const;

    /**
     *  Call the function - if the variable holds a callable thing
     *  @param  args        Optional arguments
     *  @return Value
     */
    template <typename ...Args>
    Value operator()(Args&&... args) const
    {
        // store arguments
        Value vargs[] = { static_cast<Value>(args)... };

        // call the function
        return exec(sizeof...(Args), vargs);
    }

    /**
     *  Is a method with the given name callable?
     *
     *  This is only applicable when the Value contains a PHP object
     *
     *  @param  name        Name of the function
     *  @return boolean
     */
    bool isCallable(const char *name);

    /**
     *  Call a method
     *
     *  This is only applicable when the Value contains a PHP object
     *
     *  @param  name        Name of the function
     *  @return Value
     */
    Value call(const char *name) const;
    Value call(const char *name);

    /**
     *
     *  Call the method - if the variable holds an object with the given method
     *  @param  name        name of the method to call
     *  @param  p0          The first parameter
     *  @return Value
     */
    template <typename ...Args>
    Value call(const char *name, Args&&... args) const
    {
        // store arguments
        Value vargs[] = { static_cast<Value>(args)... };

        // call the function
        return exec(name, sizeof...(Args), vargs);
    }

    template <typename ...Args>
    Value call(const char *name, Args&&... args)
    {
        // store arguments
        Value vargs[] = { static_cast<Value>(args)... };

        // call the function
        return exec(name, sizeof...(Args), vargs);
    }

    /**
     *  Retrieve the original implementation
     *
     *  This only works for classes that were implemented using PHP-CPP,
     *  it returns nullptr for all other classes
     *
     *  @return Base*
     */
    Base *implementation() const;

    /**
     *  Retrieve the original implementation
     *
     *  This only works for classes that were implemented using PHP-CPP,
     *  it returns nullptr for all other classes
     *
     *  @return mixed
     */
    template <typename T>
    T *implementation() const
    {
        // retrieve the implementation
        Base *base = implementation();
        if (!base) return nullptr;

        // try casting it
        return dynamic_cast<T*>(base);
    }

    /**
     *  Check whether this object is an instance of a certain class
     *
     *  If you set the parameter 'allowString' to true, and the Value object
     *  holds a string, the string will be treated as class name.
     *
     *  @param  classname   The class of which this should be an instance
     *  @param  size        Length of the classname string
     *  @param  allowString Is it allowed for 'this' to be a string
     *  @return bool
     */
    bool instanceOf(const char *classname, size_t size, bool allowString = false) const;
    bool instanceOf(const char *classname, bool allowString = false) const { return instanceOf(classname, strlen(classname), allowString); }
    bool instanceOf(const std::string &classname, bool allowString = false) const { return instanceOf(classname.c_str(), classname.size(), allowString); }

    /**
     *  Check whether this object is derived from a certain class.
     *
     *  If you set the parameter 'allowString' to true, and the Value object
     *  holds a string, the string will be treated as class name.
     *
     *  @param  classname   The class of which this should be an instance
     *  @param  size        Length of the classname string
     *  @param  allowString Is it allowed for 'this' to be a string
     *  @return bool
     */
    bool derivedFrom(const char *classname, size_t size, bool allowString = false) const;
    bool derivedFrom(const char *classname, bool allowString = false) const { return derivedFrom(classname, strlen(classname), allowString); }
    bool derivedFrom(const std::string &classname, bool allowString = false) const { return derivedFrom(classname.c_str(), classname.size(), allowString); }


private:
    /**
     *  Iterate over key value pairs
     *  @param  callback
     */
    void iterate(const std::function<void(const Php::Value &,const Php::Value &)> &callback) const;

    /**
     *  Call function with a number of parameters
     *  @param  argc        Number of parameters
     *  @param  argv        The parameters
     *  @return Value
     */
    Value exec(int argc, Value *argv) const;

    /**
     *  Call method with a number of parameters
     *  @param  name        Name of method to call
     *  @param  argc        Number of parameters
     *  @param  argv        The parameters
     *  @return Value
     */
    Value exec(const char *name, int argc, Value *argv) const;
    Value exec(const char *name, int argc, Value *argv);

    /**
     *  Refcount - the number of references to the value
     *  @return int
     */
    int refcount() const;

protected:
    /**
     *  The wrapped zval
     *  @var struct zval*
     */
    struct _zval_struct *_val = nullptr;

    /**
     *  Detach the zval
     *
     *  This will unlink the zval internal structure from the Value object,
     *  so that the destructor will not reduce the number of references and/or
     *  deallocate the zval structure. This is used for functions that have to
     *  return a zval pointer, that would otherwise be deallocated the moment
     *  the function returns.
     *
     *  @param  keeprefcount    Keep the same refcount
     *  @return zval
     */
    struct _zval_struct *detach(bool keeprefcount = true);

    /**
     *  Set a certain property without running any checks (you must already know
     *  for sure that this is an array, and that the index is not yet in use)
     *
     *  @param  index       Index of the property to set
     *  @param  value       Value to set
     */
    void setRaw(int index, const Value &value);

    /**
     *  Set a certain property without any checks (you must already know for
     *  sure that this is either an object or an array, and that the index is
     *  not yet in use)
     *
     *  @param  key         Key of the property to set
     *  @param  size        Size of the key
     *  @param  value       Value to set
     */
    void setRaw(const char *key, int size, const Value &value);

    /**
     *  Internal helper method to create an `
     *  @param  begin       Should the iterator start at the begin?
     *  @return iterator
     */
    iterator createIterator(bool begin) const;

    /**
     *  Retrieve the class entry
     *  @param  allowString Allow the 'this' object to be a string
     *  @return zend_class_entry
     */
    struct _zend_class_entry *classEntry(bool allowString = true) const;

    /**
     *  Functions that need access to the privates
     */
    friend Value constant(const char *name, size_t size);
    friend bool  define(const char *name, size_t size, const Value &value);

    /**
     *  The Globals and Member classes can access the zval directly
     */
    friend class Globals;
    friend class Member;
    friend class ClassImpl;
    friend class IteratorImpl;
    friend class Extension;
    friend class HashIterator;
    friend class TraverseIterator;
    friend class HashMember<int>;
    friend class HashMember<std::string>;
    friend class Callable;
    friend class Script;
    friend class ConstantImpl;

    /**
     *  Friend functions which have to access that zval directly
     */
    friend Value set_exception_handler(const std::function<Value(Parameters &params)> &handler);
    friend Value set_error_handler(const std::function<Value(Parameters &params)> &handler, Error error);
};

/**
 *  Custom output stream operator
 *  @param  stream
 *  @param  value
 *  @return ostream
 */
PHPCPP_EXPORT std::ostream &operator<<(std::ostream &stream, const Value &value);

/**
 *  Custom +=, -=, *=, /=, &= operators, to update integral types with a Php::Value
 *
 *  This code looks complicated, it ensures that the operators are only
 *  overloaded for integral types (int, bool, etc) - and not for complex types
 *  (arrays, objects, etc)
 */
template <typename X, typename std::enable_if<std::is_integral<X>::value>::type* = nullptr>
X &operator+=(X &x, const Php::Value &value) { return x += static_cast<X>(value); }
template <typename X, typename std::enable_if<std::is_integral<X>::value>::type* = nullptr>
X &operator-=(X &x, const Php::Value &value) { return x -= static_cast<X>(value); }
template <typename X, typename std::enable_if<std::is_integral<X>::value>::type* = nullptr>
X &operator*=(X &x, const Php::Value &value) { return x *= static_cast<X>(value); }
template <typename X, typename std::enable_if<std::is_integral<X>::value>::type* = nullptr>
X &operator/=(X &x, const Php::Value &value) { return x /= static_cast<X>(value); }
template <typename X, typename std::enable_if<std::is_integral<X>::value>::type* = nullptr>
X &operator%=(X &x, const Php::Value &value) { return x %= static_cast<X>(value); }

/**
 *  End of namespace
 */
}