1 /*
2  * Copyright 2011 Steven Watanabe
3  *
4  * This file is part of Jam - see jam.c for Copyright information.
5  */
6 
7 /*
8  * object.h - object manipulation routines
9  */
10 
11 #ifndef BOOST_JAM_OBJECT_H
12 #define BOOST_JAM_OBJECT_H
13 
14 #include "config.h"
15 #include <string>
16 #include <cstring>
17 
18 typedef struct _object OBJECT;
19 
20 OBJECT * object_new( char const * const );
21 OBJECT * object_new_range( char const * const, int32_t size );
22 void object_done( void );
23 
24 #if defined(NDEBUG) && !defined(BJAM_NO_MEM_CACHE)
25 
26 struct hash_header
27 {
28     unsigned int hash;
29     struct hash_item * next;
30 };
31 
32 #define object_str( obj ) ((char const *)(obj))
33 #define object_copy( obj ) (obj)
34 #define object_free( obj ) ((void)0)
35 #define object_equal( lhs, rhs ) ((lhs) == (rhs))
36 #define object_hash( obj ) (((struct hash_header *)((char *)(obj) - sizeof(struct hash_header)))->hash)
37 
38 #else
39 
40 char const * object_str  ( OBJECT * );
41 OBJECT *     object_copy ( OBJECT * );
42 void         object_free ( OBJECT * );
43 int          object_equal( OBJECT *, OBJECT * );
44 unsigned int object_hash ( OBJECT * );
45 
46 #endif
47 
48 namespace b2 { namespace jam {
49 
50     struct object
51     {
objectobject52         inline object(const object &o)
53             : obj(object_copy(o.obj)) {}
54 
objectobject55         inline explicit object(OBJECT *o)
56             : obj(object_copy(o)) {}
objectobject57         inline explicit object(const char * val)
58             : obj(object_new(val)) {}
objectobject59         inline explicit object(const std::string &val)
60             : obj(object_new(val.c_str())) {}
61 
~objectobject62         inline ~object() { if (obj) object_free(obj); }
releaseobject63         inline OBJECT * release() { OBJECT *r = obj; obj = nullptr; return r; }
64 
65         inline operator OBJECT*() const { return obj; }
stringobject66         inline operator std::string() const { return object_str(obj); }
67 
68         inline bool operator==(OBJECT *o) const { return std::strcmp(object_str(obj), object_str(o)) == 0; }
69 
70         private:
71 
72         OBJECT * obj = nullptr;
73     };
74 
75 }}
76 
77 #endif
78