macros

When to use function-like macros in C

Hi, I was reading some code written in C this evening, and at the top of the file was the function-like macro HASH: #define HASH(fp) (((unsigned long)fp)%NHASH) This left me wondering, why would somebody choose to implement a function this way using a function-like macro instead of implementing it as a regular vanilla C function? Wha...

Automate pimpl'ing of C++ classes -- is there an easy way?

Pimpl's are a source of boilerplate in a lot of C++ code. They seem like the kind of thing that a combination of macros, templates, and maybe a little external tool help could solve, but I'm not sure what the easiest way would be. I've seen templates that help do some of the lifting but not much -- you still end up needing to write forwa...

Excel Macro Match and Copy/Paste

Hi! I'm trying to build an Excel macro that will allow me to do the following: I have two excel sheets. I want the macro to match all the values in Sheet1, col A against cell A1 in Sheet2. If it matches, copy cell Dx from Sheet1 to cell D1 in Sheet2, if it doesn't just move on to cell A2 in Sheet2 and do the same, but copy cell Dx fro...

ISO C equivalent of braced-groups within expressions

How can I do the following in a compliant (ISO C99) way? #define MALLOC(type, length, message) ({ \ type * a_##__LINE__ = (type *)malloc((length) * sizeof(type)); \ assert(message && (a_##__LINE__ != NULL)); \ a_##__LINE__; ...

Append # at the beggining of a string generated by a velocity macro

Does anybody know how to get a velocity macro to run when it's prepended by an Octothorpe? I have a velocity macro called #macro(getUniqueID $id) And I want to use it to spit out an id to be used by jQuery.find() which uses CSS selectors which means that the id needs to be prepended by another octothorpe. jquery.find("##getUniqueID...

Mathematica function foo that can distinguish foo[.2] from foo[.20]

Suppose I want a function that takes a number and returns it as a string, exactly as it was given. The following doesn't work: SetAttributes[foo, HoldAllComplete]; foo[x_] := ToString[Unevaluated@x] The output for foo[.2] and foo[.20] is identical. The reason I want to do this is that I want a function that can understand dates with ...

Should I use a function or a macro to validate arguments in Clojure?

I have a group of numeric functions in Clojure that I want to validate the arguments for. There are numerous types of arguments expected by the functions, such as positive integers, percentages, sequences of numbers, sequences of non-zero numbers, and so on. I can validate the arguments to any individual function by: Writing validation...

When should you use macros instead of inline functions?

In a previous question what I thought was a good answer was voted down for the suggested use of macros #define radian2degree(a) (a * 57.295779513082) #define degree2radian(a) (a * 0.017453292519) instead of inline functions. Please excuse the newbie question, but what is so evil about macros in this case? ...

C array initialization via macro

Hi! Question's Background: void dash(int *n, char c) is to draw characters c separated by '+'. Parameter n is an array of ints, e.g. {1, 3, 2} and '-' for c should give "+-+---+--+", which works fine. To use dash I do {int f={1, 3, 2}; dash(f, '-');}, which makes the construct copy&pastable. The question itself: To avoid copy&pasting I ...

strings.h and wrapping this macro with a macro check of whether

I infer from Google search results that strings.h (from here) is for UNIX systems. I would like to wrap the following line with a macro check of whether the host's operating system is Linux/UNIX. It would be much appreciated to hear suggestions about it. Thanks in advance. #include <strings.h> ...

How would you make this into a VIM macro?

So one of the common tasks that I do as a programmer is debugging a live system. And one of the ways that I debug a live system is to capture a verbose log from the console. Typically the log file has around 20 extra lines for every one line I am interested. To minimize my macro script I went about creating a macro that will grab ONLY ...

#define for unsigned long

Hi, I'm attempting to use the #define directive to change all of "ulong" to "unsigned long". Here is an example: #define ulong unsigned long ulong idCounter = 0; Sadly, I think it ends up replacing ulong with "unsigned", rather than "unsigned long". I tried "#define ulong (unsigned long)", but that didn't worth either. ...

Generic Macro for filling values in a table in Excel

I know there must exist Excel Macros that can automatically fill in the values of empty cells in a table given the values in the header row and column as well as other configuration parameters in other cells. Of course, these macros will usually be tied to a particular set of attributes, ect. Does there exist any generic macro for this w...

Standardize word macro usage within the team

Hi, I have a team of 10 persons who work on word documents, they format them as per our defined guidlines. To complete the work fast we have created a macro that has been updated on all the machines. This macro corrects the font, size and formatting. How can I ensure and implement that nobody can change/replace or delete this macro fro...

Why are #ifndef and #define used in c++ header files

I have been seeing code like this usually in the start of header files #ifndef HEADERFILE_H #define HEADERFILE_H and at the end of the file is #endif I am confused about the purpose of this ..? ...

C to C# conversion of #define macros

Hi, How would you go about converting the following C #define into c#. #define get16bits(d) (*((const uint16_t *) (d))) #if !defined (get16bits) #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ +(uint32_t)(((const uint8_t *)(d))[0]) ) #endif I know you probably re...

Converting a Scheme expression to a string

Given an expression '(lambda (x) x) How can I translate this into a string. I thought symbol->string will do the job but no it cant not a symbol. e.g for a macro to-string: (to-string (lambda(x) x)) this should return >> "(lambda (x) x)" Any ideas folks Thanks ...

__CLASS__ macro in C++

Is there a __CLASS__ macro in C++ which gives the class name similar to __FUNCTION__ macro which gives the function name ...

How can I call a VBA macro that is defined in a different file?

The situation is the following: Macro Foo is defined in File foo.vss Macro Bar is defined in File bar.vsd. Bar() is at some point supposed to call Foo(). Is that possible? If it helps: foo.vss is a stencil file that is opened in bar.vsd. FYI: They are forcing me to use Visio/vba. Its a strange old System that generates SQL from the d...

How to reformat Outlook mail item in VBA

Okay, i've got an Outlook 2003 VBA macro that clears a mail item's categories, and this is assigned to a button. However, i've got a conditional formatting rule that's already been applied, so when (in the inbox list view) i run the macro, the categories are cleared but the conditional formatting on that item remains until i select a dif...