Skip to content

Commit

Permalink
Added explicit_bool type and cleaned up all the convert-to-booleans i…
Browse files Browse the repository at this point in the history
…n this list
  • Loading branch information
FrozenKiwi committed Oct 20, 2014
1 parent 56bc5ee commit 51e88a7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 52 deletions.
99 changes: 47 additions & 52 deletions FabricSplice.h
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,7 @@ Class Outline

#include <stdint.h>
#include <stdio.h>
#include "explicit_bool.h"

#if defined(_MSC_VER) || defined(SWIGWIN)
# if defined(__cplusplus)
Expand Down Expand Up @@ -2350,10 +2351,10 @@ namespace FabricSplice
}

// bool conversion operator
operator bool() const
{
return isValid();
}
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns the index of this symbol
unsigned int index() const
Expand Down Expand Up @@ -2511,9 +2512,9 @@ namespace FabricSplice
}

// bool conversion operator
operator bool() const
operator explicit_bool::type() const
{
return isValid();
return explicit_bool::get(isValid());
}

// returns the symbol of this KLConstant
Expand Down Expand Up @@ -2593,11 +2594,11 @@ namespace FabricSplice
return mRef != NULL;
}

// bool conversion operator
operator bool() const
{
return isValid();
}
// bool conversion operator
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns the symbol of this KLVariable
KLSymbol symbol() const
Expand Down Expand Up @@ -2660,11 +2661,11 @@ namespace FabricSplice
return mRef != NULL;
}

// bool conversion operator
operator bool() const
{
return isValid();
}
// bool conversion operator
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns the symbol of this KLStruct
KLSymbol symbol() const
Expand Down Expand Up @@ -2777,11 +2778,11 @@ namespace FabricSplice
return mRef != NULL;
}

// bool conversion operator
operator bool() const
{
return isValid();
}
// bool conversion operator
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns number of arguments in this KLArgumentList
unsigned int nbArgs() const
Expand Down Expand Up @@ -2852,11 +2853,11 @@ namespace FabricSplice
return mRef != NULL;
}

// bool conversion operator
operator bool() const
{
return isValid();
}
// bool conversion operator
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns the symbol of this KLOperator
KLSymbol symbol() const
Expand Down Expand Up @@ -2959,11 +2960,11 @@ namespace FabricSplice
return mRef != NULL;
}

// bool conversion operator
operator bool() const
{
return isValid();
}
// bool conversion operator
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns the symbol of this KLFunction
KLSymbol symbol() const
Expand Down Expand Up @@ -3066,11 +3067,11 @@ namespace FabricSplice
return mRef != NULL;
}

// bool conversion operator
operator bool() const
{
return isValid();
}
// bool conversion operator
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns the symbol of this KLInterface
KLSymbol symbol() const
Expand Down Expand Up @@ -3161,10 +3162,10 @@ namespace FabricSplice
}

// bool conversion operator
operator bool() const
{
return isValid();
}
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// returns the owner of the parser
const char * owner() const
Expand Down Expand Up @@ -3491,12 +3492,6 @@ namespace FabricSplice
friend class DGGraph;
friend class SceneManagement;

// Useful defines taken from
// http://site.zorba.io/documentation/3.0/cxx/classzorba_1_1internal_1_1ztd_1_1explicit__bool.html
// to prevent converting this class to an int accidentally
typedef void (DGPort::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}

public:

DGPort()
Expand Down Expand Up @@ -3533,9 +3528,9 @@ namespace FabricSplice
// bool conversion operator
// returning bool_type prevents the automatic
// conversion to int
operator bool_type() const
operator explicit_bool::type() const
{
return isValid() ? &DGPort::this_type_does_not_support_comparisons : 0;
return explicit_bool::get(isValid());
}

// empties the content of the port
Expand Down Expand Up @@ -4008,10 +4003,10 @@ namespace FabricSplice
}

// bool conversion operator
operator bool() const
{
return isValid();
}
operator explicit_bool::type() const
{
return explicit_bool::get(isValid());
}

// empties the content of the graph
void clear()
Expand Down
37 changes: 37 additions & 0 deletions explicit_bool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// This template class allows for defining explicit boolean
// version operators without C++0x
//
// This code is taken (mostly) verbatim from
// http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool
// modified so the actual conversion operators is no hidden on the base class
//
// Usage is like so
//
//
// class YourClass :
// public safe_bool <YourClass> // CRTP idiom
// {
// public:
// // Define boolean conversion
// operator explicit_bool::type() const {
// return explicit_bool::get(SomeLogicHere());
// }
// };
//
// This will allow
// if (yourClass)
// but not
// int var = yourClass;

#pragma once

class explicit_bool {
public:
typedef void (explicit_bool::*type)() const;
void this_type_does_not_support_comparisons() const {}

static type get(bool condition) {
return condition ? &explicit_bool::this_type_does_not_support_comparisons : 0;
}
};

0 comments on commit 51e88a7

Please sign in to comment.