/usr/include/Poco/MongoDB/Element.h is in libpoco-dev 1.8.0.1-1ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | //
// Element.h
//
// Library: MongoDB
// Package: MongoDB
// Module: Element
//
// Definition of the Element class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef MongoDB_Element_INCLUDED
#define MongoDB_Element_INCLUDED
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include "Poco/SharedPtr.h"
#include "Poco/Timestamp.h"
#include "Poco/Nullable.h"
#include "Poco/NumberFormatter.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/UTF8String.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/BSONReader.h"
#include "Poco/MongoDB/BSONWriter.h"
#include <string>
#include <sstream>
#include <iomanip>
#include <list>
namespace Poco {
namespace MongoDB {
class MongoDB_API Element
/// Represents an Element of a Document or an Array.
{
public:
typedef Poco::SharedPtr<Element> Ptr;
explicit Element(const std::string& name);
/// Creates the Element with the given name.
virtual ~Element();
/// Destructor
const std::string& name() const;
/// Returns the name of the element.
virtual std::string toString(int indent = 0) const = 0;
/// Returns a string representation of the element.
virtual int type() const = 0;
/// Returns the MongoDB type of the element.
private:
virtual void read(BinaryReader& reader) = 0;
virtual void write(BinaryWriter& writer) = 0;
friend class Document;
std::string _name;
};
//
// inlines
//
inline const std::string& Element::name() const
{
return _name;
}
typedef std::list<Element::Ptr> ElementSet;
template<typename T>
struct ElementTraits
{
};
// BSON Floating point
// spec: double
template<>
struct ElementTraits<double>
{
enum { TypeId = 0x01 };
static std::string toString(const double& value, int indent = 0)
{
return Poco::NumberFormatter::format(value);
}
};
// BSON UTF-8 string
// spec: int32 (byte*) "\x00"
// int32 is the number bytes in byte* + 1 (for trailing "\x00")
template<>
struct ElementTraits<std::string>
{
enum { TypeId = 0x02 };
static std::string toString(const std::string& value, int indent = 0)
{
std::ostringstream oss;
oss << '"';
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
{
switch (*it)
{
case '"':
oss << "\\\"";
break;
case '\\':
oss << "\\\\";
break;
case '\b':
oss << "\\b";
break;
case '\f':
oss << "\\f";
break;
case '\n':
oss << "\\n";
break;
case '\r':
oss << "\\r";
break;
case '\t':
oss << "\\t";
break;
default:
{
if ( *it > 0 && *it <= 0x1F )
{
oss << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*it);
}
else
{
oss << *it;
}
break;
}
}
}
oss << '"';
return oss.str();
}
};
template<>
inline void BSONReader::read<std::string>(std::string& to)
{
Poco::Int32 size;
_reader >> size;
_reader.readRaw(size, to);
to.erase(to.end() - 1); // remove terminating 0
}
template<>
inline void BSONWriter::write<std::string>(std::string& from)
{
_writer << (Poco::Int32) (from.length() + 1);
writeCString(from);
}
// BSON bool
// spec: "\x00" "\x01"
template<>
struct ElementTraits<bool>
{
enum { TypeId = 0x08 };
static std::string toString(const bool& value, int indent = 0)
{
return value ? "true" : "false";
}
};
template<>
inline void BSONReader::read<bool>(bool& to)
{
unsigned char b;
_reader >> b;
to = b != 0;
}
template<>
inline void BSONWriter::write<bool>(bool& from)
{
unsigned char b = from ? 0x01 : 0x00;
_writer << b;
}
// BSON 32-bit integer
// spec: int32
template<>
struct ElementTraits<Int32>
{
enum { TypeId = 0x10 };
static std::string toString(const Int32& value, int indent = 0)
{
return Poco::NumberFormatter::format(value);
}
};
// BSON UTC datetime
// spec: int64
template<>
struct ElementTraits<Timestamp>
{
enum { TypeId = 0x09 };
static std::string toString(const Timestamp& value, int indent = 0)
{
std::string result;
result.append(1, '"');
result.append(DateTimeFormatter::format(value, "%Y-%m-%dT%H:%M:%s%z"));
result.append(1, '"');
return result;
}
};
template<>
inline void BSONReader::read<Timestamp>(Timestamp& to)
{
Poco::Int64 value;
_reader >> value;
to = Timestamp::fromEpochTime(static_cast<std::time_t>(value / 1000));
to += (value % 1000 * 1000);
}
template<>
inline void BSONWriter::write<Timestamp>(Timestamp& from)
{
_writer << (from.epochMicroseconds() / 1000);
}
typedef Nullable<unsigned char> NullValue;
// BSON Null Value
// spec:
template<>
struct ElementTraits<NullValue>
{
enum { TypeId = 0x0A };
static std::string toString(const NullValue& value, int indent = 0)
{
return "null";
}
};
template<>
inline void BSONReader::read<NullValue>(NullValue& to)
{
}
template<>
inline void BSONWriter::write<NullValue>(NullValue& from)
{
}
struct BSONTimestamp
{
Poco::Timestamp ts;
Poco::Int32 inc;
};
// BSON Timestamp
// spec: int64
template<>
struct ElementTraits<BSONTimestamp>
{
enum { TypeId = 0x11 };
static std::string toString(const BSONTimestamp& value, int indent = 0)
{
std::string result;
result.append(1, '"');
result.append(DateTimeFormatter::format(value.ts, "%Y-%m-%dT%H:%M:%s%z"));
result.append(1, ' ');
result.append(NumberFormatter::format(value.inc));
result.append(1, '"');
return result;
}
};
template<>
inline void BSONReader::read<BSONTimestamp>(BSONTimestamp& to)
{
Poco::Int64 value;
_reader >> value;
to.inc = value & 0xffffffff;
value >>= 32;
to.ts = Timestamp::fromEpochTime(static_cast<std::time_t>(value));
}
template<>
inline void BSONWriter::write<BSONTimestamp>(BSONTimestamp& from)
{
Poco::Int64 value = from.ts.epochMicroseconds() / 1000;
value <<= 32;
value += from.inc;
_writer << value;
}
// BSON 64-bit integer
// spec: int64
template<>
struct ElementTraits<Int64>
{
enum { TypeId = 0x12 };
static std::string toString(const Int64& value, int indent = 0)
{
return NumberFormatter::format(value);
}
};
template<typename T>
class ConcreteElement: public Element
{
public:
ConcreteElement(const std::string& name, const T& init):
Element(name),
_value(init)
{
}
virtual ~ConcreteElement()
{
}
T value() const
{
return _value;
}
std::string toString(int indent = 0) const
{
return ElementTraits<T>::toString(_value, indent);
}
int type() const
{
return ElementTraits<T>::TypeId;
}
void read(BinaryReader& reader)
{
BSONReader(reader).read(_value);
}
void write(BinaryWriter& writer)
{
BSONWriter(writer).write(_value);
}
private:
T _value;
};
} } // namespace Poco::MongoDB
#endif // MongoDB_Element_INCLUDED
|