Advanced Macros && Preprocessors

Discussion in 'C Programming' started by Let_Me_Be, Apr 28, 2006.

  1. Let_Me_Be

    Let_Me_Be Guest

    Hi all,
    I'm developing a small defensive programming toolkit for my own
    projects. So, here are my questions.

    1) Is it possible to move from something like this: SAFECALL(foo();) to
    __safecall foo();

    2) I need to auto-add code to every method of particular class
    safeclass MyClass
    {
    public:
    void foo();
    }

    void MyClass::foo() {}

    will modify the foo implementation - add some lines of code on the end
    of the method

    3) even more advanced stuff, is it possible to modify the preprocessor
    (or add my own preprocessor)? I need to parse something like this.
    __precheck minutes >= 0 && minutes < 60 #Checking minutes variable for
    valid time information
    void foo(int minutes){}
    __postcheck minutes % 10 == 0 #Checking if minutes have been rounded to
    10 minutes intervals
    I need also the text behind #
    Let_Me_Be, Apr 28, 2006
    #1
    1. Advertising

  2. Let_Me_Be

    Vladimir Oka Guest

    Let_Me_Be opined:

    > Hi all,
    > I'm developing a small defensive programming toolkit for my own
    > projects. So, here are my questions.
    >
    > 1) Is it possible to move from something like this: SAFECALL(foo();)
    > to __safecall foo();
    >
    > 2) I need to auto-add code to every method of particular class
    > safeclass MyClass
    > {
    > public:
    > void foo();
    > }
    >
    > void MyClass::foo() {}


    Are you sure you don't want comp.lang.c++?

    >
    > will modify the foo implementation - add some lines of code on the
    > end of the method
    >
    > 3) even more advanced stuff, is it possible to modify the
    > preprocessor (or add my own preprocessor)? I need to parse something
    > like this. __precheck minutes >= 0 && minutes < 60 #Checking minutes
    > variable for valid time information
    > void foo(int minutes){}
    > __postcheck minutes % 10 == 0 #Checking if minutes have been rounded
    > to 10 minutes intervals
    > I need also the text behind #


    --
    A father doesn't destroy his children.
    -- Lt. Carolyn Palamas, "Who Mourns for Adonais?",
    stardate 3468.1.

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    Vladimir Oka, Apr 28, 2006
    #2
    1. Advertising

  3. Let_Me_Be

    Let_Me_Be Guest

    > Are you sure you don't want comp.lang.c++?
    Nope, this was just an example, my toolkit should work in both c/c++.
    Let_Me_Be, Apr 28, 2006
    #3
  4. Let_Me_Be

    Ian Collins Guest

    Let_Me_Be wrote:
    > Hi all,
    > I'm developing a small defensive programming toolkit for my own
    > projects. So, here are my questions.
    >
    > 1) Is it possible to move from something like this: SAFECALL(foo();) to
    > __safecall foo();
    >
    > 2) I need to auto-add code to every method of particular class
    > safeclass MyClass
    > {
    > public:
    > void foo();
    > }
    >
    > void MyClass::foo() {}
    >

    Wrong group, if you look carefully, you will spot the absence of "++" at
    the end of this group's name.

    --
    Ian Collins.
    Ian Collins, Apr 28, 2006
    #4
  5. Let_Me_Be

    Vladimir Oka Guest

    Let_Me_Be opined:

    >> Are you sure you don't want comp.lang.c++?

    > Nope, this was just an example, my toolkit should work in both c/c++.


    Presumably then, you posted the same question to comp.lang.c++, with an
    example in C?

    In c.l.c you can only get the C perspective. Whether that's going to be
    of any use in C++ is not something to be found (or discussed) here.

    Also, do not snip attribution lines (the ones who tell us who said
    what), and leave enough context so that your post can be grasped in
    isolation. The above worked only because I remembered it was me, and
    what I replied to. Read the link in my sig.

    --
    In Pocatello, Idaho, a law passed in 1912 provided that "The carrying
    of concealed weapons is forbidden, unless same are exhibited to public
    view."

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    Vladimir Oka, Apr 28, 2006
    #5
  6. Let_Me_Be

    Eric Sosman Guest

    Let_Me_Be wrote:

    >>Are you sure you don't want comp.lang.c++?

    >
    > Nope, this was just an example, my toolkit should work in both c/c++.


    Speaking for myself, I found the examples did not cast
    much light on your intentions. What are you trying to do?

    The first question was

    > 1) Is it possible to move from something like this: SAFECALL(foo();)
    > to
    > __safecall foo();


    .... and it's not at all clear how "like" the actual "something"
    would be, nor what sort of "motion" you desire. The second was

    > 2) I need to auto-add code to every method of particular class


    .... and I'm unable to imagine how this could have any "image" in
    C. All C functions (taking "function" as a translation of "method")
    have the same status, as it were: they do not belong to any larger
    groupings like "class" or "package," so I can see no way to decide
    which functions require manipulation and which should be left alone.
    This part of your toolkit needs more explanation -- or, as Vladimir
    suggests, you need to take your question elsewhere.

    The third example (see up-thread) looks like gibberish. You're
    evidently trying to introduce some sort of assertion or error-check,
    but you're doing so with a syntax that doesn't look anything at all
    like C. Again, either your question needs elaboration or it needs
    relocation.

    One of the most valuable skills a programmer can hone is the
    ability to ask good questions, questions that provide the necessary
    information and state clearly what is wanted. You'll forgive me,
    I hope, my opinion that you need practice in this skill.

    --
    Eric Sosman
    lid
    Eric Sosman, Apr 28, 2006
    #6
  7. Let_Me_Be

    Let_Me_Be Guest

    > The first question was
    >
    > > 1) Is it possible to move from something like this: SAFECALL(foo();)
    > > to
    > > __safecall foo();

    >
    > ... and it's not at all clear how "like" the actual "something"
    > would be, nor what sort of "motion" you desire. The second was


    Currently i'm using SAFECALL macro, the syntax is SAFECALL(something),
    well, it does work, but much nicer would be __safecall something.

    > > 2) I need to auto-add code to every method of particular class

    >
    > ... and I'm unable to imagine how this could have any "image" in
    > C. All C functions (taking "function" as a translation of "method")
    > have the same status, as it were: they do not belong to any larger
    > groupings like "class" or "package," so I can see no way to decide
    > which functions require manipulation and which should be left alone.
    > This part of your toolkit needs more explanation -- or, as Vladimir
    > suggests, you need to take your question elsewhere.


    Ok, i will try to talk about this in the c++ group.

    > The third example (see up-thread) looks like gibberish. You're
    > evidently trying to introduce some sort of assertion or error-check,
    > but you're doing so with a syntax that doesn't look anything at all
    > like C. Again, either your question needs elaboration or it needs
    > relocation.


    Problem - visualy divide main code and assertions. I'm acctualy trying
    to implement functionality similar to Eiffel.

    The syntax doesn't look like C because it isn't C :) Well, that's why
    this thread is called Advanced Macros && Preprocessors. The syntax
    actualy should look something like this MACRO-HEADER BOOLEAN-EXP
    COMMENT (the comment should appear in the error message), or even
    better
    _start of check block
    exp comment
    exp comment
    _end of check block

    Idealy the block should appear after the function header, before the
    main code.

    > One of the most valuable skills a programmer can hone is the
    > ability to ask good questions, questions that provide the necessary
    > information and state clearly what is wanted. You'll forgive me,
    > I hope, my opinion that you need practice in this skill.
    >
    > --
    > Eric Sosman
    > lid
    Let_Me_Be, Apr 28, 2006
    #7
  8. Let_Me_Be

    Eric Sosman Guest

    Let_Me_Be wrote On 04/28/06 09:21,:
    >> The first question was
    >>
    >> > 1) Is it possible to move from something like this: SAFECALL(foo();)
    >> > to
    >> > __safecall foo();

    >>
    >>... and it's not at all clear how "like" the actual "something"
    >>would be, nor what sort of "motion" you desire. The second was

    >
    >
    > Currently i'm using SAFECALL macro, the syntax is SAFECALL(something),
    > well, it does work, but much nicer would be __safecall something.


    Whether you can do this depends on what SAFECALL expands
    to. If SAFECALL(x) produces bunch_of_stuff x, then you can
    simply define a __safecall macro producing bunch_of_stuff.
    If SAFECALL(x) does something more complicated, it cannot be
    replaced by an object-like __safecall macro whose expansion
    does not itself involve x.

    By the way, using two underscores at the start of the
    macro name is begging for trouble. Such names "are always
    reserved for any use" (7.1.3/1), which means "if you use
    them in your program and something goes wrong, it's your
    fault and no one else's."

    > The syntax doesn't look like C because it isn't C :) [...]


    So why ask about it in a C forum? Wouldn't alt.woodworking
    be equally appropriate?

    --
    Eric Sosman, Apr 28, 2006
    #8
  9. Let_Me_Be

    Ben Pfaff Guest

    "Let_Me_Be" <> writes:

    > 1) Is it possible to move from something like this: SAFECALL(foo();) to
    > __safecall foo();


    This macro will make that literal substitution, although I can't
    see how it's useful:
    #define SAFECALL(arg) __safecall arg

    [...C++ question omitted...]

    > 3) even more advanced stuff, is it possible to modify the preprocessor
    > (or add my own preprocessor)? I need to parse something like
    > this. [...]


    You can always write your own preprocessor to translate whatever
    you like into C. If you use a "make" or similarly powerful build
    system, it's not any hard to build such a project than it is to
    build an ordinary C project.
    --
    "C has its problems, but a language designed from scratch would have some too,
    and we know C's problems."
    --Bjarne Stroustrup
    Ben Pfaff, Apr 28, 2006
    #9
  10. Let_Me_Be

    Thad Smith Guest

    Let_Me_Be wrote:
    >> The first question was
    >>
    >> > 1) Is it possible to move from something like this: SAFECALL(foo();)
    >> > to
    >> > __safecall foo();

    >>
    >>... and it's not at all clear how "like" the actual "something"
    >>would be, nor what sort of "motion" you desire. The second was

    >
    >
    > Currently i'm using SAFECALL macro, the syntax is SAFECALL(something),
    > well, it does work, but much nicer would be __safecall something.


    which doesn't address the issue raised above. "It works" is not a
    sufficient description.

    One of the options you mentioned earlier is to write your own
    preprocessor. If you do that, you can use any syntax you like, but if
    you need full C and C++ parsing, the job will be much bigger.

    >> > 2) I need to auto-add code to every method of particular class


    I guess you need to write your own. There are probably tools available
    to help parse your target language.


    --
    Thad
    Thad Smith, Apr 29, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Preprocessors

    , Jul 3, 2006, in forum: HTML
    Replies:
    9
    Views:
    407
    dorayme
    Jul 5, 2006
  2. Jamiil

    using preprocessors - help!!

    Jamiil, Dec 18, 2005, in forum: C Programming
    Replies:
    4
    Views:
    352
    Keith Thompson
    Dec 19, 2005
  3. Let_Me_Be
    Replies:
    1
    Views:
    326
    Phlip
    Apr 28, 2006
  4. Replies:
    11
    Views:
    517
    Chris ( Val )
    Oct 6, 2007
  5. Spartacus

    RSS preprocessors

    Spartacus, Jun 30, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    77
    Spartacus
    Jun 30, 2006
Loading...

Share This Page