Boost logo

Boost Users :

From: Andry (andry_at_[hidden])
Date: 2007-12-20 22:45:53


Hello Robert,

Friday, December 21, 2007, 1:46:24 AM, you wrote:

RD> If I have to do it manually it pretty much defeats the
RD> purpose of using it in the first place. I want it to be automated.
You can download here
https://sourceforge.net/project/showfiles.php?group_id=198908&package_id=243070&release_id=552700
patch to boost 1.34.1 which adding to preprocessor some additional
functionality in which is BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER.
Purpose of this macro is assigning to slots builtin counters like
__COUNTER__, here the example:

//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <boost/preprocessor/slot/slot.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>

#define BUILTIN_COUNTER __COUNTER__

int builtin_counter_already_utilized_somewhere[] = {
  BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER,
  BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER,
  BUILTIN_COUNTER,BUILTIN_COUNTER
};

//Remember and increment last build-in counter value.
//WARNING: Slot #1 can be used by another (external) code (for example, by another included header),
// you should exactly know that slot #1 doesn't used some else where between includes.
#define BOOST_PP_VALUE BUILTIN_COUNTER
#include BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER(1) //Evaluate BOOST_PP_VALUE as built-in counter and push it in slot #1

#define MYCOUNTER BOOST_PP_SUB(BUILTIN_COUNTER,BOOST_PP_SLOT(1))

int main() {
  int myarray[] = {MYCOUNTER,MYCOUNTER,MYCOUNTER,MYCOUNTER};
  printf("builtin_counter_already_utilized_somewhere:\n");
  for(int i = 0; i < sizeof(builtin_counter_already_utilized_somewhere)/sizeof(builtin_counter_already_utilized_somewhere[0]); i++) {
    printf("%d\n",builtin_counter_already_utilized_somewhere[i]);
  }
  printf("myarray:\n");
  for(int i = 0; i < sizeof(myarray)/sizeof(myarray[0]); i++) {
    printf("%d\n",myarray[i]);
  }
  printf("__COUNTER__ = %u\n",BUILTIN_COUNTER);
  return 0;
}
//////////////////////////////////////////////////////////////////////////

You should use BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER instead BOOST_PP_ASSIGN_SLOT
when defining BOOST_PP_VALUE to built-in counter macro.

In your code you should do something what:
//////////////////////////////////////////////////////////////////////////
    #define BOOST_PP_VALUE __COUNTER__
    #include BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER(1)

    #define CHECK_MATCH_AND_RETURN( action ) if( str == #action ) {
    return action; } void(__COUNTER__);

    CHECK_MATCH_AND_RETURN( ROTATE_LEFT )
    CHECK_MATCH_AND_RETURN( ROTATE_RIGHT )
    CHECK_MATCH_AND_RETURN( ROTATE_UP )
    CHECK_MATCH_AND_RETURN( ROTATE_DOWN )
    CHECK_MATCH_AND_RETURN( MOVE_UP )
    CHECK_MATCH_AND_RETURN( MOVE_DOWN )
    CHECK_MATCH_AND_RETURN( MOVE_LEFT )
    CHECK_MATCH_AND_RETURN( MOVE_RIGHT )
    CHECK_MATCH_AND_RETURN( MOVE_FORWARD )
    CHECK_MATCH_AND_RETURN( MOVE_BACKWARD )
    CHECK_MATCH_AND_RETURN( ROLL_RIGHT )
    CHECK_MATCH_AND_RETURN( ROLL_LEFT )
    CHECK_MATCH_AND_RETURN( INCREASE_ACCEL )
    CHECK_MATCH_AND_RETURN( DECREASE_ACCEL )
    CHECK_MATCH_AND_RETURN( ENABLE_LOOK )
    CHECK_MATCH_AND_RETURN( TOGGLE_LOOK )
    CHECK_MATCH_AND_RETURN( ENABLE_AUTO_MOVE )
    CHECK_MATCH_AND_RETURN( TOGGLE_AUTO_MOVE )

    // This helps me remember to add/remove items above.
    BOOST_STATIC_ASSERT( BOOST_PP_SUB(__COUNTER__,BOOST_PP_SLOT(1))+1 != NUM_CAMERAACTIONS );
//////////////////////////////////////////////////////////////////////////

PS: GNU C/C++ recently supports __COUNTER__ macro from 4.3 version.

-- 
Best regards,
 Andry                            mailto:andry_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net