Flags, enum (C) - Stack Overflow

another way of storing flags is to not bother with the underlying type at all. when using an enum, the enum values are stored by default into an unsigned int, which is 32 bits on a common computer. this gives you with only 32 possible flags: while certainly much, there are some cases where it is not sufficient.

now you can define your flag set this way:

typedef struct{    int takes_damage : 1;    int grabbable    : 1;    int liquid       : 1;    int some_other   : 1;} flags;

if you never encountered this, the ': 1' part tells the compiler to only use 1 bit to store this struct member.

now you can define a variable to hold the flags, and work with those flags:

flags myflags = {1,0,0,1}; // defines a variable holding a set of flags, with an initial value of takes_damage & some_othermyflags.liquid = 1; // change the flags to include the liquidif ( myflags.takes_damage ) // test for one flag    apply_damage();if ( myflags.liquid && myflags.some_other ) // test for multiple flags    show_strange_behavior();

this method allows you to define any number of flags, without limitation, and you can extend your flag set at any time without fearing an overflow. the drawback is that testing a subset of the flags is more cumbersome and necessitate more code.

原本是在看 stackoverflow 上面對 enum 的常用法,無意中看到其中一個回覆,用「:1」可以告訴 compiler 只用 1 bit 去存…,這樣就不會被限制在一個 enum flag var 只用有 32 種 flags。

How can I hide the declaration of a struct in C? - Stack Overflow

In the header file:

typedef struct _point * Point;

After the compiler sees this it knows:

  • there is s struct called _point
  • there is a pointer type Point that can refer to a _point

The compiler does not know:

  • what the _point struct looks like
  • what members it contains
  • how big it is

And not only does the compiler not know it - we as programmers don't know it either. This means we can't write code that depends on those properties of _point, which means that our code may be more portable.

對於 C opaque pointer 的解譯

Hoopla! » Rails: Custom 404 Pages

sasha said: there is another more generic way that will catch all the errors, you can inspect the exception to route to the right view, and possibly log the trace: in application.rb
def rescue_action_in_public(exception)
  # do something based on exception
  message = exception.backtrace.join("\n") unless exception
  render :file => "#{RAILS_ROOT}/public/404.html", :layout => false, :status => 404
end

def local_request?
  false
end
thanks for the CSV tip. doc is atrocious

覆寫「rescue_action_in_public」來處理 global 的 exception。

Catch-All routes in Rails | Floyd's Thoughts...

If that’s your last route, it means that anything that isn’t recognised by any of the other routes will get routed to that controller/action (page_engine/show_page). It shouldn’t interfere with images/assets because they are served with higher priority than Rails routes. It lets you have any number of forward-slashes.

如果是用 catch-all route,也就是擺在最後面的「*route」,是不會影響 asset 的東西。因為它們的 routing priority 比 routes.rb 裡面定義的還高。