|
| 1 | +// |
| 2 | +// MessagePack for C++ static resolution routine |
| 3 | +// |
| 4 | +// Copyright (C) 2015 KONDO Takatoshi |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +#ifndef MSGPACK_TYPE_VECTOR_BOOL_HPP |
| 19 | +#define MSGPACK_TYPE_VECTOR_BOOL_HPP |
| 20 | + |
| 21 | +#include "msgpack/versioning.hpp" |
| 22 | +#include "msgpack/object_fwd.hpp" |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace msgpack { |
| 26 | + |
| 27 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 28 | + |
| 29 | +inline object const& operator>> (object const& o, std::vector<bool>& v) |
| 30 | +{ |
| 31 | + if (o.type != type::ARRAY) { throw type_error(); } |
| 32 | + if (o.via.array.size > 0) { |
| 33 | + v.resize(o.via.array.size); |
| 34 | + object* p = o.via.array.ptr; |
| 35 | + for (std::vector<bool>::iterator it = v.begin(), end = v.end(); |
| 36 | + it != end; |
| 37 | + ++it) { |
| 38 | + *it = p->as<bool>(); |
| 39 | + ++p; |
| 40 | + } |
| 41 | + } |
| 42 | + return o; |
| 43 | +} |
| 44 | + |
| 45 | +template <typename Stream> |
| 46 | +inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<bool>& v) |
| 47 | +{ |
| 48 | + o.pack_array(v.size()); |
| 49 | + for(std::vector<bool>::const_iterator it(v.begin()), it_end(v.end()); |
| 50 | + it != it_end; ++it) { |
| 51 | + o.pack(static_cast<bool>(*it)); |
| 52 | + } |
| 53 | + return o; |
| 54 | +} |
| 55 | + |
| 56 | +inline void operator<< (object::with_zone& o, const std::vector<bool>& v) |
| 57 | +{ |
| 58 | + o.type = type::ARRAY; |
| 59 | + if(v.empty()) { |
| 60 | + o.via.array.ptr = nullptr; |
| 61 | + o.via.array.size = 0; |
| 62 | + } else { |
| 63 | + object* p = static_cast<object*>(o.zone.allocate_align(sizeof(object)*v.size())); |
| 64 | + object* const pend = p + v.size(); |
| 65 | + o.via.array.ptr = p; |
| 66 | + o.via.array.size = v.size(); |
| 67 | + std::vector<bool>::const_iterator it(v.begin()); |
| 68 | + do { |
| 69 | + *p = object(static_cast<bool>(*it), o.zone); |
| 70 | + ++p; |
| 71 | + ++it; |
| 72 | + } while(p < pend); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 77 | + |
| 78 | +} // namespace msgpack |
| 79 | + |
| 80 | +#endif // MSGPACK_TYPE_VECTOR_BOOL_HPP |
0 commit comments