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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <bitset>
#include <com/sun/star/container/NoSuchElementException.hpp>
#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/reflection/XConstantTypeDescription.hpp>
#include <com/sun/star/reflection/XConstantsTypeDescription.hpp>
#include <com/sun/star/reflection/XDump.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/uno/DeploymentException.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/Type.hxx>
#include <com/sun/star/uno/TypeClass.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <config_typesizes.h>
#include <cppu/unotype.hxx>
#include <cppuhelper/basemutex.hxx>
#include <cppuhelper/compbase.hxx>
#include <cppuhelper/interfacecontainer.hxx>
#include <o3tl/unreachable.hxx>
#include <osl/mutex.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
#include <sal/types.h>
#include <typelib/typedescription.h>
#include <typelib/typedescription.hxx>
#include <uno/sequence2.h>
namespace com::sun::star::uno
{
class XInterface;
}
namespace
{
template <typename T> OUString hex(T value, sal_Int32 width)
{
OUStringBuffer buf(OUString::number(value, 16));
while (buf.getLength() < width)
{
buf.insert(0, '0');
}
return buf.makeStringAndClear();
}
css::uno::TypeDescription getTypeDescription(css::uno::Type const& type)
{
typelib_TypeDescription* d = nullptr;
type.getDescription(&d);
return css::uno::TypeDescription(d);
}
OUString
getIdentifier(css::uno::Reference<css::reflection::XConstantTypeDescription> const& constant)
{
auto const n = constant->getName();
auto const i = n.lastIndexOf('.');
if (i == -1 || i == n.getLength() - 1)
{
throw css::uno::DeploymentException("bad constant name " + n);
}
return n.copy(i + 1);
}
OUString
dumpBitset(css::uno::Sequence<css::uno::Reference<css::reflection::XConstantTypeDescription>> const&
constants,
sal_uInt64 value)
{
OUStringBuffer buf;
auto a = value;
for (auto const& i : constants)
{
sal_uInt64 c;
if ((i->getConstantValue() >>= c) && std::bitset<64>{ c }.count() == 1 && (a & c) != 0)
{
if (!buf.isEmpty())
{
buf.append('+');
}
buf.append(getIdentifier(i));
a &= ~c;
}
}
return a == 0 && !buf.isEmpty() ? buf.makeStringAndClear() : OUString::number(value);
}
class Dump : public cppu::BaseMutex, public cppu::WeakComponentImplHelper<css::reflection::XDump>
{
public:
explicit Dump(css::uno::Reference<css::uno::XComponentContext> const& context)
: WeakComponentImplHelper(m_aMutex)
, manager_(context->getValueByName(
"/singletons/com.sun.star.reflection.theTypeDescriptionManager"),
css::uno::UNO_QUERY_THROW)
{
}
void SAL_CALL disposing() override
{
osl::MutexGuard g(m_aMutex);
manager_.clear();
}
OUString SAL_CALL dumpValue(css::uno::Any const& value) override
{
switch (value.getValueTypeClass())
{
case css::uno::TypeClass_VOID:
return "void";
case css::uno::TypeClass_BOOLEAN:
return OUString::boolean(value.get<bool>());
case css::uno::TypeClass_BYTE:
return OUString::number(value.get<sal_Int8>());
case css::uno::TypeClass_SHORT:
return OUString::number(value.get<sal_Int16>());
case css::uno::TypeClass_UNSIGNED_SHORT:
return OUString::number(value.get<sal_uInt16>());
case css::uno::TypeClass_LONG:
return OUString::number(value.get<sal_Int32>());
case css::uno::TypeClass_UNSIGNED_LONG:
return OUString::number(value.get<sal_uInt32>());
case css::uno::TypeClass_HYPER:
return OUString::number(value.get<sal_Int64>());
case css::uno::TypeClass_UNSIGNED_HYPER:
return OUString::number(value.get<sal_uInt64>());
case css::uno::TypeClass_FLOAT:
return OUString::number(value.get<float>());
case css::uno::TypeClass_DOUBLE:
return OUString::number(value.get<double>());
case css::uno::TypeClass_CHAR:
return "U+" + hex(value.get<sal_Unicode>(), 16);
case css::uno::TypeClass_STRING:
{
auto const s = value.get<OUString>();
OUStringBuffer buf;
for (sal_Int32 i = 0; i != s.getLength();)
{
auto const c = s.iterateCodePoints(&i);
if (c >= u8' ' && c <= u8'~')
{
if (c == u8'\"' || c == u8'\\')
{
buf.append('\\');
}
buf.append(char(c));
}
else if (c <= 0xFFFF)
{
buf.append("\\u" + hex(c, 4));
}
else
{
buf.append("\\U" + hex(c, 8));
}
}
return "\"" + buf + "\"";
}
case css::uno::TypeClass_TYPE:
return value.get<css::uno::Type>().getTypeName();
case css::uno::TypeClass_SEQUENCE:
{
css::uno::Type const t(reinterpret_cast<typelib_IndirectTypeDescription const*>(
getTypeDescription(value.getValueType()).get())
->pType);
auto const n = getTypeDescription(t).get()->nSize;
auto const s = *static_cast<uno_Sequence* const*>(value.getValue());
OUStringBuffer buf;
for (sal_Int32 i = 0; i != s->nElements; ++i)
{
if (i != 0)
{
buf.append(", ");
}
css::uno::Any const e(s->elements + i * n, t);
buf.append(t == cppu::UnoType<css::uno::Any>::get() ? dumpAny(e)
: dumpValue(e));
}
return "[" + buf + "]";
}
case css::uno::TypeClass_ENUM:
{
auto const d = getTypeDescription(value.getValueType());
auto const ed = reinterpret_cast<typelib_EnumTypeDescription const*>(d.get());
auto const e = *static_cast<sal_Int32 const*>(value.getValue());
for (sal_Int32 i = 0; i != ed->nEnumValues; ++i)
{
if (ed->pEnumValues[i] == e)
{
return OUString(ed->ppEnumNames[i]);
}
}
return OUString::number(e);
}
case css::uno::TypeClass_STRUCT:
case css::uno::TypeClass_EXCEPTION:
{
auto const d = getTypeDescription(value.getValueType());
OUStringBuffer buf;
dumpCompoundType(reinterpret_cast<typelib_CompoundTypeDescription const*>(d.get()),
value.getValue(), &buf);
return "[" + buf + "]";
}
case css::uno::TypeClass_INTERFACE:
{
auto const p = *static_cast<void* const*>(value.getValue());
return p == nullptr ? OUString("null")
: OUString("0x"
+ hex(reinterpret_cast<sal_uIntPtr>(p),
SAL_TYPES_SIZEOFPOINTER * 2));
}
default:
O3TL_UNREACHABLE;
}
}
OUString SAL_CALL dumpAny(css::uno::Any const& value) override
{
return "[" + value.getValueType().getTypeName() + ": " + dumpValue(value) + "]";
}
OUString SAL_CALL dumpConstant(OUString const& constantsGroup,
css::uno::Any const& value) override
{
css::uno::Reference<css::container::XHierarchicalNameAccess> manager;
{
osl::MutexGuard g(m_aMutex);
if (rBHelper.bDisposed)
{
throw css::lang::DisposedException("css.reflection.Dumper");
}
manager = manager_;
}
css::uno::Reference<css::reflection::XConstantsTypeDescription> g;
try
{
manager_->getByHierarchicalName(constantsGroup) >>= g;
}
catch (css::container::NoSuchElementException)
{
}
if (!g.is())
{
throw css::lang::IllegalArgumentException("not a constants group: " + constantsGroup,
{}, 0);
}
auto const s = g->getConstants();
switch (value.getValueTypeClass())
{
case css::uno::TypeClass_BOOLEAN:
for (auto const& i : s)
{
if (i->getConstantValue() == value)
{
return getIdentifier(i);
}
}
return OUString::boolean(value.get<bool>());
case css::uno::TypeClass_BYTE:
case css::uno::TypeClass_SHORT:
case css::uno::TypeClass_LONG:
case css::uno::TypeClass_HYPER:
{
auto const v = value.get<sal_Int64>();
for (auto const& i : s)
{
sal_Int64 c;
if ((i->getConstantValue() >>= c) && c == v)
{
return getIdentifier(i);
}
}
return v >= 0 ? dumpBitset(s, v) : OUString::number(v);
}
case css::uno::TypeClass_UNSIGNED_SHORT:
case css::uno::TypeClass_UNSIGNED_LONG:
case css::uno::TypeClass_UNSIGNED_HYPER:
{
auto const v = value.get<sal_uInt64>();
for (auto const& i : s)
{
sal_uInt64 c;
if ((i->getConstantValue() >>= c) && c == v)
{
return getIdentifier(i);
}
}
return dumpBitset(s, v);
}
case css::uno::TypeClass_FLOAT:
case css::uno::TypeClass_DOUBLE:
{
auto const v = value.get<double>();
for (auto const& i : s)
{
double c;
if ((i->getConstantValue() >>= c) && c == v)
{
return getIdentifier(i);
}
}
return OUString::number(v);
}
default:
throw css::lang::IllegalArgumentException(
"not a numeric type: " + value.getValueTypeName(), {}, 1);
}
}
private:
css::uno::Reference<css::container::XHierarchicalNameAccess> manager_;
void dumpCompoundType(typelib_CompoundTypeDescription const* description, void const* data,
OUStringBuffer* buffer)
{
if (auto base = description->pBaseTypeDescription)
{
dumpCompoundType(base, data, buffer);
}
for (sal_Int32 i = 0; i != description->nMembers; ++i)
{
if (!buffer->isEmpty())
{
buffer->append(", ");
}
buffer->append(OUString::unacquired(description->ppMemberNames + i) + ": ");
css::uno::Type t(description->ppTypeRefs[i]);
css::uno::Any const m(static_cast<char const*>(data) + description->pMemberOffsets[i],
t);
buffer->append(t == cppu::UnoType<css::uno::Any>::get() ? dumpAny(m) : dumpValue(m));
}
}
};
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
com_sun_star_comp_stoc_Dump_get_implementation(css::uno::XComponentContext* context,
css::uno::Sequence<css::uno::Any> const& arguments)
{
SAL_WARN_IF(arguments.hasElements(), "stoc", "unexpected singleton arguments");
return cppu::acquire(new Dump(context));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|