~paul-lucas/zorba/bug-932374

« back to all changes in this revision

Viewing changes to include/zorba/internal/ztd.h

  • Committer: Paul J. Lucas
  • Date: 2012-09-21 20:26:47 UTC
  • mfrom: (10819.2.235 zorba)
  • Revision ID: paul@lucasmail.org-20120921202647-fy9n4jduhrnljrnb
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
370
370
  return s ? s : "<null>";
371
371
}
372
372
 
 
373
////////// misc ///////////////////////////////////////////////////////////////
 
374
 
 
375
/**
 
376
 * Helper class for implementing a solution to the "explicit bool conversion"
 
377
 * problem.  The canonical use is of the form:
 
378
 * \code
 
379
 *  class your_class {
 
380
 *    // ...
 
381
 *    operator explicit_bool::type() const {
 
382
 *      return explicit_bool::value_of( some_expression );
 
383
 *    }
 
384
 *  };
 
385
 * \endcode
 
386
 *
 
387
 * See: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2333.html
 
388
 */
 
389
class explicit_bool {
 
390
  struct pointer_conversion { int valid; };
 
391
public:
 
392
  typedef int pointer_conversion::*type;
 
393
 
 
394
  /**
 
395
   * Gets the explicit \c bool value for \c false.
 
396
   *
 
397
   * @return Returns said value.
 
398
   */
 
399
  static type false_value() {
 
400
    return 0;
 
401
  }
 
402
 
 
403
  /**
 
404
   * Gets the explicit \c bool value for \c true.
 
405
   *
 
406
   * @return Returns said value.
 
407
   */
 
408
  static type true_value() {
 
409
    return &pointer_conversion::valid;
 
410
  }
 
411
 
 
412
  /**
 
413
   * Converts the the built-in \c bool value to an explicit \c bool value.
 
414
   *
 
415
   * @param value The \c bool value to convert.
 
416
   * @return Return said value.
 
417
   */
 
418
  static type value_of( bool value ) {
 
419
    return value ? true_value() : false_value();
 
420
  }
 
421
};
 
422
 
373
423
///////////////////////////////////////////////////////////////////////////////
374
424
 
375
425
} // namespace ztd