-
Notifications
You must be signed in to change notification settings - Fork 47
Introduce new macro to wrap a C++ class: cpp_class! #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d217410
Add cpp_class!
ogoffart cef628e
cpp_class! automatically implement Default and support move only types
ogoffart 26712ab
Use atomic for the counter test
ogoffart 68468e4
Change the syntax of cpp_class: replace the ',' with 'as'
ogoffart efc5a19
small changes to the documentation
ogoffart c5a61ba
Fix issues raised in the pull request
ogoffart 9ac8cea
Use #line to report the error at the cpp_class location
ogoffart 44791f4
Use C++11 unconditionally
ogoffart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,14 @@ pub const VERSION: &'static str = env!("CARGO_PKG_VERSION"); | |
pub const LIB_NAME: &'static str = "librust_cpp_generated.a"; | ||
pub const MSVC_LIB_NAME: &'static str = "rust_cpp_generated.lib"; | ||
|
||
pub mod flags { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be cleaner to use bitflags here, but I'm not super picky. This feels fine to me. |
||
pub const IS_COPY_CONSTRUCTIBLE : u32 = 0; | ||
pub const IS_DEFAULT_CONSTRUCTIBLE : u32 = 1; | ||
pub const IS_TRIVIALLY_DESTRUCTIBLE : u32 = 2; | ||
pub const IS_TRIVIALLY_COPYABLE : u32 = 3; | ||
pub const IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE : u32 = 4; | ||
} | ||
|
||
/// This constant is expected to be a unique string within the compiled binary | ||
/// which preceeds a definition of the metadata. It begins with | ||
/// rustcpp~metadata, which is printable to make it easier to locate when | ||
|
@@ -28,7 +36,7 @@ pub const MSVC_LIB_NAME: &'static str = "rust_cpp_generated.lib"; | |
pub const STRUCT_METADATA_MAGIC: [u8; 128] = [ | ||
b'r', b'u', b's', b't', b'c', b'p', b'p', b'~', | ||
b'm', b'e', b't', b'a', b'd', b'a', b't', b'a', | ||
91, 74, 112, 213, 165, 185, 214, 120, 179, 17, 185, 25, 182, 253, 82, 118, | ||
92, 74, 112, 213, 165, 185, 214, 120, 179, 17, 185, 25, 182, 253, 82, 118, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chef's kiss nice :-P |
||
148, 29, 139, 208, 59, 153, 78, 137, 230, 54, 26, 177, 232, 121, 132, 166, | ||
44, 106, 218, 57, 158, 33, 69, 32, 54, 204, 123, 226, 99, 117, 60, 173, | ||
112, 61, 56, 174, 117, 141, 126, 249, 79, 159, 6, 119, 2, 129, 147, 66, | ||
|
@@ -71,15 +79,33 @@ pub struct Closure { | |
pub body: Spanned<String>, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Class { | ||
pub name: Ident, | ||
pub cpp: String, | ||
pub public: bool, | ||
pub line: String, // the #line directive | ||
} | ||
|
||
impl Class { | ||
pub fn name_hash(&self) -> u64 { | ||
let mut hasher = DefaultHasher::new(); | ||
self.name.hash(&mut hasher); | ||
self.cpp.hash(&mut hasher); | ||
self.public.hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
} | ||
|
||
pub enum Macro { | ||
Closure(Closure), | ||
Lit(Spanned<String>), | ||
} | ||
|
||
pub mod parsing { | ||
use syn::parse::{ident, string, tt, ty}; | ||
use syn::{Spanned, Ty, DUMMY_SPAN}; | ||
use super::{Capture, Closure, ClosureSig, Macro}; | ||
use syn::{Spanned, Ty, DUMMY_SPAN, Ident}; | ||
use super::{Capture, Closure, ClosureSig, Macro, Class}; | ||
|
||
macro_rules! mac_body { | ||
($i: expr, $submac:ident!( $($args:tt)* )) => { | ||
|
@@ -95,10 +121,12 @@ pub mod parsing { | |
}; | ||
} | ||
|
||
named!(ident_or_self -> Ident, alt!( ident | keyword!("self") => { |_| "self".into() } )); | ||
|
||
named!(name_as_string -> Capture, | ||
do_parse!( | ||
is_mut: option!(keyword!("mut")) >> | ||
id: ident >> | ||
id: ident_or_self >> | ||
keyword!("as") >> | ||
cty: string >> | ||
(Capture { | ||
|
@@ -168,4 +196,20 @@ pub mod parsing { | |
map!(tuple!( | ||
punct!("@"), keyword!("TYPE"), cpp_closure | ||
), (|(_, _, x)| x)))); | ||
|
||
named!(pub cpp_class -> Class, | ||
do_parse!( | ||
is_pub: option!(keyword!("pub")) >> | ||
keyword!("struct") >> | ||
name: ident >> | ||
keyword!("as") >> | ||
cpp_type: string >> | ||
(Class { | ||
name: name, | ||
cpp: cpp_type.value, | ||
public: is_pub.is_some(), | ||
line: String::default(), | ||
}))); | ||
|
||
named!(pub class_macro -> Class , mac_body!(cpp_class)); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this could be made cleaner by unifying all of this logic into a single ::Flags-like invocation. We could just have a list of
rustcpp::Meta<{type}, {hash}ull>::value
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you mean.
You mean that rustcpp::Meta<{type}, {hash}ull>::value would be a SizeAlign object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that was my thought.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs C++11's constexpr, so it depends on the other change that enables C++11
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that MSVC 2013 which is still tested in the CI doesn't support constexpr enough to get this to work.