1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 #include "OCProperties.h"
9 using OC::OCReflect::property_type;
11 namespace OC { namespace OCReflect {
13 std::ostream& operator<<(std::ostream& os, const property_type& pt)
15 using ocpt = property_type;
19 case ocpt::nil: os << "nil"; break;
20 case ocpt::boolean: os << "boolean"; break;
21 case ocpt::integer: os << "integer"; break;
22 case ocpt::rational: os << "rational"; break;
23 case ocpt::string: os << "string"; break;
24 case ocpt::list: os << "list"; break;
26 case ocpt::INVALID: os << "INVALID"; break;
32 std::ostream& operator<<(std::ostream& os, const OC::OCReflect::property_attribute& pa)
34 using ocpa = OC::OCReflect::property_attribute;
38 case ocpa::r: os << 'r'; break;
39 case ocpa::w: os << 'w'; break;
40 case ocpa::rw: os << "rw"; break;
46 std::ostream& operator<<(std::ostream& os, const OC::OCReflect::property_signature& ps)
48 os << '(' << ps.type << ':' << ps.attribute << ')';
52 std::ostream& operator<<(std::ostream& os, const OC::OCReflect::property& p)
56 const auto& npb = get<0>(p);
57 const auto& npb_name = get<0>(npb);
58 const auto& npb_sig = get<1>(npb);
60 const auto& pd = std::get<1>(p);
62 os << "property \"" << npb_name << "\": " << npb_sig << "\"\n";
64 os << '\"' << npb_name << ' ' << npb_sig;
68 for(const unsigned char c : pd)
70 os << static_cast<int>(c);
83 std::ostream& operator<<(std::ostream& os, const property_type_vector& ptv)
85 for(const auto& pt : ptv)
91 std::ostream& operator<<(std::ostream& os, const OC::OCReflect::property_vector& pv)
93 for(const auto& p : pv)
99 std::ostream& operator<<(std::ostream& os, const OC::OCReflect::property_signature_vector& psv)
101 for(const auto& ps : psv)
102 os << '[' << ps.attribute << ']' << ps.type << "; ";
107 std::ostream& operator<<(std::ostream& os, const OC::OCReflect::method_signature& ms)
109 os << ms.param_signatures << " -> " << ms.ret_signature;
113 std::ostream& operator<<(std::ostream& os, const OC::OCReflect::method_binding& mb)
115 os << mb.name << " :: " << mb.signature;
119 }} // namespace OC::OCReflect
121 namespace OC { namespace OCReflect { namespace to_property { namespace detail {
123 // Convert a memory representation to bytes:
125 OC::OCReflect::property_data static_rep(const InT& in)
127 return OC::OCReflect::property_data(reinterpret_cast<const char *>(&in),
128 reinterpret_cast<const char *>(&in) + sizeof(in));
131 // Apply a typetag to a representation:
132 template <class RepT>
133 OC::OCReflect::tagged_property tag_rep(const property_type pt, const RepT& rep)
135 return OC::OCReflect::tagged_property({ pt }, { rep });
138 template <class SeqIterT>
139 OC::OCReflect::tagged_property tag_rep(const property_type pt, const SeqIterT& begin, const SeqIterT& end)
141 return OC::OCReflect::tagged_property({ pt },
145 }}}} // namespace OC::OCReflect::to_property::detail
147 namespace OC { namespace OCReflect { namespace to_property {
149 OC::OCReflect::tagged_property convert(const bool& in)
151 return detail::tag_rep(property_type::boolean, static_cast<char>(in));
154 OC::OCReflect::tagged_property convert(const int64_t& in)
156 return detail::tag_rep(property_type::integer, detail::static_rep(in));
159 // Convenience sugar for default platform int:
160 OC::OCReflect::tagged_property convert(const int& in)
162 static_assert(sizeof(int) <= sizeof(int64_t),
163 "conversion to int64_t may be dangerous on your platform: use int64_t explicitly");
165 return convert(static_cast<int64_t>(in));
168 OC::OCReflect::tagged_property convert(const double& in)
170 return detail::tag_rep(property_type::rational, detail::static_rep(in));
173 OC::OCReflect::tagged_property convert(const std::string& in)
175 return detail::tag_rep(property_type::string,
176 in.begin(), in.end());
179 }}} // namespace OC::OCReflect::to_property
181 pd_iter_tuple consume_typecheck(const property_type expected_pt, const OC::OCReflect::property_data& in)
183 OC::OCReflect::property_data::const_iterator begin = in.begin();
185 auto found_pt = static_cast<property_type>(*begin);
187 std::cout << "val ept=" << (int)expected_pt << ", ept: " << (int)found_pt << '\n';
189 if(expected_pt != found_pt)
191 std::ostringstream os;
192 os << "type error: " << "expected " << expected_pt << ", found " << found_pt;
193 throw OC::OCReflect::reflection_exception(os.str());
196 return std::forward_as_tuple(++begin, in.end());