from a software design perspective, not a coding perspective
however I'm not sure there's clear distinction between the two. Or, more correctly, the coding approach you take will determine your design options. For example, if you use SWIG, the options in your question don't really make sense, since you write a separate "interface" file. If you are using luabind, the options do make sense, but I would definitely choose 1) in that case as luabind headers slow compilation dramatically and I'd like to have them included in as few compilation units as possible. If your "coding" approach doesn't have that luabind shortcoming then 2) seems like the more sensible thing to do.
Your second approach will work well. One way to avoid multiple registrations is to use a static initialization list approach. Each class would add a Lua registration function to a static std::set pre-main. Then you'd walk this std::set when your application starts and add each class constructor binding to your Lua runtime. This would ensure your class bindings are registered only once.
发布评论
评论(2)
我明白你问的意思
,但我不确定两者之间有明显的区别。或者,更准确地说,您采用的编码方法将决定您的设计选项。例如,如果您使用 SWIG,问题中的选项实际上没有意义,因为您编写了一个单独的“接口”文件。如果您使用 luabind,这些选项确实有意义,但我肯定会选择 1) 在这种情况下,因为 luabind 标头会显着减慢编译速度,我希望将它们包含在尽可能少的编译单元中。如果你的“编码”方法没有 luabind 的缺点,那么 2) 似乎是更明智的做法。
I understand what you mean by asking
however I'm not sure there's clear distinction between the two. Or, more correctly, the coding approach you take will determine your design options. For example, if you use SWIG, the options in your question don't really make sense, since you write a separate "interface" file. If you are using luabind, the options do make sense, but I would definitely choose 1) in that case as luabind headers slow compilation dramatically and I'd like to have them included in as few compilation units as possible. If your "coding" approach doesn't have that luabind shortcoming then 2) seems like the more sensible thing to do.
你的第二种方法会很有效。避免多次注册的一种方法是使用静态初始化列表方法。每个类都会向静态 std::set pre-main 添加一个 Lua 注册函数。然后,当应用程序启动时,您将遍历此 std::set 并将每个类构造函数绑定添加到 Lua 运行时。这将确保您的类绑定仅注册一次。
Your second approach will work well. One way to avoid multiple registrations is to use a static initialization list approach. Each class would add a Lua registration function to a static std::set pre-main. Then you'd walk this std::set when your application starts and add each class constructor binding to your Lua runtime. This would ensure your class bindings are registered only once.