Shaka Player Embedded
any.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_MAPPING_ANY_H_
16 #define SHAKA_EMBEDDED_MAPPING_ANY_H_
17 
18 #include <string>
19 
20 #include "src/mapping/convert_js.h"
24 
25 namespace shaka {
26 
31 class Any : public GenericConverter, public memory::Traceable {
32  public:
33  static std::string name() {
34  return "anything";
35  }
36 
37  Any();
38  explicit Any(std::nullptr_t);
39  template <typename T>
40  explicit Any(const T& value) {
41  value_ = ::shaka::ToJsValue(value);
42  is_number_ = GetValueType(value_.handle()) == proto::ValueType::Number;
43  }
44  ~Any() override;
45 
46  // Chromium style complains without these.
47  Any(const Any&);
48  Any(Any&&);
49  Any& operator=(const Any&);
50  Any& operator=(Any&&);
51 
62  bool IsTruthy() const;
63 
71  template <typename T>
72  bool TryConvertTo(T* result) const {
73  return FromJsValue(value_.handle(), result);
74  }
75 
76  bool TryConvert(Handle<JsValue> value) override;
77  ReturnVal<JsValue> ToJsValue() const override;
78  void Trace(memory::HeapTracer* tracer) const override;
79 
80  private:
81  WeakJsPtr<JsValue> value_;
82  bool is_number_;
83 };
84 
85 } // namespace shaka
86 
87 #endif // SHAKA_EMBEDDED_MAPPING_ANY_H_
Definition: any.h:31
bool FromJsValue(Handle< JsValue > source, T *dest)
Definition: convert_js.h:370
~Any() override
Definition: any.cc:25
bool TryConvertTo(T *result) const
Definition: any.h:72
static std::string name()
Definition: any.h:33
ReturnVal< JsValue > ToJsValue(T &&source)
Definition: convert_js.h:381
Any()
Definition: any.cc:23
bool IsTruthy() const
Definition: any.cc:32
Any(const T &value)
Definition: any.h:40
proto::ValueType GetValueType(Handle< JsValue > value)
Definition: js_wrappers.cc:329
void Trace(memory::HeapTracer *tracer) const override
Definition: any.cc:60
Any & operator=(const Any &)
ReturnVal< JsValue > ToJsValue() const override
Definition: any.cc:56
Handle< T > handle() const
Definition: weak_js_ptr.h:87
bool TryConvert(Handle< JsValue > value) override
Definition: any.cc:50