Commit a8266ccb0ef67d07cfa92b5669b238012195d94f
Committed by
Jay Berkenbilt
1 parent
39bbaa86
Added public assert{Type} methods to QPDFObjectHandle
Showing
2 changed files
with
106 additions
and
31 deletions
include/qpdf/QPDFObjectHandle.hh
| ... | ... | @@ -410,6 +410,28 @@ class QPDFObjectHandle |
| 410 | 410 | |
| 411 | 411 | // Convenience routine: Throws if the assumption is violated. |
| 412 | 412 | QPDF_DLL |
| 413 | + void assertInitialized() const; | |
| 414 | + | |
| 415 | + QPDF_DLL | |
| 416 | + void assertNull(); | |
| 417 | + QPDF_DLL | |
| 418 | + void assertBool(); | |
| 419 | + QPDF_DLL | |
| 420 | + void assertInteger(); | |
| 421 | + QPDF_DLL | |
| 422 | + void assertReal(); | |
| 423 | + QPDF_DLL | |
| 424 | + void assertName(); | |
| 425 | + QPDF_DLL | |
| 426 | + void assertString(); | |
| 427 | + QPDF_DLL | |
| 428 | + void assertArray(); | |
| 429 | + QPDF_DLL | |
| 430 | + void assertDictionary(); | |
| 431 | + QPDF_DLL | |
| 432 | + void assertStream(); | |
| 433 | + | |
| 434 | + QPDF_DLL | |
| 413 | 435 | void assertPageObject(); |
| 414 | 436 | |
| 415 | 437 | private: |
| ... | ... | @@ -422,8 +444,7 @@ class QPDFObjectHandle |
| 422 | 444 | QPDF* qpdf, int objid, int generation, |
| 423 | 445 | QPDFObjectHandle stream_dict, qpdf_offset_t offset, size_t length); |
| 424 | 446 | |
| 425 | - void assertInitialized() const; | |
| 426 | - void assertType(char const* type_name, bool istype); | |
| 447 | + void assertType(char const* type_name, bool istype) const; | |
| 427 | 448 | void dereference(); |
| 428 | 449 | void makeDirectInternal(std::set<int>& visited); |
| 429 | 450 | void releaseResolved(); | ... | ... |
libqpdf/QPDFObjectHandle.cc
| ... | ... | @@ -183,7 +183,7 @@ QPDFObjectHandle::isScalar() |
| 183 | 183 | bool |
| 184 | 184 | QPDFObjectHandle::getBoolValue() |
| 185 | 185 | { |
| 186 | - assertType("Boolean", isBool()); | |
| 186 | + assertBool(); | |
| 187 | 187 | return dynamic_cast<QPDF_Bool*>(obj.getPointer())->getVal(); |
| 188 | 188 | } |
| 189 | 189 | |
| ... | ... | @@ -192,7 +192,7 @@ QPDFObjectHandle::getBoolValue() |
| 192 | 192 | long long |
| 193 | 193 | QPDFObjectHandle::getIntValue() |
| 194 | 194 | { |
| 195 | - assertType("Integer", isInteger()); | |
| 195 | + assertInteger(); | |
| 196 | 196 | return dynamic_cast<QPDF_Integer*>(obj.getPointer())->getVal(); |
| 197 | 197 | } |
| 198 | 198 | |
| ... | ... | @@ -201,7 +201,7 @@ QPDFObjectHandle::getIntValue() |
| 201 | 201 | std::string |
| 202 | 202 | QPDFObjectHandle::getRealValue() |
| 203 | 203 | { |
| 204 | - assertType("Real", isReal()); | |
| 204 | + assertReal(); | |
| 205 | 205 | return dynamic_cast<QPDF_Real*>(obj.getPointer())->getVal(); |
| 206 | 206 | } |
| 207 | 207 | |
| ... | ... | @@ -210,7 +210,7 @@ QPDFObjectHandle::getRealValue() |
| 210 | 210 | std::string |
| 211 | 211 | QPDFObjectHandle::getName() |
| 212 | 212 | { |
| 213 | - assertType("Name", isName()); | |
| 213 | + assertName(); | |
| 214 | 214 | return dynamic_cast<QPDF_Name*>(obj.getPointer())->getName(); |
| 215 | 215 | } |
| 216 | 216 | |
| ... | ... | @@ -219,14 +219,14 @@ QPDFObjectHandle::getName() |
| 219 | 219 | std::string |
| 220 | 220 | QPDFObjectHandle::getStringValue() |
| 221 | 221 | { |
| 222 | - assertType("String", isString()); | |
| 222 | + assertString(); | |
| 223 | 223 | return dynamic_cast<QPDF_String*>(obj.getPointer())->getVal(); |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | std::string |
| 227 | 227 | QPDFObjectHandle::getUTF8Value() |
| 228 | 228 | { |
| 229 | - assertType("String", isString()); | |
| 229 | + assertString(); | |
| 230 | 230 | return dynamic_cast<QPDF_String*>(obj.getPointer())->getUTF8Val(); |
| 231 | 231 | } |
| 232 | 232 | |
| ... | ... | @@ -235,21 +235,21 @@ QPDFObjectHandle::getUTF8Value() |
| 235 | 235 | int |
| 236 | 236 | QPDFObjectHandle::getArrayNItems() |
| 237 | 237 | { |
| 238 | - assertType("Array", isArray()); | |
| 238 | + assertArray(); | |
| 239 | 239 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->getNItems(); |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | QPDFObjectHandle |
| 243 | 243 | QPDFObjectHandle::getArrayItem(int n) |
| 244 | 244 | { |
| 245 | - assertType("Array", isArray()); | |
| 245 | + assertArray(); | |
| 246 | 246 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->getItem(n); |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | std::vector<QPDFObjectHandle> |
| 250 | 250 | QPDFObjectHandle::getArrayAsVector() |
| 251 | 251 | { |
| 252 | - assertType("Array", isArray()); | |
| 252 | + assertArray(); | |
| 253 | 253 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->getAsVector(); |
| 254 | 254 | } |
| 255 | 255 | |
| ... | ... | @@ -258,35 +258,35 @@ QPDFObjectHandle::getArrayAsVector() |
| 258 | 258 | void |
| 259 | 259 | QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) |
| 260 | 260 | { |
| 261 | - assertType("Array", isArray()); | |
| 261 | + assertArray(); | |
| 262 | 262 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->setItem(n, item); |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | void |
| 266 | 266 | QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items) |
| 267 | 267 | { |
| 268 | - assertType("Array", isArray()); | |
| 268 | + assertArray(); | |
| 269 | 269 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->setFromVector(items); |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | void |
| 273 | 273 | QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item) |
| 274 | 274 | { |
| 275 | - assertType("Array", isArray()); | |
| 275 | + assertArray(); | |
| 276 | 276 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->insertItem(at, item); |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | void |
| 280 | 280 | QPDFObjectHandle::appendItem(QPDFObjectHandle const& item) |
| 281 | 281 | { |
| 282 | - assertType("Array", isArray()); | |
| 282 | + assertArray(); | |
| 283 | 283 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->appendItem(item); |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | void |
| 287 | 287 | QPDFObjectHandle::eraseItem(int at) |
| 288 | 288 | { |
| 289 | - assertType("Array", isArray()); | |
| 289 | + assertArray(); | |
| 290 | 290 | return dynamic_cast<QPDF_Array*>(obj.getPointer())->eraseItem(at); |
| 291 | 291 | } |
| 292 | 292 | |
| ... | ... | @@ -295,28 +295,28 @@ QPDFObjectHandle::eraseItem(int at) |
| 295 | 295 | bool |
| 296 | 296 | QPDFObjectHandle::hasKey(std::string const& key) |
| 297 | 297 | { |
| 298 | - assertType("Dictionary", isDictionary()); | |
| 298 | + assertDictionary(); | |
| 299 | 299 | return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->hasKey(key); |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | QPDFObjectHandle |
| 303 | 303 | QPDFObjectHandle::getKey(std::string const& key) |
| 304 | 304 | { |
| 305 | - assertType("Dictionary", isDictionary()); | |
| 305 | + assertDictionary(); | |
| 306 | 306 | return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getKey(key); |
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | std::set<std::string> |
| 310 | 310 | QPDFObjectHandle::getKeys() |
| 311 | 311 | { |
| 312 | - assertType("Dictionary", isDictionary()); | |
| 312 | + assertDictionary(); | |
| 313 | 313 | return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getKeys(); |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | std::map<std::string, QPDFObjectHandle> |
| 317 | 317 | QPDFObjectHandle::getDictAsMap() |
| 318 | 318 | { |
| 319 | - assertType("Dictionary", isDictionary()); | |
| 319 | + assertDictionary(); | |
| 320 | 320 | return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getAsMap(); |
| 321 | 321 | } |
| 322 | 322 | |
| ... | ... | @@ -349,7 +349,7 @@ void |
| 349 | 349 | QPDFObjectHandle::replaceKey(std::string const& key, |
| 350 | 350 | QPDFObjectHandle const& value) |
| 351 | 351 | { |
| 352 | - assertType("Dictionary", isDictionary()); | |
| 352 | + assertDictionary(); | |
| 353 | 353 | return dynamic_cast<QPDF_Dictionary*>( |
| 354 | 354 | obj.getPointer())->replaceKey(key, value); |
| 355 | 355 | } |
| ... | ... | @@ -357,7 +357,7 @@ QPDFObjectHandle::replaceKey(std::string const& key, |
| 357 | 357 | void |
| 358 | 358 | QPDFObjectHandle::removeKey(std::string const& key) |
| 359 | 359 | { |
| 360 | - assertType("Dictionary", isDictionary()); | |
| 360 | + assertDictionary(); | |
| 361 | 361 | return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->removeKey(key); |
| 362 | 362 | } |
| 363 | 363 | |
| ... | ... | @@ -365,7 +365,7 @@ void |
| 365 | 365 | QPDFObjectHandle::replaceOrRemoveKey(std::string const& key, |
| 366 | 366 | QPDFObjectHandle value) |
| 367 | 367 | { |
| 368 | - assertType("Dictionary", isDictionary()); | |
| 368 | + assertDictionary(); | |
| 369 | 369 | return dynamic_cast<QPDF_Dictionary*>( |
| 370 | 370 | obj.getPointer())->replaceOrRemoveKey(key, value); |
| 371 | 371 | } |
| ... | ... | @@ -374,21 +374,21 @@ QPDFObjectHandle::replaceOrRemoveKey(std::string const& key, |
| 374 | 374 | QPDFObjectHandle |
| 375 | 375 | QPDFObjectHandle::getDict() |
| 376 | 376 | { |
| 377 | - assertType("Stream", isStream()); | |
| 377 | + assertStream(); | |
| 378 | 378 | return dynamic_cast<QPDF_Stream*>(obj.getPointer())->getDict(); |
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | PointerHolder<Buffer> |
| 382 | 382 | QPDFObjectHandle::getStreamData() |
| 383 | 383 | { |
| 384 | - assertType("Stream", isStream()); | |
| 384 | + assertStream(); | |
| 385 | 385 | return dynamic_cast<QPDF_Stream*>(obj.getPointer())->getStreamData(); |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | PointerHolder<Buffer> |
| 389 | 389 | QPDFObjectHandle::getRawStreamData() |
| 390 | 390 | { |
| 391 | - assertType("Stream", isStream()); | |
| 391 | + assertStream(); | |
| 392 | 392 | return dynamic_cast<QPDF_Stream*>(obj.getPointer())->getRawStreamData(); |
| 393 | 393 | } |
| 394 | 394 | |
| ... | ... | @@ -396,7 +396,7 @@ bool |
| 396 | 396 | QPDFObjectHandle::pipeStreamData(Pipeline* p, bool filter, |
| 397 | 397 | bool normalize, bool compress) |
| 398 | 398 | { |
| 399 | - assertType("Stream", isStream()); | |
| 399 | + assertStream(); | |
| 400 | 400 | return dynamic_cast<QPDF_Stream*>(obj.getPointer())->pipeStreamData( |
| 401 | 401 | p, filter, normalize, compress); |
| 402 | 402 | } |
| ... | ... | @@ -406,7 +406,7 @@ QPDFObjectHandle::replaceStreamData(PointerHolder<Buffer> data, |
| 406 | 406 | QPDFObjectHandle const& filter, |
| 407 | 407 | QPDFObjectHandle const& decode_parms) |
| 408 | 408 | { |
| 409 | - assertType("Stream", isStream()); | |
| 409 | + assertStream(); | |
| 410 | 410 | dynamic_cast<QPDF_Stream*>(obj.getPointer())->replaceStreamData( |
| 411 | 411 | data, filter, decode_parms); |
| 412 | 412 | } |
| ... | ... | @@ -416,7 +416,7 @@ QPDFObjectHandle::replaceStreamData(PointerHolder<StreamDataProvider> provider, |
| 416 | 416 | QPDFObjectHandle const& filter, |
| 417 | 417 | QPDFObjectHandle const& decode_parms) |
| 418 | 418 | { |
| 419 | - assertType("Stream", isStream()); | |
| 419 | + assertStream(); | |
| 420 | 420 | dynamic_cast<QPDF_Stream*>(obj.getPointer())->replaceStreamData( |
| 421 | 421 | provider, filter, decode_parms); |
| 422 | 422 | } |
| ... | ... | @@ -524,7 +524,7 @@ void |
| 524 | 524 | QPDFObjectHandle::addPageContents(QPDFObjectHandle new_contents, bool first) |
| 525 | 525 | { |
| 526 | 526 | assertPageObject(); |
| 527 | - new_contents.assertType("Stream", new_contents.isStream()); | |
| 527 | + new_contents.assertStream(); | |
| 528 | 528 | |
| 529 | 529 | std::vector<QPDFObjectHandle> orig_contents = getPageContents(); |
| 530 | 530 | |
| ... | ... | @@ -839,7 +839,7 @@ QPDFObjectHandle::assertInitialized() const |
| 839 | 839 | } |
| 840 | 840 | |
| 841 | 841 | void |
| 842 | -QPDFObjectHandle::assertType(char const* type_name, bool istype) | |
| 842 | +QPDFObjectHandle::assertType(char const* type_name, bool istype) const | |
| 843 | 843 | { |
| 844 | 844 | if (! istype) |
| 845 | 845 | { |
| ... | ... | @@ -849,6 +849,60 @@ QPDFObjectHandle::assertType(char const* type_name, bool istype) |
| 849 | 849 | } |
| 850 | 850 | |
| 851 | 851 | void |
| 852 | +QPDFObjectHandle::assertNull() | |
| 853 | +{ | |
| 854 | + assertType("Null", isNull()); | |
| 855 | +} | |
| 856 | + | |
| 857 | +void | |
| 858 | +QPDFObjectHandle::assertBool() | |
| 859 | +{ | |
| 860 | + assertType("Boolean", isBool()); | |
| 861 | +} | |
| 862 | + | |
| 863 | +void | |
| 864 | +QPDFObjectHandle::assertInteger() | |
| 865 | +{ | |
| 866 | + assertType("Integer", isInteger()); | |
| 867 | +} | |
| 868 | + | |
| 869 | +void | |
| 870 | +QPDFObjectHandle::assertReal() | |
| 871 | +{ | |
| 872 | + assertType("Real", isReal()); | |
| 873 | +} | |
| 874 | + | |
| 875 | +void | |
| 876 | +QPDFObjectHandle::assertName() | |
| 877 | +{ | |
| 878 | + assertType("Name", isName()); | |
| 879 | +} | |
| 880 | + | |
| 881 | +void | |
| 882 | +QPDFObjectHandle::assertString() | |
| 883 | +{ | |
| 884 | + assertType("String", isString()); | |
| 885 | +} | |
| 886 | + | |
| 887 | +void | |
| 888 | +QPDFObjectHandle::assertArray() | |
| 889 | +{ | |
| 890 | + assertType("Array", isArray()); | |
| 891 | +} | |
| 892 | + | |
| 893 | +void | |
| 894 | +QPDFObjectHandle::assertDictionary() | |
| 895 | +{ | |
| 896 | + assertType("Dictionary", isDictionary()); | |
| 897 | +} | |
| 898 | + | |
| 899 | +void | |
| 900 | +QPDFObjectHandle::assertStream() | |
| 901 | +{ | |
| 902 | + assertType("Stream", isStream()); | |
| 903 | +} | |
| 904 | + | |
| 905 | +void | |
| 852 | 906 | QPDFObjectHandle::assertPageObject() |
| 853 | 907 | { |
| 854 | 908 | if (! (this->isDictionary() && this->hasKey("/Type") && | ... | ... |