/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
new (getAddress<ObjectImpl>()) ObjectImpl();
}
+inline dynamic::dynamic(StringPiece s)
+ : type_(STRING)
+{
+ new (&u_.string) fbstring(s.data(), s.size());
+}
+
inline dynamic::dynamic(char const* s)
: type_(STRING)
{
new (&u_.string) fbstring(s);
}
+inline dynamic::dynamic(fbstring const& s)
+ : type_(STRING)
+{
+ new (&u_.string) fbstring(s);
+}
+
+inline dynamic::dynamic(fbstring&& s)
+ : type_(STRING)
+{
+ new (&u_.string) fbstring(std::move(s));
+}
+
inline dynamic::dynamic(std::initializer_list<dynamic> il)
: type_(ARRAY)
{
inline int64_t dynamic::asInt() const { return asImpl<int64_t>(); }
inline bool dynamic::asBool() const { return asImpl<bool>(); }
+inline const char* dynamic::data() const { return get<fbstring>().data(); }
+inline const char* dynamic::c_str() const { return get<fbstring>().c_str(); }
+
template<class T>
struct dynamic::CompareOp {
static bool comp(T const& a, T const& b) { return a < b; }
const dynamic& val_;
};
-}
+template <class V>
+class FormatValue<detail::DefaultValueWrapper<dynamic, V>> {
+ public:
+ explicit FormatValue(
+ const detail::DefaultValueWrapper<dynamic, V>& val)
+ : val_(val) { }
+
+ template <class FormatCallback>
+ void format(FormatArg& arg, FormatCallback& cb) const {
+ auto& c = val_.container;
+ switch (c.type()) {
+ case dynamic::NULLT:
+ case dynamic::BOOL:
+ case dynamic::INT64:
+ case dynamic::STRING:
+ case dynamic::DOUBLE:
+ FormatValue<dynamic>(c).format(arg, cb);
+ break;
+ case dynamic::ARRAY:
+ {
+ int key = arg.splitIntKey();
+ if (key >= 0 && key < c.size()) {
+ FormatValue<dynamic>(c.at(key)).format(arg, cb);
+ } else{
+ FormatValue<V>(val_.defaultValue).format(arg, cb);
+ }
+ }
+ break;
+ case dynamic::OBJECT:
+ {
+ auto pos = c.find(arg.splitKey());
+ if (pos != c.items().end()) {
+ FormatValue<dynamic>(pos->second).format(arg, cb);
+ } else {
+ FormatValue<V>(val_.defaultValue).format(arg, cb);
+ }
+ }
+ break;
+ }
+ }
+
+ private:
+ const detail::DefaultValueWrapper<dynamic, V>& val_;
+};
+
+} // namespaces
#undef FB_DYNAMIC_APPLY