001/*
002 * BridJ - Dynamic and blazing-fast native interop for Java.
003 * http://bridj.googlecode.com/
004 *
005 * Copyright (c) 2010-2013, Olivier Chafik (http://ochafik.com/)
006 * All rights reserved.
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions are met:
010 * 
011 *     * Redistributions of source code must retain the above copyright
012 *       notice, this list of conditions and the following disclaimer.
013 *     * Redistributions in binary form must reproduce the above copyright
014 *       notice, this list of conditions and the following disclaimer in the
015 *       documentation and/or other materials provided with the distribution.
016 *     * Neither the name of Olivier Chafik nor the
017 *       names of its contributors may be used to endorse or promote products
018 *       derived from this software without specific prior written permission.
019 * 
020 * THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
021 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
023 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
024 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
027 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 */
031package org.bridj;
032
033import org.bridj.ann.*;
034import java.util.List;
035import java.util.Date;
036
037/**
038 * Wraps a value which size is the same as the 'time_t' C type (defined in
039 * time.h)
040 *
041 * @author Olivier
042 */
043public final class TimeT extends AbstractIntegral {
044
045    public static final int SIZE = Platform.TIME_T_SIZE;
046
047    static {
048        BridJ.register();
049    }
050
051    public TimeT(long value) {
052        super(value);
053    }
054
055    @Override
056    public int byteSize() {
057        return SIZE;
058    }
059
060    public Date toDate() {
061        return new Date(value);
062    }
063
064    public static TimeT valueOf(long value) {
065        return new TimeT(value);
066    }
067
068    public static TimeT valueOf(Date value) {
069        return valueOf(value.getTime());
070    }
071
072    @Override
073    public String toString() {
074        return "TimeT(value = " + value + ", time = " + toDate() + ")";
075    }
076
077    @Struct(customizer = timeval_customizer.class)
078    public static class timeval extends StructObject {
079
080        public long getTime() {
081            return seconds() * 1000 + milliseconds();
082        }
083
084        @Field(0)
085        public long seconds() {
086            return this.io.getCLongField(this, 0);
087        }
088
089        @Field(0)
090        public timeval seconds(long seconds) {
091            this.io.setCLongField(this, 0, seconds);
092            return this;
093        }
094
095        public final long seconds_$eq(long seconds) {
096            seconds(seconds);
097            return seconds;
098        }
099
100        @Field(1)
101        public int milliseconds() {
102            return this.io.getIntField(this, 1);
103        }
104
105        @Field(1)
106        public timeval milliseconds(int milliseconds) {
107            this.io.setIntField(this, 1, milliseconds);
108            return this;
109        }
110
111        public final int milliseconds_$eq(int milliseconds) {
112            milliseconds(milliseconds);
113            return milliseconds;
114        }
115    }
116
117    public static class timeval_customizer extends StructCustomizer {
118
119        @Override
120        public void beforeLayout(StructDescription desc, List<StructFieldDescription> aggregatedFields) {
121            StructFieldDescription secondsField = aggregatedFields.get(0);
122            if (Platform.isWindows() || !Platform.is64Bits()) {
123                secondsField.byteLength = 4;
124            } else {
125                secondsField.byteLength = 8;
126            }
127
128            secondsField.alignment = secondsField.byteLength;
129        }
130    }
131}