9 static const char rcsid[] __attribute__((used)) =
"$Id: Binary.cpp 1702 2016-08-13 23:26:06Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 Binary::Binary() throw () :
Nullable(std::make_shared<std::vector<uint8_t>>()) { }
18 Binary::Binary(
const std::string &value)
throw (TypeCastException) :
Nullable() {
19 if (boost::regex_match(value, Binary::Pattern())) {
21 char buf[3] = { 0, 0, 0 };
22 this->value = std::make_shared<std::vector<uint8_t>>();
24 for (
auto it = value.begin() + 2, end = value.end(); it != end; ++it, ++it) {
28 sscanf(buf,
"%hx", &v);
30 (*std::static_pointer_cast<std::vector<uint8_t>>(this->value)).push_back((uint8_t) v);
33 THROW(TypeCastException,
"unable to cast '%s' to a binary value", value.c_str());
37 Binary::Binary(
const std::vector<uint8_t> &value)
throw () :
Nullable(std::make_shared<std::vector<uint8_t>>(value)) { }
41 if (!other.isNull()) {
43 this->value = std::make_shared<std::vector<uint8_t>>(other.get());
45 *std::static_pointer_cast<std::vector<uint8_t>>(this->value) = *std::static_pointer_cast<std::vector<uint8_t>>(other.value);
53 bool Binary::operator==(
const Binary &other)
const throw () {
54 return (*std::static_pointer_cast<std::vector<uint8_t>>(this->value) == *std::static_pointer_cast<std::vector<uint8_t>>(other.value));
57 bool Binary::operator!=(
const Binary &other)
const throw () {
58 return !(*
this == other);
61 Binary& Binary::operator=(
const Binary &other)
throw () {
62 if (LIKELY(
this != &other)) {
63 if (!other.isNull()) {
65 this->value = std::make_shared<std::vector<uint8_t>>(other.get());
67 *std::static_pointer_cast<std::vector<uint8_t>>(this->value) = *std::static_pointer_cast<std::vector<uint8_t>>(other.value);
77 Binary::~Binary() throw () { }
79 const std::vector<uint8_t>& Binary::get()
const throw (UnsupportedOperatorException) {
81 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
84 return *std::static_pointer_cast<std::vector<uint8_t>>(this->value);
87 std::vector<uint8_t>& Binary::get() throw (UnsupportedOperatorException) {
89 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
92 return *std::static_pointer_cast<std::vector<uint8_t>>(this->value);
95 size_t Binary::size()
const throw () {
103 bool Binary::empty()
const throw () {
108 return get().empty();
111 void Binary::clear() throw() {
113 this->value = std::make_shared<std::vector<uint8_t>>();
115 std::static_pointer_cast<std::vector<uint8_t>>(this->value)->clear();
123 if (UNLIKELY(isNull())) {
124 c = mpack.append(offset);
128 if (UNLIKELY(c == 0)) {
129 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
132 auto &vector = this->
get();
134 c = mpack.append_vector(offset, vector.size());
138 if (UNLIKELY(c == 0)) {
139 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
142 for (
auto v = vector.begin(), end = vector.end(); v != end; ++v) {
143 c = mpack.append(offset, (uint64_t) *v);
147 if (UNLIKELY(c == 0)) {
148 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
156 uint32_t c, o = offset;
158 if (UNLIKELY(isNull())) {
159 if (UNLIKELY((c = mpack.append(offset)) == 0)) {
160 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
165 auto &vector = this->
get();
167 if (UNLIKELY((c = mpack.append_vector(offset, vector.size())) == 0)) {
168 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
173 for (
auto v = vector.begin(), end = vector.end(); v != end; ++v) {
174 if (UNLIKELY((c = mpack.append(offset, (uint64_t) *v)) == 0)) {
175 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
186 void Binary::msgpackBufferSize(
size_t &size)
const throw () {
187 if (UNLIKELY(isNull())) {
193 size += 5 + std::static_pointer_cast<std::vector<uint8_t>>(this->value)->size();
196 std::string Binary::str(
int type)
const throw () {
197 if (UNLIKELY(isNull())) {
202 std::string output(
"0x");
203 const std::vector<uint8_t> &data = this->
get();
205 for (
auto it = data.begin(), end = data.end(); it != end; ++it) {
206 snprintf(buffer,
sizeof(buffer),
"%02x", *it);
207 output.append(buffer);
213 bool Binary::isType(
const char *value,
size_t size)
throw () {
214 if (size < 2 || value[0] !=
'0' || value[1] !=
'x' || size % 2 != 0) {
218 for (
size_t i = 2; i < size; i += 2) {
219 if (!::isxdigit(value[i]) != 0 || !::isxdigit(value[i + 1]) != 0) {